From 427066566408ab0412eaa663d2e8e1206ac8017c Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Thu, 4 Apr 2024 15:48:58 +0300 Subject: [PATCH 1/7] adding retries on hub client This reverts commit c534d1d867284ccf443bbec5e1c2ab80aad58f0f. --- settlement/dymension/dymension.go | 51 +++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index ad7a6660a..bb2489d0b 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -6,6 +6,7 @@ import ( "strconv" "time" + "cosmossdk.io/errors" "github.com/avast/retry-go/v4" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" @@ -15,7 +16,6 @@ import ( rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" "github.com/google/uuid" "github.com/ignite/cli/ignite/pkg/cosmosaccount" - "github.com/pkg/errors" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sequencertypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types" @@ -286,14 +286,22 @@ func (d *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult * // GetLatestBatch returns the latest batch from the Dymension Hub. func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieveBatch, error) { - latestStateInfoIndexResp, err := d.rollappQueryClient.LatestStateIndex(d.ctx, - &rollapptypes.QueryGetLatestStateIndexRequest{RollappId: d.config.RollappID}) - if latestStateInfoIndexResp == nil { - return nil, settlement.ErrBatchNotFound - } + var latestStateInfoIndexResp *rollapptypes.QueryGetLatestStateIndexResponse + err := retry.Do(func() error { + var err error + latestStateInfoIndexResp, err = d.rollappQueryClient.LatestStateIndex(d.ctx, + &rollapptypes.QueryGetLatestStateIndexRequest{RollappId: d.config.RollappID}) + return err + }, retry.Context(d.ctx), retry.LastErrorOnly(true), + retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) + if err != nil { return nil, err } + if latestStateInfoIndexResp == nil { + return nil, settlement.ErrBatchNotFound + } + latestBatch, err := d.GetBatchAtIndex(rollappID, latestStateInfoIndexResp.StateIndex.Index) if err != nil { return nil, err @@ -303,26 +311,43 @@ func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieve // GetBatchAtIndex returns the batch at the given index from the Dymension Hub. func (d *HubClient) GetBatchAtIndex(rollappID string, index uint64) (*settlement.ResultRetrieveBatch, error) { - stateInfoResp, err := d.rollappQueryClient.StateInfo(d.ctx, - &rollapptypes.QueryGetStateInfoRequest{RollappId: d.config.RollappID, Index: index}) - if stateInfoResp == nil { - return nil, settlement.ErrBatchNotFound - } + var stateInfoResp *rollapptypes.QueryGetStateInfoResponse + err := retry.Do(func() error { + var err error + stateInfoResp, err = d.rollappQueryClient.StateInfo(d.ctx, + &rollapptypes.QueryGetStateInfoRequest{RollappId: d.config.RollappID, Index: index}) + return err + }, retry.Context(d.ctx), retry.LastErrorOnly(true), + retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) + if err != nil { return nil, err } + if stateInfoResp == nil { + return nil, settlement.ErrBatchNotFound + } return d.convertStateInfoToResultRetrieveBatch(&stateInfoResp.StateInfo) } // GetSequencers returns the bonded sequencers of the given rollapp. func (d *HubClient) GetSequencers(rollappID string) ([]*types.Sequencer, error) { + var res *sequencertypes.QueryGetSequencersByRollappByStatusResponse req := &sequencertypes.QueryGetSequencersByRollappByStatusRequest{ RollappId: d.config.RollappID, Status: sequencertypes.Bonded, } - res, err := d.sequencerQueryClient.SequencersByRollappByStatus(d.ctx, req) + err := retry.Do(func() error { + var err error + res, err = d.sequencerQueryClient.SequencersByRollappByStatus(d.ctx, req) + if err != nil { + return errors.Wrapf(settlement.ErrNoSequencerForRollapp, "rollappID: %s", rollappID) + } + return nil + }, retry.Context(d.ctx), retry.LastErrorOnly(true), + retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) + if err != nil { - return nil, errors.Wrapf(settlement.ErrNoSequencerForRollapp, "rollappID: %s", rollappID) + return nil, err } sequencersList := make([]*types.Sequencer, 0, len(res.Sequencers)) From 2eb737c505889c370a647fa32417119cd7674f89 Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Thu, 4 Apr 2024 19:16:23 +0300 Subject: [PATCH 2/7] handling queries for non existing data --- block/manager.go | 21 +-------------------- settlement/dymension/dymension.go | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/block/manager.go b/block/manager.go index a9d1cdc29..1f8833a5f 100644 --- a/block/manager.go +++ b/block/manager.go @@ -12,7 +12,6 @@ import ( "code.cloudfoundry.org/go-diodes" - "github.com/avast/retry-go/v4" "github.com/dymensionxyz/dymint/node/events" "github.com/dymensionxyz/dymint/p2p" "github.com/dymensionxyz/dymint/utils" @@ -279,23 +278,5 @@ func (m *Manager) applyBlockCallback(event pubsub.Message) { // getLatestBatchFromSL gets the latest batch from the SL func (m *Manager) getLatestBatchFromSL(ctx context.Context) (*settlement.ResultRetrieveBatch, error) { - var resultRetrieveBatch *settlement.ResultRetrieveBatch - var err error - // Get latest batch from SL - err = retry.Do( - func() error { - resultRetrieveBatch, err = m.settlementClient.RetrieveBatch() - if err != nil { - return err - } - return nil - }, - retry.LastErrorOnly(true), - retry.Context(ctx), - retry.Attempts(1), - ) - if err != nil { - return resultRetrieveBatch, err - } - return resultRetrieveBatch, nil + return m.settlementClient.RetrieveBatch() } diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index bb2489d0b..394257888 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -6,6 +6,9 @@ import ( "strconv" "time" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "cosmossdk.io/errors" "github.com/avast/retry-go/v4" "github.com/cosmos/cosmos-sdk/client/flags" @@ -291,6 +294,11 @@ func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieve var err error latestStateInfoIndexResp, err = d.rollappQueryClient.LatestStateIndex(d.ctx, &rollapptypes.QueryGetLatestStateIndexRequest{RollappId: d.config.RollappID}) + + if status.Code(err) == codes.NotFound { + return retry.Unrecoverable(settlement.ErrBatchNotFound) + } + return err }, retry.Context(d.ctx), retry.LastErrorOnly(true), retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) @@ -298,9 +306,6 @@ func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieve if err != nil { return nil, err } - if latestStateInfoIndexResp == nil { - return nil, settlement.ErrBatchNotFound - } latestBatch, err := d.GetBatchAtIndex(rollappID, latestStateInfoIndexResp.StateIndex.Index) if err != nil { @@ -316,6 +321,11 @@ func (d *HubClient) GetBatchAtIndex(rollappID string, index uint64) (*settlement var err error stateInfoResp, err = d.rollappQueryClient.StateInfo(d.ctx, &rollapptypes.QueryGetStateInfoRequest{RollappId: d.config.RollappID, Index: index}) + + if status.Code(err) == codes.NotFound { + return retry.Unrecoverable(settlement.ErrBatchNotFound) + } + return err }, retry.Context(d.ctx), retry.LastErrorOnly(true), retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) @@ -533,11 +543,11 @@ func (d *HubClient) waitForBatchInclusion(batchStartHeight uint64) (*settlement. if err != nil { return err } - if latestBatch.Batch.StartHeight == batchStartHeight { - resultRetriveBatch = latestBatch - return nil + if latestBatch.Batch.StartHeight != batchStartHeight { + return settlement.ErrBatchNotFound } - return settlement.ErrBatchNotFound + resultRetriveBatch = latestBatch + return nil }, retry.Context(d.ctx), retry.LastErrorOnly(true), retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) return resultRetriveBatch, err From b81f064fb4fae3fee660012630e507e59c025734 Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Thu, 4 Apr 2024 19:31:27 +0300 Subject: [PATCH 3/7] fixed UT --- settlement/dymension/dymension.go | 6 +----- settlement/dymension/dymension_test.go | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index 394257888..0edccb3ec 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -9,7 +9,6 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "cosmossdk.io/errors" "github.com/avast/retry-go/v4" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" @@ -349,10 +348,7 @@ func (d *HubClient) GetSequencers(rollappID string) ([]*types.Sequencer, error) err := retry.Do(func() error { var err error res, err = d.sequencerQueryClient.SequencersByRollappByStatus(d.ctx, req) - if err != nil { - return errors.Wrapf(settlement.ErrNoSequencerForRollapp, "rollappID: %s", rollappID) - } - return nil + return err }, retry.Context(d.ctx), retry.LastErrorOnly(true), retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) diff --git a/settlement/dymension/dymension_test.go b/settlement/dymension/dymension_test.go index 9ad19ee11..4ec616f9c 100644 --- a/settlement/dymension/dymension_test.go +++ b/settlement/dymension/dymension_test.go @@ -185,7 +185,7 @@ func TestPostBatch(t *testing.T) { StartHeight: batch.StartHeight, StateInfoIndex: rollapptypes.StateInfoIndex{Index: 1}, DAPath: daMetaData.ToPath(), NumBlocks: 1}}, nil) } else { - rollappQueryClientMock.On("LatestStateIndex", mock.Anything, mock.Anything).Return(nil, nil) + rollappQueryClientMock.On("LatestStateIndex", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error")) } } hubClient, err := newDymensionHubClient(settlement.Config{}, pubsubServer, log.TestingLogger(), options...) From 50fbf2b4785e58595917d2fbee073e18daedea38 Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Thu, 4 Apr 2024 21:06:59 +0300 Subject: [PATCH 4/7] added additional check as reponse is ptr --- settlement/dymension/dymension.go | 14 +++++++++++++- settlement/errors.go | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index 0edccb3ec..70ddcbc5c 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -306,6 +306,11 @@ func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieve return nil, err } + // not supposed to happen, but just in case + if latestStateInfoIndexResp == nil { + return nil, settlement.ErrEmptyResponse + } + latestBatch, err := d.GetBatchAtIndex(rollappID, latestStateInfoIndexResp.StateIndex.Index) if err != nil { return nil, err @@ -332,9 +337,11 @@ func (d *HubClient) GetBatchAtIndex(rollappID string, index uint64) (*settlement if err != nil { return nil, err } + // not supposed to happen, but just in case if stateInfoResp == nil { - return nil, settlement.ErrBatchNotFound + return nil, settlement.ErrEmptyResponse } + return d.convertStateInfoToResultRetrieveBatch(&stateInfoResp.StateInfo) } @@ -356,6 +363,11 @@ func (d *HubClient) GetSequencers(rollappID string) ([]*types.Sequencer, error) return nil, err } + // not supposed to happen, but just in case + if res == nil { + return nil, settlement.ErrEmptyResponse + } + sequencersList := make([]*types.Sequencer, 0, len(res.Sequencers)) for _, sequencer := range res.Sequencers { var pubKey cryptotypes.PubKey diff --git a/settlement/errors.go b/settlement/errors.go index b55c1c58d..0aaf13f73 100644 --- a/settlement/errors.go +++ b/settlement/errors.go @@ -5,6 +5,8 @@ import "errors" var ( // ErrBatchNotFound is returned when a batch is not found for the rollapp. ErrBatchNotFound = errors.New("batch not found") + // ErrEmptyResponse is returned when the response is empty. + ErrEmptyResponse = errors.New("empty response") // ErrNoSequencerForRollapp is returned when a sequencer is not found for the rollapp. ErrNoSequencerForRollapp = errors.New("no sequencer registered on the hub for this rollapp") // ErrBatchNotAccepted is returned when a batch is not accepted by the settlement layer. From 4eb4983d0fca954d7b920ce94318b97f8bd4ce02 Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Thu, 4 Apr 2024 21:27:03 +0300 Subject: [PATCH 5/7] moved log interface to types. moved test logger to tetutil --- block/manager.go | 7 +++---- da/avail/avail.go | 5 ++--- da/avail/avail_test.go | 4 ++-- da/celestia/celestia.go | 5 ++--- da/celestia/mock/server.go | 5 ++--- da/da.go | 3 +-- da/grpc/grpc.go | 5 ++--- da/mock/mock.go | 5 ++--- mocks/settlement/settlement_layer.go | 2 +- p2p/client.go | 6 +++--- p2p/client_test.go | 4 ++-- p2p/gossip.go | 6 +++--- p2p/utils_test.go | 4 ++-- p2p/validator.go | 6 +++--- rpc/json/handler.go | 6 +++--- rpc/json/service.go | 8 ++++---- rpc/json/ws.go | 4 ++-- settlement/base.go | 5 ++--- settlement/dymension/dymension.go | 7 +++---- settlement/grpc/grpc.go | 7 +++---- settlement/mock/mock.go | 7 +++---- settlement/settlement.go | 3 +-- state/executor.go | 5 ++--- log/test/loggers.go => testutil/logger.go | 2 +- {log => types}/logger.go | 2 +- utils/events.go | 4 ++-- 26 files changed, 57 insertions(+), 70 deletions(-) rename log/test/loggers.go => testutil/logger.go (99%) rename {log => types}/logger.go (93%) diff --git a/block/manager.go b/block/manager.go index 1f8833a5f..3122c5c4e 100644 --- a/block/manager.go +++ b/block/manager.go @@ -24,7 +24,6 @@ import ( "github.com/dymensionxyz/dymint/config" "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/mempool" "github.com/dymensionxyz/dymint/settlement" "github.com/dymensionxyz/dymint/state" @@ -80,14 +79,14 @@ type Manager struct { syncCache map[uint64]*types.Block - logger log.Logger + logger types.Logger prevBlock map[uint64]*types.Block prevCommit map[uint64]*types.Commit } // getInitialState tries to load lastState from Store, and if it's not available it reads GenesisDoc. -func getInitialState(store store.Store, genesis *tmtypes.GenesisDoc, logger log.Logger) (types.State, error) { +func getInitialState(store store.Store, genesis *tmtypes.GenesisDoc, logger types.Logger) (types.State, error) { s, err := store.LoadState() if err == types.ErrNoStateFound { logger.Info("failed to find state in the store, creating new state from genesis") @@ -110,7 +109,7 @@ func NewManager( eventBus *tmtypes.EventBus, pubsub *pubsub.Server, p2pClient *p2p.Client, - logger log.Logger, + logger types.Logger, ) (*Manager, error) { proposerAddress, err := getAddress(proposerKey) diff --git a/da/avail/avail.go b/da/avail/avail.go index a5dd3e469..c3808205c 100644 --- a/da/avail/avail.go +++ b/da/avail/avail.go @@ -8,7 +8,6 @@ import ( "time" "github.com/avast/retry-go/v4" - "github.com/dymensionxyz/dymint/log" "github.com/gogo/protobuf/proto" "github.com/dymensionxyz/dymint/types" @@ -59,7 +58,7 @@ type DataAvailabilityLayerClient struct { client SubstrateApiI pubsubServer *pubsub.Server config Config - logger log.Logger + logger types.Logger ctx context.Context cancel context.CancelFunc txInclusionTimeout time.Duration @@ -99,7 +98,7 @@ func WithBatchRetryAttempts(attempts uint) da.Option { } // Init initializes DataAvailabilityLayerClient instance. -func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger log.Logger, options ...da.Option) error { +func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger types.Logger, options ...da.Option) error { c.logger = logger if len(config) > 0 { diff --git a/da/avail/avail_test.go b/da/avail/avail_test.go index c5f71d260..a7d64657f 100644 --- a/da/avail/avail_test.go +++ b/da/avail/avail_test.go @@ -7,8 +7,8 @@ import ( availtypes "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/dymensionxyz/dymint/da" "github.com/dymensionxyz/dymint/da/avail" - "github.com/dymensionxyz/dymint/log/test" mocks "github.com/dymensionxyz/dymint/mocks/da/avail" + "github.com/dymensionxyz/dymint/testutil" "github.com/dymensionxyz/dymint/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -133,7 +133,7 @@ func TestRetriveBatches(t *testing.T) { assert.NoError(err) // Start the DALC dalc := avail.DataAvailabilityLayerClient{} - err = dalc.Init(configBytes, pubsubServer, nil, test.NewLogger(t), options...) + err = dalc.Init(configBytes, pubsubServer, nil, testutil.NewLogger(t), options...) require.NoError(err) err = dalc.Start() require.NoError(err) diff --git a/da/celestia/celestia.go b/da/celestia/celestia.go index e9a41a2bd..de8c7bcbc 100644 --- a/da/celestia/celestia.go +++ b/da/celestia/celestia.go @@ -20,7 +20,6 @@ import ( "github.com/dymensionxyz/dymint/da" celtypes "github.com/dymensionxyz/dymint/da/celestia/types" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/store" "github.com/dymensionxyz/dymint/types" pb "github.com/dymensionxyz/dymint/types/pb/dymint" @@ -32,7 +31,7 @@ type DataAvailabilityLayerClient struct { pubsubServer *pubsub.Server config Config - logger log.Logger + logger types.Logger ctx context.Context cancel context.CancelFunc rpcRetryDelay time.Duration @@ -72,7 +71,7 @@ func WithSubmitRetryDelay(delay time.Duration) da.Option { } // Init initializes DataAvailabilityLayerClient instance. -func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger log.Logger, options ...da.Option) error { +func (c *DataAvailabilityLayerClient) Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger types.Logger, options ...da.Option) error { c.logger = logger if len(config) <= 0 { diff --git a/da/celestia/mock/server.go b/da/celestia/mock/server.go index 511df0b17..7e0677ecb 100644 --- a/da/celestia/mock/server.go +++ b/da/celestia/mock/server.go @@ -16,7 +16,6 @@ import ( "github.com/celestiaorg/go-cnc" "github.com/dymensionxyz/dymint/da" mockda "github.com/dymensionxyz/dymint/da/mock" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/store" "github.com/dymensionxyz/dymint/types" ) @@ -26,11 +25,11 @@ type Server struct { mock *mockda.DataAvailabilityLayerClient blockTime time.Duration server *http.Server - logger log.Logger + logger types.Logger } // NewServer creates new instance of Server. -func NewServer(blockTime time.Duration, logger log.Logger) *Server { +func NewServer(blockTime time.Duration, logger types.Logger) *Server { return &Server{ mock: new(mockda.DataAvailabilityLayerClient), blockTime: blockTime, diff --git a/da/da.go b/da/da.go index 1cece95ba..23e0ebb1a 100644 --- a/da/da.go +++ b/da/da.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/cometbft/cometbft/crypto/merkle" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/store" "github.com/dymensionxyz/dymint/types" "github.com/rollkit/celestia-openrpc/types/blob" @@ -182,7 +181,7 @@ type ResultRetrieveBatch struct { // It also contains life-cycle methods. type DataAvailabilityLayerClient interface { // Init is called once to allow DA client to read configuration and initialize resources. - Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger log.Logger, options ...Option) error + Init(config []byte, pubsubServer *pubsub.Server, kvStore store.KVStore, logger types.Logger, options ...Option) error // Start is called once, after Init. It's implementation should start operation of DataAvailabilityLayerClient. Start() error diff --git a/da/grpc/grpc.go b/da/grpc/grpc.go index 07eaf55d3..8fb4bf20a 100644 --- a/da/grpc/grpc.go +++ b/da/grpc/grpc.go @@ -9,7 +9,6 @@ import ( "google.golang.org/grpc/credentials/insecure" "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/store" "github.com/dymensionxyz/dymint/types" "github.com/dymensionxyz/dymint/types/pb/dalc" @@ -23,7 +22,7 @@ type DataAvailabilityLayerClient struct { conn *grpc.ClientConn client dalc.DALCServiceClient - logger log.Logger + logger types.Logger } // Config contains configuration options for DataAvailabilityLayerClient. @@ -43,7 +42,7 @@ var _ da.DataAvailabilityLayerClient = &DataAvailabilityLayerClient{} var _ da.BatchRetriever = &DataAvailabilityLayerClient{} // Init sets the configuration options. -func (d *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, _ store.KVStore, logger log.Logger, options ...da.Option) error { +func (d *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, _ store.KVStore, logger types.Logger, options ...da.Option) error { d.logger = logger if len(config) == 0 { d.config = DefaultConfig diff --git a/da/mock/mock.go b/da/mock/mock.go index decc6e896..834031632 100644 --- a/da/mock/mock.go +++ b/da/mock/mock.go @@ -8,7 +8,6 @@ import ( "time" "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/store" "github.com/dymensionxyz/dymint/types" "github.com/tendermint/tendermint/libs/pubsub" @@ -17,7 +16,7 @@ import ( // DataAvailabilityLayerClient is intended only for usage in tests. // It does actually ensures DA - it stores data in-memory. type DataAvailabilityLayerClient struct { - logger log.Logger + logger types.Logger dalcKV store.KVStore daHeight uint64 config config @@ -33,7 +32,7 @@ var _ da.DataAvailabilityLayerClient = &DataAvailabilityLayerClient{} var _ da.BatchRetriever = &DataAvailabilityLayerClient{} // Init is called once to allow DA client to read configuration and initialize resources. -func (m *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, dalcKV store.KVStore, logger log.Logger, options ...da.Option) error { +func (m *DataAvailabilityLayerClient) Init(config []byte, _ *pubsub.Server, dalcKV store.KVStore, logger types.Logger, options ...da.Option) error { m.logger = logger m.dalcKV = dalcKV m.daHeight = 1 diff --git a/mocks/settlement/settlement_layer.go b/mocks/settlement/settlement_layer.go index 1a789e023..a6e39e0b0 100644 --- a/mocks/settlement/settlement_layer.go +++ b/mocks/settlement/settlement_layer.go @@ -4,7 +4,7 @@ package mocks import ( da "github.com/dymensionxyz/dymint/da" - log "github.com/dymensionxyz/dymint/log" + log "github.com/dymensionxyz/dymint/types" mock "github.com/stretchr/testify/mock" diff --git a/p2p/client.go b/p2p/client.go index 042bdc617..04e1891be 100644 --- a/p2p/client.go +++ b/p2p/client.go @@ -23,7 +23,7 @@ import ( "go.uber.org/multierr" "github.com/dymensionxyz/dymint/config" - "github.com/dymensionxyz/dymint/log" + "github.com/dymensionxyz/dymint/types" ) // TODO(tzdybal): refactor to configuration parameters @@ -71,14 +71,14 @@ type Client struct { // it's required because of discovery.Advertise call cancel context.CancelFunc - logger log.Logger + logger types.Logger } // NewClient creates new Client object. // // Basic checks on parameters are done, and default parameters are provided for unset-configuration // TODO(tzdybal): consider passing entire config, not just P2P config, to reduce number of arguments -func NewClient(conf config.P2PConfig, privKey crypto.PrivKey, chainID string, logger log.Logger) (*Client, error) { +func NewClient(conf config.P2PConfig, privKey crypto.PrivKey, chainID string, logger types.Logger) (*Client, error) { if privKey == nil { return nil, errNoPrivKey } diff --git a/p2p/client_test.go b/p2p/client_test.go index 739aea83d..a486d5536 100644 --- a/p2p/client_test.go +++ b/p2p/client_test.go @@ -16,7 +16,7 @@ import ( "github.com/tendermint/tendermint/libs/log" "github.com/dymensionxyz/dymint/config" - "github.com/dymensionxyz/dymint/log/test" + "github.com/dymensionxyz/dymint/testutil" ) func TestClientStartup(t *testing.T) { @@ -166,7 +166,7 @@ func TestSeedStringParsing(t *testing.T) { t.Run(c.name, func(t *testing.T) { assert := assert.New(t) require := require.New(t) - logger := &test.MockLogger{} + logger := &testutil.MockLogger{} client, err := NewClient(config.P2PConfig{ GossipCacheSize: 50, BoostrapTime: 30 * time.Second}, privKey, "TestNetwork", logger) diff --git a/p2p/gossip.go b/p2p/gossip.go index 84a18a9ec..16de820d1 100644 --- a/p2p/gossip.go +++ b/p2p/gossip.go @@ -9,7 +9,7 @@ import ( "github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/peer" - "github.com/dymensionxyz/dymint/log" + "github.com/dymensionxyz/dymint/types" ) // GossipMessage represents message gossiped via P2P network (e.g. transaction, Block etc). @@ -36,13 +36,13 @@ type Gossiper struct { topic *pubsub.Topic sub *pubsub.Subscription - logger log.Logger + logger types.Logger } // NewGossiper creates new, ready to use instance of Gossiper. // // Returned Gossiper object can be used for sending (Publishing) and receiving messages in topic identified by topicStr. -func NewGossiper(host host.Host, ps *pubsub.PubSub, topicStr string, logger log.Logger, options ...GossiperOption) (*Gossiper, error) { +func NewGossiper(host host.Host, ps *pubsub.PubSub, topicStr string, logger types.Logger, options ...GossiperOption) (*Gossiper, error) { topic, err := ps.Join(topicStr) if err != nil { return nil, err diff --git a/p2p/utils_test.go b/p2p/utils_test.go index b9c6e7322..ade6347f4 100644 --- a/p2p/utils_test.go +++ b/p2p/utils_test.go @@ -17,7 +17,7 @@ import ( "go.uber.org/multierr" "github.com/dymensionxyz/dymint/config" - "github.com/dymensionxyz/dymint/log" + "github.com/dymensionxyz/dymint/types" ) type testNet []*Client @@ -63,7 +63,7 @@ func getAddr(sk crypto.PrivKey) (multiaddr.Multiaddr, error) { return a, nil } -func startTestNetwork(ctx context.Context, t *testing.T, n int, conf map[int]hostDescr, validators []GossipValidator, logger log.Logger) testNet { +func startTestNetwork(ctx context.Context, t *testing.T, n int, conf map[int]hostDescr, validators []GossipValidator, logger types.Logger) testNet { t.Helper() require := require.New(t) diff --git a/p2p/validator.go b/p2p/validator.go index 43f542ff1..95483d3ae 100644 --- a/p2p/validator.go +++ b/p2p/validator.go @@ -4,9 +4,9 @@ import ( "context" "errors" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/mempool" nodemempool "github.com/dymensionxyz/dymint/node/mempool" + "github.com/dymensionxyz/dymint/types" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/pubsub" corep2p "github.com/tendermint/tendermint/p2p" @@ -24,14 +24,14 @@ type IValidator interface { // Validator is a validator for messages gossiped in the p2p network. type Validator struct { - logger log.Logger + logger types.Logger localPubsubServer *pubsub.Server } var _ IValidator = (*Validator)(nil) // NewValidator creates a new Validator. -func NewValidator(logger log.Logger, pusbsubServer *pubsub.Server) *Validator { +func NewValidator(logger types.Logger, pusbsubServer *pubsub.Server) *Validator { return &Validator{ logger: logger, localPubsubServer: pusbsubServer, diff --git a/rpc/json/handler.go b/rpc/json/handler.go index 955b96403..69f104b04 100644 --- a/rpc/json/handler.go +++ b/rpc/json/handler.go @@ -13,7 +13,7 @@ import ( tmjson "github.com/tendermint/tendermint/libs/json" - "github.com/dymensionxyz/dymint/log" + "github.com/dymensionxyz/dymint/types" "github.com/gorilla/rpc/v2" "github.com/gorilla/rpc/v2/json2" ) @@ -22,10 +22,10 @@ type handler struct { srv *service mux *http.ServeMux codec rpc.Codec - logger log.Logger + logger types.Logger } -func newHandler(s *service, codec rpc.Codec, logger log.Logger) *handler { +func newHandler(s *service, codec rpc.Codec, logger types.Logger) *handler { mux := http.NewServeMux() h := &handler{ srv: s, diff --git a/rpc/json/service.go b/rpc/json/service.go index 2ca67b739..42ff81bd0 100644 --- a/rpc/json/service.go +++ b/rpc/json/service.go @@ -17,12 +17,12 @@ import ( ctypes "github.com/tendermint/tendermint/rpc/core/types" rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/rpc/client" + "github.com/dymensionxyz/dymint/types" ) // GetHTTPHandler returns handler configured to serve Tendermint-compatible RPC. -func GetHTTPHandler(l *client.Client, logger log.Logger) (http.Handler, error) { +func GetHTTPHandler(l *client.Client, logger types.Logger) (http.Handler, error) { return newHandler(newService(l, logger), json2.NewCodec(), logger), nil } @@ -47,10 +47,10 @@ func newMethod(m interface{}) *method { type service struct { client *client.Client methods map[string]*method - logger log.Logger + logger types.Logger } -func newService(c *client.Client, l log.Logger) *service { +func newService(c *client.Client, l types.Logger) *service { s := service{ client: c, logger: l, diff --git a/rpc/json/ws.go b/rpc/json/ws.go index 84139c8a4..e5210fe5d 100644 --- a/rpc/json/ws.go +++ b/rpc/json/ws.go @@ -8,14 +8,14 @@ import ( "github.com/gorilla/websocket" - "github.com/dymensionxyz/dymint/log" + "github.com/dymensionxyz/dymint/types" tmpubsub "github.com/tendermint/tendermint/libs/pubsub" ) type wsConn struct { conn *websocket.Conn queue chan []byte - logger log.Logger + logger types.Logger } func (wsc *wsConn) sendLoop() { diff --git a/settlement/base.go b/settlement/base.go index 93f3a697b..2f55c63ae 100644 --- a/settlement/base.go +++ b/settlement/base.go @@ -5,14 +5,13 @@ import ( "fmt" "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/types" "github.com/tendermint/tendermint/libs/pubsub" ) // BaseLayerClient is intended only for usage in tests. type BaseLayerClient struct { - logger log.Logger + logger types.Logger pubsub *pubsub.Server sequencersList []*types.Sequencer config Config @@ -31,7 +30,7 @@ func WithHubClient(hubClient HubClient) Option { } // Init is called once. it initializes the struct members. -func (b *BaseLayerClient) Init(config Config, pubsub *pubsub.Server, logger log.Logger, options ...Option) error { +func (b *BaseLayerClient) Init(config Config, pubsub *pubsub.Server, logger types.Logger, options ...Option) error { var err error b.config = config b.pubsub = pubsub diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index 70ddcbc5c..bdb6a20a2 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -22,7 +22,6 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sequencertypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types" "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/settlement" "github.com/dymensionxyz/dymint/types" "github.com/dymensionxyz/dymint/utils" @@ -61,7 +60,7 @@ type LayerClient struct { var _ settlement.LayerI = &LayerClient{} // Init is called once. it initializes the struct members. -func (dlc *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, logger log.Logger, options ...settlement.Option) error { +func (dlc *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, logger types.Logger, options ...settlement.Option) error { DymensionCosmosClient, err := newDymensionHubClient(config, pubsub, logger) if err != nil { return err @@ -85,7 +84,7 @@ func (dlc *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, lo // HubClient is the client for the Dymension Hub. type HubClient struct { config *settlement.Config - logger log.Logger + logger types.Logger pubsub *pubsub.Server client CosmosClient ctx context.Context @@ -135,7 +134,7 @@ func WithBatchRetryDelay(batchRetryDelay time.Duration) Option { } } -func newDymensionHubClient(config settlement.Config, pubsub *pubsub.Server, logger log.Logger, options ...Option) (*HubClient, error) { +func newDymensionHubClient(config settlement.Config, pubsub *pubsub.Server, logger types.Logger, options ...Option) (*HubClient, error) { ctx, cancel := context.WithCancel(context.Background()) eventMap := map[string]string{ fmt.Sprintf(eventStateUpdate, config.RollappID): settlement.EventNewSettlementBatchAccepted, diff --git a/settlement/grpc/grpc.go b/settlement/grpc/grpc.go index 363091251..839f6e68b 100644 --- a/settlement/grpc/grpc.go +++ b/settlement/grpc/grpc.go @@ -20,7 +20,6 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/settlement" "github.com/dymensionxyz/dymint/types" @@ -38,7 +37,7 @@ type LayerClient struct { var _ settlement.LayerI = (*LayerClient)(nil) // Init initializes the mock layer client. -func (m *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, logger log.Logger, options ...settlement.Option) error { +func (m *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, logger types.Logger, options ...settlement.Option) error { HubClientMock, err := newHubClient(config, pubsub, logger) if err != nil { return err @@ -64,7 +63,7 @@ type HubGrpcClient struct { ctx context.Context ProposerPubKey string slStateIndex uint64 - logger log.Logger + logger types.Logger pubsub *pubsub.Server latestHeight uint64 conn *grpc.ClientConn @@ -75,7 +74,7 @@ type HubGrpcClient struct { var _ settlement.HubClient = &HubGrpcClient{} -func newHubClient(config settlement.Config, pubsub *pubsub.Server, logger log.Logger) (*HubGrpcClient, error) { +func newHubClient(config settlement.Config, pubsub *pubsub.Server, logger types.Logger) (*HubGrpcClient, error) { ctx := context.Background() latestHeight := uint64(0) diff --git a/settlement/mock/mock.go b/settlement/mock/mock.go index 9773ad708..e3325d47e 100644 --- a/settlement/mock/mock.go +++ b/settlement/mock/mock.go @@ -18,7 +18,6 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types" "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/settlement" "github.com/dymensionxyz/dymint/store" "github.com/dymensionxyz/dymint/types" @@ -40,7 +39,7 @@ type LayerClient struct { var _ settlement.LayerI = (*LayerClient)(nil) // Init initializes the mock layer client. -func (m *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, logger log.Logger, options ...settlement.Option) error { +func (m *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, logger types.Logger, options ...settlement.Option) error { HubClientMock, err := newHubClient(config, pubsub, logger) if err != nil { return err @@ -65,7 +64,7 @@ func (m *LayerClient) Init(config settlement.Config, pubsub *pubsub.Server, logg type HubClient struct { ProposerPubKey string slStateIndex uint64 - logger log.Logger + logger types.Logger pubsub *pubsub.Server latestHeight uint64 settlementKV store.KVStore @@ -73,7 +72,7 @@ type HubClient struct { var _ settlement.HubClient = &HubClient{} -func newHubClient(config settlement.Config, pubsub *pubsub.Server, logger log.Logger) (*HubClient, error) { +func newHubClient(config settlement.Config, pubsub *pubsub.Server, logger types.Logger) (*HubClient, error) { latestHeight := uint64(0) slStateIndex := uint64(0) slstore, proposer, err := initConfig(config) diff --git a/settlement/settlement.go b/settlement/settlement.go index a873297af..406342dd1 100644 --- a/settlement/settlement.go +++ b/settlement/settlement.go @@ -2,7 +2,6 @@ package settlement import ( "github.com/dymensionxyz/dymint/da" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/types" "github.com/tendermint/tendermint/libs/pubsub" ) @@ -56,7 +55,7 @@ type Option func(LayerI) type LayerI interface { // Init is called once for the client initialization - Init(config Config, pubsub *pubsub.Server, logger log.Logger, options ...Option) error + Init(config Config, pubsub *pubsub.Server, logger types.Logger, options ...Option) error // Start is called once, after Init. It's implementation should start the client service. Start() error diff --git a/state/executor.go b/state/executor.go index 886ef4431..cecfa4416 100644 --- a/state/executor.go +++ b/state/executor.go @@ -18,7 +18,6 @@ import ( "go.uber.org/multierr" abciconv "github.com/dymensionxyz/dymint/conv/abci" - "github.com/dymensionxyz/dymint/log" "github.com/dymensionxyz/dymint/mempool" "github.com/dymensionxyz/dymint/types" ) @@ -34,12 +33,12 @@ type BlockExecutor struct { eventBus *tmtypes.EventBus - logger log.Logger + logger types.Logger } // NewBlockExecutor creates new instance of BlockExecutor. // Proposer address and namespace ID will be used in all newly created blocks. -func NewBlockExecutor(proposerAddress []byte, namespaceID string, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConns, eventBus *tmtypes.EventBus, logger log.Logger) (*BlockExecutor, error) { +func NewBlockExecutor(proposerAddress []byte, namespaceID string, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConns, eventBus *tmtypes.EventBus, logger types.Logger) (*BlockExecutor, error) { bytes, err := hex.DecodeString(namespaceID) if err != nil { return nil, err diff --git a/log/test/loggers.go b/testutil/logger.go similarity index 99% rename from log/test/loggers.go rename to testutil/logger.go index e855a4183..3ef7a902d 100644 --- a/log/test/loggers.go +++ b/testutil/logger.go @@ -1,4 +1,4 @@ -package test +package testutil import ( "fmt" diff --git a/log/logger.go b/types/logger.go similarity index 93% rename from log/logger.go rename to types/logger.go index 0f765f841..e2c8fcdac 100644 --- a/log/logger.go +++ b/types/logger.go @@ -1,4 +1,4 @@ -package log +package types // Logger interface is compatible with Tendermint logger type Logger interface { diff --git a/utils/events.go b/utils/events.go index 89a053173..537a305e9 100644 --- a/utils/events.go +++ b/utils/events.go @@ -3,12 +3,12 @@ package utils import ( "context" - "github.com/dymensionxyz/dymint/log" + "github.com/dymensionxyz/dymint/types" "github.com/tendermint/tendermint/libs/pubsub" ) // SubscribeAndHandleEvents subscribes to events and sends back a callback -func SubscribeAndHandleEvents(ctx context.Context, pubsubServer *pubsub.Server, clientID string, eventQuery pubsub.Query, callback func(event pubsub.Message), logger log.Logger, outCapacity ...int) { +func SubscribeAndHandleEvents(ctx context.Context, pubsubServer *pubsub.Server, clientID string, eventQuery pubsub.Query, callback func(event pubsub.Message), logger types.Logger, outCapacity ...int) { subscription, err := pubsubServer.Subscribe(ctx, clientID, eventQuery, outCapacity...) if err != nil { logger.Error("failed to subscribe to events") From 820f23490c227faf83e7a0bd040096e95091233b Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Sat, 6 Apr 2024 18:55:30 +0300 Subject: [PATCH 6/7] cleaning block manager --- block/manager.go | 79 ++++++++++++++++++++---------------------------- block/types.go | 16 ++++++++++ 2 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 block/types.go diff --git a/block/manager.go b/block/manager.go index 3122c5c4e..7195b9323 100644 --- a/block/manager.go +++ b/block/manager.go @@ -8,8 +8,6 @@ import ( "sync/atomic" "time" - // Importing the general purpose Cosmos blockchain client - "code.cloudfoundry.org/go-diodes" "github.com/dymensionxyz/dymint/node/events" @@ -31,71 +29,48 @@ import ( "github.com/dymensionxyz/dymint/types" ) -type blockSource string - -const ( - producedBlock blockSource = "produced" - gossipedBlock blockSource = "gossip" - daBlock blockSource = "da" -) - -type blockMetaData struct { - source blockSource - daHeight uint64 -} - // Manager is responsible for aggregating transactions into blocks. type Manager struct { - pubsub *pubsub.Server - - p2pClient *p2p.Client - - lastState types.State - - conf config.BlockManagerConfig - genesis *tmtypes.GenesisDoc - + // Configuration + conf config.BlockManagerConfig + genesis *tmtypes.GenesisDoc proposerKey crypto.PrivKey - store store.Store - executor *state.BlockExecutor + // Store and execution + store store.Store + lastState types.State + executor *state.BlockExecutor + // Clients and servers + pubsub *pubsub.Server + p2pClient *p2p.Client dalc da.DataAvailabilityLayerClient settlementClient settlement.LayerI - retriever da.BatchRetriever + // Data retrieval + retriever da.BatchRetriever + + // Synchronization syncTargetDiode diodes.Diode + syncTarget uint64 + isSyncedCond sync.Cond + // Block production shouldProduceBlocksCh chan bool produceEmptyBlockCh chan bool - - syncTarget uint64 - lastSubmissionTime int64 - batchInProcess atomic.Value - isSyncedCond sync.Cond - + lastSubmissionTime int64 + batchInProcess atomic.Value produceBlockMutex sync.Mutex applyCachedBlockMutex sync.Mutex - syncCache map[uint64]*types.Block - + // Logging logger types.Logger + // Previous data prevBlock map[uint64]*types.Block prevCommit map[uint64]*types.Commit } -// getInitialState tries to load lastState from Store, and if it's not available it reads GenesisDoc. -func getInitialState(store store.Store, genesis *tmtypes.GenesisDoc, logger types.Logger) (types.State, error) { - s, err := store.LoadState() - if err == types.ErrNoStateFound { - logger.Info("failed to find state in the store, creating new state from genesis") - return types.NewFromGenesisDoc(genesis) - } - - return s, err -} - // NewManager creates new block Manager. func NewManager( proposerKey crypto.PrivKey, @@ -143,7 +118,6 @@ func NewManager( retriever: dalc.(da.BatchRetriever), // channels are buffered to avoid blocking on input/output operations, buffer sizes are arbitrary syncTargetDiode: diodes.NewOneToOne(1, nil), - syncCache: make(map[uint64]*types.Block), isSyncedCond: *sync.NewCond(new(sync.Mutex)), batchInProcess: batchInProcess, shouldProduceBlocksCh: make(chan bool, 1), @@ -279,3 +253,14 @@ func (m *Manager) applyBlockCallback(event pubsub.Message) { func (m *Manager) getLatestBatchFromSL(ctx context.Context) (*settlement.ResultRetrieveBatch, error) { return m.settlementClient.RetrieveBatch() } + +// getInitialState tries to load lastState from Store, and if it's not available it reads GenesisDoc. +func getInitialState(store store.Store, genesis *tmtypes.GenesisDoc, logger types.Logger) (types.State, error) { + s, err := store.LoadState() + if err == types.ErrNoStateFound { + logger.Info("failed to find state in the store, creating new state from genesis") + return types.NewFromGenesisDoc(genesis) + } + + return s, err +} diff --git a/block/types.go b/block/types.go new file mode 100644 index 000000000..785e9a907 --- /dev/null +++ b/block/types.go @@ -0,0 +1,16 @@ +package block + +//TODO: move to types package + +type blockSource string + +const ( + producedBlock blockSource = "produced" + gossipedBlock blockSource = "gossip" + daBlock blockSource = "da" +) + +type blockMetaData struct { + source blockSource + daHeight uint64 +} From 9efb82ca549fd62b756b8b4e3baf8c9f3325f799 Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Sat, 6 Apr 2024 20:29:05 +0300 Subject: [PATCH 7/7] pr comments --- settlement/dymension/dymension.go | 44 ++++++++++++++------------ settlement/dymension/dymension_test.go | 3 +- settlement/grpc/grpc.go | 1 - settlement/grpc/mockserv/mockserv.go | 7 ++-- settlement/mock/mock.go | 6 ++-- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/settlement/dymension/dymension.go b/settlement/dymension/dymension.go index 70ddcbc5c..4e8ec7d43 100644 --- a/settlement/dymension/dymension.go +++ b/settlement/dymension/dymension.go @@ -187,7 +187,6 @@ func (d *HubClient) Start() error { } go d.eventHandler() return nil - } // Stop stops the HubClient. @@ -289,7 +288,8 @@ func (d *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult * // GetLatestBatch returns the latest batch from the Dymension Hub. func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieveBatch, error) { var latestStateInfoIndexResp *rollapptypes.QueryGetLatestStateIndexResponse - err := retry.Do(func() error { + + err := d.RunWithRetry(func() error { var err error latestStateInfoIndexResp, err = d.rollappQueryClient.LatestStateIndex(d.ctx, &rollapptypes.QueryGetLatestStateIndexRequest{RollappId: d.config.RollappID}) @@ -299,9 +299,7 @@ func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieve } return err - }, retry.Context(d.ctx), retry.LastErrorOnly(true), - retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) - + }) if err != nil { return nil, err } @@ -321,7 +319,7 @@ func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieve // GetBatchAtIndex returns the batch at the given index from the Dymension Hub. func (d *HubClient) GetBatchAtIndex(rollappID string, index uint64) (*settlement.ResultRetrieveBatch, error) { var stateInfoResp *rollapptypes.QueryGetStateInfoResponse - err := retry.Do(func() error { + err := d.RunWithRetry(func() error { var err error stateInfoResp, err = d.rollappQueryClient.StateInfo(d.ctx, &rollapptypes.QueryGetStateInfoRequest{RollappId: d.config.RollappID, Index: index}) @@ -331,9 +329,7 @@ func (d *HubClient) GetBatchAtIndex(rollappID string, index uint64) (*settlement } return err - }, retry.Context(d.ctx), retry.LastErrorOnly(true), - retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) - + }) if err != nil { return nil, err } @@ -352,13 +348,11 @@ func (d *HubClient) GetSequencers(rollappID string) ([]*types.Sequencer, error) RollappId: d.config.RollappID, Status: sequencertypes.Bonded, } - err := retry.Do(func() error { + err := d.RunWithRetry(func() error { var err error res, err = d.sequencerQueryClient.SequencersByRollappByStatus(d.ctx, req) return err - }, retry.Context(d.ctx), retry.LastErrorOnly(true), - retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) - + }) if err != nil { return nil, err } @@ -390,15 +384,14 @@ func (d *HubClient) GetSequencers(rollappID string) ([]*types.Sequencer, error) } func (d *HubClient) submitBatch(msgUpdateState *rollapptypes.MsgUpdateState) error { - err := retry.Do(func() error { + err := d.RunWithRetry(func() error { txResp, err := d.client.BroadcastTx(d.config.DymAccountName, msgUpdateState) if err != nil || txResp.Code != 0 { d.logger.Error("Error sending batch to settlement layer", "error", err) return err } return nil - }, retry.Context(d.ctx), retry.LastErrorOnly(true), retry.Delay(d.batchRetryDelay), - retry.MaxDelay(batchRetryMaxDelay), retry.Attempts(d.batchRetryAttempts)) + }) return err } @@ -466,7 +459,6 @@ func (d *HubClient) convertBatchToMsgUpdateState(batch *types.Batch, daResult *d BDs: rollapptypes.BlockDescriptors{BD: blockDescriptors}, } return settlementBatch, nil - } func getCosmosClientOptions(config *settlement.Config) []cosmosclient.Option { @@ -539,14 +531,15 @@ func (d *HubClient) convertStateInfoToResultRetrieveBatch(stateInfo *rollapptype } return &settlement.ResultRetrieveBatch{ BaseResult: settlement.BaseResult{Code: settlement.StatusSuccess, StateIndex: stateInfo.StateInfoIndex.Index}, - Batch: batchResult}, nil + Batch: batchResult, + }, nil } // TODO(omritoptix): Change the retry attempts to be only for the batch polling. Also we need to have a more // bullet proof check as theoretically the tx can stay in the mempool longer then our retry attempts. func (d *HubClient) waitForBatchInclusion(batchStartHeight uint64) (*settlement.ResultRetrieveBatch, error) { var resultRetriveBatch *settlement.ResultRetrieveBatch - err := retry.Do(func() error { + err := d.RunWithRetry(func() error { latestBatch, err := d.GetLatestBatch(d.config.RollappID) if err != nil { return err @@ -556,7 +549,16 @@ func (d *HubClient) waitForBatchInclusion(batchStartHeight uint64) (*settlement. } resultRetriveBatch = latestBatch return nil - }, retry.Context(d.ctx), retry.LastErrorOnly(true), - retry.Delay(d.batchRetryDelay), retry.Attempts(d.batchRetryAttempts), retry.MaxDelay(batchRetryMaxDelay)) + }) return resultRetriveBatch, err } + +func (d *HubClient) RunWithRetry(operation func() error) error { + return retry.Do(operation, + retry.Context(d.ctx), + retry.LastErrorOnly(true), + retry.Delay(d.batchRetryDelay), + retry.Attempts(d.batchRetryAttempts), + retry.MaxDelay(batchRetryMaxDelay), + ) +} diff --git a/settlement/dymension/dymension_test.go b/settlement/dymension/dymension_test.go index 4ec616f9c..783a0ccb7 100644 --- a/settlement/dymension/dymension_test.go +++ b/settlement/dymension/dymension_test.go @@ -182,7 +182,8 @@ func TestPostBatch(t *testing.T) { } rollappQueryClientMock.On("StateInfo", mock.Anything, mock.Anything).Return( &rollapptypes.QueryGetStateInfoResponse{StateInfo: rollapptypes.StateInfo{ - StartHeight: batch.StartHeight, StateInfoIndex: rollapptypes.StateInfoIndex{Index: 1}, DAPath: daMetaData.ToPath(), NumBlocks: 1}}, + StartHeight: batch.StartHeight, StateInfoIndex: rollapptypes.StateInfoIndex{Index: 1}, DAPath: daMetaData.ToPath(), NumBlocks: 1, + }}, nil) } else { rollappQueryClientMock.On("LatestStateIndex", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error")) diff --git a/settlement/grpc/grpc.go b/settlement/grpc/grpc.go index 363091251..d379d003c 100644 --- a/settlement/grpc/grpc.go +++ b/settlement/grpc/grpc.go @@ -130,7 +130,6 @@ func newHubClient(config settlement.Config, pubsub *pubsub.Server, logger log.Lo func initConfig(conf settlement.Config) (proposer string, err error) { if conf.KeyringHomeDir == "" { - if conf.ProposerPubKey != "" { proposer = conf.ProposerPubKey } else { diff --git a/settlement/grpc/mockserv/mockserv.go b/settlement/grpc/mockserv/mockserv.go index 31fcc761d..4ee8d6726 100644 --- a/settlement/grpc/mockserv/mockserv.go +++ b/settlement/grpc/mockserv/mockserv.go @@ -11,8 +11,10 @@ import ( "github.com/dymensionxyz/dymint/store" ) -var settlementKVPrefix = []byte{0} -var slStateIndexKey = []byte("slStateIndex") +var ( + settlementKVPrefix = []byte{0} + slStateIndexKey = []byte("slStateIndex") +) type server struct { slmock.UnimplementedMockSLServer @@ -63,7 +65,6 @@ func (s *server) SetBatch(ctx context.Context, in *slmock.SLSetBatchRequest) (*s } func GetServer(conf settlement.GrpcConfig) *grpc.Server { - srv := grpc.NewServer() slstore := store.NewDefaultKVStore(".", "db", "settlement") diff --git a/settlement/mock/mock.go b/settlement/mock/mock.go index 9773ad708..c5fab95c9 100644 --- a/settlement/mock/mock.go +++ b/settlement/mock/mock.go @@ -28,8 +28,10 @@ import ( const kvStoreDBName = "settlement" -var settlementKVPrefix = []byte{0} -var slStateIndexKey = []byte("slStateIndex") +var ( + settlementKVPrefix = []byte{0} + slStateIndexKey = []byte("slStateIndex") +) // LayerClient is an extension of the base settlement layer client // for usage in tests and local development.