Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waggle sign API server #11

Merged
merged 10 commits into from
Aug 24, 2023
Merged

Waggle sign API server #11

merged 10 commits into from
Aug 24, 2023

Conversation

kompotkot
Copy link
Collaborator

Handle API requests for batch sign

Example of API request:

{
    "chain_id": 80001,
    "dropper": "0x4ec36E288E1b5d6914851a141cb041152Cf95328",
    "signer": "0x123c51488a18fc75d3b8993743f3c132316952c8",
    "requests": [
        {
            "dropId": "2",
            "requestID": "5",
            "claimant": "0x000000000000000000000000000000000000dEaD",
            "blockDeadline": "40000000",
            "amount": "3000000000000000000"
        },
        {
            "dropId": "2",
            "requestID": "6",
            "claimant": "0x000000000000000000000000000000000000dEaD",
            "blockDeadline": "40000000",
            "amount": "3000000000000000000"
        }
    ]
}

Example of response:

{
    "chain_id": 80001,
    "dropper": "0x4ec36E288E1b5d6914851a141cb041152Cf95328",
    "signer": "0x123c51488a18fc75d3b8993743f3c132316952c8",
    "sensible": false,
    "requests": [
        {
            "dropId": "2",
            "requestID": "5",
            "claimant": "0x000000000000000000000000000000000000dEaD",
            "blockDeadline": "40000000",
            "amount": "3000000000000000000",
            "signature": "1165f3f1edba760f570a833891ef238c9e40d2e2d1c6d66ab39904v1934c4bb9342e463bd24e0464cceb66a91ea96a48965bb7603d59dc6b859f1112d077a5e41b",
            "signer": "0x123c51488a18fc75d3b8993743f3c132316952c8"
        },
        {
            "dropId": "2",
            "requestID": "6",
            "claimant": "0x000000000000000000000000000000000000dEaD",
            "blockDeadline": "40000000",
            "amount": "3000000000000000000",
            "signature": "85127edfac02da74776d761f230dc8d1c361ec8fb400881224d8e6001b00b17326048930b0b6e4f03ce407993e12399f80b325cc0be075fad855a2c6168f3b221c",
            "signer": "0x123c51488a18fc75d3b8993743f3c132316952c8"
        }
    ]
}

@kompotkot
Copy link
Collaborator Author

Resolves #11

@kompotkot
Copy link
Collaborator Author

kompotkot commented Aug 15, 2023

@bugout-dev check

Copy link
Contributor

@zomglings zomglings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like availableSigners global variable. Left a few other comments and suggestions.

settings.go Outdated Show resolved Hide resolved
func ReadServerConfig(rawConfigPath string) (*[]ServerSignerConfig, error) {
var configs []ServerSignerConfig

configPath := strings.TrimSuffix(rawConfigPath, "/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?

Copy link
Collaborator Author

@kompotkot kompotkot Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if path will be passed acidently with trailing slash, like --config ~/config.json/

if err != nil {
log.Fatal(err)
}
configTemp := &[]ServerSignerConfig{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use:

configTemp := []ServerSignerConfig{}
unmarshalErr := json.Unmarshal(rawBytes, &configTemp)

This is the pattern we use everywhere else:

$ git grep Unmarshal
bugout.go:              parseErr := json.Unmarshal([]byte(result.Content), &reports[i])
cmd.go:                         parseErr := json.Unmarshal(batchRaw, &batch)
cmd.go:                                 rowParseErr := json.Unmarshal(jsonString, &batch[i])
cmd.go:                 parseErr := json.Unmarshal(messagesRaw, &messages)
moonstream.go:  unmarshalErr := json.Unmarshal(responseBody, &contracts)
moonstream.go:  unmarshalErr := json.Unmarshal(responseBody, &callRequests)
server.go:      err = json.Unmarshal(body, &req)
settings.go:    err = json.Unmarshal(rawBytes, configTemp)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  1. Reference passed in json.Unmarshal call rather than declaring variable to be the reference.
  2. Create a new error for unmarshalling to make the code more readable - unmarshalErr instead of err.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used this, because nodebalancer, monitoring, etc code written in this way. If it is important, I can follow as you wish.

Copy link
Collaborator Author

@kompotkot kompotkot Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I am using err because:

func abc(someInts []*int) (int, error) {
  var num *int
  for _, si := range someInts {
    // Here I forced to use `:=` but I want to be asure to use variable `num` scope from func `abc`
    num, numErr := someOperation(si)
     ...
  }
}

So I prefer this code:

func abc(someInts []*int) (int, error) {
  var num *int
  var err error
  for _, si := range someInts {
    num, err = someOperation(si)
     ...
  }
}

server.go Outdated
json.NewEncoder(w).Encode(req)
}

func ServerRun(host string, port int) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename it simpler? func Serve(...) error {}?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me its is not clear naming, but ok

server.go Outdated
)

var (
availableSigners map[string]AvailableSigner
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only big problem I have with this PR. Why does this need to be a global variable?

Can't you pass it as an argument to the server?

This should definitely be an argument to ServerRun(host string, port int, availableSigners map[string]AvailableSigner).

You can create a WaggleServer struct and define the handlers on that struct if you want, so that they have access to the availableSigners member.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid these kind of global variables. It limits how waggle can be used (e.g. you can't instantiate multiple waggle servers in the same process)`.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem not in passing to ServerRun but in passing variable in each mux

serveMux := http.NewServeMux()
serveMux.HandleFunc("/ping", pingRoute)
serveMux.HandleFunc("/sign/dropper", signDropperRoute)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I ll pass in signDropperRoute

cmd.go Outdated Show resolved Hide resolved
@@ -504,3 +565,52 @@ func CreateMoonstreamCommand() *cobra.Command {

return moonstreamCommand
}

func CreateServerCommand() *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to have:

  • waggle server configure (replacing waggle accounts config)
  • waggle server run

Copy link
Contributor

@zomglings zomglings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@zomglings zomglings merged commit 40e6a4f into main Aug 24, 2023
1 check passed
@zomglings zomglings deleted the waggle-sign-server branch August 24, 2023 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants