From b5069865d874ca1921a6d16e10ad1ab918b811e4 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 4 Oct 2024 16:59:19 +0400 Subject: [PATCH 01/19] upgrade to latest go-sequencing --- block/manager.go | 4 +++- cmd/rollkit/commands/run_node.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- mempool/reaper.go | 4 +++- node/node_test.go | 2 +- sequencing/mock/cmd/main.go | 2 +- test/server/server.go | 2 +- 8 files changed, 13 insertions(+), 9 deletions(-) diff --git a/block/manager.go b/block/manager.go index da24521df2..fc9beddca6 100644 --- a/block/manager.go +++ b/block/manager.go @@ -415,10 +415,12 @@ func (m *Manager) BatchRetrieveLoop(ctx context.Context) { case <-batchTimer.C: // Define the start time for the block production period start := time.Now() - batch, batchTime, err := m.seqClient.GetNextBatch(ctx, m.lastBatchHash) + res, err := m.seqClient.GetNextBatch(ctx, sequencing.GetNextBatchRequest{RollupId: []byte(m.genesis.ChainID), LastBatchHash: m.lastBatchHash}) if err != nil && ctx.Err() == nil { m.logger.Error("error while retrieving batch", "error", err) } + batch := res.Batch + batchTime := res.Timestamp // Add the batch to the batch queue if batch != nil && batch.Transactions != nil { m.bq.AddBatch(BatchWithTime{batch, batchTime}) diff --git a/cmd/rollkit/commands/run_node.go b/cmd/rollkit/commands/run_node.go index dd4a7f72d4..9e737269f5 100644 --- a/cmd/rollkit/commands/run_node.go +++ b/cmd/rollkit/commands/run_node.go @@ -248,7 +248,7 @@ func startMockDAServJSONRPC(ctx context.Context) (*proxy.Server, error) { // startMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func startMockSequencerServerGRPC(listenAddress string) (*grpc.Server, error) { - dummySeq := seqTest.NewDummySequencer() + dummySeq := seqTest.NewDummySequencer([]byte("rollupId")) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) lis, err := net.Listen("tcp", listenAddress) if err != nil { diff --git a/go.mod b/go.mod index a016878899..5750063db6 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,7 @@ require ( github.com/celestiaorg/go-header v0.6.2 github.com/ipfs/go-ds-badger4 v0.1.5 github.com/mitchellh/mapstructure v1.5.0 - github.com/rollkit/go-sequencing v0.1.0 + github.com/rollkit/go-sequencing v0.2.0 ) require ( diff --git a/go.sum b/go.sum index 0e87621377..e3b0484cad 100644 --- a/go.sum +++ b/go.sum @@ -1441,8 +1441,8 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/rollkit/go-da v0.7.0 h1:b4o5dWCCqwH0fbbhfNOoLraAsCHjN5PV1Z2rNkUB+pU= github.com/rollkit/go-da v0.7.0/go.mod h1:9YCbEhUkF/QHbbKPBe5uvu93Co/mgPRtYjlwj6GPuhI= -github.com/rollkit/go-sequencing v0.1.0 h1:LL6QT5pekydPLxeU8ncRoRlkdbJpM8fcsu+Uw15H4sE= -github.com/rollkit/go-sequencing v0.1.0/go.mod h1:s/3XzHYeY+bximgM8PDdQmoe9aWBSOa4NDQmBxpMJ4A= +github.com/rollkit/go-sequencing v0.2.0 h1:IrEA29p07zPDbqY29AzI+q2zUqm7QACAoR+/PBpexvw= +github.com/rollkit/go-sequencing v0.2.0/go.mod h1:P/cQXTw3rWpPqhqnCwKzlkS39XM8ugmyf2u63twBgG8= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA= diff --git a/mempool/reaper.go b/mempool/reaper.go index aac0089828..d059eb2bd5 100644 --- a/mempool/reaper.go +++ b/mempool/reaper.go @@ -8,6 +8,7 @@ import ( cmtypes "github.com/cometbft/cometbft/types" "github.com/cometbft/cometbft/libs/log" + "github.com/rollkit/go-sequencing" "github.com/rollkit/go-sequencing/proxy/grpc" ) @@ -101,7 +102,8 @@ func (r *CListMempoolReaper) reap(ctx context.Context) { func (reaper *CListMempoolReaper) retrySubmitTransaction(ctx context.Context, tx cmtypes.Tx, maxRetries int, delay time.Duration) error { var err error for i := 0; i < maxRetries; i++ { - err = reaper.grpcClient.SubmitRollupTransaction(ctx, reaper.rollupId, tx) + // ignore the response for now as nothing is in there + _, err = reaper.grpcClient.SubmitRollupTransaction(ctx, sequencing.SubmitRollupTransactionRequest{RollupId: reaper.rollupId, Tx: tx}) if err == nil { return nil } diff --git a/node/node_test.go b/node/node_test.go index 5a00a58fa1..006c314a8e 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -78,7 +78,7 @@ func startMockGRPCServ() *grpc.Server { // startMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func startMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer() + dummySeq := seqTest.NewDummySequencer([]byte("rollupId")) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) lis, err := net.Listen("tcp", listenAddress) if err != nil { diff --git a/sequencing/mock/cmd/main.go b/sequencing/mock/cmd/main.go index 38b4b5fcab..77650cad33 100644 --- a/sequencing/mock/cmd/main.go +++ b/sequencing/mock/cmd/main.go @@ -23,7 +23,7 @@ func main() { log.Fatalf("Failed to listen: %v", err) } - dummySequencer := test.NewDummySequencer() + dummySequencer := test.NewDummySequencer([]byte("rollupId")) srv := grpc.NewServer(dummySequencer, dummySequencer, dummySequencer) log.Printf("Listening on: %s:%s", "localhost", "50051") diff --git a/test/server/server.go b/test/server/server.go index b6457d878e..2a25994ed5 100644 --- a/test/server/server.go +++ b/test/server/server.go @@ -48,7 +48,7 @@ func StartMockDAServJSONRPC(ctx context.Context, listenAddress string) *jsonrpc. // StartMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func StartMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer() + dummySeq := seqTest.NewDummySequencer([]byte("rollupId")) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) addr, _ := url.Parse(listenAddress) lis, err := net.Listen("tcp", addr.Host) From 28d094c24bd87fd039a1bfeaf93d211d2efe7063 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 4 Oct 2024 17:07:49 +0400 Subject: [PATCH 02/19] use sequencer timestamp for header time --- block/manager.go | 13 +++++++------ block/manager_test.go | 2 +- state/executor.go | 4 ++-- state/executor_test.go | 12 ++++++------ 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/block/manager.go b/block/manager.go index fc9beddca6..cc825d02ce 100644 --- a/block/manager.go +++ b/block/manager.go @@ -986,16 +986,16 @@ func (m *Manager) getSignature(header types.Header) (*types.Signature, error) { return &signature, nil } -func (m *Manager) getTxsFromBatch() cmtypes.Txs { +func (m *Manager) getTxsFromBatch() (cmtypes.Txs, time.Time) { batch := m.bq.Next() if batch == nil { - return make(cmtypes.Txs, 0) + return make(cmtypes.Txs, 0), time.Now() } txs := make(cmtypes.Txs, 0, len(batch.Transactions)) for _, tx := range batch.Transactions { txs = append(txs, tx) } - return txs + return txs, batch.Time } func (m *Manager) publishBlock(ctx context.Context) error { @@ -1058,7 +1058,8 @@ func (m *Manager) publishBlock(ctx context.Context) error { return fmt.Errorf("failed to load extended commit for height %d: %w", height, err) } - header, data, err = m.createBlock(newHeight, lastSignature, lastHeaderHash, extendedCommit, m.getTxsFromBatch()) + txs, timestamp := m.getTxsFromBatch() + header, data, err = m.createBlock(newHeight, lastSignature, lastHeaderHash, extendedCommit, txs, timestamp) if err != nil { return err } @@ -1399,10 +1400,10 @@ func (m *Manager) getLastBlockTime() time.Time { return m.lastState.LastBlockTime } -func (m *Manager) createBlock(height uint64, lastSignature *types.Signature, lastHeaderHash types.Hash, extendedCommit abci.ExtendedCommitInfo, txs cmtypes.Txs) (*types.SignedHeader, *types.Data, error) { +func (m *Manager) createBlock(height uint64, lastSignature *types.Signature, lastHeaderHash types.Hash, extendedCommit abci.ExtendedCommitInfo, txs cmtypes.Txs, timestamp time.Time) (*types.SignedHeader, *types.Data, error) { m.lastStateMtx.RLock() defer m.lastStateMtx.RUnlock() - return m.executor.CreateBlock(height, lastSignature, extendedCommit, lastHeaderHash, m.lastState, txs) + return m.executor.CreateBlock(height, lastSignature, extendedCommit, lastHeaderHash, m.lastState, txs, timestamp) } func (m *Manager) applyBlock(ctx context.Context, header *types.SignedHeader, data *types.Data) (types.State, *abci.ResponseFinalizeBlock, error) { diff --git a/block/manager_test.go b/block/manager_test.go index 1c121f5a15..02d9b8d9df 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -719,7 +719,7 @@ func TestManager_publishBlock(t *testing.T) { t.Run("height should not be updated if saving block responses fails", func(t *testing.T) { mockStore.On("Height").Return(uint64(0)) signature := types.Signature([]byte{1, 1, 1}) - header, data, err := executor.CreateBlock(0, &signature, abci.ExtendedCommitInfo{}, []byte{}, lastState, cmtypes.Txs{}) + header, data, err := executor.CreateBlock(0, &signature, abci.ExtendedCommitInfo{}, []byte{}, lastState, cmtypes.Txs{}, time.Now()) require.NoError(err) require.NotNil(header) require.NotNil(data) diff --git a/state/executor.go b/state/executor.go index 109981e413..d5ce1df0db 100644 --- a/state/executor.go +++ b/state/executor.go @@ -95,7 +95,7 @@ func (e *BlockExecutor) InitChain(genesis *cmtypes.GenesisDoc) (*abci.ResponseIn } // CreateBlock reaps transactions from mempool and builds a block. -func (e *BlockExecutor) CreateBlock(height uint64, lastSignature *types.Signature, lastExtendedCommit abci.ExtendedCommitInfo, lastHeaderHash types.Hash, state types.State, txs cmtypes.Txs) (*types.SignedHeader, *types.Data, error) { +func (e *BlockExecutor) CreateBlock(height uint64, lastSignature *types.Signature, lastExtendedCommit abci.ExtendedCommitInfo, lastHeaderHash types.Hash, state types.State, txs cmtypes.Txs, timestamp time.Time) (*types.SignedHeader, *types.Data, error) { maxBytes := state.ConsensusParams.Block.MaxBytes emptyMaxBytes := maxBytes == -1 if emptyMaxBytes { @@ -115,7 +115,7 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastSignature *types.Signatur BaseHeader: types.BaseHeader{ ChainID: e.chainID, Height: height, - Time: uint64(time.Now().UnixNano()), //nolint:gosec + Time: uint64(timestamp.UnixNano()), //nolint:gosec }, DataHash: make(types.Hash, 32), ConsensusHash: make(types.Hash, 32), diff --git a/state/executor_test.go b/state/executor_test.go index dadc67cc59..59a2da2daa 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -77,7 +77,7 @@ func doTestCreateBlock(t *testing.T) { state.Validators = cmtypes.NewValidatorSet(validators) // empty block - header, data, err := executor.CreateBlock(1, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{}) + header, data, err := executor.CreateBlock(1, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{}, time.Now()) require.NoError(err) require.NotNil(header) assert.Empty(data.Txs) @@ -87,7 +87,7 @@ func doTestCreateBlock(t *testing.T) { tx := []byte{1, 2, 3, 4} err = mpool.CheckTx(tx, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{}) require.NoError(err) - header, data, err = executor.CreateBlock(2, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx}) + header, data, err = executor.CreateBlock(2, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx}, time.Now()) require.NoError(err) require.NotNil(header) assert.Equal(uint64(2), header.Height()) @@ -100,7 +100,7 @@ func doTestCreateBlock(t *testing.T) { require.NoError(err) err = mpool.CheckTx(tx2, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{}) require.NoError(err) - header, data, err = executor.CreateBlock(3, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx1, tx2}) + header, data, err = executor.CreateBlock(3, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx1, tx2}, time.Now()) require.Error(err) require.Nil(header) require.Nil(data) @@ -111,7 +111,7 @@ func doTestCreateBlock(t *testing.T) { executor.maxBytes = 10 err = mpool.CheckTx(tx, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{}) require.NoError(err) - header, data, err = executor.CreateBlock(4, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx}) + header, data, err = executor.CreateBlock(4, &types.Signature{}, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx}, time.Now()) require.Error(err) require.Nil(header) require.Nil(data) @@ -206,7 +206,7 @@ func doTestApplyBlock(t *testing.T) { err = mpool.CheckTx(tx, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{}) require.NoError(err) signature := types.Signature([]byte{1, 1, 1}) - header, data, err := executor.CreateBlock(1, &signature, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx}) + header, data, err := executor.CreateBlock(1, &signature, abci.ExtendedCommitInfo{}, []byte{}, state, cmtypes.Txs{tx}, time.Now()) require.NoError(err) require.NotNil(header) assert.Equal(uint64(1), header.Height()) @@ -236,7 +236,7 @@ func doTestApplyBlock(t *testing.T) { require.NoError(mpool.CheckTx(tx2, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{})) require.NoError(mpool.CheckTx(tx3, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{})) signature = types.Signature([]byte{1, 1, 1}) - header, data, err = executor.CreateBlock(2, &signature, abci.ExtendedCommitInfo{}, []byte{}, newState, cmtypes.Txs{tx1, tx2, tx3}) + header, data, err = executor.CreateBlock(2, &signature, abci.ExtendedCommitInfo{}, []byte{}, newState, cmtypes.Txs{tx1, tx2, tx3}, time.Now()) require.NoError(err) require.NotNil(header) assert.Equal(uint64(2), header.Height()) From 266a4c45761d7e3e9da36f43a5113b236af6bf11 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 4 Oct 2024 17:10:54 +0400 Subject: [PATCH 03/19] upgrade go-da --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5750063db6..0345957d22 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/multiformats/go-multiaddr v0.13.0 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.20.4 - github.com/rollkit/go-da v0.7.0 + github.com/rollkit/go-da v0.8.0 github.com/rs/cors v1.11.1 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 diff --git a/go.sum b/go.sum index e3b0484cad..62126dfd96 100644 --- a/go.sum +++ b/go.sum @@ -1439,8 +1439,8 @@ github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4 github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= -github.com/rollkit/go-da v0.7.0 h1:b4o5dWCCqwH0fbbhfNOoLraAsCHjN5PV1Z2rNkUB+pU= -github.com/rollkit/go-da v0.7.0/go.mod h1:9YCbEhUkF/QHbbKPBe5uvu93Co/mgPRtYjlwj6GPuhI= +github.com/rollkit/go-da v0.8.0 h1:oJKojC421eRC4mNqbujf40GzLFNp7HapgeB7Z/r0tyc= +github.com/rollkit/go-da v0.8.0/go.mod h1:3eHWK5gkv8lhwq6bjOZOi82WwHyS2B9rQOlUrE1GGws= github.com/rollkit/go-sequencing v0.2.0 h1:IrEA29p07zPDbqY29AzI+q2zUqm7QACAoR+/PBpexvw= github.com/rollkit/go-sequencing v0.2.0/go.mod h1:P/cQXTw3rWpPqhqnCwKzlkS39XM8ugmyf2u63twBgG8= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= From 2e63f61345ef34c95461d11e6acc5ccf47a50f29 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 4 Oct 2024 17:32:43 +0400 Subject: [PATCH 04/19] fix nil pointer --- block/manager.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/block/manager.go b/block/manager.go index cc825d02ce..991af29e18 100644 --- a/block/manager.go +++ b/block/manager.go @@ -419,18 +419,20 @@ func (m *Manager) BatchRetrieveLoop(ctx context.Context) { if err != nil && ctx.Err() == nil { m.logger.Error("error while retrieving batch", "error", err) } - batch := res.Batch - batchTime := res.Timestamp - // Add the batch to the batch queue - if batch != nil && batch.Transactions != nil { - m.bq.AddBatch(BatchWithTime{batch, batchTime}) - // Calculate the hash of the batch and store it for the next batch retrieval - batchBytes, err := batch.Marshal() - if err != nil { - m.logger.Error("error while marshaling batch", "error", err) + if res != nil { + batch := res.Batch + batchTime := res.Timestamp + // Add the batch to the batch queue + if batch != nil && batch.Transactions != nil { + m.bq.AddBatch(BatchWithTime{batch, batchTime}) + // Calculate the hash of the batch and store it for the next batch retrieval + batchBytes, err := batch.Marshal() + if err != nil { + m.logger.Error("error while marshaling batch", "error", err) + } + h := sha256.Sum256(batchBytes) + m.lastBatchHash = h[:] } - h := sha256.Sum256(batchBytes) - m.lastBatchHash = h[:] } // Reset the batchTimer to signal the next batch production // period based on the batch retrieval time. From d60614e8944ae2a8d82f3df58684ede3ee31bfdf Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 4 Oct 2024 18:23:16 +0400 Subject: [PATCH 05/19] use test as default rollupId --- cmd/rollkit/commands/run_node.go | 2 +- node/node_test.go | 2 +- test/server/server.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/rollkit/commands/run_node.go b/cmd/rollkit/commands/run_node.go index 9e737269f5..d0b6bc24f6 100644 --- a/cmd/rollkit/commands/run_node.go +++ b/cmd/rollkit/commands/run_node.go @@ -248,7 +248,7 @@ func startMockDAServJSONRPC(ctx context.Context) (*proxy.Server, error) { // startMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func startMockSequencerServerGRPC(listenAddress string) (*grpc.Server, error) { - dummySeq := seqTest.NewDummySequencer([]byte("rollupId")) + dummySeq := seqTest.NewDummySequencer([]byte("test")) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) lis, err := net.Listen("tcp", listenAddress) if err != nil { diff --git a/node/node_test.go b/node/node_test.go index 006c314a8e..527481701e 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -78,7 +78,7 @@ func startMockGRPCServ() *grpc.Server { // startMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func startMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer([]byte("rollupId")) + dummySeq := seqTest.NewDummySequencer([]byte("test")) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) lis, err := net.Listen("tcp", listenAddress) if err != nil { diff --git a/test/server/server.go b/test/server/server.go index 2a25994ed5..305081999d 100644 --- a/test/server/server.go +++ b/test/server/server.go @@ -48,7 +48,7 @@ func StartMockDAServJSONRPC(ctx context.Context, listenAddress string) *jsonrpc. // StartMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func StartMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer([]byte("rollupId")) + dummySeq := seqTest.NewDummySequencer([]byte("test")) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) addr, _ := url.Parse(listenAddress) lis, err := net.Listen("tcp", addr.Host) From 22407d0a6a38c92077c010f75d286620454b0cc8 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 4 Oct 2024 18:24:46 +0400 Subject: [PATCH 06/19] minor --- sequencing/mock/cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencing/mock/cmd/main.go b/sequencing/mock/cmd/main.go index 77650cad33..73a0feb558 100644 --- a/sequencing/mock/cmd/main.go +++ b/sequencing/mock/cmd/main.go @@ -23,7 +23,7 @@ func main() { log.Fatalf("Failed to listen: %v", err) } - dummySequencer := test.NewDummySequencer([]byte("rollupId")) + dummySequencer := test.NewDummySequencer([]byte("test")) srv := grpc.NewServer(dummySequencer, dummySequencer, dummySequencer) log.Printf("Listening on: %s:%s", "localhost", "50051") From c8163dfed664fce37c35ec0d70403a4e341033d6 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 4 Oct 2024 19:44:00 +0400 Subject: [PATCH 07/19] use batch Hash, add empty batches to queue to capture sequencer timestamp --- block/manager.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/block/manager.go b/block/manager.go index 991af29e18..a0de0785c4 100644 --- a/block/manager.go +++ b/block/manager.go @@ -3,7 +3,6 @@ package block import ( "bytes" "context" - "crypto/sha256" "encoding/binary" "encoding/hex" "errors" @@ -423,15 +422,17 @@ func (m *Manager) BatchRetrieveLoop(ctx context.Context) { batch := res.Batch batchTime := res.Timestamp // Add the batch to the batch queue - if batch != nil && batch.Transactions != nil { + if batch != nil { m.bq.AddBatch(BatchWithTime{batch, batchTime}) - // Calculate the hash of the batch and store it for the next batch retrieval - batchBytes, err := batch.Marshal() - if err != nil { - m.logger.Error("error while marshaling batch", "error", err) + // update lastBatchHash only if the batch has actual txs + if batch.Transactions != nil { + // Calculate the hash of the batch and store it for the next batch retrieval + h, err := batch.Hash() + if err != nil { + m.logger.Error("error while hashing batch", "error", err) + } + m.lastBatchHash = h } - h := sha256.Sum256(batchBytes) - m.lastBatchHash = h[:] } } // Reset the batchTimer to signal the next batch production @@ -991,6 +992,8 @@ func (m *Manager) getSignature(header types.Header) (*types.Signature, error) { func (m *Manager) getTxsFromBatch() (cmtypes.Txs, time.Time) { batch := m.bq.Next() if batch == nil { + // this excludes empty batches + // empty batches still gets the timestamp from the sequencer return make(cmtypes.Txs, 0), time.Now() } txs := make(cmtypes.Txs, 0, len(batch.Transactions)) From 7104199421904212da4be7607aabc2ca91d1d762 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Tue, 8 Oct 2024 10:40:37 +0400 Subject: [PATCH 08/19] return error when no batch --- block/manager.go | 19 ++++++++++------ block/manager_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/block/manager.go b/block/manager.go index a0de0785c4..8c6fb4b785 100644 --- a/block/manager.go +++ b/block/manager.go @@ -80,6 +80,9 @@ const DAIncludedHeightKey = "da included height" // dataHashForEmptyTxs to be used while only syncing headers from DA and no p2p to get the Data for no txs scenarios, the syncing can proceed without getting stuck forever. var dataHashForEmptyTxs = []byte{110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63, 179, 55, 56, 118, 133, 17, 163, 6, 23, 175, 160, 29} +// ErrNoBatch indicate no batch is available for creating block +var ErrNoBatch = errors.New("no batch to process") + // NewHeaderEvent is used to pass header and DA height to headerInCh type NewHeaderEvent struct { Header *types.SignedHeader @@ -989,18 +992,17 @@ func (m *Manager) getSignature(header types.Header) (*types.Signature, error) { return &signature, nil } -func (m *Manager) getTxsFromBatch() (cmtypes.Txs, time.Time) { +func (m *Manager) getTxsFromBatch() (cmtypes.Txs, *time.Time, error) { batch := m.bq.Next() if batch == nil { - // this excludes empty batches - // empty batches still gets the timestamp from the sequencer - return make(cmtypes.Txs, 0), time.Now() + // batch is nil when there is nothing to process + return nil, nil, ErrNoBatch } txs := make(cmtypes.Txs, 0, len(batch.Transactions)) for _, tx := range batch.Transactions { txs = append(txs, tx) } - return txs, batch.Time + return txs, &batch.Time, nil } func (m *Manager) publishBlock(ctx context.Context) error { @@ -1063,8 +1065,11 @@ func (m *Manager) publishBlock(ctx context.Context) error { return fmt.Errorf("failed to load extended commit for height %d: %w", height, err) } - txs, timestamp := m.getTxsFromBatch() - header, data, err = m.createBlock(newHeight, lastSignature, lastHeaderHash, extendedCommit, txs, timestamp) + txs, timestamp, err := m.getTxsFromBatch() + if err != nil { + return fmt.Errorf("failed to get transactions from batch: %w", err) + } + header, data, err = m.createBlock(newHeight, lastSignature, lastHeaderHash, extendedCommit, txs, *timestamp) if err != nil { return err } diff --git a/block/manager_test.go b/block/manager_test.go index 02d9b8d9df..acb21033f4 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -30,6 +30,7 @@ import ( goDA "github.com/rollkit/go-da" goDATest "github.com/rollkit/go-da/test" + "github.com/rollkit/go-sequencing" seqGRPC "github.com/rollkit/go-sequencing/proxy/grpc" "github.com/rollkit/rollkit/config" @@ -920,3 +921,53 @@ func TestNormalAggregationLoop(t *testing.T) { // Wait for the function to complete or timeout <-ctx.Done() } + +func TestGetTxsFromBatch_NoBatch(t *testing.T) { + // Mocking a manager with an empty batch queue + m := &Manager{ + bq: &BatchQueue{queue: nil}, // No batch available + } + + // Call the method and assert the results + txs, timestamp, err := m.getTxsFromBatch() + + // Assertions + assert.Nil(t, txs, "Transactions should be nil when no batch exists") + assert.Nil(t, timestamp, "Timestamp should be nil when no batch exists") + assert.Equal(t, ErrNoBatch, err, "Expected ErrNoBatch error") +} + +func TestGetTxsFromBatch_EmptyBatch(t *testing.T) { + // Mocking a manager with an empty batch + m := &Manager{ + bq: &BatchQueue{queue: []BatchWithTime{ + {Batch: &sequencing.Batch{Transactions: nil}, Time: time.Now()}, + }}, + } + + // Call the method and assert the results + txs, timestamp, err := m.getTxsFromBatch() + + // Assertions + require.NoError(t, err, "Expected no error for empty batch") + assert.Empty(t, txs, "Transactions should be empty when batch has no transactions") + assert.NotNil(t, timestamp, "Timestamp should not be nil for empty batch") +} + +func TestGetTxsFromBatch_ValidBatch(t *testing.T) { + // Mocking a manager with a valid batch + m := &Manager{ + bq: &BatchQueue{queue: []BatchWithTime{ + {Batch: &sequencing.Batch{Transactions: [][]byte{[]byte("tx1"), []byte("tx2")}}, Time: time.Now()}, + }}, + } + + // Call the method and assert the results + txs, timestamp, err := m.getTxsFromBatch() + + // Assertions + require.NoError(t, err, "Expected no error for valid batch") + assert.Len(t, txs, 2, "Expected 2 transactions") + assert.NotNil(t, timestamp, "Timestamp should not be nil for valid batch") + assert.Equal(t, cmtypes.Txs{cmtypes.Tx([]byte("tx1")), cmtypes.Tx([]byte("tx2"))}, txs, "Transactions do not match") +} From 6b744629d6694af77413a25798b6ee3f63051502 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Tue, 8 Oct 2024 11:18:14 +0400 Subject: [PATCH 09/19] faster batch retrieval --- block/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/manager.go b/block/manager.go index 8c6fb4b785..33a26477b5 100644 --- a/block/manager.go +++ b/block/manager.go @@ -52,7 +52,7 @@ const defaultBlockTime = 1 * time.Second const defaultLazyBlockTime = 60 * time.Second // defaultBatchRetrievalInterval is the interval at which the sequencer retrieves batches -const defaultBatchRetrievalInterval = 1 * time.Second +const defaultBatchRetrievalInterval = 100 * time.Microsecond // defaultMempoolTTL is the number of blocks until transaction is dropped from mempool const defaultMempoolTTL = 25 From a682912e6940d5d38b7e9fd37bb655a747d294bd Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Wed, 9 Oct 2024 10:01:58 +0400 Subject: [PATCH 10/19] revert default retrieval time to 1s and fix mempool2node test --- block/manager.go | 2 +- node/full_client_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/block/manager.go b/block/manager.go index 33a26477b5..8c6fb4b785 100644 --- a/block/manager.go +++ b/block/manager.go @@ -52,7 +52,7 @@ const defaultBlockTime = 1 * time.Second const defaultLazyBlockTime = 60 * time.Second // defaultBatchRetrievalInterval is the interval at which the sequencer retrieves batches -const defaultBatchRetrievalInterval = 100 * time.Microsecond +const defaultBatchRetrievalInterval = 1 * time.Second // defaultMempoolTTL is the number of blocks until transaction is dropped from mempool const defaultMempoolTTL = 25 diff --git a/node/full_client_test.go b/node/full_client_test.go index 771348f533..d35c95d832 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -821,6 +821,7 @@ func TestMempool2Nodes(t *testing.T) { ListenAddress: "/ip4/127.0.0.1/tcp/9001", }, BlockManagerConfig: getBMConfig(), + SequencerAddress: MockSequencerAddress, }, key1, signingKey1, proxy.NewLocalClientCreator(app), genesisDoc, DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) require.NoError(err) require.NotNil(node1) @@ -832,6 +833,7 @@ func TestMempool2Nodes(t *testing.T) { ListenAddress: "/ip4/127.0.0.1/tcp/9002", Seeds: "/ip4/127.0.0.1/tcp/9001/p2p/" + id1.Loggable()["peerID"].(string), }, + SequencerAddress: MockSequencerAddress, }, key2, signingKey2, proxy.NewLocalClientCreator(app), genesisDoc, DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) require.NoError(err) require.NotNil(node2) From e64384281bf0d4afa1b0711c8c928400e19ac41b Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Wed, 9 Oct 2024 10:45:22 +0400 Subject: [PATCH 11/19] fix cli issue --- cmd/rollkit/commands/run_node.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/rollkit/commands/run_node.go b/cmd/rollkit/commands/run_node.go index d0b6bc24f6..41c84fbb31 100644 --- a/cmd/rollkit/commands/run_node.go +++ b/cmd/rollkit/commands/run_node.go @@ -137,7 +137,7 @@ func NewRunNodeCmd() *cobra.Command { // use mock grpc sequencer server by default if !cmd.Flags().Lookup("rollkit.sequencer_address").Changed { - srv, err := startMockSequencerServerGRPC(MockSequencerAddress) + srv, err := startMockSequencerServerGRPC(MockSequencerAddress, genDoc.ChainID) if err != nil { return fmt.Errorf("failed to launch mock sequencing server: %w", err) } @@ -247,8 +247,8 @@ func startMockDAServJSONRPC(ctx context.Context) (*proxy.Server, error) { } // startMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. -func startMockSequencerServerGRPC(listenAddress string) (*grpc.Server, error) { - dummySeq := seqTest.NewDummySequencer([]byte("test")) +func startMockSequencerServerGRPC(listenAddress string, rollupId string) (*grpc.Server, error) { + dummySeq := seqTest.NewDummySequencer([]byte(rollupId)) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) lis, err := net.Listen("tcp", listenAddress) if err != nil { From f6ba822aae96ad6b273bfe191120f30f04a89df5 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Wed, 9 Oct 2024 11:23:45 +0400 Subject: [PATCH 12/19] test to TestChainID --- node/node_test.go | 2 +- state/executor_test.go | 2 +- test/server/server.go | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/node/node_test.go b/node/node_test.go index 527481701e..af23888801 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -78,7 +78,7 @@ func startMockGRPCServ() *grpc.Server { // startMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func startMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer([]byte("test")) + dummySeq := seqTest.NewDummySequencer([]byte(types.TestChainID)) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) lis, err := net.Listen("tcp", listenAddress) if err != nil { diff --git a/state/executor_test.go b/state/executor_test.go index 59a2da2daa..934d258ed8 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -165,7 +165,7 @@ func doTestApplyBlock(t *testing.T) { MockSequencerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), )) - mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte("test"), seqClient, logger) + mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(types.TestChainID), seqClient, logger) ctx := context.Background() require.NoError(mpoolReaper.StartReaper(ctx)) txQuery, err := query.New("tm.event='Tx'") diff --git a/test/server/server.go b/test/server/server.go index 305081999d..dd831ccd2a 100644 --- a/test/server/server.go +++ b/test/server/server.go @@ -13,6 +13,7 @@ import ( "github.com/rollkit/go-da/test" seqGRPC "github.com/rollkit/go-sequencing/proxy/grpc" seqTest "github.com/rollkit/go-sequencing/test" + "github.com/rollkit/rollkit/types" ) // StartMockDAServGRPC starts a mock gRPC server with the given listenAddress. @@ -48,7 +49,7 @@ func StartMockDAServJSONRPC(ctx context.Context, listenAddress string) *jsonrpc. // StartMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func StartMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer([]byte("test")) + dummySeq := seqTest.NewDummySequencer([]byte(types.TestChainID)) server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) addr, _ := url.Parse(listenAddress) lis, err := net.Listen("tcp", addr.Host) From 3549603dba96afb395e405e2931a7366ba67e4b6 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Wed, 9 Oct 2024 12:48:03 +0400 Subject: [PATCH 13/19] try fixing failing tests --- node/full_client_test.go | 12 ++++++++---- node/full_node_integration_test.go | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/node/full_client_test.go b/node/full_client_test.go index d35c95d832..7cd0d007a8 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -69,8 +69,9 @@ func getRPC(t *testing.T) (*mocks.Application, *FullClient) { node, err := newFullNode( ctx, config.NodeConfig{ - DAAddress: MockDAAddress, - DANamespace: MockDANamespace, + DAAddress: MockDAAddress, + DANamespace: MockDANamespace, + SequencerAddress: MockSequencerAddress, }, key, signingKey, @@ -172,7 +173,7 @@ func TestGenesisChunked(t *testing.T) { signingKey, _, _ := crypto.GenerateEd25519Key(crand.Reader) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - n, _ := newFullNode(ctx, config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace}, privKey, signingKey, proxy.NewLocalClientCreator(mockApp), genDoc, DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), test.NewFileLogger(t)) + n, _ := newFullNode(ctx, config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace, SequencerAddress: MockSequencerAddress}, privKey, signingKey, proxy.NewLocalClientCreator(mockApp), genDoc, DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), test.NewFileLogger(t)) rpc := NewFullClient(n) @@ -900,6 +901,7 @@ func TestStatus(t *testing.T) { BlockManagerConfig: config.BlockManagerConfig{ BlockTime: 10 * time.Millisecond, }, + SequencerAddress: MockSequencerAddress, }, key, signingKey, @@ -1038,7 +1040,9 @@ func TestFutureGenesisTime(t *testing.T) { Aggregator: true, BlockManagerConfig: config.BlockManagerConfig{ BlockTime: 200 * time.Millisecond, - }}, + }, + SequencerAddress: MockSequencerAddress, + }, key, signingKey, proxy.NewLocalClientCreator(mockApp), &cmtypes.GenesisDoc{ diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index ac1e7a0da8..b20cb16dc2 100644 --- a/node/full_node_integration_test.go +++ b/node/full_node_integration_test.go @@ -67,7 +67,7 @@ func TestAggregatorMode(t *testing.T) { } ctx, cancel := context.WithCancel(context.Background()) defer cancel() - node, err := newFullNode(ctx, config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace, Aggregator: true, BlockManagerConfig: blockManagerConfig}, key, signingKey, proxy.NewLocalClientCreator(app), genesisDoc, DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) + node, err := newFullNode(ctx, config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace, Aggregator: true, BlockManagerConfig: blockManagerConfig, SequencerAddress: MockSequencerAddress}, key, signingKey, proxy.NewLocalClientCreator(app), genesisDoc, DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) require.NoError(err) require.NotNil(node) From 558cb1e1112935b39ad6956f09478cca4b638037 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 11 Oct 2024 11:54:09 +0400 Subject: [PATCH 14/19] use multi-rollup sequencer and test name as chainId which is intern the rollupId for the sequencer comms --- block/cache_test.go | 5 +- block/manager.go | 22 +++--- block/manager_test.go | 45 ++++++----- block/pending_test.go | 10 +-- da/da_test.go | 17 ++-- go.mod | 2 +- go.sum | 4 +- node/full_client_test.go | 121 +++++++++++++++++------------ node/full_node_integration_test.go | 42 +++++----- node/full_node_test.go | 37 +++++---- node/helpers_test.go | 7 +- node/light_client_test.go | 2 +- node/node_test.go | 20 ++--- rpc/json/helpers_test.go | 7 +- rpc/json/service_test.go | 10 +-- rpc/json/ws_test.go | 15 ++-- sequencing/mock/cmd/main.go | 2 +- state/executor_test.go | 18 +++-- store/store_test.go | 26 ++++--- test/server/server.go | 3 +- types/abci/block_test.go | 8 +- types/signed_header_test.go | 5 +- types/utils.go | 47 ++++++----- types/utils_test.go | 2 +- 24 files changed, 261 insertions(+), 216 deletions(-) diff --git a/block/cache_test.go b/block/cache_test.go index 674bba5006..0310f27906 100644 --- a/block/cache_test.go +++ b/block/cache_test.go @@ -9,6 +9,7 @@ import ( ) func TestBlockCache(t *testing.T) { + chainId := "TestBlockCache" require := require.New(t) // Create new HeaderCache and DataCache and verify not nil hc := NewHeaderCache() @@ -19,7 +20,7 @@ func TestBlockCache(t *testing.T) { // Test setBlock and getBlock height, nTxs := uint64(1), 2 - header, data := types.GetRandomBlock(height, nTxs) + header, data := types.GetRandomBlock(height, nTxs, chainId) hc.setHeader(height, header) gotHeader := hc.getHeader(height) require.NotNil(gotHeader, "getHeader should return non-nil after setHeader") @@ -30,7 +31,7 @@ func TestBlockCache(t *testing.T) { require.Equal(data, gotData) // Test overwriting a block - header1, data1 := types.GetRandomBlock(height, nTxs) + header1, data1 := types.GetRandomBlock(height, nTxs, chainId) hc.setHeader(height, header1) gotHeader1 := hc.getHeader(height) require.NotNil(gotHeader1, "getHeader should return non-nil after overwriting a header") diff --git a/block/manager.go b/block/manager.go index 8c6fb4b785..80727238da 100644 --- a/block/manager.go +++ b/block/manager.go @@ -426,15 +426,17 @@ func (m *Manager) BatchRetrieveLoop(ctx context.Context) { batchTime := res.Timestamp // Add the batch to the batch queue if batch != nil { - m.bq.AddBatch(BatchWithTime{batch, batchTime}) - // update lastBatchHash only if the batch has actual txs - if batch.Transactions != nil { - // Calculate the hash of the batch and store it for the next batch retrieval - h, err := batch.Hash() - if err != nil { - m.logger.Error("error while hashing batch", "error", err) + // Calculate the hash of the batch and store it for the next batch retrieval + h, err := batch.Hash() + if err == nil { + // add batch to the queue even if its empty (no txs) + m.bq.AddBatch(BatchWithTime{batch, batchTime}) + // update lastBatchHash only if the batch has actual txs + if batch.Transactions != nil { + m.lastBatchHash = h } - m.lastBatchHash = h + } else { + m.logger.Error("error while hashing batch", "error", err) } } } @@ -442,8 +444,8 @@ func (m *Manager) BatchRetrieveLoop(ctx context.Context) { // period based on the batch retrieval time. remainingSleep := time.Duration(0) elapsed := time.Since(start) - if elapsed < defaultBatchRetrievalInterval { - remainingSleep = defaultBatchRetrievalInterval - elapsed + if elapsed < m.conf.BlockTime { + remainingSleep = m.conf.BlockTime - elapsed } batchTimer.Reset(remainingSleep) } diff --git a/block/manager_test.go b/block/manager_test.go index 8ce7663147..621b0bbf09 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -86,9 +86,9 @@ func getManager(t *testing.T, backend goDA.DA) *Manager { func TestInitialStateClean(t *testing.T) { require := require.New(t) - genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestInitialStateClean") genesis := &cmtypes.GenesisDoc{ - ChainID: "myChain", + ChainID: "TestInitialStateClean", InitialHeight: 1, Validators: genesisDoc.Validators, AppHash: []byte("app hash"), @@ -103,16 +103,16 @@ func TestInitialStateClean(t *testing.T) { func TestInitialStateStored(t *testing.T) { require := require.New(t) - genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestInitialStateStored") valset := types.GetRandomValidatorSet() genesis := &cmtypes.GenesisDoc{ - ChainID: "myChain", + ChainID: "TestInitialStateStored", InitialHeight: 1, Validators: genesisDoc.Validators, AppHash: []byte("app hash"), } sampleState := types.State{ - ChainID: "myChain", + ChainID: "TestInitialStateStored", InitialHeight: 1, LastBlockHeight: 100, Validators: valset, @@ -180,16 +180,16 @@ func TestHandleEmptyDataHash(t *testing.T) { func TestInitialStateUnexpectedHigherGenesis(t *testing.T) { require := require.New(t) - genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestInitialStateUnexpectedHigherGenesis") valset := types.GetRandomValidatorSet() genesis := &cmtypes.GenesisDoc{ - ChainID: "myChain", + ChainID: "TestInitialStateUnexpectedHigherGenesis", InitialHeight: 2, Validators: genesisDoc.Validators, AppHash: []byte("app hash"), } sampleState := types.State{ - ChainID: "myChain", + ChainID: "TestInitialStateUnexpectedHigherGenesis", InitialHeight: 1, LastBlockHeight: 0, Validators: valset, @@ -283,7 +283,7 @@ func TestSubmitBlocksToMockDA(t *testing.T) { m.store = store.New(kvStore) var blobs [][]byte - header, data := types.GetRandomBlock(1, 5) + header, data := types.GetRandomBlock(1, 5, "TestSubmitBlocksToMockDA") blob, err := header.MarshalBinary() require.NoError(t, err) @@ -458,15 +458,16 @@ func TestSubmitBlocksToMockDA(t *testing.T) { // Test_submitBlocksToDA_BlockMarshalErrorCase1: A itself has a marshalling error. So A, B and C never get submitted. func Test_submitBlocksToDA_BlockMarshalErrorCase1(t *testing.T) { + chainId := "Test_submitBlocksToDA_BlockMarshalErrorCase1" assert := assert.New(t) require := require.New(t) ctx := context.Background() m := getManager(t, goDATest.NewDummyDA()) - header1, data1 := types.GetRandomBlock(uint64(1), 5) - header2, data2 := types.GetRandomBlock(uint64(2), 5) - header3, data3 := types.GetRandomBlock(uint64(3), 5) + header1, data1 := types.GetRandomBlock(uint64(1), 5, chainId) + header2, data2 := types.GetRandomBlock(uint64(2), 5, chainId) + header3, data3 := types.GetRandomBlock(uint64(3), 5, chainId) store := mocks.NewStore(t) invalidateBlockHeader(header1) @@ -492,15 +493,16 @@ func Test_submitBlocksToDA_BlockMarshalErrorCase1(t *testing.T) { // Test_submitBlocksToDA_BlockMarshalErrorCase2: A and B are fair blocks, but C has a marshalling error // - Block A and B get submitted to DA layer not block C func Test_submitBlocksToDA_BlockMarshalErrorCase2(t *testing.T) { + chainId := "Test_submitBlocksToDA_BlockMarshalErrorCase2" assert := assert.New(t) require := require.New(t) ctx := context.Background() m := getManager(t, goDATest.NewDummyDA()) - header1, data1 := types.GetRandomBlock(uint64(1), 5) - header2, data2 := types.GetRandomBlock(uint64(2), 5) - header3, data3 := types.GetRandomBlock(uint64(3), 5) + header1, data1 := types.GetRandomBlock(uint64(1), 5, chainId) + header2, data2 := types.GetRandomBlock(uint64(2), 5, chainId) + header3, data3 := types.GetRandomBlock(uint64(3), 5, chainId) store := mocks.NewStore(t) invalidateBlockHeader(header3) @@ -553,7 +555,7 @@ func Test_isProposer(t *testing.T) { { name: "Signing key matches genesis proposer public key", args: func() args { - genesisData, privKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisData, privKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "Test_isProposer") s, err := types.NewFromGenesisDoc(genesisData) require.NoError(err) signingKey, err := types.PrivKeyToSigningKey(privKey) @@ -569,7 +571,7 @@ func Test_isProposer(t *testing.T) { { name: "Signing key does not match genesis proposer public key", args: func() args { - genesisData, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisData, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "Test_isProposer") s, err := types.NewFromGenesisDoc(genesisData) require.NoError(err) @@ -587,7 +589,7 @@ func Test_isProposer(t *testing.T) { { name: "No validators found in genesis", args: func() args { - genesisData, privKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisData, privKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "Test_isProposer") genesisData.Validators = nil s, err := types.NewFromGenesisDoc(genesisData) require.NoError(err) @@ -685,14 +687,15 @@ func TestManager_publishBlock(t *testing.T) { lastState.NextValidators = cmtypes.NewValidatorSet(validators) lastState.LastValidators = cmtypes.NewValidatorSet(validators) + chainId := "TestManager_publishBlock" mpool := mempool.NewCListMempool(cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0) seqClient := seqGRPC.NewClient() require.NoError(seqClient.Start( MockSequencerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), )) - mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte("test"), seqClient, logger) - executor := state.NewBlockExecutor(vKey.PubKey().Address(), "test", mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, 100, logger, state.NopMetrics()) + mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainId), seqClient, logger) + executor := state.NewBlockExecutor(vKey.PubKey().Address(), chainId, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, 100, logger, state.NopMetrics()) signingKey, err := types.PrivKeyToSigningKey(vKey) require.NoError(err) @@ -705,7 +708,7 @@ func TestManager_publishBlock(t *testing.T) { store: mockStore, logger: mockLogger, genesis: &cmtypes.GenesisDoc{ - ChainID: "myChain", + ChainID: chainId, InitialHeight: 1, AppHash: []byte("app hash"), }, diff --git a/block/pending_test.go b/block/pending_test.go index 333d749825..c3982645ce 100644 --- a/block/pending_test.go +++ b/block/pending_test.go @@ -21,13 +21,13 @@ const ( func TestPendingBlocks(t *testing.T) { cases := []struct { name string - init func(context.Context, *testing.T, *PendingHeaders) + init func(context.Context, *testing.T, *PendingHeaders, string) exec func(context.Context, *testing.T, *PendingHeaders) expectedBlocksAfterInit int expectedBlocksAfterExec int }{ {name: "empty store", - init: func(context.Context, *testing.T, *PendingHeaders) {}, + init: func(context.Context, *testing.T, *PendingHeaders, string) {}, exec: func(context.Context, *testing.T, *PendingHeaders) {}, expectedBlocksAfterInit: 0, expectedBlocksAfterExec: 0, @@ -68,7 +68,7 @@ func TestPendingBlocks(t *testing.T) { defer cancel() pb := newPendingBlocks(t) - tc.init(ctx, t, pb) + tc.init(ctx, t, pb, "TestPendingBlocks") checkRequirements(ctx, t, pb, tc.expectedBlocksAfterInit) tc.exec(ctx, t, pb) @@ -85,9 +85,9 @@ func newPendingBlocks(t *testing.T) *PendingHeaders { return pendingBlocks } -func fillWithBlockData(ctx context.Context, t *testing.T, pb *PendingHeaders) { +func fillWithBlockData(ctx context.Context, t *testing.T, pb *PendingHeaders, chainId string) { for i := uint64(1); i <= numBlocks; i++ { - h, d := types.GetRandomBlock(i, 0) + h, d := types.GetRandomBlock(i, 0, chainId) require.NoError(t, pb.store.SaveBlockData(ctx, h, d, &types.Signature{})) pb.store.SetHeight(ctx, i) } diff --git a/da/da_test.go b/da/da_test.go index f90c32059d..702769978e 100644 --- a/da/da_test.go +++ b/da/da_test.go @@ -68,10 +68,11 @@ func TestMain(m *testing.M) { } func TestMockDAErrors(t *testing.T) { + chainId := "TestMockDAErrors" t.Run("submit_timeout", func(t *testing.T) { mockDA := &damock.MockDA{} dalc := NewDAClient(mockDA, -1, -1, nil, nil, log.TestingLogger()) - header, _ := types.GetRandomBlock(1, 0) + header, _ := types.GetRandomBlock(1, 0, chainId) headers := []*types.SignedHeader{header} var blobs []da.Blob for _, header := range headers { @@ -97,7 +98,7 @@ func TestMockDAErrors(t *testing.T) { t.Run("tx_too_large", func(t *testing.T) { mockDA := &damock.MockDA{} dalc := NewDAClient(mockDA, -1, -1, nil, nil, log.TestingLogger()) - header, _ := types.GetRandomBlock(1, 0) + header, _ := types.GetRandomBlock(1, 0, chainId) headers := []*types.SignedHeader{header} var blobs []da.Blob for _, header := range headers { @@ -218,7 +219,7 @@ func doTestSubmitRetrieve(t *testing.T, dalc *DAClient) { for batch := uint64(0); batch < numBatches; batch++ { headers := make([]*types.SignedHeader, numHeaders) for i := range headers { - headers[i], _ = types.GetRandomBlock(batch*numBatches+uint64(i), rand.Int()%20) //nolint:gosec + headers[i], _ = types.GetRandomBlock(batch*numBatches+uint64(i), rand.Int()%20, "doTestSubmitRetrieve") //nolint:gosec } submitAndRecordHeaders(headers) time.Sleep(time.Duration(rand.Int63() % MockDABlockTime.Milliseconds())) //nolint:gosec @@ -266,8 +267,9 @@ func doTestSubmitEmptyBlocks(t *testing.T, dalc *DAClient) { assert := assert.New(t) - header1, _ := types.GetRandomBlock(1, 0) - header2, _ := types.GetRandomBlock(1, 0) + chainId := "doTestSubmitEmptyBlocks" + header1, _ := types.GetRandomBlock(1, 0, chainId) + header2, _ := types.GetRandomBlock(1, 0, chainId) resp := dalc.SubmitHeaders(ctx, []*types.SignedHeader{header1, header2}, maxBlobSize, -1) assert.Equal(StatusSuccess, resp.Code, "empty blocks should submit") assert.EqualValues(resp.SubmittedCount, 2, "empty blocks should batch") @@ -297,8 +299,9 @@ func doTestSubmitSmallBlocksBatch(t *testing.T, dalc *DAClient) { assert := assert.New(t) - header1, _ := types.GetRandomBlock(1, 1) - header2, _ := types.GetRandomBlock(1, 2) + chainId := "doTestSubmitSmallBlocksBatch" + header1, _ := types.GetRandomBlock(1, 1, chainId) + header2, _ := types.GetRandomBlock(1, 2, chainId) resp := dalc.SubmitHeaders(ctx, []*types.SignedHeader{header1, header2}, maxBlobSize, -1) assert.Equal(StatusSuccess, resp.Code, "small blocks should submit") assert.EqualValues(resp.SubmittedCount, 2, "small blocks should batch") diff --git a/go.mod b/go.mod index ca68d53ae5..0abb410998 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,7 @@ require ( github.com/celestiaorg/go-header v0.6.2 github.com/ipfs/go-ds-badger4 v0.1.5 github.com/mitchellh/mapstructure v1.5.0 - github.com/rollkit/go-sequencing v0.2.0 + github.com/rollkit/go-sequencing v0.2.1-0.20241010053131-3134457dc4e5 ) require ( diff --git a/go.sum b/go.sum index 45b2b2bfe7..83af281415 100644 --- a/go.sum +++ b/go.sum @@ -1445,8 +1445,8 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/rollkit/go-da v0.8.0 h1:oJKojC421eRC4mNqbujf40GzLFNp7HapgeB7Z/r0tyc= github.com/rollkit/go-da v0.8.0/go.mod h1:3eHWK5gkv8lhwq6bjOZOi82WwHyS2B9rQOlUrE1GGws= -github.com/rollkit/go-sequencing v0.2.0 h1:IrEA29p07zPDbqY29AzI+q2zUqm7QACAoR+/PBpexvw= -github.com/rollkit/go-sequencing v0.2.0/go.mod h1:P/cQXTw3rWpPqhqnCwKzlkS39XM8ugmyf2u63twBgG8= +github.com/rollkit/go-sequencing v0.2.1-0.20241010053131-3134457dc4e5 h1:4qHTCZsG81CE2QvvDeu2xCSIG1fkDyYbNgGwrBx98XA= +github.com/rollkit/go-sequencing v0.2.1-0.20241010053131-3134457dc4e5/go.mod h1:P/cQXTw3rWpPqhqnCwKzlkS39XM8ugmyf2u63twBgG8= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA= diff --git a/node/full_client_test.go b/node/full_client_test.go index 7cd0d007a8..0105126745 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -56,14 +56,14 @@ func getBlockMeta(rpc *FullClient, n int64) *cmtypes.BlockMeta { return bmeta } -func getRPC(t *testing.T) (*mocks.Application, *FullClient) { +func getRPC(t *testing.T, chainId string) (*mocks.Application, *FullClient) { t.Helper() require := require.New(t) app := &mocks.Application{} app.On("InitChain", mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil) key, _, _ := crypto.GenerateEd25519Key(crand.Reader) ctx := context.Background() - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) node, err := newFullNode( @@ -126,14 +126,14 @@ func indexBlocks(t *testing.T, rpc *FullClient, heights []int64) { func TestConnectionGetter(t *testing.T) { assert := assert.New(t) - _, rpc := getRPC(t) + _, rpc := getRPC(t, "TestConnectionGetter") assert.NotNil(rpc.appClient()) } func TestInfo(t *testing.T) { assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestInfo") mockApp.On("Info", mock.Anything, mock.Anything).Return(expectedInfo, nil) info, err := rpc.ABCIInfo(context.Background()) @@ -146,7 +146,7 @@ func TestCheckTx(t *testing.T) { expectedTx := []byte("tx data") - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestCheckTx") mockApp.On("CheckTx", context.Background(), &abci.RequestCheckTx{Tx: expectedTx}).Once().Return(&abci.ResponseCheckTx{}, nil) res, err := rpc.CheckTx(context.Background(), expectedTx) @@ -159,7 +159,7 @@ func TestGenesisChunked(t *testing.T) { assert := assert.New(t) genDoc := &cmtypes.GenesisDoc{ - ChainID: "test", + ChainID: "TestGenesisChunked", InitialHeight: int64(1), AppHash: []byte("test hash"), Validators: []cmtypes.GenesisValidator{ @@ -201,7 +201,7 @@ func TestBroadcastTxAsync(t *testing.T) { expectedTx := []byte("tx data") - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestBroadcastTxAsync") mockApp.On("CheckTx", mock.Anything, &abci.RequestCheckTx{Tx: expectedTx}).Return(&abci.ResponseCheckTx{}, nil) startNodeWithCleanup(t, rpc.node) @@ -231,7 +231,7 @@ func TestBroadcastTxSync(t *testing.T) { Codespace: "space", } - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestBroadcastTxSync") startNodeWithCleanup(t, rpc.node) mockApp.On("CheckTx", mock.Anything, &abci.RequestCheckTx{Tx: expectedTx}).Return(&expectedResponse, nil) @@ -308,14 +308,15 @@ func TestGetBlock(t *testing.T) { assert := assert.New(t) require := require.New(t) - mockApp, rpc := getRPC(t) + chainId := "TestGetBlock" + mockApp, rpc := getRPC(t, chainId) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) startNodeWithCleanup(t, rpc.node) ctx := context.Background() - header, data := types.GetRandomBlock(1, 10) + header, data := types.GetRandomBlock(1, 10, chainId) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) rpc.node.Store.SetHeight(ctx, header.Height()) require.NoError(err) @@ -328,16 +329,17 @@ func TestGetBlock(t *testing.T) { } func TestGetCommit(t *testing.T) { + chainId := "TestGetCommit" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, chainId) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) - header1, data1 := types.GetRandomBlock(1, 5) - header2, data2 := types.GetRandomBlock(2, 6) - header3, data3 := types.GetRandomBlock(3, 8) - header4, data4 := types.GetRandomBlock(4, 10) + header1, data1 := types.GetRandomBlock(1, 5, chainId) + header2, data2 := types.GetRandomBlock(2, 6, chainId) + header3, data3 := types.GetRandomBlock(3, 8, chainId) + header4, data4 := types.GetRandomBlock(4, 10, chainId) headers := []*types.SignedHeader{header1, header2, header3, header4} data := []*types.Data{data1, data2, data3, data4} @@ -368,9 +370,10 @@ func TestGetCommit(t *testing.T) { } func TestCometBFTLightClientCompability(t *testing.T) { + chainId := "TestCometBFTLightClientCompability" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, chainId) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) @@ -379,9 +382,9 @@ func TestCometBFTLightClientCompability(t *testing.T) { Height: 1, NTxs: 1, } - header1, data1, privKey := types.GenerateRandomBlockCustom(&config) - header2, data2 := types.GetRandomNextBlock(header1, data1, privKey, []byte{}, 2) - header3, data3 := types.GetRandomNextBlock(header2, data2, privKey, []byte{}, 3) + header1, data1, privKey := types.GenerateRandomBlockCustom(&config, chainId) + header2, data2 := types.GetRandomNextBlock(header1, data1, privKey, []byte{}, 2, chainId) + header3, data3 := types.GetRandomNextBlock(header2, data2, privKey, []byte{}, 3, chainId) headers := []*types.SignedHeader{header1, header2, header3} data := []*types.Data{data1, data2, data3} @@ -437,16 +440,17 @@ func TestCometBFTLightClientCompability(t *testing.T) { } func TestBlockSearch(t *testing.T) { + chainId := "TestBlockSearch" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, chainId) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) ctx := context.Background() heights := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} for _, h := range heights { - header, data := types.GetRandomBlock(uint64(h), 5) + header, data := types.GetRandomBlock(uint64(h), 5, chainId) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) require.NoError(err) } @@ -495,17 +499,18 @@ func TestBlockSearch(t *testing.T) { } func TestGetBlockByHash(t *testing.T) { + chainId := "TestGetBlockByHash" assert := assert.New(t) require := require.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, chainId) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) startNodeWithCleanup(t, rpc.node) ctx := context.Background() - header, data := types.GetRandomBlock(1, 10) + header, data := types.GetRandomBlock(1, 10, chainId) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) require.NoError(err) abciBlock, err := abciconv.ToABCIBlock(header, data) @@ -548,7 +553,7 @@ func TestTx(t *testing.T) { mockApp.On("PrepareProposal", mock.Anything, mock.Anything).Return(prepareProposalResponse).Maybe() mockApp.On("ProcessProposal", mock.Anything, mock.Anything).Return(&abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT}, nil) key, _, _ := crypto.GenerateEd25519Key(crand.Reader) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestTx") signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) ctx, cancel := context.WithCancel(context.Background()) @@ -617,7 +622,7 @@ func TestUnconfirmedTxs(t *testing.T) { t.Run(c.name, func(t *testing.T) { assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestUnconfirmedTxs") mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) @@ -651,7 +656,7 @@ func TestUnconfirmedTxs(t *testing.T) { func TestUnconfirmedTxsLimit(t *testing.T) { assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestUnconfirmedTxsLimit") mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) @@ -684,7 +689,7 @@ func TestConsensusState(t *testing.T) { assert := assert.New(t) require := require.New(t) - _, rpc := getRPC(t) + _, rpc := getRPC(t, "TestConsensusState") require.NotNil(rpc) resp1, err := rpc.ConsensusState(context.Background()) @@ -697,16 +702,17 @@ func TestConsensusState(t *testing.T) { } func TestBlockchainInfo(t *testing.T) { + chainId := "TestBlockchainInfo" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, chainId) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) ctx := context.Background() heights := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} for _, h := range heights { - header, data := types.GetRandomBlock(uint64(h), 5) + header, data := types.GetRandomBlock(uint64(h), 5, chainId) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) rpc.node.Store.SetHeight(ctx, header.Height()) require.NoError(err) @@ -789,7 +795,7 @@ func TestMempool2Nodes(t *testing.T) { assert := assert.New(t) require := require.New(t) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestMempool2Nodes") signingKey1, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) @@ -875,6 +881,7 @@ func TestMempool2Nodes(t *testing.T) { } func TestStatus(t *testing.T) { + chainId := "TestStatus" assert := assert.New(t) require := require.New(t) @@ -883,7 +890,7 @@ func TestStatus(t *testing.T) { app.On("PrepareProposal", mock.Anything, mock.Anything).Return(prepareProposalResponse).Maybe() app.On("ProcessProposal", mock.Anything, mock.Anything).Return(&abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT}, nil) key, _, _ := crypto.GenerateEd25519Key(crand.Reader) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) pubKey := genesisDoc.Validators[0].PubKey @@ -931,7 +938,7 @@ func TestStatus(t *testing.T) { NTxs: 1, ProposerAddr: pubKey.Bytes(), } - earliestHeader, earliestData, _ := types.GenerateRandomBlockCustom(&config) + earliestHeader, earliestData, _ := types.GenerateRandomBlockCustom(&config, chainId) err = rpc.node.Store.SaveBlockData(ctx, earliestHeader, earliestData, &types.Signature{}) rpc.node.Store.SetHeight(ctx, earliestHeader.Height()) require.NoError(err) @@ -941,7 +948,7 @@ func TestStatus(t *testing.T) { NTxs: 1, ProposerAddr: pubKey.Bytes(), } - latestHeader, latestData, _ := types.GenerateRandomBlockCustom(&config) + latestHeader, latestData, _ := types.GenerateRandomBlockCustom(&config, chainId) err = rpc.node.Store.SaveBlockData(ctx, latestHeader, latestData, &types.Signature{}) rpc.node.Store.SetHeight(ctx, latestHeader.Height()) require.NoError(err) @@ -1028,25 +1035,29 @@ func TestFutureGenesisTime(t *testing.T) { mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) key, _, _ := crypto.GenerateEd25519Key(crand.Reader) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + chainId := "TestFutureGenesisTime" + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) - genesisTime := time.Now().Local().Add(time.Second * time.Duration(1)) + + // Increase the genesis time slightly for timing buffer + genesisTime := time.Now().Local().Add(2 * time.Second) ctx, cancel := context.WithCancel(context.Background()) defer cancel() - node, err := newFullNode(ctx, config.NodeConfig{ - DAAddress: MockDAAddress, - DANamespace: MockDANamespace, - Aggregator: true, - BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 200 * time.Millisecond, + node, err := newFullNode(ctx, + config.NodeConfig{ + DAAddress: MockDAAddress, + DANamespace: MockDANamespace, + Aggregator: true, + BlockManagerConfig: config.BlockManagerConfig{ + BlockTime: 200 * time.Millisecond, + }, + SequencerAddress: MockSequencerAddress, }, - SequencerAddress: MockSequencerAddress, - }, key, signingKey, proxy.NewLocalClientCreator(mockApp), &cmtypes.GenesisDoc{ - ChainID: "test", + ChainID: chainId, InitialHeight: 1, GenesisTime: genesisTime, Validators: genesisDoc.Validators, @@ -1057,15 +1068,29 @@ func TestFutureGenesisTime(t *testing.T) { require.NotNil(node) startNodeWithCleanup(t, node) - wg.Wait() - assert.True(beginBlockTime.After(genesisTime)) + // Set a timeout for the WaitGroup + waitTimeout := time.After(5 * time.Second) + waitDone := make(chan struct{}) + + go func() { + wg.Wait() + close(waitDone) + }() + + select { + case <-waitDone: + // Assert if the beginBlockTime is after genesisTime + assert.True(beginBlockTime.After(genesisTime), "beginBlockTime should be after genesisTime") + case <-waitTimeout: + t.Fatal("Timed out waiting for FinalizeBlock to be called") + } } func TestHealth(t *testing.T) { assert := assert.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestHealth") mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(abci.ResponseCheckTx{}, nil) mockApp.On("Commit", mock.Anything).Return(abci.ResponseCommit{}, nil) @@ -1081,7 +1106,7 @@ func TestNetInfo(t *testing.T) { assert := assert.New(t) require := require.New(t) - mockApp, rpc := getRPC(t) + mockApp, rpc := getRPC(t, "TestNetInfo") mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index b20cb16dc2..f054a90624 100644 --- a/node/full_node_integration_test.go +++ b/node/full_node_integration_test.go @@ -59,7 +59,7 @@ func TestAggregatorMode(t *testing.T) { app.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) key, _, _ := crypto.GenerateEd25519Key(rand.Reader) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestAggregatorMode") signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) blockManagerConfig := config.BlockManagerConfig{ @@ -98,7 +98,7 @@ func TestTxGossipingAndAggregation(t *testing.T) { clientNodes := 4 aggCtx := context.Background() ctx := context.Background() - nodes, apps := createNodes(aggCtx, ctx, clientNodes+1, getBMConfig(), types.TestChainID, false, t) + nodes, apps := createNodes(aggCtx, ctx, clientNodes+1, getBMConfig(), "TestTxGossipingAndAggregation", false, t) startNodes(nodes, apps, t) defer func() { for _, n := range nodes { @@ -175,7 +175,7 @@ func TestLazyAggregator(t *testing.T) { app.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) key, _, _ := crypto.GenerateEd25519Key(rand.Reader) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestLazyAggregator") signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) blockManagerConfig := config.BlockManagerConfig{ @@ -247,7 +247,7 @@ func TestFastDASync(t *testing.T) { const numberOfBlocksToSyncTill = 5 // Create the 2 nodes - nodes, _ := createNodes(aggCtx, ctx, clientNodes, bmConfig, types.TestChainID, false, t) + nodes, _ := createNodes(aggCtx, ctx, clientNodes, bmConfig, "TestFastDASync", false, t) node1 := nodes[0] node2 := nodes[1] @@ -334,7 +334,7 @@ func TestChangeValSet(t *testing.T) { // tmpubKey1 key, _, _ := crypto.GenerateEd25519Key(rand.Reader) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestChangeValSet") signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) tmPubKey1, err := cryptoenc.PubKeyToProto(genesisDoc.Validators[0].PubKey) @@ -430,12 +430,12 @@ func TestSingleAggregatorTwoFullNodesBlockSyncSpeed(t *testing.T) { clientNodes := 3 bmConfig := getBMConfig() bmConfig.BlockTime = 1 * time.Second - bmConfig.DABlockTime = 10 * time.Second + bmConfig.DABlockTime = 25 * time.Second // takes longer to sync due to communication with sequencer const numberOfBlocksTSyncTill = 5 ch := make(chan struct{}) - defer close(ch) - timer := time.NewTimer(bmConfig.DABlockTime - 1*time.Second) + defer safeClose(ch) + timer := time.NewTimer(bmConfig.DABlockTime) go func() { select { @@ -448,7 +448,7 @@ func TestSingleAggregatorTwoFullNodesBlockSyncSpeed(t *testing.T) { return } }() - nodes, _ := createNodes(aggCtx, ctx, clientNodes, bmConfig, types.TestChainID, false, t) + nodes, _ := createNodes(aggCtx, ctx, clientNodes, bmConfig, "TestSingleAggregatorTwoFullNodesBlockSyncSpeed", false, t) node1 := nodes[0] node2 := nodes[1] @@ -501,7 +501,7 @@ func TestSubmitBlocksToDA(t *testing.T) { nodes, _ := createNodes(ctx, context.Background(), clientNodes, config.BlockManagerConfig{ DABlockTime: 20 * time.Millisecond, BlockTime: 10 * time.Millisecond, - }, types.TestChainID, false, t) + }, "TestSubmitBlocksToDA", false, t) seq := nodes[0] startNodeWithCleanup(t, seq) @@ -667,13 +667,13 @@ func doTestMaxPending(maxPending uint64, t *testing.T) { require := require.New(t) clientNodes := 1 - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() nodes, _ := createNodes(ctx, context.Background(), clientNodes, config.BlockManagerConfig{ DABlockTime: 20 * time.Millisecond, BlockTime: 10 * time.Millisecond, MaxPendingBlocks: maxPending, - }, types.TestChainID, false, t) + }, "TestMaxPending", false, t) seq := nodes[0] mockDA := &damock.MockDA{} @@ -816,7 +816,7 @@ func testSingleAggregatorSingleFullNode(t *testing.T, source Source) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() clientNodes := 2 - nodes, _ := createNodes(aggCtx, ctx, clientNodes, getBMConfig(), types.TestChainID, false, t) + nodes, _ := createNodes(aggCtx, ctx, clientNodes, getBMConfig(), "testSingleAggregatorSingleFullNode", false, t) node1 := nodes[0] node2 := nodes[1] @@ -838,7 +838,7 @@ func testSingleAggregatorTwoFullNode(t *testing.T, source Source) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() clientNodes := 3 - nodes, _ := createNodes(aggCtx, ctx, clientNodes, getBMConfig(), types.TestChainID, false, t) + nodes, _ := createNodes(aggCtx, ctx, clientNodes, getBMConfig(), "testSingleAggregatorTwoFullNode", false, t) node1 := nodes[0] node2 := nodes[1] @@ -865,7 +865,7 @@ func testSingleAggregatorSingleFullNodeTrustedHash(t *testing.T, source Source) ctx, cancel := context.WithCancel(context.Background()) defer cancel() clientNodes := 2 - nodes, _ := createNodes(aggCtx, ctx, clientNodes, getBMConfig(), types.TestChainID, false, t) + nodes, _ := createNodes(aggCtx, ctx, clientNodes, getBMConfig(), "testSingleAggregatorSingleFullNodeTrustedHash", false, t) node1 := nodes[0] node2 := nodes[1] @@ -898,11 +898,13 @@ func testSingleAggregatorSingleFullNodeSingleLightNode(t *testing.T) { } dalc := getMockDA(t) bmConfig := getBMConfig() - sequencer, _ := createAndConfigureNode(aggCtx, 0, true, false, keys, bmConfig, dalc, t) - fullNode, _ := createAndConfigureNode(ctx, 1, false, false, keys, bmConfig, dalc, t) - lightNode, _ := createNode(ctx, 2, false, true, keys, bmConfig, types.TestChainID, false, t) + chainId := "testSingleAggregatorSingleFullNodeSingleLightNode" + sequencer, _ := createAndConfigureNode(aggCtx, 0, true, false, chainId, keys, bmConfig, dalc, t) + fullNode, _ := createAndConfigureNode(ctx, 1, false, false, chainId, keys, bmConfig, dalc, t) + lightNode, _ := createNode(ctx, 2, false, true, keys, bmConfig, chainId, false, t) startNodeWithCleanup(t, sequencer) + require.NoError(waitForFirstBlock(sequencer, Header)) startNodeWithCleanup(t, fullNode) startNodeWithCleanup(t, lightNode) @@ -1091,9 +1093,9 @@ func createNode(ctx context.Context, n int, aggregator bool, isLight bool, keys return node, app } -func createAndConfigureNode(ctx context.Context, n int, aggregator bool, isLight bool, keys []crypto.PrivKey, bmConfig config.BlockManagerConfig, dalc *da.DAClient, t *testing.T) (Node, *mocks.Application) { +func createAndConfigureNode(ctx context.Context, n int, aggregator bool, isLight bool, chainId string, keys []crypto.PrivKey, bmConfig config.BlockManagerConfig, dalc *da.DAClient, t *testing.T) (Node, *mocks.Application) { t.Helper() - node, app := createNode(ctx, n, aggregator, isLight, keys, bmConfig, types.TestChainID, false, t) + node, app := createNode(ctx, n, aggregator, isLight, keys, bmConfig, chainId, false, t) node.(*FullNode).dalc = dalc node.(*FullNode).blockManager.SetDALC(dalc) diff --git a/node/full_node_test.go b/node/full_node_test.go index 9e3a208438..58ccb58683 100644 --- a/node/full_node_test.go +++ b/node/full_node_test.go @@ -14,7 +14,6 @@ import ( abci "github.com/cometbft/cometbft/abci/types" cmconfig "github.com/cometbft/cometbft/config" cmcrypto "github.com/cometbft/cometbft/crypto" - cmproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cometbft/cometbft/proxy" cmtypes "github.com/cometbft/cometbft/types" @@ -27,6 +26,7 @@ import ( testutils "github.com/celestiaorg/utils/test" + cmproto "github.com/cometbft/cometbft/proto/tendermint/types" goDA "github.com/rollkit/go-da" damock "github.com/rollkit/go-da/mocks" "github.com/rollkit/rollkit/block" @@ -41,14 +41,14 @@ import ( // simply check that node is starting and stopping without panicking func TestStartup(t *testing.T) { ctx := context.Background() - node := initAndStartNodeWithCleanup(ctx, t, Full) + node := initAndStartNodeWithCleanup(ctx, t, Full, "TestStartup") require.IsType(t, new(FullNode), node) } func TestMempoolDirectly(t *testing.T) { ctx := context.Background() - fn := initAndStartNodeWithCleanup(ctx, t, Full) + fn := initAndStartNodeWithCleanup(ctx, t, Full, "TestMempoolDirectly") require.IsType(t, new(FullNode), fn) node := fn.(*FullNode) @@ -59,9 +59,10 @@ func TestMempoolDirectly(t *testing.T) { // Tests that the node is able to sync multiple blocks even if blocks arrive out of order func TestTrySyncNextBlockMultiple(t *testing.T) { + chainId := "TestTrySyncNextBlockMultiple" ctx, cancel := context.WithCancel(context.Background()) defer cancel() - node, signingKey := setupTestNode(ctx, t, Full) + node, signingKey := setupTestNode(ctx, t, Full, chainId) fullNode, ok := node.(*FullNode) require.True(t, ok) @@ -73,8 +74,8 @@ func TestTrySyncNextBlockMultiple(t *testing.T) { NTxs: 0, PrivKey: signingKey, } - h1, d1, _ := types.GenerateRandomBlockCustom(&config) - h2, d2 := types.GetRandomNextBlock(h1, d1, signingKey, []byte{1, 2, 3, 4}, 0) + h1, d1, _ := types.GenerateRandomBlockCustom(&config, chainId) + h2, d2 := types.GetRandomNextBlock(h1, d1, signingKey, []byte{1, 2, 3, 4}, 0, chainId) h2.AppHash = []byte{1, 2, 3, 4} // Update state with hashes generated from block @@ -119,9 +120,10 @@ func TestTrySyncNextBlockMultiple(t *testing.T) { // Tests that the node ignores invalid blocks posted to the DA layer func TestInvalidBlocksIgnored(t *testing.T) { + chainId := "TestInvalidBlocksIgnored" ctx, cancel := context.WithCancel(context.Background()) defer cancel() - node, signingKey := setupTestNode(ctx, t, Full) + node, signingKey := setupTestNode(ctx, t, Full, chainId) fullNode, ok := node.(*FullNode) require.True(t, ok) store := fullNode.Store @@ -135,7 +137,7 @@ func TestInvalidBlocksIgnored(t *testing.T) { PrivKey: signingKey, } - h1, _, _ := types.GenerateRandomBlockCustom(&config) + h1, _, _ := types.GenerateRandomBlockCustom(&config, chainId) // Update state with hashes generated from block state, err := store.GetState(ctx) @@ -210,7 +212,7 @@ func TestPendingBlocks(t *testing.T) { _ = os.RemoveAll(dbPath) }() - genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestPendingBlocks") node, _ := createAggregatorWithPersistence(ctx, dbPath, dac, genesis, genesisValidatorKey, t) err = node.Start() @@ -281,7 +283,7 @@ func TestVoteExtension(t *testing.T) { const voteExtensionEnableHeight = 5 const expectedExtension = "vote extension from height %d" - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) // just need little more time after the sequencer integration defer cancel() testCases := []struct { @@ -294,7 +296,8 @@ func TestVoteExtension(t *testing.T) { for _, tc := range testCases { // TestPrepareProposalVoteExtChecker t.Run("TestPrepareProposalVoteExtChecker", func(t *testing.T) { - app, node, pubKey := createNodeAndApp(ctx, voteExtensionEnableHeight, tc.sigingKeyType, t) + chainId := "TestPrepareProposalVoteExtChecker" + app, node, pubKey := createNodeAndApp(ctx, chainId, voteExtensionEnableHeight, tc.sigingKeyType, t) prepareProposalVoteExtChecker := func(_ context.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) { if req.Height <= voteExtensionEnableHeight { @@ -314,7 +317,7 @@ func TestVoteExtension(t *testing.T) { Round: 0, Extension: extendedCommit.VoteExtension, } - extSignBytes := cmtypes.VoteExtensionSignBytes(types.TestChainID, vote) + extSignBytes := cmtypes.VoteExtensionSignBytes(chainId, vote) fmt.Println("failing block height", req.Height) ok := pubKey.VerifySignature(extSignBytes, extendedCommit.ExtensionSignature) require.True(ok) @@ -343,7 +346,7 @@ func TestVoteExtension(t *testing.T) { } // TestExtendVoteFailure t.Run("TestExtendVoteFailure", func(t *testing.T) { - app, node, _ := createNodeAndApp(ctx, voteExtensionEnableHeight, types.DefaultSigningKeyType, t) + app, node, _ := createNodeAndApp(ctx, "TestExtendVoteFailure", voteExtensionEnableHeight, types.DefaultSigningKeyType, t) require.NotNil(node) // Create a channel to signal from extendVoteFailure @@ -396,12 +399,12 @@ func TestVoteExtension(t *testing.T) { } // Create & configure node with app. Get signing key for mock functions. -func createNodeAndApp(ctx context.Context, voteExtensionEnableHeight int64, sigingKeyType string, t *testing.T) (*mocks.Application, Node, cmcrypto.PubKey) { +func createNodeAndApp(ctx context.Context, chainId string, voteExtensionEnableHeight int64, sigingKeyType string, t *testing.T) (*mocks.Application, Node, cmcrypto.PubKey) { require := require.New(t) app := &mocks.Application{} app.On("InitChain", mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil) - node, pubKey := createAggregatorWithApp(ctx, app, voteExtensionEnableHeight, sigingKeyType, t) + node, pubKey := createAggregatorWithApp(ctx, chainId, app, voteExtensionEnableHeight, sigingKeyType, t) require.NotNil(node) require.NotNil(pubKey) return app, node, pubKey @@ -447,11 +450,11 @@ func createAggregatorWithPersistence(ctx context.Context, dbPath string, dalc *d return fullNode, app } -func createAggregatorWithApp(ctx context.Context, app abci.Application, voteExtensionEnableHeight int64, sigingKeyType string, t *testing.T) (Node, cmcrypto.PubKey) { +func createAggregatorWithApp(ctx context.Context, chainId string, app abci.Application, voteExtensionEnableHeight int64, sigingKeyType string, t *testing.T) (Node, cmcrypto.PubKey) { t.Helper() key, _, _ := crypto.GenerateEd25519Key(rand.Reader) - genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(sigingKeyType) + genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(sigingKeyType, chainId) fmt.Println("genesis key type", genesis.Validators[0].PubKey.Type()) genesis.ConsensusParams = &cmtypes.ConsensusParams{ Block: cmtypes.DefaultBlockParams(), diff --git a/node/helpers_test.go b/node/helpers_test.go index dc446440c3..ffefacd255 100644 --- a/node/helpers_test.go +++ b/node/helpers_test.go @@ -15,7 +15,6 @@ import ( goDATest "github.com/rollkit/go-da/test" "github.com/rollkit/rollkit/da" - "github.com/rollkit/rollkit/types" ) func getMockDA(t *testing.T) *da.DAClient { @@ -44,10 +43,12 @@ func TestGetNodeHeight(t *testing.T) { keys[i], _, _ = crypto.GenerateEd25519Key(rand.Reader) } bmConfig := getBMConfig() - fullNode, _ := createAndConfigureNode(ctx, 0, true, false, keys, bmConfig, dalc, t) - lightNode, _ := createNode(ctx, 1, false, true, keys, bmConfig, types.TestChainID, false, t) + chainId := "TestGetNodeHeight" + fullNode, _ := createAndConfigureNode(ctx, 0, true, false, chainId, keys, bmConfig, dalc, t) + lightNode, _ := createNode(ctx, 1, false, true, keys, bmConfig, chainId, false, t) startNodeWithCleanup(t, fullNode) + require.NoError(waitForFirstBlock(fullNode, Store)) startNodeWithCleanup(t, lightNode) cases := []struct { diff --git a/node/light_client_test.go b/node/light_client_test.go index e3be155bf2..2366be67de 100644 --- a/node/light_client_test.go +++ b/node/light_client_test.go @@ -17,7 +17,7 @@ import ( func TestLightClient_Panics(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - ln := initAndStartNodeWithCleanup(ctx, t, Light) + ln := initAndStartNodeWithCleanup(ctx, t, Light, "TestLightClient_Panics") require.IsType(t, new(LightNode), ln) tests := []struct { diff --git a/node/node_test.go b/node/node_test.go index af23888801..786cbc1c1d 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -78,7 +78,7 @@ func startMockGRPCServ() *grpc.Server { // startMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func startMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer([]byte(types.TestChainID)) + dummySeq := seqTest.NewMultiRollupSequencer() server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) lis, err := net.Listen("tcp", listenAddress) if err != nil { @@ -114,16 +114,16 @@ func cleanUpNode(node Node, t *testing.T) { } // initAndStartNodeWithCleanup initializes and starts a node of the specified type. -func initAndStartNodeWithCleanup(ctx context.Context, t *testing.T, nodeType NodeType) Node { - node, _ := setupTestNode(ctx, t, nodeType) +func initAndStartNodeWithCleanup(ctx context.Context, t *testing.T, nodeType NodeType, chainId string) Node { + node, _ := setupTestNode(ctx, t, nodeType, chainId) startNodeWithCleanup(t, node) return node } // setupTestNode sets up a test node based on the NodeType. -func setupTestNode(ctx context.Context, t *testing.T, nodeType NodeType) (Node, cmcrypto.PrivKey) { - node, privKey, err := newTestNode(ctx, t, nodeType) +func setupTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainId string) (Node, cmcrypto.PrivKey) { + node, privKey, err := newTestNode(ctx, t, nodeType, chainId) require.NoError(t, err) require.NotNil(t, node) @@ -131,7 +131,7 @@ func setupTestNode(ctx context.Context, t *testing.T, nodeType NodeType) (Node, } // newTestNode creates a new test node based on the NodeType. -func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType) (Node, cmcrypto.PrivKey, error) { +func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainId string) (Node, cmcrypto.PrivKey, error) { config := config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace} switch nodeType { case Light: @@ -142,7 +142,7 @@ func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType) (Node, cm panic(fmt.Sprintf("invalid node type: %v", nodeType)) } app := setupMockApplication() - genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType) + genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) if err != nil { return nil, nil, err @@ -157,9 +157,9 @@ func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType) (Node, cm func TestNewNode(t *testing.T) { ctx := context.Background() - - ln := initAndStartNodeWithCleanup(ctx, t, Light) + chainId := "TestNewNode" + ln := initAndStartNodeWithCleanup(ctx, t, Light, chainId) require.IsType(t, new(LightNode), ln) - fn := initAndStartNodeWithCleanup(ctx, t, Full) + fn := initAndStartNodeWithCleanup(ctx, t, Full, chainId) require.IsType(t, new(FullNode), fn) } diff --git a/rpc/json/helpers_test.go b/rpc/json/helpers_test.go index 07fe69e61f..631c720238 100644 --- a/rpc/json/helpers_test.go +++ b/rpc/json/helpers_test.go @@ -32,6 +32,9 @@ const ( // MockDANamespace is the mock namespace MockDANamespace = "00000000000000000000000000000000000000000000000000deadbeef" + + // MockSequencerAddress is a sample address used by the mock sequencer + MockSequencerAddress = "localhost:50051" ) // TestMain starts the mock gRPC server @@ -59,7 +62,7 @@ func prepareProposalResponse(_ context.Context, req *abci.RequestPrepareProposal } // copied from rpc -func getRPC(t *testing.T) (*mocks.Application, rpcclient.Client) { +func getRPC(t *testing.T, chainId string) (*mocks.Application, rpcclient.Client) { t.Helper() require := require.New(t) app := &mocks.Application{} @@ -103,7 +106,7 @@ func getRPC(t *testing.T) (*mocks.Application, rpcclient.Client) { genesisValidators := []cmtypes.GenesisValidator{ {Address: pubKey.Address(), PubKey: pubKey, Power: int64(100), Name: "gen #1"}, } - n, err := node.NewNode(context.Background(), config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace, Aggregator: true, BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, node.DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) + n, err := node.NewNode(context.Background(), config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace, Aggregator: true, BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false, SequencerAddress: MockSequencerAddress}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: chainId, Validators: genesisValidators}, node.DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) require.NoError(err) require.NotNil(n) diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index a3441aa4e3..0238cac5f4 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -31,7 +31,7 @@ func TestHandlerMapping(t *testing.T) { assert := assert.New(t) require := require.New(t) - _, local := getRPC(t) + _, local := getRPC(t, "TestHandlerMapping") handler, err := GetHTTPHandler(local, log.TestingLogger()) require.NoError(err) @@ -82,7 +82,7 @@ func TestREST(t *testing.T) { {"invalid/hex param", "/check_tx?tx=QWERTY", http.StatusOK, int(json2.E_PARSE), "failed to parse param 'tx'"}, } - _, local := getRPC(t) + _, local := getRPC(t, "TestREST") handler, err := GetHTTPHandler(local, log.TestingLogger()) require.NoError(err) @@ -113,7 +113,7 @@ func TestEmptyRequest(t *testing.T) { assert := assert.New(t) require := require.New(t) - _, local := getRPC(t) + _, local := getRPC(t, "TestEmptyRequest") handler, err := GetHTTPHandler(local, log.TestingLogger()) require.NoError(err) @@ -128,7 +128,7 @@ func TestStringyRequest(t *testing.T) { assert := assert.New(t) require := require.New(t) - _, local := getRPC(t) + _, local := getRPC(t, "TestStringyRequest") handler, err := GetHTTPHandler(local, log.TestingLogger()) require.NoError(err) @@ -182,7 +182,7 @@ func TestSubscription(t *testing.T) { require.NoError(err) require.NotEmpty(unsubscribeAllReq) - _, local := getRPC(t) + _, local := getRPC(t, "TestSubscription") handler, err := GetHTTPHandler(local, log.TestingLogger()) require.NoError(err) diff --git a/rpc/json/ws_test.go b/rpc/json/ws_test.go index 6a69b7d3a6..5c6030405a 100644 --- a/rpc/json/ws_test.go +++ b/rpc/json/ws_test.go @@ -24,11 +24,12 @@ func TestWebSockets(t *testing.T) { assert := assert.New(t) require := require.New(t) - _, local := getRPC(t) + _, local := getRPC(t, "TestWebSockets") handler, err := GetHTTPHandler(local, log.TestingLogger()) require.NoError(err) srv := httptest.NewServer(handler) + defer srv.Close() conn, resp, err := websocket.DefaultDialer.Dial(strings.Replace(srv.URL, "http://", "ws://", 1)+"/websocket", nil) require.NoError(err) @@ -42,12 +43,12 @@ func TestWebSockets(t *testing.T) { err = conn.WriteMessage(websocket.TextMessage, []byte(` { - "jsonrpc": "2.0", - "method": "subscribe", - "id": 7, - "params": { - "query": "tm.event='NewBlock'" - } +"jsonrpc": "2.0", +"method": "subscribe", +"id": 7, +"params": { +"query": "tm.event='NewBlock'" +} } `)) require.NoError(err) diff --git a/sequencing/mock/cmd/main.go b/sequencing/mock/cmd/main.go index 73a0feb558..14b6ceaf98 100644 --- a/sequencing/mock/cmd/main.go +++ b/sequencing/mock/cmd/main.go @@ -23,7 +23,7 @@ func main() { log.Fatalf("Failed to listen: %v", err) } - dummySequencer := test.NewDummySequencer([]byte("test")) + dummySequencer := test.NewMultiRollupSequencer() srv := grpc.NewServer(dummySequencer, dummySequencer, dummySequencer) log.Printf("Listening on: %s:%s", "localhost", "50051") diff --git a/state/executor_test.go b/state/executor_test.go index 934d258ed8..2057dd720e 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -56,7 +56,7 @@ func doTestCreateBlock(t *testing.T) { fmt.Println("Made NID") mpool := mempool.NewCListMempool(cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0) fmt.Println("Made a NewTxMempool") - executor := NewBlockExecutor([]byte("test address"), "test", mpool, nil, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, 100, logger, NopMetrics()) + executor := NewBlockExecutor([]byte("test address"), "doTestCreateBlock", mpool, nil, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, 100, logger, NopMetrics()) fmt.Println("Made a New Block Executor") state := types.State{} @@ -131,6 +131,8 @@ func doTestApplyBlock(t *testing.T) { _, err := rand.Read(mockAppHash[:]) require.NoError(err) + chainId := "doTestApplyBlock" + app := &mocks.Application{} app.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) app.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) @@ -165,7 +167,7 @@ func doTestApplyBlock(t *testing.T) { MockSequencerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), )) - mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(types.TestChainID), seqClient, logger) + mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainId), seqClient, logger) ctx := context.Background() require.NoError(mpoolReaper.StartReaper(ctx)) txQuery, err := query.New("tm.event='Tx'") @@ -199,8 +201,8 @@ func doTestApplyBlock(t *testing.T) { state.ConsensusParams.Block = &cmproto.BlockParams{} state.ConsensusParams.Block.MaxBytes = 100 state.ConsensusParams.Block.MaxGas = 100000 - chainID := "test" - executor := NewBlockExecutor(vKey.PubKey().Address().Bytes(), chainID, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) + + executor := NewBlockExecutor(vKey.PubKey().Address().Bytes(), chainId, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) tx := []byte{1, 2, 3, 4} err = mpool.CheckTx(tx, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{}) @@ -297,7 +299,7 @@ func TestUpdateStateConsensusParams(t *testing.T) { require.NoError(t, err) require.NotNil(t, client) - chainID := "test" + chainId := "TestUpdateStateConsensusParams" mpool := mempool.NewCListMempool(cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0) seqClient := seqGRPC.NewClient() @@ -305,11 +307,11 @@ func TestUpdateStateConsensusParams(t *testing.T) { MockSequencerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), )) - mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte("test"), seqClient, logger) + mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainId), seqClient, logger) require.NoError(t, mpoolReaper.StartReaper(context.Background())) eventBus := cmtypes.NewEventBus() require.NoError(t, eventBus.Start()) - executor := NewBlockExecutor([]byte("test address"), chainID, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) + executor := NewBlockExecutor([]byte("test address"), chainId, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) state := types.State{ ConsensusParams: cmproto.ConsensusParams{ @@ -329,7 +331,7 @@ func TestUpdateStateConsensusParams(t *testing.T) { NextValidators: cmtypes.NewValidatorSet([]*cmtypes.Validator{{Address: []byte("test"), PubKey: nil, VotingPower: 100, ProposerPriority: 1}}), } - header, data := types.GetRandomBlock(1234, 2) + header, data := types.GetRandomBlock(1234, 2, chainId) txResults := make([]*abci.ExecTxResult, len(data.Txs)) for idx := range data.Txs { diff --git a/store/store_test.go b/store/store_test.go index a5c35af998..866c8d2ccd 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -18,15 +18,16 @@ import ( func TestStoreHeight(t *testing.T) { t.Parallel() - header1, data1 := types.GetRandomBlock(1, 0) - header2, data2 := types.GetRandomBlock(1, 0) - header3, data3 := types.GetRandomBlock(2, 0) - header4, data4 := types.GetRandomBlock(2, 0) - header5, data5 := types.GetRandomBlock(3, 0) - header6, data6 := types.GetRandomBlock(1, 0) - header7, data7 := types.GetRandomBlock(1, 0) - header8, data8 := types.GetRandomBlock(9, 0) - header9, data9 := types.GetRandomBlock(10, 0) + chainId := "TestStoreHeight" + header1, data1 := types.GetRandomBlock(1, 0, chainId) + header2, data2 := types.GetRandomBlock(1, 0, chainId) + header3, data3 := types.GetRandomBlock(2, 0, chainId) + header4, data4 := types.GetRandomBlock(2, 0, chainId) + header5, data5 := types.GetRandomBlock(3, 0, chainId) + header6, data6 := types.GetRandomBlock(1, 0, chainId) + header7, data7 := types.GetRandomBlock(1, 0, chainId) + header8, data8 := types.GetRandomBlock(9, 0, chainId) + header9, data9 := types.GetRandomBlock(10, 0, chainId) cases := []struct { name string headers []*types.SignedHeader @@ -61,9 +62,10 @@ func TestStoreHeight(t *testing.T) { func TestStoreLoad(t *testing.T) { t.Parallel() - header1, data1 := types.GetRandomBlock(1, 10) - header2, data2 := types.GetRandomBlock(1, 10) - header3, data3 := types.GetRandomBlock(2, 20) + chainId := "TestStoreLoad" + header1, data1 := types.GetRandomBlock(1, 10, chainId) + header2, data2 := types.GetRandomBlock(1, 10, chainId) + header3, data3 := types.GetRandomBlock(2, 20, chainId) cases := []struct { name string headers []*types.SignedHeader diff --git a/test/server/server.go b/test/server/server.go index dd831ccd2a..5f73e6ed7f 100644 --- a/test/server/server.go +++ b/test/server/server.go @@ -13,7 +13,6 @@ import ( "github.com/rollkit/go-da/test" seqGRPC "github.com/rollkit/go-sequencing/proxy/grpc" seqTest "github.com/rollkit/go-sequencing/test" - "github.com/rollkit/rollkit/types" ) // StartMockDAServGRPC starts a mock gRPC server with the given listenAddress. @@ -49,7 +48,7 @@ func StartMockDAServJSONRPC(ctx context.Context, listenAddress string) *jsonrpc. // StartMockSequencerServerGRPC starts a mock gRPC server with the given listenAddress. func StartMockSequencerServerGRPC(listenAddress string) *grpc.Server { - dummySeq := seqTest.NewDummySequencer([]byte(types.TestChainID)) + dummySeq := seqTest.NewMultiRollupSequencer() server := seqGRPC.NewServer(dummySeq, dummySeq, dummySeq) addr, _ := url.Parse(listenAddress) lis, err := net.Listen("tcp", addr.Host) diff --git a/types/abci/block_test.go b/types/abci/block_test.go index 4e98732179..45480c82ed 100644 --- a/types/abci/block_test.go +++ b/types/abci/block_test.go @@ -16,7 +16,7 @@ import ( ) func TestToABCIHeaderPB(t *testing.T) { - header := types.GetRandomHeader() + header := types.GetRandomHeader("TestToABCIHeaderPB") expected := cmproto.Header{ Version: cmversion.Consensus{ Block: header.Version.Block, @@ -52,7 +52,7 @@ func TestToABCIHeaderPB(t *testing.T) { } func TestToABCIHeader(t *testing.T) { - header := types.GetRandomHeader() + header := types.GetRandomHeader("TestToABCIHeader") expected := cmtypes.Header{ Version: cmversion.Consensus{ Block: header.Version.Block, @@ -89,7 +89,7 @@ func TestToABCIHeader(t *testing.T) { func TestToABCIBlock(t *testing.T) { blockHeight, nTxs := uint64(1), 2 - header, data := types.GetRandomBlock(blockHeight, nTxs) + header, data := types.GetRandomBlock(blockHeight, nTxs, "TestToABCIBlock") abciHeader, err := ToABCIHeader(&header.Header) if err != nil { t.Fatal(err) @@ -127,7 +127,7 @@ func TestToABCIBlock(t *testing.T) { func TestToABCIBlockMeta(t *testing.T) { blockHeight, nTxs := uint64(1), 2 - header, data := types.GetRandomBlock(blockHeight, nTxs) + header, data := types.GetRandomBlock(blockHeight, nTxs, "TestToABCIBlockMeta") cmblock, err := ToABCIBlock(header, data) if err != nil { t.Fatal(err) diff --git a/types/signed_header_test.go b/types/signed_header_test.go index 1c04f3ba76..e553c4eac9 100644 --- a/types/signed_header_test.go +++ b/types/signed_header_test.go @@ -13,11 +13,12 @@ import ( ) func TestSignedHeader(t *testing.T) { + chainId := "TestSignedHeader" // Generate a random signed header - trusted, privKey, err := GetRandomSignedHeader() + trusted, privKey, err := GetRandomSignedHeader(chainId) require.NoError(t, err) // Get the next random header - untrustedAdj, err := GetRandomNextSignedHeader(trusted, privKey) + untrustedAdj, err := GetRandomNextSignedHeader(trusted, privKey, chainId) require.NoError(t, err) t.Run("Test Verify", func(t *testing.T) { testVerify(t, trusted, untrustedAdj, privKey) diff --git a/types/utils.go b/types/utils.go index 87bc5118be..2fd6e45b56 100644 --- a/types/utils.go +++ b/types/utils.go @@ -17,9 +17,6 @@ import ( "github.com/libp2p/go-libp2p/core/crypto" ) -// TestChainID is a constant used for testing purposes. It represents a mock chain ID. -const TestChainID = "test" - // DefaultSigningKeyType is the key type used by the sequencer signing key const DefaultSigningKeyType = "ed25519" @@ -69,19 +66,19 @@ type BlockConfig struct { // GetRandomBlock creates a block with a given height and number of transactions, intended for testing. // It's tailored for simplicity, primarily used in test setups where additional outputs are not needed. -func GetRandomBlock(height uint64, nTxs int) (*SignedHeader, *Data) { +func GetRandomBlock(height uint64, nTxs int, chainId string) (*SignedHeader, *Data) { config := BlockConfig{ Height: height, NTxs: nTxs, } // Assuming GenerateBlock modifies the context directly with the generated block and other needed data. - header, data, _ := GenerateRandomBlockCustom(&config) + header, data, _ := GenerateRandomBlockCustom(&config, chainId) return header, data } // GenerateRandomBlockCustom returns a block with random data and the given height, transactions, privateKey and proposer address. -func GenerateRandomBlockCustom(config *BlockConfig) (*SignedHeader, *Data, cmcrypto.PrivKey) { +func GenerateRandomBlockCustom(config *BlockConfig, chainId string) (*SignedHeader, *Data, cmcrypto.PrivKey) { data := getBlockDataWith(config.NTxs) dataHash := data.Hash() @@ -95,7 +92,7 @@ func GenerateRandomBlockCustom(config *BlockConfig) (*SignedHeader, *Data, cmcry PrivKey: config.PrivKey, } - signedHeader, err := GetRandomSignedHeaderCustom(&headerConfig) + signedHeader, err := GetRandomSignedHeaderCustom(&headerConfig, chainId) if err != nil { panic(err) } @@ -105,7 +102,7 @@ func GenerateRandomBlockCustom(config *BlockConfig) (*SignedHeader, *Data, cmcry } data.Metadata = &Metadata{ - ChainID: TestChainID, + ChainID: chainId, Height: signedHeader.Height(), LastDataHash: nil, Time: uint64(signedHeader.Time().UnixNano()), @@ -115,13 +112,13 @@ func GenerateRandomBlockCustom(config *BlockConfig) (*SignedHeader, *Data, cmcry } // GetRandomNextBlock returns a block with random data and height of +1 from the provided block -func GetRandomNextBlock(header *SignedHeader, data *Data, privKey cmcrypto.PrivKey, appHash header.Hash, nTxs int) (*SignedHeader, *Data) { +func GetRandomNextBlock(header *SignedHeader, data *Data, privKey cmcrypto.PrivKey, appHash header.Hash, nTxs int, chainId string) (*SignedHeader, *Data) { nextData := getBlockDataWith(nTxs) dataHash := nextData.Hash() valSet := header.Validators newSignedHeader := &SignedHeader{ - Header: GetRandomNextHeader(header.Header), + Header: GetRandomNextHeader(header.Header, chainId), Validators: valSet, } newSignedHeader.ProposerAddress = header.Header.ProposerAddress @@ -137,7 +134,7 @@ func GetRandomNextBlock(header *SignedHeader, data *Data, privKey cmcrypto.PrivK } newSignedHeader.Signature = *signature nextData.Metadata = &Metadata{ - ChainID: TestChainID, + ChainID: chainId, Height: newSignedHeader.Height(), LastDataHash: nil, Time: uint64(newSignedHeader.Time().UnixNano()), @@ -154,12 +151,12 @@ type HeaderConfig struct { } // GetRandomHeader returns a header with random fields and current time -func GetRandomHeader() Header { +func GetRandomHeader(chainId string) Header { return Header{ BaseHeader: BaseHeader{ Height: uint64(rand.Int63()), //nolint:gosec Time: uint64(time.Now().UnixNano()), - ChainID: TestChainID, + ChainID: chainId, }, Version: Version{ Block: InitStateVersion.Consensus.Block, @@ -178,8 +175,8 @@ func GetRandomHeader() Header { // GetRandomNextHeader returns a header with random data and height of +1 from // the provided Header -func GetRandomNextHeader(header Header) Header { - nextHeader := GetRandomHeader() +func GetRandomNextHeader(header Header, chainId string) Header { + nextHeader := GetRandomHeader(chainId) nextHeader.BaseHeader.Height = header.Height() + 1 nextHeader.BaseHeader.Time = uint64(time.Now().Add(1 * time.Second).UnixNano()) nextHeader.LastHeaderHash = header.Hash() @@ -189,7 +186,7 @@ func GetRandomNextHeader(header Header) Header { } // GetRandomSignedHeader generates a signed header with random data and returns it. -func GetRandomSignedHeader() (*SignedHeader, cmcrypto.PrivKey, error) { +func GetRandomSignedHeader(chainId string) (*SignedHeader, cmcrypto.PrivKey, error) { config := HeaderConfig{ Height: uint64(rand.Int63()), //nolint:gosec DataHash: GetRandomBytes(32), @@ -197,7 +194,7 @@ func GetRandomSignedHeader() (*SignedHeader, cmcrypto.PrivKey, error) { VotingPower: 1, } - signedHeader, err := GetRandomSignedHeaderCustom(&config) + signedHeader, err := GetRandomSignedHeaderCustom(&config, chainId) if err != nil { return nil, ed25519.PrivKey{}, err } @@ -205,14 +202,14 @@ func GetRandomSignedHeader() (*SignedHeader, cmcrypto.PrivKey, error) { } // GetRandomSignedHeaderCustom creates a signed header based on the provided HeaderConfig. -func GetRandomSignedHeaderCustom(config *HeaderConfig) (*SignedHeader, error) { +func GetRandomSignedHeaderCustom(config *HeaderConfig, chainId string) (*SignedHeader, error) { valsetConfig := ValidatorConfig{ PrivKey: config.PrivKey, VotingPower: 1, } valSet := GetValidatorSetCustom(valsetConfig) signedHeader := &SignedHeader{ - Header: GetRandomHeader(), + Header: GetRandomHeader(chainId), Validators: valSet, } signedHeader.Header.BaseHeader.Height = config.Height @@ -231,10 +228,10 @@ func GetRandomSignedHeaderCustom(config *HeaderConfig) (*SignedHeader, error) { // GetRandomNextSignedHeader returns a signed header with random data and height of +1 from // the provided signed header -func GetRandomNextSignedHeader(signedHeader *SignedHeader, privKey cmcrypto.PrivKey) (*SignedHeader, error) { +func GetRandomNextSignedHeader(signedHeader *SignedHeader, privKey cmcrypto.PrivKey, chainId string) (*SignedHeader, error) { valSet := signedHeader.Validators newSignedHeader := &SignedHeader{ - Header: GetRandomNextHeader(signedHeader.Header), + Header: GetRandomNextHeader(signedHeader.Header, chainId), Validators: valSet, } newSignedHeader.LastCommitHash = signedHeader.Signature.GetCommitHash( @@ -272,12 +269,12 @@ func GetNodeKey(nodeKey *p2p.NodeKey) (crypto.PrivKey, error) { } // GetFirstSignedHeader creates a 1st signed header for a chain, given a valset and signing key. -func GetFirstSignedHeader(privkey ed25519.PrivKey, valSet *cmtypes.ValidatorSet) (*SignedHeader, error) { +func GetFirstSignedHeader(privkey ed25519.PrivKey, valSet *cmtypes.ValidatorSet, chainId string) (*SignedHeader, error) { header := Header{ BaseHeader: BaseHeader{ Height: 1, Time: uint64(time.Now().UnixNano()), - ChainID: TestChainID, + ChainID: chainId, }, Version: Version{ Block: InitStateVersion.Consensus.Block, @@ -321,7 +318,7 @@ func GetValidatorSetFromGenesis(g *cmtypes.GenesisDoc) cmtypes.ValidatorSet { } // GetGenesisWithPrivkey returns a genesis doc with a single validator and a signing key -func GetGenesisWithPrivkey(sigingKeyType string) (*cmtypes.GenesisDoc, cmcrypto.PrivKey) { +func GetGenesisWithPrivkey(sigingKeyType string, chainId string) (*cmtypes.GenesisDoc, cmcrypto.PrivKey) { var genesisValidatorKey cmcrypto.PrivKey switch sigingKeyType { case "secp256k1": @@ -338,7 +335,7 @@ func GetGenesisWithPrivkey(sigingKeyType string) (*cmtypes.GenesisDoc, cmcrypto. Name: "sequencer", }} genDoc := &cmtypes.GenesisDoc{ - ChainID: TestChainID, + ChainID: chainId, InitialHeight: 0, Validators: genesisValidators, } diff --git a/types/utils_test.go b/types/utils_test.go index 5c27f78b32..77fee62dba 100644 --- a/types/utils_test.go +++ b/types/utils_test.go @@ -55,7 +55,7 @@ func TestGetRandomHeader(t *testing.T) { // Generate 100 random headers and check that they are all unique headerSet := make(map[string]bool) for i := 0; i < 100; i++ { - header := GetRandomHeader() + header := GetRandomHeader("TestGetRandomHeader") headerHash := header.Hash().String() if _, ok := headerSet[headerHash]; ok { t.Errorf("Duplicate header generated: %v", header) From f072ab8f7a9c466d9045475a431d90aa67ac2c7b Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 11 Oct 2024 11:58:59 +0400 Subject: [PATCH 15/19] fix lint --- block/manager.go | 3 --- node/full_node_test.go | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/block/manager.go b/block/manager.go index 80727238da..c747cd8a2b 100644 --- a/block/manager.go +++ b/block/manager.go @@ -51,9 +51,6 @@ const defaultBlockTime = 1 * time.Second // defaultLazyBlockTime is used only if LazyBlockTime is not configured for manager const defaultLazyBlockTime = 60 * time.Second -// defaultBatchRetrievalInterval is the interval at which the sequencer retrieves batches -const defaultBatchRetrievalInterval = 1 * time.Second - // defaultMempoolTTL is the number of blocks until transaction is dropped from mempool const defaultMempoolTTL = 25 diff --git a/node/full_node_test.go b/node/full_node_test.go index 58ccb58683..f988d505b4 100644 --- a/node/full_node_test.go +++ b/node/full_node_test.go @@ -14,6 +14,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" cmconfig "github.com/cometbft/cometbft/config" cmcrypto "github.com/cometbft/cometbft/crypto" + cmproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cometbft/cometbft/proxy" cmtypes "github.com/cometbft/cometbft/types" @@ -26,7 +27,6 @@ import ( testutils "github.com/celestiaorg/utils/test" - cmproto "github.com/cometbft/cometbft/proto/tendermint/types" goDA "github.com/rollkit/go-da" damock "github.com/rollkit/go-da/mocks" "github.com/rollkit/rollkit/block" From efa64336267fbd64b72517ecb30a2f0fcb0adf0e Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 11 Oct 2024 12:41:18 +0400 Subject: [PATCH 16/19] siging to signing --- node/full_node_test.go | 16 ++++++++-------- types/utils.go | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/node/full_node_test.go b/node/full_node_test.go index f988d505b4..1452ee1538 100644 --- a/node/full_node_test.go +++ b/node/full_node_test.go @@ -287,17 +287,17 @@ func TestVoteExtension(t *testing.T) { defer cancel() testCases := []struct { - sigingKeyType string + signingKeyType string }{ - {sigingKeyType: "ed25519"}, - {sigingKeyType: "secp256k1"}, + {signingKeyType: "ed25519"}, + {signingKeyType: "secp256k1"}, } for _, tc := range testCases { // TestPrepareProposalVoteExtChecker t.Run("TestPrepareProposalVoteExtChecker", func(t *testing.T) { chainId := "TestPrepareProposalVoteExtChecker" - app, node, pubKey := createNodeAndApp(ctx, chainId, voteExtensionEnableHeight, tc.sigingKeyType, t) + app, node, pubKey := createNodeAndApp(ctx, chainId, voteExtensionEnableHeight, tc.signingKeyType, t) prepareProposalVoteExtChecker := func(_ context.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) { if req.Height <= voteExtensionEnableHeight { @@ -399,12 +399,12 @@ func TestVoteExtension(t *testing.T) { } // Create & configure node with app. Get signing key for mock functions. -func createNodeAndApp(ctx context.Context, chainId string, voteExtensionEnableHeight int64, sigingKeyType string, t *testing.T) (*mocks.Application, Node, cmcrypto.PubKey) { +func createNodeAndApp(ctx context.Context, chainId string, voteExtensionEnableHeight int64, signingKeyType string, t *testing.T) (*mocks.Application, Node, cmcrypto.PubKey) { require := require.New(t) app := &mocks.Application{} app.On("InitChain", mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil) - node, pubKey := createAggregatorWithApp(ctx, chainId, app, voteExtensionEnableHeight, sigingKeyType, t) + node, pubKey := createAggregatorWithApp(ctx, chainId, app, voteExtensionEnableHeight, signingKeyType, t) require.NotNil(node) require.NotNil(pubKey) return app, node, pubKey @@ -450,11 +450,11 @@ func createAggregatorWithPersistence(ctx context.Context, dbPath string, dalc *d return fullNode, app } -func createAggregatorWithApp(ctx context.Context, chainId string, app abci.Application, voteExtensionEnableHeight int64, sigingKeyType string, t *testing.T) (Node, cmcrypto.PubKey) { +func createAggregatorWithApp(ctx context.Context, chainId string, app abci.Application, voteExtensionEnableHeight int64, signingKeyType string, t *testing.T) (Node, cmcrypto.PubKey) { t.Helper() key, _, _ := crypto.GenerateEd25519Key(rand.Reader) - genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(sigingKeyType, chainId) + genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(signingKeyType, chainId) fmt.Println("genesis key type", genesis.Validators[0].PubKey.Type()) genesis.ConsensusParams = &cmtypes.ConsensusParams{ Block: cmtypes.DefaultBlockParams(), diff --git a/types/utils.go b/types/utils.go index 2fd6e45b56..19936175f2 100644 --- a/types/utils.go +++ b/types/utils.go @@ -318,9 +318,9 @@ func GetValidatorSetFromGenesis(g *cmtypes.GenesisDoc) cmtypes.ValidatorSet { } // GetGenesisWithPrivkey returns a genesis doc with a single validator and a signing key -func GetGenesisWithPrivkey(sigingKeyType string, chainId string) (*cmtypes.GenesisDoc, cmcrypto.PrivKey) { +func GetGenesisWithPrivkey(signingKeyType string, chainId string) (*cmtypes.GenesisDoc, cmcrypto.PrivKey) { var genesisValidatorKey cmcrypto.PrivKey - switch sigingKeyType { + switch signingKeyType { case "secp256k1": genesisValidatorKey = secp256k1.GenPrivKey() default: From b8cc013b85d7f176ff4841403bbafb07c5ac69a7 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 11 Oct 2024 12:48:44 +0400 Subject: [PATCH 17/19] chainId to chainID --- block/cache_test.go | 6 +-- block/manager_test.go | 29 +++++++------- block/pending_test.go | 4 +- da/da_test.go | 18 ++++----- node/full_client_test.go | 64 +++++++++++++++--------------- node/full_node_integration_test.go | 12 +++--- node/full_node_test.go | 28 ++++++------- node/helpers_test.go | 6 +-- node/node_test.go | 18 ++++----- rpc/json/helpers_test.go | 4 +- state/executor_test.go | 14 +++---- store/store_test.go | 28 ++++++------- types/signed_header_test.go | 6 +-- types/utils.go | 44 ++++++++++---------- 14 files changed, 141 insertions(+), 140 deletions(-) diff --git a/block/cache_test.go b/block/cache_test.go index 0310f27906..4664284a9e 100644 --- a/block/cache_test.go +++ b/block/cache_test.go @@ -9,7 +9,7 @@ import ( ) func TestBlockCache(t *testing.T) { - chainId := "TestBlockCache" + chainID := "TestBlockCache" require := require.New(t) // Create new HeaderCache and DataCache and verify not nil hc := NewHeaderCache() @@ -20,7 +20,7 @@ func TestBlockCache(t *testing.T) { // Test setBlock and getBlock height, nTxs := uint64(1), 2 - header, data := types.GetRandomBlock(height, nTxs, chainId) + header, data := types.GetRandomBlock(height, nTxs, chainID) hc.setHeader(height, header) gotHeader := hc.getHeader(height) require.NotNil(gotHeader, "getHeader should return non-nil after setHeader") @@ -31,7 +31,7 @@ func TestBlockCache(t *testing.T) { require.Equal(data, gotData) // Test overwriting a block - header1, data1 := types.GetRandomBlock(height, nTxs, chainId) + header1, data1 := types.GetRandomBlock(height, nTxs, chainID) hc.setHeader(height, header1) gotHeader1 := hc.getHeader(height) require.NotNil(gotHeader1, "getHeader should return non-nil after overwriting a header") diff --git a/block/manager_test.go b/block/manager_test.go index 621b0bbf09..14fcdef045 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -85,10 +85,11 @@ func getManager(t *testing.T, backend goDA.DA) *Manager { // } func TestInitialStateClean(t *testing.T) { + const chainID = "TestInitialStateClean" require := require.New(t) - genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestInitialStateClean") + genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainID) genesis := &cmtypes.GenesisDoc{ - ChainID: "TestInitialStateClean", + ChainID: chainID, InitialHeight: 1, Validators: genesisDoc.Validators, AppHash: []byte("app hash"), @@ -458,16 +459,16 @@ func TestSubmitBlocksToMockDA(t *testing.T) { // Test_submitBlocksToDA_BlockMarshalErrorCase1: A itself has a marshalling error. So A, B and C never get submitted. func Test_submitBlocksToDA_BlockMarshalErrorCase1(t *testing.T) { - chainId := "Test_submitBlocksToDA_BlockMarshalErrorCase1" + chainID := "Test_submitBlocksToDA_BlockMarshalErrorCase1" assert := assert.New(t) require := require.New(t) ctx := context.Background() m := getManager(t, goDATest.NewDummyDA()) - header1, data1 := types.GetRandomBlock(uint64(1), 5, chainId) - header2, data2 := types.GetRandomBlock(uint64(2), 5, chainId) - header3, data3 := types.GetRandomBlock(uint64(3), 5, chainId) + header1, data1 := types.GetRandomBlock(uint64(1), 5, chainID) + header2, data2 := types.GetRandomBlock(uint64(2), 5, chainID) + header3, data3 := types.GetRandomBlock(uint64(3), 5, chainID) store := mocks.NewStore(t) invalidateBlockHeader(header1) @@ -493,16 +494,16 @@ func Test_submitBlocksToDA_BlockMarshalErrorCase1(t *testing.T) { // Test_submitBlocksToDA_BlockMarshalErrorCase2: A and B are fair blocks, but C has a marshalling error // - Block A and B get submitted to DA layer not block C func Test_submitBlocksToDA_BlockMarshalErrorCase2(t *testing.T) { - chainId := "Test_submitBlocksToDA_BlockMarshalErrorCase2" + chainID := "Test_submitBlocksToDA_BlockMarshalErrorCase2" assert := assert.New(t) require := require.New(t) ctx := context.Background() m := getManager(t, goDATest.NewDummyDA()) - header1, data1 := types.GetRandomBlock(uint64(1), 5, chainId) - header2, data2 := types.GetRandomBlock(uint64(2), 5, chainId) - header3, data3 := types.GetRandomBlock(uint64(3), 5, chainId) + header1, data1 := types.GetRandomBlock(uint64(1), 5, chainID) + header2, data2 := types.GetRandomBlock(uint64(2), 5, chainID) + header3, data3 := types.GetRandomBlock(uint64(3), 5, chainID) store := mocks.NewStore(t) invalidateBlockHeader(header3) @@ -687,15 +688,15 @@ func TestManager_publishBlock(t *testing.T) { lastState.NextValidators = cmtypes.NewValidatorSet(validators) lastState.LastValidators = cmtypes.NewValidatorSet(validators) - chainId := "TestManager_publishBlock" + chainID := "TestManager_publishBlock" mpool := mempool.NewCListMempool(cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0) seqClient := seqGRPC.NewClient() require.NoError(seqClient.Start( MockSequencerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), )) - mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainId), seqClient, logger) - executor := state.NewBlockExecutor(vKey.PubKey().Address(), chainId, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, 100, logger, state.NopMetrics()) + mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainID), seqClient, logger) + executor := state.NewBlockExecutor(vKey.PubKey().Address(), chainID, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, 100, logger, state.NopMetrics()) signingKey, err := types.PrivKeyToSigningKey(vKey) require.NoError(err) @@ -708,7 +709,7 @@ func TestManager_publishBlock(t *testing.T) { store: mockStore, logger: mockLogger, genesis: &cmtypes.GenesisDoc{ - ChainID: chainId, + ChainID: chainID, InitialHeight: 1, AppHash: []byte("app hash"), }, diff --git a/block/pending_test.go b/block/pending_test.go index c3982645ce..d5e82e6815 100644 --- a/block/pending_test.go +++ b/block/pending_test.go @@ -85,9 +85,9 @@ func newPendingBlocks(t *testing.T) *PendingHeaders { return pendingBlocks } -func fillWithBlockData(ctx context.Context, t *testing.T, pb *PendingHeaders, chainId string) { +func fillWithBlockData(ctx context.Context, t *testing.T, pb *PendingHeaders, chainID string) { for i := uint64(1); i <= numBlocks; i++ { - h, d := types.GetRandomBlock(i, 0, chainId) + h, d := types.GetRandomBlock(i, 0, chainID) require.NoError(t, pb.store.SaveBlockData(ctx, h, d, &types.Signature{})) pb.store.SetHeight(ctx, i) } diff --git a/da/da_test.go b/da/da_test.go index 702769978e..2a09153002 100644 --- a/da/da_test.go +++ b/da/da_test.go @@ -68,11 +68,11 @@ func TestMain(m *testing.M) { } func TestMockDAErrors(t *testing.T) { - chainId := "TestMockDAErrors" + chainID := "TestMockDAErrors" t.Run("submit_timeout", func(t *testing.T) { mockDA := &damock.MockDA{} dalc := NewDAClient(mockDA, -1, -1, nil, nil, log.TestingLogger()) - header, _ := types.GetRandomBlock(1, 0, chainId) + header, _ := types.GetRandomBlock(1, 0, chainID) headers := []*types.SignedHeader{header} var blobs []da.Blob for _, header := range headers { @@ -98,7 +98,7 @@ func TestMockDAErrors(t *testing.T) { t.Run("tx_too_large", func(t *testing.T) { mockDA := &damock.MockDA{} dalc := NewDAClient(mockDA, -1, -1, nil, nil, log.TestingLogger()) - header, _ := types.GetRandomBlock(1, 0, chainId) + header, _ := types.GetRandomBlock(1, 0, chainID) headers := []*types.SignedHeader{header} var blobs []da.Blob for _, header := range headers { @@ -267,9 +267,9 @@ func doTestSubmitEmptyBlocks(t *testing.T, dalc *DAClient) { assert := assert.New(t) - chainId := "doTestSubmitEmptyBlocks" - header1, _ := types.GetRandomBlock(1, 0, chainId) - header2, _ := types.GetRandomBlock(1, 0, chainId) + chainID := "doTestSubmitEmptyBlocks" + header1, _ := types.GetRandomBlock(1, 0, chainID) + header2, _ := types.GetRandomBlock(1, 0, chainID) resp := dalc.SubmitHeaders(ctx, []*types.SignedHeader{header1, header2}, maxBlobSize, -1) assert.Equal(StatusSuccess, resp.Code, "empty blocks should submit") assert.EqualValues(resp.SubmittedCount, 2, "empty blocks should batch") @@ -299,9 +299,9 @@ func doTestSubmitSmallBlocksBatch(t *testing.T, dalc *DAClient) { assert := assert.New(t) - chainId := "doTestSubmitSmallBlocksBatch" - header1, _ := types.GetRandomBlock(1, 1, chainId) - header2, _ := types.GetRandomBlock(1, 2, chainId) + chainID := "doTestSubmitSmallBlocksBatch" + header1, _ := types.GetRandomBlock(1, 1, chainID) + header2, _ := types.GetRandomBlock(1, 2, chainID) resp := dalc.SubmitHeaders(ctx, []*types.SignedHeader{header1, header2}, maxBlobSize, -1) assert.Equal(StatusSuccess, resp.Code, "small blocks should submit") assert.EqualValues(resp.SubmittedCount, 2, "small blocks should batch") diff --git a/node/full_client_test.go b/node/full_client_test.go index 0105126745..5feca8817c 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -56,14 +56,14 @@ func getBlockMeta(rpc *FullClient, n int64) *cmtypes.BlockMeta { return bmeta } -func getRPC(t *testing.T, chainId string) (*mocks.Application, *FullClient) { +func getRPC(t *testing.T, chainID string) (*mocks.Application, *FullClient) { t.Helper() require := require.New(t) app := &mocks.Application{} app.On("InitChain", mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil) key, _, _ := crypto.GenerateEd25519Key(crand.Reader) ctx := context.Background() - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainID) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) node, err := newFullNode( @@ -308,15 +308,15 @@ func TestGetBlock(t *testing.T) { assert := assert.New(t) require := require.New(t) - chainId := "TestGetBlock" - mockApp, rpc := getRPC(t, chainId) + chainID := "TestGetBlock" + mockApp, rpc := getRPC(t, chainID) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) startNodeWithCleanup(t, rpc.node) ctx := context.Background() - header, data := types.GetRandomBlock(1, 10, chainId) + header, data := types.GetRandomBlock(1, 10, chainID) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) rpc.node.Store.SetHeight(ctx, header.Height()) require.NoError(err) @@ -329,17 +329,17 @@ func TestGetBlock(t *testing.T) { } func TestGetCommit(t *testing.T) { - chainId := "TestGetCommit" + chainID := "TestGetCommit" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t, chainId) + mockApp, rpc := getRPC(t, chainID) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) - header1, data1 := types.GetRandomBlock(1, 5, chainId) - header2, data2 := types.GetRandomBlock(2, 6, chainId) - header3, data3 := types.GetRandomBlock(3, 8, chainId) - header4, data4 := types.GetRandomBlock(4, 10, chainId) + header1, data1 := types.GetRandomBlock(1, 5, chainID) + header2, data2 := types.GetRandomBlock(2, 6, chainID) + header3, data3 := types.GetRandomBlock(3, 8, chainID) + header4, data4 := types.GetRandomBlock(4, 10, chainID) headers := []*types.SignedHeader{header1, header2, header3, header4} data := []*types.Data{data1, data2, data3, data4} @@ -370,10 +370,10 @@ func TestGetCommit(t *testing.T) { } func TestCometBFTLightClientCompability(t *testing.T) { - chainId := "TestCometBFTLightClientCompability" + chainID := "TestCometBFTLightClientCompability" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t, chainId) + mockApp, rpc := getRPC(t, chainID) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) @@ -382,9 +382,9 @@ func TestCometBFTLightClientCompability(t *testing.T) { Height: 1, NTxs: 1, } - header1, data1, privKey := types.GenerateRandomBlockCustom(&config, chainId) - header2, data2 := types.GetRandomNextBlock(header1, data1, privKey, []byte{}, 2, chainId) - header3, data3 := types.GetRandomNextBlock(header2, data2, privKey, []byte{}, 3, chainId) + header1, data1, privKey := types.GenerateRandomBlockCustom(&config, chainID) + header2, data2 := types.GetRandomNextBlock(header1, data1, privKey, []byte{}, 2, chainID) + header3, data3 := types.GetRandomNextBlock(header2, data2, privKey, []byte{}, 3, chainID) headers := []*types.SignedHeader{header1, header2, header3} data := []*types.Data{data1, data2, data3} @@ -440,17 +440,17 @@ func TestCometBFTLightClientCompability(t *testing.T) { } func TestBlockSearch(t *testing.T) { - chainId := "TestBlockSearch" + chainID := "TestBlockSearch" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t, chainId) + mockApp, rpc := getRPC(t, chainID) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) ctx := context.Background() heights := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} for _, h := range heights { - header, data := types.GetRandomBlock(uint64(h), 5, chainId) + header, data := types.GetRandomBlock(uint64(h), 5, chainID) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) require.NoError(err) } @@ -499,18 +499,18 @@ func TestBlockSearch(t *testing.T) { } func TestGetBlockByHash(t *testing.T) { - chainId := "TestGetBlockByHash" + chainID := "TestGetBlockByHash" assert := assert.New(t) require := require.New(t) - mockApp, rpc := getRPC(t, chainId) + mockApp, rpc := getRPC(t, chainID) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) startNodeWithCleanup(t, rpc.node) ctx := context.Background() - header, data := types.GetRandomBlock(1, 10, chainId) + header, data := types.GetRandomBlock(1, 10, chainID) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) require.NoError(err) abciBlock, err := abciconv.ToABCIBlock(header, data) @@ -702,17 +702,17 @@ func TestConsensusState(t *testing.T) { } func TestBlockchainInfo(t *testing.T) { - chainId := "TestBlockchainInfo" + chainID := "TestBlockchainInfo" require := require.New(t) assert := assert.New(t) - mockApp, rpc := getRPC(t, chainId) + mockApp, rpc := getRPC(t, chainID) mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) ctx := context.Background() heights := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} for _, h := range heights { - header, data := types.GetRandomBlock(uint64(h), 5, chainId) + header, data := types.GetRandomBlock(uint64(h), 5, chainID) err := rpc.node.Store.SaveBlockData(ctx, header, data, &types.Signature{}) rpc.node.Store.SetHeight(ctx, header.Height()) require.NoError(err) @@ -881,7 +881,7 @@ func TestMempool2Nodes(t *testing.T) { } func TestStatus(t *testing.T) { - chainId := "TestStatus" + chainID := "TestStatus" assert := assert.New(t) require := require.New(t) @@ -890,7 +890,7 @@ func TestStatus(t *testing.T) { app.On("PrepareProposal", mock.Anything, mock.Anything).Return(prepareProposalResponse).Maybe() app.On("ProcessProposal", mock.Anything, mock.Anything).Return(&abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT}, nil) key, _, _ := crypto.GenerateEd25519Key(crand.Reader) - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainID) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) pubKey := genesisDoc.Validators[0].PubKey @@ -938,7 +938,7 @@ func TestStatus(t *testing.T) { NTxs: 1, ProposerAddr: pubKey.Bytes(), } - earliestHeader, earliestData, _ := types.GenerateRandomBlockCustom(&config, chainId) + earliestHeader, earliestData, _ := types.GenerateRandomBlockCustom(&config, chainID) err = rpc.node.Store.SaveBlockData(ctx, earliestHeader, earliestData, &types.Signature{}) rpc.node.Store.SetHeight(ctx, earliestHeader.Height()) require.NoError(err) @@ -948,7 +948,7 @@ func TestStatus(t *testing.T) { NTxs: 1, ProposerAddr: pubKey.Bytes(), } - latestHeader, latestData, _ := types.GenerateRandomBlockCustom(&config, chainId) + latestHeader, latestData, _ := types.GenerateRandomBlockCustom(&config, chainID) err = rpc.node.Store.SaveBlockData(ctx, latestHeader, latestData, &types.Signature{}) rpc.node.Store.SetHeight(ctx, latestHeader.Height()) require.NoError(err) @@ -1035,8 +1035,8 @@ func TestFutureGenesisTime(t *testing.T) { mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) mockApp.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) key, _, _ := crypto.GenerateEd25519Key(crand.Reader) - chainId := "TestFutureGenesisTime" - genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) + chainID := "TestFutureGenesisTime" + genesisDoc, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainID) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) require.NoError(err) @@ -1057,7 +1057,7 @@ func TestFutureGenesisTime(t *testing.T) { key, signingKey, proxy.NewLocalClientCreator(mockApp), &cmtypes.GenesisDoc{ - ChainID: chainId, + ChainID: chainID, InitialHeight: 1, GenesisTime: genesisTime, Validators: genesisDoc.Validators, diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index f054a90624..e585762cef 100644 --- a/node/full_node_integration_test.go +++ b/node/full_node_integration_test.go @@ -898,10 +898,10 @@ func testSingleAggregatorSingleFullNodeSingleLightNode(t *testing.T) { } dalc := getMockDA(t) bmConfig := getBMConfig() - chainId := "testSingleAggregatorSingleFullNodeSingleLightNode" - sequencer, _ := createAndConfigureNode(aggCtx, 0, true, false, chainId, keys, bmConfig, dalc, t) - fullNode, _ := createAndConfigureNode(ctx, 1, false, false, chainId, keys, bmConfig, dalc, t) - lightNode, _ := createNode(ctx, 2, false, true, keys, bmConfig, chainId, false, t) + chainID := "testSingleAggregatorSingleFullNodeSingleLightNode" + sequencer, _ := createAndConfigureNode(aggCtx, 0, true, false, chainID, keys, bmConfig, dalc, t) + fullNode, _ := createAndConfigureNode(ctx, 1, false, false, chainID, keys, bmConfig, dalc, t) + lightNode, _ := createNode(ctx, 2, false, true, keys, bmConfig, chainID, false, t) startNodeWithCleanup(t, sequencer) require.NoError(waitForFirstBlock(sequencer, Header)) @@ -1093,9 +1093,9 @@ func createNode(ctx context.Context, n int, aggregator bool, isLight bool, keys return node, app } -func createAndConfigureNode(ctx context.Context, n int, aggregator bool, isLight bool, chainId string, keys []crypto.PrivKey, bmConfig config.BlockManagerConfig, dalc *da.DAClient, t *testing.T) (Node, *mocks.Application) { +func createAndConfigureNode(ctx context.Context, n int, aggregator bool, isLight bool, chainID string, keys []crypto.PrivKey, bmConfig config.BlockManagerConfig, dalc *da.DAClient, t *testing.T) (Node, *mocks.Application) { t.Helper() - node, app := createNode(ctx, n, aggregator, isLight, keys, bmConfig, chainId, false, t) + node, app := createNode(ctx, n, aggregator, isLight, keys, bmConfig, chainID, false, t) node.(*FullNode).dalc = dalc node.(*FullNode).blockManager.SetDALC(dalc) diff --git a/node/full_node_test.go b/node/full_node_test.go index 1452ee1538..c0d840a265 100644 --- a/node/full_node_test.go +++ b/node/full_node_test.go @@ -59,10 +59,10 @@ func TestMempoolDirectly(t *testing.T) { // Tests that the node is able to sync multiple blocks even if blocks arrive out of order func TestTrySyncNextBlockMultiple(t *testing.T) { - chainId := "TestTrySyncNextBlockMultiple" + chainID := "TestTrySyncNextBlockMultiple" ctx, cancel := context.WithCancel(context.Background()) defer cancel() - node, signingKey := setupTestNode(ctx, t, Full, chainId) + node, signingKey := setupTestNode(ctx, t, Full, chainID) fullNode, ok := node.(*FullNode) require.True(t, ok) @@ -74,8 +74,8 @@ func TestTrySyncNextBlockMultiple(t *testing.T) { NTxs: 0, PrivKey: signingKey, } - h1, d1, _ := types.GenerateRandomBlockCustom(&config, chainId) - h2, d2 := types.GetRandomNextBlock(h1, d1, signingKey, []byte{1, 2, 3, 4}, 0, chainId) + h1, d1, _ := types.GenerateRandomBlockCustom(&config, chainID) + h2, d2 := types.GetRandomNextBlock(h1, d1, signingKey, []byte{1, 2, 3, 4}, 0, chainID) h2.AppHash = []byte{1, 2, 3, 4} // Update state with hashes generated from block @@ -120,10 +120,10 @@ func TestTrySyncNextBlockMultiple(t *testing.T) { // Tests that the node ignores invalid blocks posted to the DA layer func TestInvalidBlocksIgnored(t *testing.T) { - chainId := "TestInvalidBlocksIgnored" + chainID := "TestInvalidBlocksIgnored" ctx, cancel := context.WithCancel(context.Background()) defer cancel() - node, signingKey := setupTestNode(ctx, t, Full, chainId) + node, signingKey := setupTestNode(ctx, t, Full, chainID) fullNode, ok := node.(*FullNode) require.True(t, ok) store := fullNode.Store @@ -137,7 +137,7 @@ func TestInvalidBlocksIgnored(t *testing.T) { PrivKey: signingKey, } - h1, _, _ := types.GenerateRandomBlockCustom(&config, chainId) + h1, _, _ := types.GenerateRandomBlockCustom(&config, chainID) // Update state with hashes generated from block state, err := store.GetState(ctx) @@ -296,8 +296,8 @@ func TestVoteExtension(t *testing.T) { for _, tc := range testCases { // TestPrepareProposalVoteExtChecker t.Run("TestPrepareProposalVoteExtChecker", func(t *testing.T) { - chainId := "TestPrepareProposalVoteExtChecker" - app, node, pubKey := createNodeAndApp(ctx, chainId, voteExtensionEnableHeight, tc.signingKeyType, t) + chainID := "TestPrepareProposalVoteExtChecker" + app, node, pubKey := createNodeAndApp(ctx, chainID, voteExtensionEnableHeight, tc.signingKeyType, t) prepareProposalVoteExtChecker := func(_ context.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) { if req.Height <= voteExtensionEnableHeight { @@ -317,7 +317,7 @@ func TestVoteExtension(t *testing.T) { Round: 0, Extension: extendedCommit.VoteExtension, } - extSignBytes := cmtypes.VoteExtensionSignBytes(chainId, vote) + extSignBytes := cmtypes.VoteExtensionSignBytes(chainID, vote) fmt.Println("failing block height", req.Height) ok := pubKey.VerifySignature(extSignBytes, extendedCommit.ExtensionSignature) require.True(ok) @@ -399,12 +399,12 @@ func TestVoteExtension(t *testing.T) { } // Create & configure node with app. Get signing key for mock functions. -func createNodeAndApp(ctx context.Context, chainId string, voteExtensionEnableHeight int64, signingKeyType string, t *testing.T) (*mocks.Application, Node, cmcrypto.PubKey) { +func createNodeAndApp(ctx context.Context, chainID string, voteExtensionEnableHeight int64, signingKeyType string, t *testing.T) (*mocks.Application, Node, cmcrypto.PubKey) { require := require.New(t) app := &mocks.Application{} app.On("InitChain", mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil) - node, pubKey := createAggregatorWithApp(ctx, chainId, app, voteExtensionEnableHeight, signingKeyType, t) + node, pubKey := createAggregatorWithApp(ctx, chainID, app, voteExtensionEnableHeight, signingKeyType, t) require.NotNil(node) require.NotNil(pubKey) return app, node, pubKey @@ -450,11 +450,11 @@ func createAggregatorWithPersistence(ctx context.Context, dbPath string, dalc *d return fullNode, app } -func createAggregatorWithApp(ctx context.Context, chainId string, app abci.Application, voteExtensionEnableHeight int64, signingKeyType string, t *testing.T) (Node, cmcrypto.PubKey) { +func createAggregatorWithApp(ctx context.Context, chainID string, app abci.Application, voteExtensionEnableHeight int64, signingKeyType string, t *testing.T) (Node, cmcrypto.PubKey) { t.Helper() key, _, _ := crypto.GenerateEd25519Key(rand.Reader) - genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(signingKeyType, chainId) + genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(signingKeyType, chainID) fmt.Println("genesis key type", genesis.Validators[0].PubKey.Type()) genesis.ConsensusParams = &cmtypes.ConsensusParams{ Block: cmtypes.DefaultBlockParams(), diff --git a/node/helpers_test.go b/node/helpers_test.go index ffefacd255..fc03cba19c 100644 --- a/node/helpers_test.go +++ b/node/helpers_test.go @@ -43,9 +43,9 @@ func TestGetNodeHeight(t *testing.T) { keys[i], _, _ = crypto.GenerateEd25519Key(rand.Reader) } bmConfig := getBMConfig() - chainId := "TestGetNodeHeight" - fullNode, _ := createAndConfigureNode(ctx, 0, true, false, chainId, keys, bmConfig, dalc, t) - lightNode, _ := createNode(ctx, 1, false, true, keys, bmConfig, chainId, false, t) + chainID := "TestGetNodeHeight" + fullNode, _ := createAndConfigureNode(ctx, 0, true, false, chainID, keys, bmConfig, dalc, t) + lightNode, _ := createNode(ctx, 1, false, true, keys, bmConfig, chainID, false, t) startNodeWithCleanup(t, fullNode) require.NoError(waitForFirstBlock(fullNode, Store)) diff --git a/node/node_test.go b/node/node_test.go index 786cbc1c1d..44df620fa0 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -114,16 +114,16 @@ func cleanUpNode(node Node, t *testing.T) { } // initAndStartNodeWithCleanup initializes and starts a node of the specified type. -func initAndStartNodeWithCleanup(ctx context.Context, t *testing.T, nodeType NodeType, chainId string) Node { - node, _ := setupTestNode(ctx, t, nodeType, chainId) +func initAndStartNodeWithCleanup(ctx context.Context, t *testing.T, nodeType NodeType, chainID string) Node { + node, _ := setupTestNode(ctx, t, nodeType, chainID) startNodeWithCleanup(t, node) return node } // setupTestNode sets up a test node based on the NodeType. -func setupTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainId string) (Node, cmcrypto.PrivKey) { - node, privKey, err := newTestNode(ctx, t, nodeType, chainId) +func setupTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainID string) (Node, cmcrypto.PrivKey) { + node, privKey, err := newTestNode(ctx, t, nodeType, chainID) require.NoError(t, err) require.NotNil(t, node) @@ -131,7 +131,7 @@ func setupTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainId } // newTestNode creates a new test node based on the NodeType. -func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainId string) (Node, cmcrypto.PrivKey, error) { +func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainID string) (Node, cmcrypto.PrivKey, error) { config := config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace} switch nodeType { case Light: @@ -142,7 +142,7 @@ func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainId s panic(fmt.Sprintf("invalid node type: %v", nodeType)) } app := setupMockApplication() - genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainId) + genesis, genesisValidatorKey := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainID) signingKey, err := types.PrivKeyToSigningKey(genesisValidatorKey) if err != nil { return nil, nil, err @@ -157,9 +157,9 @@ func newTestNode(ctx context.Context, t *testing.T, nodeType NodeType, chainId s func TestNewNode(t *testing.T) { ctx := context.Background() - chainId := "TestNewNode" - ln := initAndStartNodeWithCleanup(ctx, t, Light, chainId) + chainID := "TestNewNode" + ln := initAndStartNodeWithCleanup(ctx, t, Light, chainID) require.IsType(t, new(LightNode), ln) - fn := initAndStartNodeWithCleanup(ctx, t, Full, chainId) + fn := initAndStartNodeWithCleanup(ctx, t, Full, chainID) require.IsType(t, new(FullNode), fn) } diff --git a/rpc/json/helpers_test.go b/rpc/json/helpers_test.go index 631c720238..a383f38aa4 100644 --- a/rpc/json/helpers_test.go +++ b/rpc/json/helpers_test.go @@ -62,7 +62,7 @@ func prepareProposalResponse(_ context.Context, req *abci.RequestPrepareProposal } // copied from rpc -func getRPC(t *testing.T, chainId string) (*mocks.Application, rpcclient.Client) { +func getRPC(t *testing.T, chainID string) (*mocks.Application, rpcclient.Client) { t.Helper() require := require.New(t) app := &mocks.Application{} @@ -106,7 +106,7 @@ func getRPC(t *testing.T, chainId string) (*mocks.Application, rpcclient.Client) genesisValidators := []cmtypes.GenesisValidator{ {Address: pubKey.Address(), PubKey: pubKey, Power: int64(100), Name: "gen #1"}, } - n, err := node.NewNode(context.Background(), config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace, Aggregator: true, BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false, SequencerAddress: MockSequencerAddress}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: chainId, Validators: genesisValidators}, node.DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) + n, err := node.NewNode(context.Background(), config.NodeConfig{DAAddress: MockDAAddress, DANamespace: MockDANamespace, Aggregator: true, BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false, SequencerAddress: MockSequencerAddress}, key, signingKey, proxy.NewLocalClientCreator(app), &cmtypes.GenesisDoc{ChainID: chainID, Validators: genesisValidators}, node.DefaultMetricsProvider(cmconfig.DefaultInstrumentationConfig()), log.TestingLogger()) require.NoError(err) require.NotNil(n) diff --git a/state/executor_test.go b/state/executor_test.go index 2057dd720e..46f536fb00 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -131,7 +131,7 @@ func doTestApplyBlock(t *testing.T) { _, err := rand.Read(mockAppHash[:]) require.NoError(err) - chainId := "doTestApplyBlock" + chainID := "doTestApplyBlock" app := &mocks.Application{} app.On("CheckTx", mock.Anything, mock.Anything).Return(&abci.ResponseCheckTx{}, nil) @@ -167,7 +167,7 @@ func doTestApplyBlock(t *testing.T) { MockSequencerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), )) - mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainId), seqClient, logger) + mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainID), seqClient, logger) ctx := context.Background() require.NoError(mpoolReaper.StartReaper(ctx)) txQuery, err := query.New("tm.event='Tx'") @@ -202,7 +202,7 @@ func doTestApplyBlock(t *testing.T) { state.ConsensusParams.Block.MaxBytes = 100 state.ConsensusParams.Block.MaxGas = 100000 - executor := NewBlockExecutor(vKey.PubKey().Address().Bytes(), chainId, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) + executor := NewBlockExecutor(vKey.PubKey().Address().Bytes(), chainID, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) tx := []byte{1, 2, 3, 4} err = mpool.CheckTx(tx, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{}) @@ -299,7 +299,7 @@ func TestUpdateStateConsensusParams(t *testing.T) { require.NoError(t, err) require.NotNil(t, client) - chainId := "TestUpdateStateConsensusParams" + chainID := "TestUpdateStateConsensusParams" mpool := mempool.NewCListMempool(cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0) seqClient := seqGRPC.NewClient() @@ -307,11 +307,11 @@ func TestUpdateStateConsensusParams(t *testing.T) { MockSequencerAddress, grpc.WithTransportCredentials(insecure.NewCredentials()), )) - mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainId), seqClient, logger) + mpoolReaper := mempool.NewCListMempoolReaper(mpool, []byte(chainID), seqClient, logger) require.NoError(t, mpoolReaper.StartReaper(context.Background())) eventBus := cmtypes.NewEventBus() require.NoError(t, eventBus.Start()) - executor := NewBlockExecutor([]byte("test address"), chainId, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) + executor := NewBlockExecutor([]byte("test address"), chainID, mpool, mpoolReaper, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, 100, logger, NopMetrics()) state := types.State{ ConsensusParams: cmproto.ConsensusParams{ @@ -331,7 +331,7 @@ func TestUpdateStateConsensusParams(t *testing.T) { NextValidators: cmtypes.NewValidatorSet([]*cmtypes.Validator{{Address: []byte("test"), PubKey: nil, VotingPower: 100, ProposerPriority: 1}}), } - header, data := types.GetRandomBlock(1234, 2, chainId) + header, data := types.GetRandomBlock(1234, 2, chainID) txResults := make([]*abci.ExecTxResult, len(data.Txs)) for idx := range data.Txs { diff --git a/store/store_test.go b/store/store_test.go index 866c8d2ccd..865e920451 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -18,16 +18,16 @@ import ( func TestStoreHeight(t *testing.T) { t.Parallel() - chainId := "TestStoreHeight" - header1, data1 := types.GetRandomBlock(1, 0, chainId) - header2, data2 := types.GetRandomBlock(1, 0, chainId) - header3, data3 := types.GetRandomBlock(2, 0, chainId) - header4, data4 := types.GetRandomBlock(2, 0, chainId) - header5, data5 := types.GetRandomBlock(3, 0, chainId) - header6, data6 := types.GetRandomBlock(1, 0, chainId) - header7, data7 := types.GetRandomBlock(1, 0, chainId) - header8, data8 := types.GetRandomBlock(9, 0, chainId) - header9, data9 := types.GetRandomBlock(10, 0, chainId) + chainID := "TestStoreHeight" + header1, data1 := types.GetRandomBlock(1, 0, chainID) + header2, data2 := types.GetRandomBlock(1, 0, chainID) + header3, data3 := types.GetRandomBlock(2, 0, chainID) + header4, data4 := types.GetRandomBlock(2, 0, chainID) + header5, data5 := types.GetRandomBlock(3, 0, chainID) + header6, data6 := types.GetRandomBlock(1, 0, chainID) + header7, data7 := types.GetRandomBlock(1, 0, chainID) + header8, data8 := types.GetRandomBlock(9, 0, chainID) + header9, data9 := types.GetRandomBlock(10, 0, chainID) cases := []struct { name string headers []*types.SignedHeader @@ -62,10 +62,10 @@ func TestStoreHeight(t *testing.T) { func TestStoreLoad(t *testing.T) { t.Parallel() - chainId := "TestStoreLoad" - header1, data1 := types.GetRandomBlock(1, 10, chainId) - header2, data2 := types.GetRandomBlock(1, 10, chainId) - header3, data3 := types.GetRandomBlock(2, 20, chainId) + chainID := "TestStoreLoad" + header1, data1 := types.GetRandomBlock(1, 10, chainID) + header2, data2 := types.GetRandomBlock(1, 10, chainID) + header3, data3 := types.GetRandomBlock(2, 20, chainID) cases := []struct { name string headers []*types.SignedHeader diff --git a/types/signed_header_test.go b/types/signed_header_test.go index e553c4eac9..16b46aa02e 100644 --- a/types/signed_header_test.go +++ b/types/signed_header_test.go @@ -13,12 +13,12 @@ import ( ) func TestSignedHeader(t *testing.T) { - chainId := "TestSignedHeader" + chainID := "TestSignedHeader" // Generate a random signed header - trusted, privKey, err := GetRandomSignedHeader(chainId) + trusted, privKey, err := GetRandomSignedHeader(chainID) require.NoError(t, err) // Get the next random header - untrustedAdj, err := GetRandomNextSignedHeader(trusted, privKey, chainId) + untrustedAdj, err := GetRandomNextSignedHeader(trusted, privKey, chainID) require.NoError(t, err) t.Run("Test Verify", func(t *testing.T) { testVerify(t, trusted, untrustedAdj, privKey) diff --git a/types/utils.go b/types/utils.go index 19936175f2..d0cd0f59c1 100644 --- a/types/utils.go +++ b/types/utils.go @@ -66,19 +66,19 @@ type BlockConfig struct { // GetRandomBlock creates a block with a given height and number of transactions, intended for testing. // It's tailored for simplicity, primarily used in test setups where additional outputs are not needed. -func GetRandomBlock(height uint64, nTxs int, chainId string) (*SignedHeader, *Data) { +func GetRandomBlock(height uint64, nTxs int, chainID string) (*SignedHeader, *Data) { config := BlockConfig{ Height: height, NTxs: nTxs, } // Assuming GenerateBlock modifies the context directly with the generated block and other needed data. - header, data, _ := GenerateRandomBlockCustom(&config, chainId) + header, data, _ := GenerateRandomBlockCustom(&config, chainID) return header, data } // GenerateRandomBlockCustom returns a block with random data and the given height, transactions, privateKey and proposer address. -func GenerateRandomBlockCustom(config *BlockConfig, chainId string) (*SignedHeader, *Data, cmcrypto.PrivKey) { +func GenerateRandomBlockCustom(config *BlockConfig, chainID string) (*SignedHeader, *Data, cmcrypto.PrivKey) { data := getBlockDataWith(config.NTxs) dataHash := data.Hash() @@ -92,7 +92,7 @@ func GenerateRandomBlockCustom(config *BlockConfig, chainId string) (*SignedHead PrivKey: config.PrivKey, } - signedHeader, err := GetRandomSignedHeaderCustom(&headerConfig, chainId) + signedHeader, err := GetRandomSignedHeaderCustom(&headerConfig, chainID) if err != nil { panic(err) } @@ -102,7 +102,7 @@ func GenerateRandomBlockCustom(config *BlockConfig, chainId string) (*SignedHead } data.Metadata = &Metadata{ - ChainID: chainId, + ChainID: chainID, Height: signedHeader.Height(), LastDataHash: nil, Time: uint64(signedHeader.Time().UnixNano()), @@ -112,13 +112,13 @@ func GenerateRandomBlockCustom(config *BlockConfig, chainId string) (*SignedHead } // GetRandomNextBlock returns a block with random data and height of +1 from the provided block -func GetRandomNextBlock(header *SignedHeader, data *Data, privKey cmcrypto.PrivKey, appHash header.Hash, nTxs int, chainId string) (*SignedHeader, *Data) { +func GetRandomNextBlock(header *SignedHeader, data *Data, privKey cmcrypto.PrivKey, appHash header.Hash, nTxs int, chainID string) (*SignedHeader, *Data) { nextData := getBlockDataWith(nTxs) dataHash := nextData.Hash() valSet := header.Validators newSignedHeader := &SignedHeader{ - Header: GetRandomNextHeader(header.Header, chainId), + Header: GetRandomNextHeader(header.Header, chainID), Validators: valSet, } newSignedHeader.ProposerAddress = header.Header.ProposerAddress @@ -134,7 +134,7 @@ func GetRandomNextBlock(header *SignedHeader, data *Data, privKey cmcrypto.PrivK } newSignedHeader.Signature = *signature nextData.Metadata = &Metadata{ - ChainID: chainId, + ChainID: chainID, Height: newSignedHeader.Height(), LastDataHash: nil, Time: uint64(newSignedHeader.Time().UnixNano()), @@ -151,12 +151,12 @@ type HeaderConfig struct { } // GetRandomHeader returns a header with random fields and current time -func GetRandomHeader(chainId string) Header { +func GetRandomHeader(chainID string) Header { return Header{ BaseHeader: BaseHeader{ Height: uint64(rand.Int63()), //nolint:gosec Time: uint64(time.Now().UnixNano()), - ChainID: chainId, + ChainID: chainID, }, Version: Version{ Block: InitStateVersion.Consensus.Block, @@ -175,8 +175,8 @@ func GetRandomHeader(chainId string) Header { // GetRandomNextHeader returns a header with random data and height of +1 from // the provided Header -func GetRandomNextHeader(header Header, chainId string) Header { - nextHeader := GetRandomHeader(chainId) +func GetRandomNextHeader(header Header, chainID string) Header { + nextHeader := GetRandomHeader(chainID) nextHeader.BaseHeader.Height = header.Height() + 1 nextHeader.BaseHeader.Time = uint64(time.Now().Add(1 * time.Second).UnixNano()) nextHeader.LastHeaderHash = header.Hash() @@ -186,7 +186,7 @@ func GetRandomNextHeader(header Header, chainId string) Header { } // GetRandomSignedHeader generates a signed header with random data and returns it. -func GetRandomSignedHeader(chainId string) (*SignedHeader, cmcrypto.PrivKey, error) { +func GetRandomSignedHeader(chainID string) (*SignedHeader, cmcrypto.PrivKey, error) { config := HeaderConfig{ Height: uint64(rand.Int63()), //nolint:gosec DataHash: GetRandomBytes(32), @@ -194,7 +194,7 @@ func GetRandomSignedHeader(chainId string) (*SignedHeader, cmcrypto.PrivKey, err VotingPower: 1, } - signedHeader, err := GetRandomSignedHeaderCustom(&config, chainId) + signedHeader, err := GetRandomSignedHeaderCustom(&config, chainID) if err != nil { return nil, ed25519.PrivKey{}, err } @@ -202,14 +202,14 @@ func GetRandomSignedHeader(chainId string) (*SignedHeader, cmcrypto.PrivKey, err } // GetRandomSignedHeaderCustom creates a signed header based on the provided HeaderConfig. -func GetRandomSignedHeaderCustom(config *HeaderConfig, chainId string) (*SignedHeader, error) { +func GetRandomSignedHeaderCustom(config *HeaderConfig, chainID string) (*SignedHeader, error) { valsetConfig := ValidatorConfig{ PrivKey: config.PrivKey, VotingPower: 1, } valSet := GetValidatorSetCustom(valsetConfig) signedHeader := &SignedHeader{ - Header: GetRandomHeader(chainId), + Header: GetRandomHeader(chainID), Validators: valSet, } signedHeader.Header.BaseHeader.Height = config.Height @@ -228,10 +228,10 @@ func GetRandomSignedHeaderCustom(config *HeaderConfig, chainId string) (*SignedH // GetRandomNextSignedHeader returns a signed header with random data and height of +1 from // the provided signed header -func GetRandomNextSignedHeader(signedHeader *SignedHeader, privKey cmcrypto.PrivKey, chainId string) (*SignedHeader, error) { +func GetRandomNextSignedHeader(signedHeader *SignedHeader, privKey cmcrypto.PrivKey, chainID string) (*SignedHeader, error) { valSet := signedHeader.Validators newSignedHeader := &SignedHeader{ - Header: GetRandomNextHeader(signedHeader.Header, chainId), + Header: GetRandomNextHeader(signedHeader.Header, chainID), Validators: valSet, } newSignedHeader.LastCommitHash = signedHeader.Signature.GetCommitHash( @@ -269,12 +269,12 @@ func GetNodeKey(nodeKey *p2p.NodeKey) (crypto.PrivKey, error) { } // GetFirstSignedHeader creates a 1st signed header for a chain, given a valset and signing key. -func GetFirstSignedHeader(privkey ed25519.PrivKey, valSet *cmtypes.ValidatorSet, chainId string) (*SignedHeader, error) { +func GetFirstSignedHeader(privkey ed25519.PrivKey, valSet *cmtypes.ValidatorSet, chainID string) (*SignedHeader, error) { header := Header{ BaseHeader: BaseHeader{ Height: 1, Time: uint64(time.Now().UnixNano()), - ChainID: chainId, + ChainID: chainID, }, Version: Version{ Block: InitStateVersion.Consensus.Block, @@ -318,7 +318,7 @@ func GetValidatorSetFromGenesis(g *cmtypes.GenesisDoc) cmtypes.ValidatorSet { } // GetGenesisWithPrivkey returns a genesis doc with a single validator and a signing key -func GetGenesisWithPrivkey(signingKeyType string, chainId string) (*cmtypes.GenesisDoc, cmcrypto.PrivKey) { +func GetGenesisWithPrivkey(signingKeyType string, chainID string) (*cmtypes.GenesisDoc, cmcrypto.PrivKey) { var genesisValidatorKey cmcrypto.PrivKey switch signingKeyType { case "secp256k1": @@ -335,7 +335,7 @@ func GetGenesisWithPrivkey(signingKeyType string, chainId string) (*cmtypes.Gene Name: "sequencer", }} genDoc := &cmtypes.GenesisDoc{ - ChainID: chainId, + ChainID: chainID, InitialHeight: 0, Validators: genesisValidators, } From d034e4558fcb24b015fd2662faccdd8a14b06f09 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Fri, 11 Oct 2024 12:52:51 +0400 Subject: [PATCH 18/19] minor --- block/manager_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/block/manager_test.go b/block/manager_test.go index 14fcdef045..ff6977ea87 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -103,17 +103,18 @@ func TestInitialStateClean(t *testing.T) { } func TestInitialStateStored(t *testing.T) { + chainID := "TestInitialStateStored" require := require.New(t) - genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, "TestInitialStateStored") + genesisDoc, _ := types.GetGenesisWithPrivkey(types.DefaultSigningKeyType, chainID) valset := types.GetRandomValidatorSet() genesis := &cmtypes.GenesisDoc{ - ChainID: "TestInitialStateStored", + ChainID: chainID, InitialHeight: 1, Validators: genesisDoc.Validators, AppHash: []byte("app hash"), } sampleState := types.State{ - ChainID: "TestInitialStateStored", + ChainID: chainID, InitialHeight: 1, LastBlockHeight: 100, Validators: valset, From 10bdaac15164a7274a36481086a89e0854730f25 Mon Sep 17 00:00:00 2001 From: gupadhyaya Date: Thu, 17 Oct 2024 11:04:40 +0400 Subject: [PATCH 19/19] fix complicated checks in batch retrieve loop and add sanity check for the timestamp received from sequencer --- block/manager.go | 60 +++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/block/manager.go b/block/manager.go index c747cd8a2b..ae1e620aad 100644 --- a/block/manager.go +++ b/block/manager.go @@ -404,45 +404,53 @@ func (m *Manager) getRemainingSleep(start time.Time) time.Duration { // BatchRetrieveLoop is responsible for retrieving batches from the sequencer. func (m *Manager) BatchRetrieveLoop(ctx context.Context) { - // batchTimer is used to signal when to retrieve batch from the sequencer + // Initialize batchTimer to fire immediately on start batchTimer := time.NewTimer(0) defer batchTimer.Stop() + for { select { case <-ctx.Done(): return case <-batchTimer.C: - // Define the start time for the block production period start := time.Now() - res, err := m.seqClient.GetNextBatch(ctx, sequencing.GetNextBatchRequest{RollupId: []byte(m.genesis.ChainID), LastBatchHash: m.lastBatchHash}) - if err != nil && ctx.Err() == nil { + + // Skip batch retrieval if context is already done + if ctx.Err() != nil { + return + } + + res, err := m.seqClient.GetNextBatch(ctx, sequencing.GetNextBatchRequest{ + RollupId: []byte(m.genesis.ChainID), + LastBatchHash: m.lastBatchHash, + }) + + if err != nil { m.logger.Error("error while retrieving batch", "error", err) } - if res != nil { + + if res != nil && res.Batch != nil { batch := res.Batch batchTime := res.Timestamp - // Add the batch to the batch queue - if batch != nil { - // Calculate the hash of the batch and store it for the next batch retrieval - h, err := batch.Hash() - if err == nil { - // add batch to the queue even if its empty (no txs) - m.bq.AddBatch(BatchWithTime{batch, batchTime}) - // update lastBatchHash only if the batch has actual txs - if batch.Transactions != nil { - m.lastBatchHash = h - } - } else { - m.logger.Error("error while hashing batch", "error", err) + + // Calculate and store batch hash only if hashing succeeds + if h, err := batch.Hash(); err == nil { + m.bq.AddBatch(BatchWithTime{Batch: batch, Time: batchTime}) + + // Update lastBatchHash only if the batch contains transactions + if batch.Transactions != nil { + m.lastBatchHash = h } + } else { + m.logger.Error("error while hashing batch", "error", err) } } - // Reset the batchTimer to signal the next batch production - // period based on the batch retrieval time. - remainingSleep := time.Duration(0) + + // Determine remaining time for the next batch and reset timer elapsed := time.Since(start) - if elapsed < m.conf.BlockTime { - remainingSleep = m.conf.BlockTime - elapsed + remainingSleep := m.conf.BlockTime - elapsed + if remainingSleep < 0 { + remainingSleep = 0 } batchTimer.Reset(remainingSleep) } @@ -1024,6 +1032,7 @@ func (m *Manager) publishBlock(ctx context.Context) error { lastSignature *types.Signature lastHeaderHash types.Hash lastDataHash types.Hash + lastHeaderTime time.Time err error ) height := m.store.Height() @@ -1042,6 +1051,7 @@ func (m *Manager) publishBlock(ctx context.Context) error { } lastHeaderHash = lastHeader.Hash() lastDataHash = lastData.Hash() + lastHeaderTime = lastHeader.Time() } var ( @@ -1068,6 +1078,10 @@ func (m *Manager) publishBlock(ctx context.Context) error { if err != nil { return fmt.Errorf("failed to get transactions from batch: %w", err) } + // sanity check timestamp for monotonically increasing + if timestamp.Before(lastHeaderTime) { + return fmt.Errorf("timestamp is not monotonically increasing: %s < %s", timestamp, m.getLastBlockTime()) + } header, data, err = m.createBlock(newHeight, lastSignature, lastHeaderHash, extendedCommit, txs, *timestamp) if err != nil { return err