Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions block/block_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment thread
MSevey marked this conversation as resolved.
Resolved
Expand Down
46 changes: 46 additions & 0 deletions block/block_cache_test.go
Original file line number Diff line number Diff line change
@@ -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")
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved
}
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved
16 changes: 16 additions & 0 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -459,18 +464,29 @@ 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)
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved
for {
select {
case <-ctx.Done():
return
case <-m.retrieveCh:
case <-blockFoundCh:
}
daHeight := atomic.LoadUint64(&m.daHeight)
err := m.processNextDABlock(ctx)
if err != nil {
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:
}
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved
atomic.AddUint64(&m.daHeight, 1)
}
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved
}
Expand Down
18 changes: 18 additions & 0 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,21 @@ func getMockDALC(logger log.Logger) da.DataAvailabilityLayerClient {
_ = dalc.Start()
return dalc
}
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved

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))
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
1 change: 0 additions & 1 deletion node/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions node/full_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,15 +736,16 @@ 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],
}
signingKey, err := GetNodeKey(nodeKey)
require.NoError(err)
nodes[i], err = newFullNode(
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved
context.Background(),
ctx,
config.NodeConfig{
DALayer: "mock",
Aggregator: true,
Expand Down Expand Up @@ -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
Expand Down
91 changes: 91 additions & 0 deletions node/full_node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Comment thread
Manav-Aggarwal marked this conversation as resolved.
Resolved

// 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)
Expand Down
9 changes: 9 additions & 0 deletions node/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions state/indexer/block/kv/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cometbft/cometbft/libs/pubsub/query"
"github.com/cometbft/cometbft/types"

"github.com/rollkit/rollkit/store"
)

Expand Down