diff --git a/assembler/bsc_assembler.go b/assembler/bsc_assembler.go index 88be3b9..cda2ddc 100644 --- a/assembler/bsc_assembler.go +++ b/assembler/bsc_assembler.go @@ -1,9 +1,9 @@ package assembler import ( + "bytes" "encoding/hex" "fmt" - "github.com/bnb-chain/greenfield-relayer/metric" "time" "github.com/bnb-chain/greenfield-relayer/common" @@ -13,18 +13,20 @@ import ( "github.com/bnb-chain/greenfield-relayer/db/model" "github.com/bnb-chain/greenfield-relayer/executor" "github.com/bnb-chain/greenfield-relayer/logging" + "github.com/bnb-chain/greenfield-relayer/metric" "github.com/bnb-chain/greenfield-relayer/types" "github.com/bnb-chain/greenfield-relayer/util" "github.com/bnb-chain/greenfield-relayer/vote" ) type BSCAssembler struct { - config *config.Config - greenfieldExecutor *executor.GreenfieldExecutor - bscExecutor *executor.BSCExecutor - daoManager *dao.DaoManager - blsPubKey string - metricService *metric.MetricService + config *config.Config + greenfieldExecutor *executor.GreenfieldExecutor + bscExecutor *executor.BSCExecutor + daoManager *dao.DaoManager + blsPubKey []byte + hasRetrievedSequence bool + metricService *metric.MetricService } func NewBSCAssembler(cfg *config.Config, executor *executor.BSCExecutor, dao *dao.DaoManager, greenfieldExecutor *executor.GreenfieldExecutor, ms *metric.MetricService) *BSCAssembler { @@ -33,7 +35,7 @@ func NewBSCAssembler(cfg *config.Config, executor *executor.BSCExecutor, dao *da bscExecutor: executor, daoManager: dao, greenfieldExecutor: greenfieldExecutor, - blsPubKey: hex.EncodeToString(util.BlsPubKeyFromPrivKeyStr(cfg.GreenfieldConfig.BlsPrivateKey)), + blsPubKey: util.BlsPubKeyFromPrivKeyStr(cfg.GreenfieldConfig.BlsPrivateKey), metricService: ms, } } @@ -57,9 +59,14 @@ func (a *BSCAssembler) process(channelId types.ChannelId) error { if err != nil { return err } - isInturnRelyer := inturnRelayer.BlsPubKey == a.blsPubKey + inturnRelayerPubkey, err := hex.DecodeString(inturnRelayer.BlsPubKey) + if err != nil { + return err + } + isInturnRelyer := bytes.Equal(a.blsPubKey, inturnRelayerPubkey) a.metricService.SetGnfdInturnRelayerMetrics(isInturnRelyer, inturnRelayer.RelayInterval.Start, inturnRelayer.RelayInterval.End) var startSequence uint64 + if isInturnRelyer { seq, err := a.daoManager.SequenceDao.GetByChannelId(uint8(channelId)) if err != nil { @@ -67,15 +74,17 @@ func (a *BSCAssembler) process(channelId types.ChannelId) error { } startSequence = uint64(seq.Sequence) - // in-turn relayer get the start sequence from chain first time, it starts to relay after the sequence gets updated - now := time.Now().Unix() - timeDiff := now - int64(inturnRelayer.RelayInterval.Start) + if !a.hasRetrievedSequence { + // in-turn relayer get the start sequence from chain first time, it starts to relay after the sequence gets updated + now := time.Now().Unix() + timeDiff := now - int64(inturnRelayer.RelayInterval.Start) - if timeDiff < a.config.RelayConfig.GreenfieldSequenceUpdateLatency { - if timeDiff < 0 { - return fmt.Errorf("blockchain time and relayer time is not consistent, now %d should be after %d", now, inturnRelayer.RelayInterval.Start) + if timeDiff < a.config.RelayConfig.GreenfieldSequenceUpdateLatency { + if timeDiff < 0 { + return fmt.Errorf("blockchain time and relayer time is not consistent, now %d should be after %d", now, inturnRelayer.RelayInterval.Start) + } + return nil } - time.Sleep(time.Duration(timeDiff) * time.Second) startSequence, err = a.bscExecutor.GetNextDeliveryOracleSequenceWithRetry() if err != nil { return err @@ -84,7 +93,6 @@ func (a *BSCAssembler) process(channelId types.ChannelId) error { return err } } - logging.Logger.Debug("bsc relay as in-turn relayer") a.metricService.SetNextSequenceForChannelFromDB(uint8(channelId), startSequence) seqFromChain, err := a.bscExecutor.GetNextDeliveryOracleSequenceWithRetry() if err != nil { @@ -92,13 +100,13 @@ func (a *BSCAssembler) process(channelId types.ChannelId) error { } a.metricService.SetNextSequenceForChannelFromChain(uint8(channelId), seqFromChain) } else { + a.hasRetrievedSequence = false // non-inturn relayer retries every 10 second, gets the sequence from chain time.Sleep(time.Duration(a.config.RelayConfig.GreenfieldSequenceUpdateLatency) * time.Second) startSequence, err = a.bscExecutor.GetNextDeliveryOracleSequenceWithRetry() if err != nil { return err } - logging.Logger.Debug("bsc relay as out-turn relayer") if err := a.daoManager.BSCDao.UpdateBatchPackagesStatusToDelivered(startSequence); err != nil { return err } diff --git a/assembler/greenfield_assembler.go b/assembler/greenfield_assembler.go index 182c114..f63fee1 100644 --- a/assembler/greenfield_assembler.go +++ b/assembler/greenfield_assembler.go @@ -1,8 +1,10 @@ package assembler import ( + "bytes" "encoding/hex" "fmt" + "sync" "time" "github.com/bnb-chain/greenfield-relayer/common" @@ -19,51 +21,69 @@ import ( ) type GreenfieldAssembler struct { - config *config.Config - bscExecutor *executor.BSCExecutor - greenfieldExecutor *executor.GreenfieldExecutor - daoManager *dao.DaoManager - blsPubKey string - metricService *metric.MetricService + mutex sync.RWMutex + config *config.Config + bscExecutor *executor.BSCExecutor + greenfieldExecutor *executor.GreenfieldExecutor + daoManager *dao.DaoManager + blsPubKey []byte + hasRetrievedSequenceByChannelMap map[types.ChannelId]bool // flag for in-turn relayer that if it has requested the sequence from chain during its interval + metricService *metric.MetricService } func NewGreenfieldAssembler(cfg *config.Config, executor *executor.GreenfieldExecutor, dao *dao.DaoManager, bscExecutor *executor.BSCExecutor, ms *metric.MetricService) *GreenfieldAssembler { + channels := cfg.GreenfieldConfig.MonitorChannelList + retrievedSequenceByChannelMap := make(map[types.ChannelId]bool) + for _, c := range channels { + retrievedSequenceByChannelMap[types.ChannelId(c)] = false + } return &GreenfieldAssembler{ - config: cfg, - greenfieldExecutor: executor, - daoManager: dao, - bscExecutor: bscExecutor, - blsPubKey: hex.EncodeToString(util.BlsPubKeyFromPrivKeyStr(cfg.GreenfieldConfig.BlsPrivateKey)), - metricService: ms, + config: cfg, + greenfieldExecutor: executor, + daoManager: dao, + bscExecutor: bscExecutor, + blsPubKey: util.BlsPubKeyFromPrivKeyStr(cfg.GreenfieldConfig.BlsPrivateKey), + hasRetrievedSequenceByChannelMap: retrievedSequenceByChannelMap, + metricService: ms, } } // AssembleTransactionsLoop assemble a tx by gathering votes signature and then call the build-in smart-contract func (a *GreenfieldAssembler) AssembleTransactionsLoop() { - for _, c := range a.getMonitorChannels() { - go a.assembleTransactionAndSendForChannel(types.ChannelId(c)) - } -} - -func (a *GreenfieldAssembler) assembleTransactionAndSendForChannel(channelId types.ChannelId) { ticker := time.NewTicker(common.RetryInterval) for range ticker.C { - if err := a.process(channelId); err != nil { - logging.Logger.Errorf("encounter error when relaying tx, err=%s ", err.Error()) + inturnRelayer, err := a.bscExecutor.GetInturnRelayer() + if err != nil { + logging.Logger.Errorf("encounter error when retrieving in-turn relayer from chain, err=%s ", err.Error()) + continue + } + wg := new(sync.WaitGroup) + errChan := make(chan error) + for _, c := range a.getMonitorChannels() { + wg.Add(1) + go a.assembleTransactionAndSendForChannel(types.ChannelId(c), inturnRelayer, errChan, wg) } + wg.Wait() + } +} + +func (a *GreenfieldAssembler) assembleTransactionAndSendForChannel(channelId types.ChannelId, inturnRelayer *types.InturnRelayer, errChan chan error, wg *sync.WaitGroup) { + defer wg.Done() + err := a.process(channelId, inturnRelayer) + if err != nil { + errChan <- err } } -func (a *GreenfieldAssembler) process(channelId types.ChannelId) error { - logging.Logger.Infof("current time is %d", time.Now().Unix()) - inturnRelayer, err := a.bscExecutor.GetInturnRelayer() +func (a *GreenfieldAssembler) process(channelId types.ChannelId, inturnRelayer *types.InturnRelayer) error { + var startSequence uint64 + inturnRelayerPubkey, err := hex.DecodeString(inturnRelayer.BlsPublicKey) if err != nil { return err } - isInturnRelyer := inturnRelayer.BlsPublicKey == a.blsPubKey + isInturnRelyer := bytes.Equal(a.blsPubKey, inturnRelayerPubkey) a.metricService.SetBSCInturnRelayerMetrics(isInturnRelyer, inturnRelayer.Start, inturnRelayer.End) - var startSequence uint64 if isInturnRelyer { // get next delivered sequence from DB seq, err := a.daoManager.SequenceDao.GetByChannelId(uint8(channelId)) @@ -71,17 +91,16 @@ func (a *GreenfieldAssembler) process(channelId types.ChannelId) error { return err } startSequence = uint64(seq.Sequence) - - // in-turn relayer get the start sequence from chain first time, it starts to relay after the sequence - // get updated - now := time.Now().Unix() - timeDiff := now - int64(inturnRelayer.Start) - - if timeDiff < a.config.RelayConfig.BSCSequenceUpdateLatency { - if timeDiff < 0 { - return fmt.Errorf("blockchain time and relayer time is not consistent, now %d should be after %d", now, inturnRelayer.Start) + // in-turn relayer get the start sequence from chain once during its interval + if !a.hasRetrievedSequenceByChannelMap[channelId] { + now := time.Now().Unix() + timeDiff := now - int64(inturnRelayer.Start) + if timeDiff < a.config.RelayConfig.BSCSequenceUpdateLatency { + if timeDiff < 0 { + return fmt.Errorf("blockchain time and relayer time is not consistent, now %d should be after %d", now, inturnRelayer.Start) + } + return nil } - time.Sleep(time.Duration(timeDiff) * time.Second) startSequence, err = a.greenfieldExecutor.GetNextDeliverySequenceForChannelWithRetry(channelId) if err != nil { return err @@ -89,6 +108,9 @@ func (a *GreenfieldAssembler) process(channelId types.ChannelId) error { if err = a.daoManager.SequenceDao.Upsert(uint8(channelId), startSequence); err != nil { return err } + a.mutex.Lock() + a.hasRetrievedSequenceByChannelMap[channelId] = true + a.mutex.Unlock() } a.metricService.SetNextSequenceForChannelFromDB(uint8(channelId), startSequence) seqFromChain, err := a.greenfieldExecutor.GetNextDeliverySequenceForChannelWithRetry(channelId) @@ -97,12 +119,15 @@ func (a *GreenfieldAssembler) process(channelId types.ChannelId) error { } a.metricService.SetNextSequenceForChannelFromChain(uint8(channelId), seqFromChain) } else { + a.mutex.Lock() + a.hasRetrievedSequenceByChannelMap[channelId] = false + a.mutex.Unlock() + time.Sleep(time.Duration(a.config.RelayConfig.BSCSequenceUpdateLatency) * time.Second) - startSequence, err = a.greenfieldExecutor.GetNextDeliverySequenceForChannelWithRetry(channelId) + startSequence, err := a.greenfieldExecutor.GetNextDeliverySequenceForChannelWithRetry(channelId) if err != nil { return err } - logging.Logger.Debug("gnfd relay as out-turn relayer") if err := a.daoManager.GreenfieldDao.UpdateBatchTransactionStatusToDelivered(startSequence); err != nil { return err } @@ -115,7 +140,6 @@ func (a *GreenfieldAssembler) process(channelId types.ChannelId) error { if endSequence == -1 { return nil } - logging.Logger.Debugf("channel %d start seq is %d, end seq is %d ", channelId, startSequence, endSequence) nonce, err := a.bscExecutor.GetNonce() if err != nil { return err @@ -141,7 +165,6 @@ func (a *GreenfieldAssembler) process(channelId types.ChannelId) error { return err } logging.Logger.Infof("relayed tx with channel id %d and sequence %d ", tx.ChannelId, tx.Sequence) - nonce++ } return nil diff --git a/config/config.json b/config/config.json index 168ad25..3fd998f 100644 --- a/config/config.json +++ b/config/config.json @@ -40,8 +40,8 @@ "greenfield_event_type_cross_chain": "cosmos.crosschain.v1.EventCrossChain", "bsc_cross_chain_package_event_name": "CrossChainPackage", "cross_chain_package_event_hex": "0x64998dc5a229e7324e622192f111c691edccc3534bbea4b2bd90fbaec936845a", - "cross_chain_contract_addr": "0xd2253A26e6d5b729dDBf4bCce5A78F93C725b455", - "greenfield_light_client_contract_addr": "0x349a42f907c7562B3aaD4431780E4596bC2a053f" + "cross_chain_contract_addr": "0x3a282380958194D1131bC49056abb712Ab98b82B", + "greenfield_light_client_contract_addr": "0x60B1E6259944Ea8CEEfFAe2d50Df33EE3CCc593A" }, "log_config": { "level": "DEBUG", @@ -54,7 +54,7 @@ "compress": false }, "admin_config": { - "listen_addr": "0.0.0.0:8080" + "port": 8080 }, "db_config": { "dialect": "mysql", diff --git a/config/local/config_local_0.json b/config/local/config_local_0.json index 6233be2..38dd34c 100644 --- a/config/local/config_local_0.json +++ b/config/local/config_local_0.json @@ -40,8 +40,8 @@ "greenfield_event_type_cross_chain": "cosmos.crosschain.v1.EventCrossChain", "bsc_cross_chain_package_event_name": "CrossChainPackage", "cross_chain_package_event_hex": "0x64998dc5a229e7324e622192f111c691edccc3534bbea4b2bd90fbaec936845a", - "cross_chain_contract_addr": "0xd2253A26e6d5b729dDBf4bCce5A78F93C725b455", - "greenfield_light_client_contract_addr": "0x349a42f907c7562B3aaD4431780E4596bC2a053f" + "cross_chain_contract_addr": "0x3a282380958194D1131bC49056abb712Ab98b82B", + "greenfield_light_client_contract_addr": "0x60B1E6259944Ea8CEEfFAe2d50Df33EE3CCc593A" }, "log_config": { "level": "DEBUG", @@ -54,7 +54,7 @@ "compress": false }, "admin_config": { - "listen_addr": "0.0.0.0:8080" + "port": 8080 }, "db_config": { "dialect": "mysql", diff --git a/config/local/config_local_1.json b/config/local/config_local_1.json index 8555ff1..941ab10 100644 --- a/config/local/config_local_1.json +++ b/config/local/config_local_1.json @@ -40,8 +40,8 @@ "greenfield_event_type_cross_chain": "cosmos.crosschain.v1.EventCrossChain", "bsc_cross_chain_package_event_name": "CrossChainPackage", "cross_chain_package_event_hex": "0x64998dc5a229e7324e622192f111c691edccc3534bbea4b2bd90fbaec936845a", - "cross_chain_contract_addr": "0xd2253A26e6d5b729dDBf4bCce5A78F93C725b455", - "greenfield_light_client_contract_addr": "0x349a42f907c7562B3aaD4431780E4596bC2a053f" + "cross_chain_contract_addr": "0x3a282380958194D1131bC49056abb712Ab98b82B", + "greenfield_light_client_contract_addr": "0x60B1E6259944Ea8CEEfFAe2d50Df33EE3CCc593A" }, "log_config": { "level": "DEBUG", @@ -54,7 +54,7 @@ "compress": false }, "admin_config": { - "listen_addr": "0.0.0.0:8080" + "port": 8081 }, "db_config": { "dialect": "mysql", diff --git a/config/local/config_local_2.json b/config/local/config_local_2.json index cd1a31b..5aec5a9 100644 --- a/config/local/config_local_2.json +++ b/config/local/config_local_2.json @@ -40,8 +40,8 @@ "greenfield_event_type_cross_chain": "cosmos.crosschain.v1.EventCrossChain", "bsc_cross_chain_package_event_name": "CrossChainPackage", "cross_chain_package_event_hex": "0x64998dc5a229e7324e622192f111c691edccc3534bbea4b2bd90fbaec936845a", - "cross_chain_contract_addr": "0xd2253A26e6d5b729dDBf4bCce5A78F93C725b455", - "greenfield_light_client_contract_addr": "0x349a42f907c7562B3aaD4431780E4596bC2a053f" + "cross_chain_contract_addr": "0x3a282380958194D1131bC49056abb712Ab98b82B", + "greenfield_light_client_contract_addr": "0x60B1E6259944Ea8CEEfFAe2d50Df33EE3CCc593A" }, "log_config": { "level": "DEBUG", @@ -54,7 +54,7 @@ "compress": false }, "admin_config": { - "listen_addr": "0.0.0.0:8080" + "port": 8082 }, "db_config": { "dialect": "mysql", diff --git a/db/dao/greenfield_dao.go b/db/dao/greenfield_dao.go index 082e82d..4b9b103 100644 --- a/db/dao/greenfield_dao.go +++ b/db/dao/greenfield_dao.go @@ -129,3 +129,12 @@ func (d *GreenfieldDao) SaveSyncLightBlockTransaction(t *model.SyncLightBlockTra return dbTx.Create(t).Error }) } + +func (d *GreenfieldDao) GetLatestSyncedTransaction() (*model.SyncLightBlockTransaction, error) { + tx := model.SyncLightBlockTransaction{} + err := d.DB.Model(model.SyncLightBlockTransaction{}).Order("height desc").Take(&tx).Error + if err != nil && err != gorm.ErrRecordNotFound { + return nil, err + } + return &tx, nil +} diff --git a/executor/greenfield_executor.go b/executor/greenfield_executor.go index 9db8814..b65d5bd 100644 --- a/executor/greenfield_executor.go +++ b/executor/greenfield_executor.go @@ -227,7 +227,7 @@ func (e *GreenfieldExecutor) GetValidatorsBlsPublicKey() ([]string, error) { } var keys []string for _, v := range validators { - keys = append(keys, hex.EncodeToString(v.RelayerBlsKey)) + keys = append(keys, hex.EncodeToString(v.BlsKey)) } return keys, nil } diff --git a/executor/greenfield_executor_test.go b/executor/greenfield_executor_test.go index 855b528..46d0dda 100644 --- a/executor/greenfield_executor_test.go +++ b/executor/greenfield_executor_test.go @@ -59,7 +59,7 @@ func TestGetConsensusStatus(t *testing.T) { t.Logf("validator pubkey %s", hexutil.Encode(validator.PubKey.Bytes())) t.Logf("validator votingpower %d", validator.VotingPower) t.Logf("relayeraddress %s", hex.EncodeToString(validator.RelayerAddress)) - t.Logf("relayer bls pub key %s", hex.EncodeToString(validator.RelayerBlsKey)) + t.Logf("relayer bls pub key %s", hex.EncodeToString(validator.BlsKey)) } } @@ -73,6 +73,6 @@ func TestGetLatestValidators(t *testing.T) { t.Logf("validator pubkey %s", hexutil.Encode(validator.PubKey.Bytes())) t.Logf("validator votingpower %d", validator.VotingPower) t.Logf("relayeraddress %s", hex.EncodeToString(validator.RelayerAddress)) - t.Logf("relayer bls pub key %s", hex.EncodeToString(validator.RelayerBlsKey)) + t.Logf("relayer bls pub key %s", hex.EncodeToString(validator.BlsKey)) } } diff --git a/go.mod b/go.mod index 6fe4ed9..abcfcb2 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/avast/retry-go/v4 v4.3.1 github.com/aws/aws-sdk-go v1.40.45 - github.com/bnb-chain/greenfield-go-sdk v0.0.0-20230320034245-a8a57f1afdf2 + github.com/bnb-chain/greenfield-go-sdk v0.0.8-0.20230322041202-565a9730724b github.com/cosmos/cosmos-sdk v0.46.4 github.com/ethereum/go-ethereum v1.10.26 github.com/evmos/ethermint v0.6.1-0.20220919141022-34226aa7b1fa @@ -159,9 +159,9 @@ require ( replace ( cosmossdk.io/math => github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c github.com/bnb-chain/greenfield => github.com/bnb-chain/greenfield v0.0.9 - github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.0.11 + github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.0.2-0.20230320055343-ede3d1df3a12 github.com/ferranbt/fastssz => github.com/prysmaticlabs/fastssz v0.0.0-20220110145812-fafb696cae88 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/grpc-ecosystem/grpc-gateway/v2 => github.com/prysmaticlabs/grpc-gateway/v2 v2.3.1-0.20210702154020-550e1cd83ec1 - github.com/tendermint/tendermint => github.com/bnb-chain/greenfield-tendermint v0.0.2 + github.com/tendermint/tendermint => github.com/bnb-chain/greenfield-tendermint v0.0.3 ) diff --git a/go.sum b/go.sum index 26be866..71e3260 100644 --- a/go.sum +++ b/go.sum @@ -156,14 +156,14 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c= github.com/bnb-chain/greenfield v0.0.9 h1:nU3TgGloyysyyrbW/t9X2gdEvllxfIBudhuOdZTljV4= github.com/bnb-chain/greenfield v0.0.9/go.mod h1:uH3iyy7RfTaO+qM34AF1XN4CrCPcoUSTJs3zvaIWIf4= -github.com/bnb-chain/greenfield-cosmos-sdk v0.0.11 h1:8Vb57cJUoMVv9EL5ce43V8ZCPOvjNjrxs9+KyJw6JOg= -github.com/bnb-chain/greenfield-cosmos-sdk v0.0.11/go.mod h1:C0poOez5FBpH4Y/oa9jmco2fRojeMsJZGOZEX+GOv8w= +github.com/bnb-chain/greenfield-cosmos-sdk v0.0.2-0.20230320055343-ede3d1df3a12 h1:SfcVi85KJLVfzdChdfOjuZuyNl6pJEP26q3/OVqkZLw= +github.com/bnb-chain/greenfield-cosmos-sdk v0.0.2-0.20230320055343-ede3d1df3a12/go.mod h1:rvAY7ga/AakZWyYkA1zAsNtvKpdoyRFZTqF4MhFYzZ8= github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c h1:BLmdYaj7Dx0YOhfk77+KPPJSMCwpQl6f4Y30+801bf0= github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230228075616-68ac309b432c/go.mod h1:u/MXvf8wbUbCsAEyQSSYXXMsczAsFX48e2D6JI86T4o= -github.com/bnb-chain/greenfield-go-sdk v0.0.0-20230320034245-a8a57f1afdf2 h1:AORPMJYLfLP1lFPiLbYMFObJCuLb+e9/ZkHOoLHv9Ek= -github.com/bnb-chain/greenfield-go-sdk v0.0.0-20230320034245-a8a57f1afdf2/go.mod h1:0/EP4qwa/8Ok87tHGrk8k/hakxMKsYLA75DD+jd+aTE= -github.com/bnb-chain/greenfield-tendermint v0.0.2 h1:SNb3C9QhYklaLgY6onimLHR42IlB6NQJvQqwmj/1ybM= -github.com/bnb-chain/greenfield-tendermint v0.0.2/go.mod h1:/v9z9F6cq0+f7EGG92lYSLBcPYQDILoK91X8YM28hWo= +github.com/bnb-chain/greenfield-go-sdk v0.0.8-0.20230322041202-565a9730724b h1:d+JDpqg7985Uf11/0DStqX88oZ1ukxvKcdMz1p0vPhI= +github.com/bnb-chain/greenfield-go-sdk v0.0.8-0.20230322041202-565a9730724b/go.mod h1:349krYN7g4YPUYD5nKLGFU8+cyKTKqGapEzrjEHXfOs= +github.com/bnb-chain/greenfield-tendermint v0.0.3 h1:oWD58n7U5phHpVyfnezhg6WBxITRQpRwOWOhKDQ/kVE= +github.com/bnb-chain/greenfield-tendermint v0.0.3/go.mod h1:/v9z9F6cq0+f7EGG92lYSLBcPYQDILoK91X8YM28hWo= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/bradfitz/gomemcache v0.0.0-20170208213004-1952afaa557d/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60= diff --git a/listener/bsc_listener.go b/listener/bsc_listener.go index 2a45abd..595b9e4 100644 --- a/listener/bsc_listener.go +++ b/listener/bsc_listener.go @@ -79,8 +79,7 @@ func (l *BSCListener) poll() error { return nil } } - err = l.monitorCrossChainPkgAt(nextHeight, latestPolledBlock) - if err != nil { + if err = l.monitorCrossChainPkgAt(nextHeight, latestPolledBlock); err != nil { logging.Logger.Errorf("encounter error when monitor cross-chain packages at blockHeight=%d, err=%s", nextHeight, err.Error()) return err } diff --git a/listener/greenfield_listener.go b/listener/greenfield_listener.go index 8b558cd..4830400 100644 --- a/listener/greenfield_listener.go +++ b/listener/greenfield_listener.go @@ -2,6 +2,7 @@ package listener import ( "bytes" + "encoding/hex" "strconv" "sync" "time" @@ -142,49 +143,65 @@ func (l *GreenfieldListener) monitorEndBlockEvents(height uint64, endBlockEvents func (l *GreenfieldListener) monitorValidators(block *tmtypes.Block, errChan chan error, wg *sync.WaitGroup) { defer wg.Done() + if err := l.monitorValidatorsHelper(block); err != nil { + errChan <- err + } +} + +func (l *GreenfieldListener) monitorValidatorsHelper(block *tmtypes.Block) error { + lightClientLatestHeight, err := l.bscExecutor.GetLightClientLatestHeight() if err != nil { - errChan <- err - return + return err } nextHeight := uint64(block.Height) // happen when re-process block if nextHeight <= lightClientLatestHeight { - return + return nil } - logging.Logger.Infof("monitoring validator at height %d", nextHeight) + latestSyncedLightBlockTx, err := l.DaoManager.GreenfieldDao.GetLatestSyncedTransaction() + if err != nil { + return err + } + latestValidatorsHashFromDB, err := hex.DecodeString(latestSyncedLightBlockTx.ValidatorsHash) + if err != nil { + return err + } + + if bytes.Equal(block.ValidatorsHash[:], latestValidatorsHashFromDB) { + return nil + } nextValidators, err := l.greenfieldExecutor.QueryValidatorsAtHeight(nextHeight) if err != nil { - errChan <- err - return + return err } curValidators, err := l.greenfieldExecutor.QueryValidatorsAtHeight(nextHeight - 1) if err != nil { - errChan <- err - return + return err } if len(nextValidators) != len(curValidators) { if err := l.sync(nextHeight, block.ValidatorsHash.String()); err != nil { - errChan <- err + return err } - return + return nil } for idx, nextVal := range nextValidators { curVal := curValidators[idx] if !bytes.Equal(nextVal.Address.Bytes(), curVal.Address.Bytes()) || - !bytes.Equal(nextVal.RelayerBlsKey, curVal.RelayerBlsKey) || + !bytes.Equal(nextVal.BlsKey, curVal.BlsKey) || !bytes.Equal(nextVal.RelayerAddress, curVal.RelayerAddress) { if err := l.sync(nextHeight, block.ValidatorsHash.String()); err != nil { - errChan <- err + return err } break } } + return nil } func (l *GreenfieldListener) calNextHeight() (uint64, error) { diff --git a/vote/bsc_vote_processor.go b/vote/bsc_vote_processor.go index da22d10..f591843 100644 --- a/vote/bsc_vote_processor.go +++ b/vote/bsc_vote_processor.go @@ -358,7 +358,7 @@ func (p *BSCVoteProcessor) constructSignedVote(eventHash []byte) *votepool.Vote func (p *BSCVoteProcessor) isVotePubKeyValid(v *votepool.Vote, validators []*tmtypes.Validator) bool { for _, validator := range validators { - if bytes.Equal(v.PubKey[:], validator.RelayerBlsKey[:]) { + if bytes.Equal(v.PubKey[:], validator.BlsKey[:]) { return true } } diff --git a/vote/util.go b/vote/util.go index 00ae71f..9edc272 100644 --- a/vote/util.go +++ b/vote/util.go @@ -48,7 +48,7 @@ func AggregateSignatureAndValidatorBitSet(votes []*model.Vote, validators interf } } else { for idx, valInfo := range validators.([]*tmtypes.Validator) { - if _, ok := voteAddrSet[hex.EncodeToString(valInfo.RelayerBlsKey[:])]; ok { + if _, ok := voteAddrSet[hex.EncodeToString(valInfo.BlsKey[:])]; ok { valBitSet.Set(uint(idx)) } }