Skip to content

Commit

Permalink
Adding feature: waitConfirm flag in relay requests to hold the conn…
Browse files Browse the repository at this point in the history
…ection until transaction will be minted
  • Loading branch information
olegfomenko committed Nov 29, 2023
1 parent df62c4a commit 7fbad1d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 55 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ volumes:
```json
{
"chain": "The name of chain submit to according to the service configuration",
"hash": "The state hash (the same as on state contract) in 0x... hex format"
"hash": "The state hash (the same as on state contract) in 0x... hex format",
"waitConfirm": true
}
```

Expand All @@ -136,10 +137,14 @@ volumes:
```json
{
"chain": "The name of chain submit to according to the service configuration",
"hash": "The GIST hash (the same as on state contract) in 0x... hex format"
"hash": "The GIST hash (the same as on state contract) in 0x... hex format",
"waitConfirm": true
}
```

`"waitConfirm": true` - indicates if request should wait until transaction will be included into the block.
Default - `false`.

The response will be:

* Code 200, successful relay, tx hash in body.
Expand Down
3 changes: 3 additions & 0 deletions docs/static/service.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
},
"chain": {
"type": "string"
},
"waitConfirm": {
"type": "boolean"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions internal/proto/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ message MsgGISTRelayRequest {
message RelayBody {
string hash = 1;
string chain = 2;
bool waitConfirm = 3;
}

message MsgRelayResponse {
Expand Down
48 changes: 42 additions & 6 deletions internal/services/relayer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/rarimo/identity-relayer-svc/internal/config"
"github.com/rarimo/identity-relayer-svc/internal/core"
"github.com/rarimo/identity-relayer-svc/internal/data"
"github.com/rarimo/identity-relayer-svc/internal/data/pg"
"github.com/rarimo/identity-relayer-svc/internal/utils"
"github.com/rarimo/identity-relayer-svc/pkg/polygonid/contracts"
rarimocore "github.com/rarimo/rarimo-core/x/rarimocore/types"
"gitlab.com/distributed_lab/logan/v3"
Expand Down Expand Up @@ -73,7 +76,7 @@ func (c *Service) GistRelays(ctx context.Context, gist string) ([]data.GistTrans
return transitions, nil
}

func (c *Service) StateRelay(ctx context.Context, state string, chainName string) (txhash string, err error) {
func (c *Service) StateRelay(ctx context.Context, state string, chainName string, waitTxConfirm bool) (txhash string, err error) {
chain, ok := c.chains.GetChainByName(chainName)
if !ok {
return "", ErrChainNotFound
Expand All @@ -92,10 +95,10 @@ func (c *Service) StateRelay(ctx context.Context, state string, chainName string
return "", err
}

return c.processIdentityStateTransfer(ctx, chain, entry)
return c.processIdentityStateTransfer(ctx, chain, entry, waitTxConfirm)
}

func (c *Service) GistRelay(ctx context.Context, gist string, chainName string) (txhash string, err error) {
func (c *Service) GistRelay(ctx context.Context, gist string, chainName string, waitTxConfirm bool) (txhash string, err error) {
chain, ok := c.chains.GetChainByName(chainName)
if !ok {
return "", ErrChainNotFound
Expand All @@ -114,7 +117,7 @@ func (c *Service) GistRelay(ctx context.Context, gist string, chainName string)
return "", err
}

return c.processIdentityGISTTransfer(ctx, chain, entry)
return c.processIdentityGISTTransfer(ctx, chain, entry, waitTxConfirm)
}

func (c *Service) checkTransitionNotExist(ctx context.Context, state, chain string) error {
Expand Down Expand Up @@ -155,7 +158,7 @@ func (c *Service) checkGISTTransitionNotExist(ctx context.Context, state, chain
return nil
}

func (c *Service) processIdentityStateTransfer(ctx context.Context, chain *config.EVMChain, entry *data.State) (txhash string, err error) {
func (c *Service) processIdentityStateTransfer(ctx context.Context, chain *config.EVMChain, entry *data.State, waitTxConfirm bool) (txhash string, err error) {
opts := chain.TransactorOpts()

nonce, err := chain.RPC.PendingNonceAt(context.TODO(), chain.SubmitterAddress)
Expand Down Expand Up @@ -208,10 +211,14 @@ func (c *Service) processIdentityStateTransfer(ctx context.Context, chain *confi
c.log.WithError(err).Error("failed to create transition entry")
}

if waitTxConfirm {
c.waitTxConfirmation(ctx, chain, tx)
}

return tx.Hash().Hex(), nil
}

func (c *Service) processIdentityGISTTransfer(ctx context.Context, chain *config.EVMChain, entry *data.Gist) (txhash string, err error) {
func (c *Service) processIdentityGISTTransfer(ctx context.Context, chain *config.EVMChain, entry *data.Gist, waitTxConfirm bool) (txhash string, err error) {
opts := chain.TransactorOpts()

nonce, err := chain.RPC.PendingNonceAt(context.TODO(), chain.SubmitterAddress)
Expand Down Expand Up @@ -264,9 +271,38 @@ func (c *Service) processIdentityGISTTransfer(ctx context.Context, chain *config
c.log.WithError(err).Error("failed to create transition entry")
}

if waitTxConfirm {
c.waitTxConfirmation(ctx, chain, tx)
}

return tx.Hash().Hex(), nil
}

func (c *Service) waitTxConfirmation(ctx context.Context, chain *config.EVMChain, tx *types.Transaction) {
receipt, err := bind.WaitMined(ctx, chain.RPC, tx)
if err != nil {
c.log.WithError(err).Error("failed to wait for state transition tx")
return
}

if receipt.Status == 0 {
c.log.WithError(err).WithFields(logan.F{
"receipt": utils.Prettify(receipt),
"chain": chain.Name,
}).Error("failed to wait for state transition tx")
return
}

c.log.
WithFields(logan.F{
"tx_id": tx.Hash(),
"tx_index": receipt.TransactionIndex,
"block_number": receipt.BlockNumber,
"gas_used": receipt.GasUsed,
}).
Info("evm transaction confirmed")
}

func getStateInfo(transfer *rarimocore.IdentityStateTransfer) (state contracts.ILightweightStateV2StateData, err error) {
state.Id = new(big.Int).SetBytes(hexutil.MustDecode(transfer.Id))

Expand Down
4 changes: 2 additions & 2 deletions internal/services/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *ServerImpl) StateRelay(ctx context.Context, req *types.MsgStateRelayReq

s.log.Debugf("Relay request: State - %s; chain - %s", req.Body.Hash, req.Body.Chain)

tx, err := s.relayer.StateRelay(ctx, req.Body.Hash, req.Body.Chain)
tx, err := s.relayer.StateRelay(ctx, req.Body.Hash, req.Body.Chain, req.Body.WaitConfirm)

if err != nil {
s.log.WithError(err).Debugf("Request failed")
Expand Down Expand Up @@ -86,7 +86,7 @@ func (s *ServerImpl) GistRelay(ctx context.Context, req *types.MsgGISTRelayReque

s.log.Infof("Relay request: GIST - %s; chain - %s", req.Body.Hash, req.Body.Chain)

tx, err := s.relayer.GistRelay(ctx, req.Body.Hash, req.Body.Chain)
tx, err := s.relayer.GistRelay(ctx, req.Body.Hash, req.Body.Chain, req.Body.WaitConfirm)

if err != nil {
s.log.WithError(err).Debugf("Request failed")
Expand Down
101 changes: 56 additions & 45 deletions internal/types/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7fbad1d

Please sign in to comment.