diff --git a/block/block_cache.go b/block/block_cache.go index dcb9a99906..661e4a78b8 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.RLock() + defer bc.mtx.RUnlock() + return bc.hardConfirmations[hash] +} + func (bc *BlockCache) setHardConfirmed(hash string) { bc.mtx.Lock() defer bc.mtx.Unlock() diff --git a/block/block_cache_test.go b/block/block_cache_test.go new file mode 100644 index 0000000000..2fecc46e44 --- /dev/null +++ b/block/block_cache_test.go @@ -0,0 +1,46 @@ +package block + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/rollkit/rollkit/types" +) + +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, 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 := types.GetRandomBlock(height, nTxs) + bc.setBlock(height, block1) + gotBlock1, ok1 := bc.getBlock(height) + require.True(ok1, "getBlock should 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.isHardConfirmed("hash"), "hardConfirmations should be false for unseen hash") + bc.setHardConfirmed("hash") + require.True(bc.isHardConfirmed("hash"), "hardConfirmations should be true for seen hash") +} diff --git a/block/manager.go b/block/manager.go index cc80fa345f..e779354910 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) @@ -459,11 +464,17 @@ 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 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(): return case <-m.retrieveCh: + case <-blockFoundCh: } daHeight := atomic.LoadUint64(&m.daHeight) err := m.processNextDABlock(ctx) @@ -471,6 +482,11 @@ 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 + select { + case blockFoundCh <- struct{}{}: + default: + } atomic.AddUint64(&m.daHeight, 1) } } 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)) +} 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= 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_client_test.go b/node/full_client_test.go index 95c33334af..52e6e7bef6 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) + t.Log("Finished creating validators, waiting on wg") wg.Wait() // test first blocks diff --git a/node/full_node_integration_test.go b/node/full_node_integration_test.go index 0a2f3c5365..204c5ca371 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 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) + 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) + // 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: + // 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) 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" )