From e2f5ae2314042c4d5247ecf8c59e7bcd207458b1 Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko Date: Thu, 16 May 2024 14:24:43 +0300 Subject: [PATCH 1/3] update: refactor sending legacy transactions, combine gas costs in one function --- internal/service/api/handlers/register.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/service/api/handlers/register.go b/internal/service/api/handlers/register.go index 13c0871..155cfab 100644 --- a/internal/service/api/handlers/register.go +++ b/internal/service/api/handlers/register.go @@ -22,50 +22,51 @@ func Register(w http.ResponseWriter, r *http.Request) { ape.RenderErr(w, problems.BadRequest(err)...) return } + log := Log(r).WithField("calldata", req.Data.TxData) dataBytes, err := hexutil.Decode(req.Data.TxData) if err != nil { - Log(r).WithError(err).Error("failed to decode data") + log.WithError(err).Error("failed to decode data") ape.RenderErr(w, problems.BadRequest(err)...) return } registration, registerProofParams, err := getRegistrationTxDataParams(r, dataBytes) if err != nil { - Log(r).WithError(err).Error("failed to get tx data params") + log.WithError(err).Error("failed to get tx data params") ape.RenderErr(w, problems.InternalError()) return } exists, err := VotingRegistry(r).IsPoolExistByProposer(&bind.CallOpts{}, NetworkConfig(r).Proposer, registration) if err != nil { - Log(r).WithError(err).Error("failed to check if registration exists by proposer") + log.WithError(err).Error("failed to check if registration exists by proposer") ape.RenderErr(w, problems.InternalError()) return } if !exists { - Log(r).WithField("registration", registration.String()).Error("registration does not exist") + log.WithField("registration", registration.String()).Error("registration does not exist") ape.RenderErr(w, problems.BadRequest(errors.New("registration does not exist"))...) return } voteVerifier, err := contracts.NewVoteVerifier(registration, EthClient(r)) if err != nil { - Log(r).WithError(err).Error("failed to initialize new vote verifier") + log.WithError(err).Error("failed to initialize new vote verifier") ape.RenderErr(w, problems.InternalError()) return } isRegistered, err := voteVerifier.IsUserRegistered(&bind.CallOpts{}, registerProofParams.DocumentNullifier) if err != nil { - Log(r).WithError(err).Error("failed to check if user is registered by document nullifier") + log.WithError(err).Error("failed to check if user is registered by document nullifier") ape.RenderErr(w, problems.InternalError()) return } if isRegistered { - Log(r).WithFields(logan.F{ + log.WithFields(logan.F{ "registration": registration.Hex(), "document_nullifier": registerProofParams.DocumentNullifier.String(), }).Error("too many requests for registration and document nullifier") @@ -78,7 +79,7 @@ func Register(w http.ResponseWriter, r *http.Request) { tx, err := processLegacyTx(r, registration, dataBytes) if err != nil { - Log(r).WithError(err).Error("failed to process legacy transaction") + log.WithError(err).Error("failed to process legacy transaction") ape.RenderErr(w, problems.InternalError()) return } From 267f8528d452b83e41485aca5cc7cd9b24d9993b Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko Date: Fri, 17 May 2024 16:37:07 +0300 Subject: [PATCH 2/3] add: error handler for execution reverted error --- internal/service/api/handlers/register.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/service/api/handlers/register.go b/internal/service/api/handlers/register.go index 155cfab..cf23dfb 100644 --- a/internal/service/api/handlers/register.go +++ b/internal/service/api/handlers/register.go @@ -7,6 +7,9 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core/vm" + validation "github.com/go-ozzo/ozzo-validation/v4" + pkgErrors "github.com/pkg/errors" "github.com/rarimo/proof-verification-relayer/internal/contracts" "github.com/rarimo/proof-verification-relayer/internal/service/api/requests" "gitlab.com/distributed_lab/ape" @@ -80,6 +83,12 @@ func Register(w http.ResponseWriter, r *http.Request) { tx, err := processLegacyTx(r, registration, dataBytes) if err != nil { log.WithError(err).Error("failed to process legacy transaction") + if pkgErrors.Is(err, vm.ErrExecutionReverted) { + ape.RenderErr(w, problems.BadRequest(validation.Errors{ + "tx": pkgErrors.Cause(err), + }.Filter())...) + return + } ape.RenderErr(w, problems.InternalError()) return } From 5acc180fddb30c18276766ab24d193347455a549 Mon Sep 17 00:00:00 2001 From: Maksym Hrynenko Date: Sun, 19 May 2024 13:12:24 +0300 Subject: [PATCH 3/3] add: user agent log --- internal/service/api/handlers/register.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/service/api/handlers/register.go b/internal/service/api/handlers/register.go index cf23dfb..079ab1e 100644 --- a/internal/service/api/handlers/register.go +++ b/internal/service/api/handlers/register.go @@ -25,7 +25,10 @@ func Register(w http.ResponseWriter, r *http.Request) { ape.RenderErr(w, problems.BadRequest(err)...) return } - log := Log(r).WithField("calldata", req.Data.TxData) + log := Log(r).WithFields(logan.F{ + "user-agent": r.Header.Get("User-Agent"), + "calldata": req.Data.TxData, + }) dataBytes, err := hexutil.Decode(req.Data.TxData) if err != nil {