Skip to content

Commit

Permalink
adding check state/gist exist
Browse files Browse the repository at this point in the history
  • Loading branch information
olegfomenko committed Mar 20, 2024
1 parent 9b1ee87 commit 44832f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/ethereum/go-ethereum v1.10.26
github.com/go-redis/redis/v8 v8.11.5
github.com/gogo/protobuf v1.3.3
github.com/rarimo/rarimo-core v1.0.7-0.20231123231906-566dc0033e75
github.com/rarimo/rarimo-core v1.1.0
github.com/rarimo/saver-grpc-lib v1.0.0
github.com/spf13/cast v1.5.1
github.com/tendermint/tendermint v0.34.27
Expand Down Expand Up @@ -119,6 +119,7 @@ require (
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rarimo/broadcaster-svc v1.0.2 // indirect
github.com/rarimo/go-merkle v0.0.0-20231004122345-36fa49031c66 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,10 @@ github.com/rarimo/broadcaster-svc v1.0.2 h1:ExQcjjWCRP5+POLDlZHrTD1ffUsBH+Dgv5FA
github.com/rarimo/broadcaster-svc v1.0.2/go.mod h1:lYIHy+X4IqQt4eBdtMN/V352H3EV0/gO8G+32SFwUWI=
github.com/rarimo/cosmos-sdk v0.46.7 h1:jU2PiWzc+19SF02cXM0O0puKPeH1C6Q6t2lzJ9s1ejc=
github.com/rarimo/cosmos-sdk v0.46.7/go.mod h1:fqKqz39U5IlEFb4nbQ72951myztsDzFKKDtffYJ63nk=
github.com/rarimo/rarimo-core v1.0.7-0.20231123231906-566dc0033e75 h1:gCVlgChRHbV6aemA+WM6BDyexRvr/V8MtG7cInwF/k4=
github.com/rarimo/rarimo-core v1.0.7-0.20231123231906-566dc0033e75/go.mod h1:S8d3kZWPFuNsy7llrzUd8Rj92WSCrjSH2YEUD+2A+Ww=
github.com/rarimo/go-merkle v0.0.0-20231004122345-36fa49031c66 h1:1KAU4rfWWJwAJ/kencagL3k5BXN3HhP004QNkEUgvDE=
github.com/rarimo/go-merkle v0.0.0-20231004122345-36fa49031c66/go.mod h1:5Pt9Lk8w7fWhyRO/NMb5x8DRhF2lESRVPT5uOlezInQ=
github.com/rarimo/rarimo-core v1.1.0 h1:b3zN2HYBJY5AUFFm285i9vsyTTM9b7XwQGshrLYkD1I=
github.com/rarimo/rarimo-core v1.1.0/go.mod h1:auP3KgxaSwkOUrLWBifjnMbbSQTSBA9Y/gVgq1WU/B4=
github.com/rarimo/saver-grpc-lib v1.0.0 h1:MGUVjYg7unmodYczVsLqlqZNkT4CIgKqdo6aQtL1qdE=
github.com/rarimo/saver-grpc-lib v1.0.0/go.mod h1:DpugWK5B7Hi0bdC3MPe/9FD2zCxaRwsyykdwxtF1Zgg=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
Expand Down
60 changes: 60 additions & 0 deletions internal/services/evm/stateChangeListener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package evm

import (
"context"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/rarimo/rarimo-core/x/rarimocore/crypto/pkg"
rarimocore "github.com/rarimo/rarimo-core/x/rarimocore/types"
"google.golang.org/grpc"
"math/big"
"time"

Expand Down Expand Up @@ -59,6 +63,7 @@ func RunStateChangeListener(ctx context.Context, cfg config.Config) {
fromBlock: cfg.Ethereum().StartFromBlock,
blockWindow: cfg.Ethereum().BlockWindow,
maxBlocks: cfg.States().MaxBlocksPerRequest,
cosmos: cfg.Cosmos(),
}

running.WithBackOff(ctx, log, runnerName,
Expand All @@ -81,6 +86,7 @@ type stateChangeListener struct {
broadcaster broadcaster.Broadcaster
msger stateUpdateMsger
blockHandler blockHandler
cosmos *grpc.ClientConn

filter func(string) bool
fromBlock uint64
Expand Down Expand Up @@ -154,6 +160,16 @@ func (l *stateChangeListener) subscription(ctx context.Context) error {
continue
}

exist, err := l.checkGISTExist(ctx, msg1)
if err != nil {
l.log.WithError(err).WithField("tx_hash", e.Raw.TxHash.String()).Error("failed to check operation already exist")
}

if exist {
l.log.WithField("tx_hash", e.Raw.TxHash.String()).Debug("operation already exist")
continue
}

if err := l.broadcaster.BroadcastTx(ctx, msg1); err != nil {
l.log.WithError(err).WithField("tx_hash", e.Raw.TxHash.String()).Error(err, "failed to broadcast GIST updated msg")
continue
Expand All @@ -175,6 +191,16 @@ func (l *stateChangeListener) subscription(ctx context.Context) error {
continue
}

exist, err = l.checkStateExist(ctx, msg)
if err != nil {
l.log.WithError(err).WithField("tx_hash", e.Raw.TxHash.String()).Error("failed to check operation already exist")
}

if exist {
l.log.WithField("tx_hash", e.Raw.TxHash.String()).Debug("operation already exist")
continue
}

if err := l.broadcaster.BroadcastTx(ctx, msg); err != nil {
l.log.WithError(err).WithField("tx_hash", e.Raw.TxHash.String()).Error(err, "failed to broadcast state updated msg")
continue
Expand All @@ -184,6 +210,40 @@ func (l *stateChangeListener) subscription(ctx context.Context) error {
return nil
}

func (l *stateChangeListener) checkGISTExist(ctx context.Context, msg *oracletypes.MsgCreateIdentityGISTTransferOp) (bool, error) {
resp, err := oracletypes.NewQueryClient(l.cosmos).IdentityGISTTransfer(ctx, &oracletypes.QueryGetIdentityGISTTransferRequest{Msg: *msg})
if err != nil {
return false, errors.Wrap(err, "failed to get operation by message")
}

content, err := pkg.GetIdentityGISTTransferContent(&resp.Transfer)
if err != nil {
return false, errors.Wrap(err, "failed to get operation content")
}

index := hexutil.Encode(content.CalculateHash())

_, err = rarimocore.NewQueryClient(l.cosmos).Operation(ctx, &rarimocore.QueryGetOperationRequest{Index: index})
return err == nil, nil
}

func (l *stateChangeListener) checkStateExist(ctx context.Context, msg *oracletypes.MsgCreateIdentityStateTransferOp) (bool, error) {
resp, err := oracletypes.NewQueryClient(l.cosmos).IdentityStateTransfer(ctx, &oracletypes.QueryGetIdentityStateTransferRequest{Msg: *msg})
if err != nil {
return false, errors.Wrap(err, "failed to get operation by message")
}

content, err := pkg.GetIdentityStateTransferContent(&resp.Transfer)
if err != nil {
return false, errors.Wrap(err, "failed to get operation content")
}

index := hexutil.Encode(content.CalculateHash())

_, err = rarimocore.NewQueryClient(l.cosmos).Operation(ctx, &rarimocore.QueryGetOperationRequest{Index: index})
return err == nil, nil
}

func Map[T comparable](arr []T) map[T]struct{} {
res := make(map[T]struct{})
for _, v := range arr {
Expand Down

0 comments on commit 44832f2

Please sign in to comment.