Skip to content

Commit

Permalink
add nonce resetting when encountering nonce errors
Browse files Browse the repository at this point in the history
  • Loading branch information
freigeistig committed Mar 9, 2024
1 parent efbed28 commit 46bbfd0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
1 change: 0 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ network:
verifier_address: ""
vault_address: "http://127.0.0.1:8200"
vault_mount_path: "secret_data"
no_send: false

log:
level: debug
Expand Down
12 changes: 10 additions & 2 deletions internal/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type NetworkConfig struct {
VotingRegistry common.Address `fig:"voting_registry,required"`
Address string `fig:"vault_address,required"`
MountPath string `fig:"vault_mount_path,required"`
NoSend bool `fig:"no_send"`

ChainID *big.Int `fig:"chain_id"`
Token string `dig:"VAULT_TOKEN,clear"`
Expand Down Expand Up @@ -80,7 +79,6 @@ func (e *ethereum) NetworkConfig() *NetworkConfig {
"voting_registry": result.VotingRegistry,
"vault_address": result.Address,
"vault_mount_path": result.MountPath,
"no_send": result.NoSend,
}).Now(); err != nil {
panic(err)
}
Expand Down Expand Up @@ -142,3 +140,13 @@ func (n *NetworkConfig) Nonce() uint64 {
func (n *NetworkConfig) IncrementNonce() {
n.nonce++
}

// ResetNonce sets nonce to the value received from a node
func (n *NetworkConfig) ResetNonce(client *ethclient.Client) error {
nonce, err := client.NonceAt(context.Background(), crypto.PubkeyToAddress(n.PrivateKey.PublicKey), nil)
if err != nil {
return errors.Wrap(err, "failed to get nonce")
}
n.nonce = nonce
return nil
}
38 changes: 34 additions & 4 deletions internal/service/api/handlers/verify_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"encoding/json"
"net/http"
"strings"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -116,8 +117,37 @@ func VerifyProof(w http.ResponseWriter, r *http.Request) {
return
}

if !NetworkConfig(r).NoSend {
if err := EthClient(r).SendTransaction(r.Context(), tx); err != nil {
if err := EthClient(r).SendTransaction(r.Context(), tx); err != nil {
if strings.Contains(err.Error(), "nonce") {
if err := NetworkConfig(r).ResetNonce(EthClient(r)); err != nil {
Log(r).WithError(err).Error("failed to reset nonce")
ape.RenderErr(w, problems.InternalError())
return
}

tx, err = types.SignNewTx(
NetworkConfig(r).PrivateKey,
types.NewCancunSigner(NetworkConfig(r).ChainID),
&types.LegacyTx{
Nonce: NetworkConfig(r).Nonce(),
Gas: gas,
GasPrice: gasPrice,
To: &registration,
Data: dataBytes,
},
)
if err != nil {
Log(r).WithError(err).Error("failed to sign new tx")
ape.RenderErr(w, problems.InternalError())
return
}

if err := EthClient(r).SendTransaction(r.Context(), tx); err != nil {
Log(r).WithError(err).Error("failed to send transaction")
ape.RenderErr(w, problems.InternalError())
return
}
} else {
Log(r).WithError(err).Error("failed to send transaction")
ape.RenderErr(w, problems.InternalError())
return
Expand Down Expand Up @@ -158,8 +188,8 @@ func getTxDataParams(r *http.Request, data []byte) (
return common.Address{}, contracts.IRegisterVerifierRegisterProofParams{}, errors.Wrap(err, "failed to unmarshal JSON")
}

addr1Bytes := proveIdentityParams.Inputs[len(proveIdentityParams.Inputs)-2].Bytes()
registration := common.BytesToAddress(addr1Bytes)
registrationAddressBytes := proveIdentityParams.Inputs[len(proveIdentityParams.Inputs)-2].Bytes()
registration := common.BytesToAddress(registrationAddressBytes)

registerProofParamsJSON, err := json.Marshal(unpackResult[1])
if err != nil {
Expand Down

0 comments on commit 46bbfd0

Please sign in to comment.