From 65847810374a4f7e1962093b437626f25dbaf74b Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Tue, 31 Oct 2023 13:31:22 +0100 Subject: [PATCH 01/15] feat: create blockFoundCh in RetrieveLoop to enable syncing faster than the DA block time --- block/manager.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/block/manager.go b/block/manager.go index cc80fa345f..62679afc04 100644 --- a/block/manager.go +++ b/block/manager.go @@ -459,11 +459,16 @@ func (m *Manager) getBlocksFromBlockStore(ctx context.Context, startHeight, endH // RetrieveLoop is responsible for interacting with DA layer. func (m *Manager) RetrieveLoop(ctx context.Context) { + // blockFoundCh is used to track when we successfully found a block so + // that we can continue to try and find blocks are the next DA height. + // This enables syncing faster than the DA block time. + blockFoundCh := make(chan struct{}, 1) for { select { case <-ctx.Done(): return case <-m.retrieveCh: + case <-blockFoundCh: } daHeight := atomic.LoadUint64(&m.daHeight) err := m.processNextDABlock(ctx) @@ -471,6 +476,8 @@ func (m *Manager) RetrieveLoop(ctx context.Context) { m.logger.Error("failed to retrieve block from DALC", "daHeight", daHeight, "errors", err.Error()) continue } + // Signal the blockFoundCh to try and retrieve the next block + blockFoundCh <- struct{}{} atomic.AddUint64(&m.daHeight, 1) } } From 139ce2e3dea28915201f6277bb27e62e9cf83031 Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Tue, 31 Oct 2023 14:24:25 +0100 Subject: [PATCH 02/15] fix: fix deadlock in blockFoundCh --- block/manager.go | 5 ++++- node/full_client_test.go | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/block/manager.go b/block/manager.go index 62679afc04..5357587742 100644 --- a/block/manager.go +++ b/block/manager.go @@ -477,7 +477,10 @@ func (m *Manager) RetrieveLoop(ctx context.Context) { continue } // Signal the blockFoundCh to try and retrieve the next block - blockFoundCh <- struct{}{} + select { + case blockFoundCh <- struct{}{}: + default: + } atomic.AddUint64(&m.daHeight, 1) } } diff --git a/node/full_client_test.go b/node/full_client_test.go index 95c33334af..47d33b030e 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -736,7 +736,8 @@ func createGenesisValidators(t *testing.T, numNodes int, appCreator func(require t.Cleanup(func() { require.NoError(dalc.Stop()) }) - + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(func() { cancel() }) for i := 0; i < len(nodes); i++ { nodeKey := &p2p.NodeKey{ PrivKey: vKeys[i], @@ -744,7 +745,7 @@ func createGenesisValidators(t *testing.T, numNodes int, appCreator func(require signingKey, err := GetNodeKey(nodeKey) require.NoError(err) nodes[i], err = newFullNode( - context.Background(), + ctx, config.NodeConfig{ DALayer: "mock", Aggregator: true, @@ -839,6 +840,7 @@ func TestValidatorSetHandling(t *testing.T) { numNodes := 2 rpc := createGenesisValidators(t, numNodes, createApp, &wg) + fmt.Println("Finished creating validators, waiting on wg") wg.Wait() // test first blocks From 003bb0beeaeccefc718989ed37cd049eb12c3160 Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Tue, 31 Oct 2023 19:54:06 +0100 Subject: [PATCH 03/15] ci: add test for fast DA layer syncing --- block/block_cache.go | 6 ++ block/manager.go | 5 ++ node/full.go | 1 - node/full_node_integration_test.go | 91 ++++++++++++++++++++++++++++++ node/test_helpers.go | 9 +++ 5 files changed, 111 insertions(+), 1 deletion(-) diff --git a/block/block_cache.go b/block/block_cache.go index dcb9a99906..2e8c6d8f38 100644 --- a/block/block_cache.go +++ b/block/block_cache.go @@ -53,6 +53,12 @@ func (bc *BlockCache) setSeen(hash string) { bc.hashes[hash] = true } +func (bc *BlockCache) isHardConfirmed(hash string) bool { + bc.mtx.Lock() + defer bc.mtx.Unlock() + return bc.hardConfirmations[hash] +} + func (bc *BlockCache) setHardConfirmed(hash string) { bc.mtx.Lock() defer bc.mtx.Unlock() diff --git a/block/manager.go b/block/manager.go index 5357587742..c6eeba8671 100644 --- a/block/manager.go +++ b/block/manager.go @@ -207,6 +207,11 @@ func (m *Manager) GetStoreHeight() uint64 { return m.store.Height() } +// GetHardConfirmation returns true if the block is hard confirmed +func (m *Manager) GetHardConfirmation(hash types.Hash) bool { + return m.blockCache.isHardConfirmed(hash.String()) +} + // AggregationLoop is responsible for aggregating transactions into rollup-blocks. func (m *Manager) AggregationLoop(ctx context.Context, lazy bool) { initialHeight := uint64(m.genesis.InitialHeight) diff --git a/node/full.go b/node/full.go index 0ca78e51cf..d72ae217ea 100644 --- a/node/full.go +++ b/node/full.go @@ -304,7 +304,6 @@ func (n *FullNode) blockPublishLoop(ctx context.Context) { // OnStart is a part of Service interface. func (n *FullNode) OnStart() error { - n.Logger.Info("starting P2P client") err := n.p2pClient.Start(n.ctx) if err != nil { diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index 0a2f3c5365..3288a72ba9 100644 --- a/node/full_node_integration_test.go +++ b/node/full_node_integration_test.go @@ -189,6 +189,97 @@ func TestLazyAggregator(t *testing.T) { require.NoError(waitForAtLeastNBlocks(node, 4, Header)) } +// TestFastDASync verifies that nodes can quick DA blocks faster than the DA +// block time +func TestFastDASync(t *testing.T) { + // Test setup, create require and contexts for aggregator and client nodes + require := require.New(t) + aggCtx, aggCancel := context.WithCancel(context.Background()) + defer aggCancel() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + // Set test params + clientNodes := 2 + bmConfig := getBMConfig() + // Set the DABlockTime to a large value to avoid test failures due to + // slow CI machines + bmConfig.DABlockTime = 1 * time.Second + // Set BlockTime to 2x DABlockTime to ensure that the aggregator node is + // producing DA blocks faster than rollup blocks. This is to force the + // block syncing to align with hard confirmations. + bmConfig.BlockTime = 2 * bmConfig.DABlockTime + const numberOfBlocksToSyncTill = 5 + + // Create the 2 nodes + nodes, _ := createNodes(aggCtx, ctx, clientNodes, bmConfig, t) + node1 := nodes[0] + node2 := nodes[1] + + // Start node 1 + require.NoError(node1.Start()) + defer func() { + require.NoError(node1.Stop()) + }() + + // Wait for node 1 to sync the first numberOfBLocksToSyncTill + require.NoError(waitForAtLeastNBlocks(node1, numberOfBlocksToSyncTill, Store)) + + // Now that node 1 has already synced, start the second node + require.NoError(node2.Start()) + defer func() { + require.NoError(node2.Stop()) + }() + + // Start and launch the timer in a go routine to ensure that the test + // fails if the nodes do not sync before the timer expires + ch := make(chan struct{}) + defer safeClose(ch) + // Set the timer to expire 1 block before the numberOfBlocksToSyncTill + timer := time.NewTimer((numberOfBlocksToSyncTill - 1) * bmConfig.DABlockTime) + go func() { + select { + case <-ch: + // Channel closed before timer expired. + return + case <-timer.C: + // Timer expired before channel closed. + safeClose(ch) + require.FailNow("nodes did not sync before DA Block time") + return + } + }() + + // Check that the nodes are synced in a loop. We don't use the helper + // function here so that we can catch if the channel is closed to exit + // the test quickly. + require.NoError(testutils.Retry(300, 100*time.Millisecond, func() error { + select { + case <-ch: + require.FailNow("channel closed") + default: + } + nHeight, err := getNodeHeight(node2, Store) + if err != nil { + return err + } + if nHeight >= uint64(numberOfBlocksToSyncTill) { + return nil + } + return fmt.Errorf("expected height > %v, got %v", numberOfBlocksToSyncTill, nHeight) + })) + + // Verify the nodes are synced + require.NoError(verifyNodesSynced(node1, node2, Store)) + + // Verify that the block we synced to is hard confirmed. This is to + // ensure that the test is passing due to the DA syncing, since the P2P + // block sync will sync quickly but the block won't be hard confirmed. + block, err := node2.Store.LoadBlock(numberOfBlocksToSyncTill) + require.NoError(err) + require.True(node2.blockManager.GetHardConfirmation(block.Hash())) +} + // TestSingleAggregatorTwoFullNodesBlockSyncSpeed tests the scenario where the chain's block time is much faster than the DA's block time. In this case, the full nodes should be able to use block sync to sync blocks much faster than syncing from the DA layer, and the test should conclude within block time func TestSingleAggregatorTwoFullNodesBlockSyncSpeed(t *testing.T) { require := require.New(t) diff --git a/node/test_helpers.go b/node/test_helpers.go index da564ee2ab..b1142125db 100644 --- a/node/test_helpers.go +++ b/node/test_helpers.go @@ -87,6 +87,15 @@ func getNodeHeightFromStore(node Node) (uint64, error) { return 0, errors.New("not a full node") } +// safeClose closes the channel if it's not closed already +func safeClose(ch chan struct{}) { + select { + case <-ch: + default: + close(ch) + } +} + func verifyNodesSynced(node1, node2 Node, source Source) error { return testutils.Retry(300, 100*time.Millisecond, func() error { n1Height, err := getNodeHeight(node1, source) From e59dec90ca2bd619c7eca31fc249002efb110abf Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Tue, 31 Oct 2023 15:00:32 -0400 Subject: [PATCH 04/15] Update node/full_node_integration_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- node/full_node_integration_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index 3288a72ba9..67ad6ce870 100644 --- a/node/full_node_integration_test.go +++ b/node/full_node_integration_test.go @@ -189,8 +189,7 @@ func TestLazyAggregator(t *testing.T) { require.NoError(waitForAtLeastNBlocks(node, 4, Header)) } -// TestFastDASync verifies that nodes can quick DA blocks faster than the DA -// block time +// TestFastDASync verifies that nodes can sync DA blocks faster than the DA block time func TestFastDASync(t *testing.T) { // Test setup, create require and contexts for aggregator and client nodes require := require.New(t) From 9980ddf0fe256d46e01695e9d48e301d43cf9cc5 Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Tue, 31 Oct 2023 15:01:24 -0400 Subject: [PATCH 05/15] Update block/block_cache.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- block/block_cache.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/block/block_cache.go b/block/block_cache.go index 2e8c6d8f38..661e4a78b8 100644 --- a/block/block_cache.go +++ b/block/block_cache.go @@ -54,8 +54,8 @@ func (bc *BlockCache) setSeen(hash string) { } func (bc *BlockCache) isHardConfirmed(hash string) bool { - bc.mtx.Lock() - defer bc.mtx.Unlock() + bc.mtx.RLock() + defer bc.mtx.RUnlock() return bc.hardConfirmations[hash] } From 8caba75ecde0f6f4ec1dcb4cfcc87efedc05cc24 Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Wed, 1 Nov 2023 10:24:09 +0100 Subject: [PATCH 06/15] ci: add unit tests for block cache --- block/block_cache_test.go | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 block/block_cache_test.go diff --git a/block/block_cache_test.go b/block/block_cache_test.go new file mode 100644 index 0000000000..70a0cc41f6 --- /dev/null +++ b/block/block_cache_test.go @@ -0,0 +1,71 @@ +package block + +import ( + "testing" + + "github.com/rollkit/rollkit/types" + "github.com/stretchr/testify/require" +) + +// TODO: refactor this helper as it is defined in multiple places +func getRandomBlock(height uint64, nTxs int) *types.Block { + block := &types.Block{ + SignedHeader: types.SignedHeader{ + Header: types.Header{ + BaseHeader: types.BaseHeader{ + Height: height, + }, + AggregatorsHash: make([]byte, 32), + }}, + Data: types.Data{ + Txs: make(types.Txs, nTxs), + IntermediateStateRoots: types.IntermediateStateRoots{ + RawRootsList: make([][]byte, nTxs), + }, + }, + } + + for i := 0; i < nTxs; i++ { + block.Data.Txs[i] = types.GetRandomTx() + block.Data.IntermediateStateRoots.RawRootsList[i] = types.GetRandomBytes(32) + } + + return block +} + +func TestBlockCache(t *testing.T) { + require := require.New(t) + // Create new BlockCache and verify not nil + bc := NewBlockCache() + require.NotNil(bc) + + // Test setBlock and getBlock + height := uint64(1) + block := getRandomBlock(height, 2) + bc.setBlock(height, block) + gotBlock, ok := bc.getBlock(height) + require.True(ok, "getBlock should return true after setBlock") + require.Equal(block, gotBlock) + + // Test overwriting a block + block1 := getRandomBlock(height, 2) + bc.setBlock(height, block1) + gotBlock1, ok1 := bc.getBlock(height) + require.True(ok1, "getBlock hsould return true after overwriting a block") + require.Equal(block1, gotBlock1) + + // Test deleteBlock + bc.deleteBlock(height) + _, ok = bc.getBlock(height) + require.False(ok, "getBlock should return false after deleteBlock") + + // Test isSeen and setSeen + require.False(bc.isSeen("hash"), "isSeen should return false for unseen hash") + bc.setSeen("hash") + require.True(bc.isSeen("hash"), "isSeen should return true for seen hash") + + // Test setHardConfirmed + require.False(bc.hardConfirmations["hash"], "hardConfirmations should be false for unseen hash") + bc.setHardConfirmed("hash") + require.True(bc.hardConfirmations["hash"], "hardConfirmations should be true for seen hash") +} From 81cae13b772a5149eb5c6555bf3415b8fbea0c5f Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Wed, 1 Nov 2023 10:33:27 +0100 Subject: [PATCH 07/15] ci: add unit test for manager hard confirmed --- block/manager_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/block/manager_test.go b/block/manager_test.go index 866855f540..4974888324 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -102,3 +102,21 @@ func getMockDALC(logger log.Logger) da.DataAvailabilityLayerClient { _ = dalc.Start() return dalc } + +func TestGetHardConfirmation(t *testing.T) { + require := require.New(t) + + // Create a minimalistic block manager + m := &Manager{ + blockCache: NewBlockCache(), + } + hash := types.Hash([]byte("hash")) + + // GetHardConfirmation should return false for unseen hash + require.False(m.GetHardConfirmation(hash)) + + // Set the hash as hard confirmed and verify GetHardConfirmation returns + // true + m.blockCache.setHardConfirmed(hash.String()) + require.True(m.GetHardConfirmation(hash)) +} From 74bd7fb674350e46d3e61c17e7644be58644c4a7 Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Wed, 1 Nov 2023 12:58:45 +0100 Subject: [PATCH 08/15] ci: fix lint errors --- block/block_cache_test.go | 3 ++- go.mod | 6 +++--- go.sum | 12 ++++++------ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/block/block_cache_test.go b/block/block_cache_test.go index 70a0cc41f6..3d190c0524 100644 --- a/block/block_cache_test.go +++ b/block/block_cache_test.go @@ -3,8 +3,9 @@ package block import ( "testing" - "github.com/rollkit/rollkit/types" "github.com/stretchr/testify/require" + + "github.com/rollkit/rollkit/types" ) // TODO: refactor this helper as it is defined in multiple places diff --git a/go.mod b/go.mod index 3e01c4e78a..389840498b 100644 --- a/go.mod +++ b/go.mod @@ -174,11 +174,11 @@ require ( go.uber.org/zap v1.25.0 // indirect golang.org/x/crypto v0.14.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/mod v0.12.0 // indirect - golang.org/x/sync v0.3.0 // indirect + golang.org/x/mod v0.13.0 // indirect + golang.org/x/sync v0.4.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect - golang.org/x/tools v0.13.0 // indirect + golang.org/x/tools v0.14.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect gonum.org/v1/gonum v0.11.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect diff --git a/go.sum b/go.sum index dc4bbf56ae..25a3bcfaa9 100644 --- a/go.sum +++ b/go.sum @@ -1776,8 +1776,8 @@ golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1894,8 +1894,8 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -2162,8 +2162,8 @@ golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= From 8b47a960752e61fa5ef39feccbc78c9330cc26c1 Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Fri, 3 Nov 2023 14:48:04 +0100 Subject: [PATCH 09/15] Update block/manager.go Co-authored-by: Javed Khan --- block/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/manager.go b/block/manager.go index c6eeba8671..3ef901cb09 100644 --- a/block/manager.go +++ b/block/manager.go @@ -465,7 +465,7 @@ func (m *Manager) getBlocksFromBlockStore(ctx context.Context, startHeight, endH // RetrieveLoop is responsible for interacting with DA layer. func (m *Manager) RetrieveLoop(ctx context.Context) { // blockFoundCh is used to track when we successfully found a block so - // that we can continue to try and find blocks are the next DA height. + // that we can continue to try and find blocks that are in the next DA height. // This enables syncing faster than the DA block time. blockFoundCh := make(chan struct{}, 1) for { From 2a55231c9a935a5eba934c49bd993a84c3dae1be Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Fri, 3 Nov 2023 12:18:03 -0400 Subject: [PATCH 10/15] Apply suggestions from code review Co-authored-by: Javed Khan --- block/block_cache_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/block/block_cache_test.go b/block/block_cache_test.go index 3d190c0524..c2f149bd76 100644 --- a/block/block_cache_test.go +++ b/block/block_cache_test.go @@ -66,7 +66,7 @@ func TestBlockCache(t *testing.T) { require.True(bc.isSeen("hash"), "isSeen should return true for seen hash") // Test setHardConfirmed - require.False(bc.hardConfirmations["hash"], "hardConfirmations should be false for unseen hash") + require.False(bc.isHardConfirmed("hash"), "hardConfirmations should be false for unseen hash") bc.setHardConfirmed("hash") - require.True(bc.hardConfirmations["hash"], "hardConfirmations should be true for seen hash") + require.True(bc.isHardConfirmed("hash"), "hardConfirmations should be true for seen hash") } From f68ed4a08f97d9d9136e21de0675e216ddb7de41 Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Mon, 6 Nov 2023 20:36:37 +0100 Subject: [PATCH 11/15] Reuse GetRandomBlock from types package --- block/block_cache_test.go | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/block/block_cache_test.go b/block/block_cache_test.go index c2f149bd76..704fa84032 100644 --- a/block/block_cache_test.go +++ b/block/block_cache_test.go @@ -3,37 +3,10 @@ package block import ( "testing" - "github.com/stretchr/testify/require" - "github.com/rollkit/rollkit/types" + "github.com/stretchr/testify/require" ) -// TODO: refactor this helper as it is defined in multiple places -func getRandomBlock(height uint64, nTxs int) *types.Block { - block := &types.Block{ - SignedHeader: types.SignedHeader{ - Header: types.Header{ - BaseHeader: types.BaseHeader{ - Height: height, - }, - AggregatorsHash: make([]byte, 32), - }}, - Data: types.Data{ - Txs: make(types.Txs, nTxs), - IntermediateStateRoots: types.IntermediateStateRoots{ - RawRootsList: make([][]byte, nTxs), - }, - }, - } - - for i := 0; i < nTxs; i++ { - block.Data.Txs[i] = types.GetRandomTx() - block.Data.IntermediateStateRoots.RawRootsList[i] = types.GetRandomBytes(32) - } - - return block -} - func TestBlockCache(t *testing.T) { require := require.New(t) // Create new BlockCache and verify not nil @@ -41,15 +14,15 @@ func TestBlockCache(t *testing.T) { require.NotNil(bc) // Test setBlock and getBlock - height := uint64(1) - block := getRandomBlock(height, 2) + height, nTxs := uint64(1), 2 + block := types.GetRandomBlock(height, nTxs) bc.setBlock(height, block) gotBlock, ok := bc.getBlock(height) require.True(ok, "getBlock should return true after setBlock") require.Equal(block, gotBlock) // Test overwriting a block - block1 := getRandomBlock(height, 2) + block1 := types.GetRandomBlock(height, nTxs) bc.setBlock(height, block1) gotBlock1, ok1 := bc.getBlock(height) require.True(ok1, "getBlock hsould return true after overwriting a block") From 38850e1ce3b5abe52490412309bf6e0b0ecc81cc Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Mon, 6 Nov 2023 20:37:08 +0100 Subject: [PATCH 12/15] Fix typo Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- block/block_cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/block_cache_test.go b/block/block_cache_test.go index 704fa84032..72ef9499ba 100644 --- a/block/block_cache_test.go +++ b/block/block_cache_test.go @@ -25,7 +25,7 @@ func TestBlockCache(t *testing.T) { block1 := types.GetRandomBlock(height, nTxs) bc.setBlock(height, block1) gotBlock1, ok1 := bc.getBlock(height) - require.True(ok1, "getBlock hsould return true after overwriting a block") + require.True(ok1, "getBlock should return true after overwriting a block") require.Equal(block1, gotBlock1) // Test deleteBlock From b59fe7bf19542f7dfaf9b4b4981db716ce9908f4 Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Mon, 6 Nov 2023 20:48:03 +0100 Subject: [PATCH 13/15] Address comments and apply goimports --- block/block_cache_test.go | 3 ++- block/manager.go | 1 + node/full_client_test.go | 2 +- state/indexer/block/kv/util.go | 1 + 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/block/block_cache_test.go b/block/block_cache_test.go index 72ef9499ba..2fecc46e44 100644 --- a/block/block_cache_test.go +++ b/block/block_cache_test.go @@ -3,8 +3,9 @@ package block import ( "testing" - "github.com/rollkit/rollkit/types" "github.com/stretchr/testify/require" + + "github.com/rollkit/rollkit/types" ) func TestBlockCache(t *testing.T) { diff --git a/block/manager.go b/block/manager.go index 3ef901cb09..e779354910 100644 --- a/block/manager.go +++ b/block/manager.go @@ -468,6 +468,7 @@ func (m *Manager) RetrieveLoop(ctx context.Context) { // that we can continue to try and find blocks that are in the next DA height. // This enables syncing faster than the DA block time. blockFoundCh := make(chan struct{}, 1) + defer close(blockFoundCh) for { select { case <-ctx.Done(): diff --git a/node/full_client_test.go b/node/full_client_test.go index 47d33b030e..52e6e7bef6 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -840,7 +840,7 @@ func TestValidatorSetHandling(t *testing.T) { numNodes := 2 rpc := createGenesisValidators(t, numNodes, createApp, &wg) - fmt.Println("Finished creating validators, waiting on wg") + t.Log("Finished creating validators, waiting on wg") wg.Wait() // test first blocks diff --git a/state/indexer/block/kv/util.go b/state/indexer/block/kv/util.go index e69dd38f1f..63065d6ef2 100644 --- a/state/indexer/block/kv/util.go +++ b/state/indexer/block/kv/util.go @@ -9,6 +9,7 @@ import ( "github.com/cometbft/cometbft/libs/pubsub/query" "github.com/cometbft/cometbft/types" + "github.com/rollkit/rollkit/store" ) From e5ba7c22ef48a68a5f93758ad99f74d7f8a28e05 Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Thu, 9 Nov 2023 23:25:08 +0300 Subject: [PATCH 14/15] Modify timer in test to be lower --- node/full_node_integration_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index 67ad6ce870..0a008667bf 100644 --- a/node/full_node_integration_test.go +++ b/node/full_node_integration_test.go @@ -234,8 +234,9 @@ func TestFastDASync(t *testing.T) { // fails if the nodes do not sync before the timer expires ch := make(chan struct{}) defer safeClose(ch) - // Set the timer to expire 1 block before the numberOfBlocksToSyncTill - timer := time.NewTimer((numberOfBlocksToSyncTill - 1) * bmConfig.DABlockTime) + // After the first DA block time passes, the node should signal RetrieveLoop once, and it + // should catch up to the latest block height pretty soon after. + timer := time.NewTimer(1*bmConfig.DABlockTime + 250*time.Millisecond) go func() { select { case <-ch: From 9756030b69be59bd2ae7873c9e73fabcdc2d23a7 Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Thu, 9 Nov 2023 23:25:42 +0300 Subject: [PATCH 15/15] Update node/full_node_integration_test.go Co-authored-by: Javed Khan --- node/full_node_integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index 0a008667bf..204c5ca371 100644 --- a/node/full_node_integration_test.go +++ b/node/full_node_integration_test.go @@ -221,7 +221,7 @@ func TestFastDASync(t *testing.T) { require.NoError(node1.Stop()) }() - // Wait for node 1 to sync the first numberOfBLocksToSyncTill + // Wait for node 1 to sync the first numberOfBlocksToSyncTill require.NoError(waitForAtLeastNBlocks(node1, numberOfBlocksToSyncTill, Store)) // Now that node 1 has already synced, start the second node