diff --git a/go.mod b/go.mod index 4d178cc..ff6d777 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 9815ea2..fd34d53 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/services/evm/stateChangeListener.go b/internal/services/evm/stateChangeListener.go index 78b0940..0fda80c 100644 --- a/internal/services/evm/stateChangeListener.go +++ b/internal/services/evm/stateChangeListener.go @@ -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" @@ -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, @@ -81,6 +86,7 @@ type stateChangeListener struct { broadcaster broadcaster.Broadcaster msger stateUpdateMsger blockHandler blockHandler + cosmos *grpc.ClientConn filter func(string) bool fromBlock uint64 @@ -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 @@ -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 @@ -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 {