Skip to content

Commit

Permalink
tests fix
Browse files Browse the repository at this point in the history
  • Loading branch information
srene committed Oct 22, 2024
1 parent 060a11f commit 5552b77
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 53 deletions.
33 changes: 12 additions & 21 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
func TestInitialState(t *testing.T) {
var err error
assert := assert.New(t)
genesis := testutil.GenerateGenesis("test", 123)
genesis := testutil.GenerateGenesis(123)
key, _, _ := crypto.GenerateEd25519Key(rand.Reader)
raw, _ := key.GetPublic().Raw()
pubkey := ed25519.PubKey(raw)
Expand Down Expand Up @@ -179,7 +179,6 @@ func TestProduceOnlyAfterSynced(t *testing.T) {
lastBlockHeaderHash = batch.Blocks[len(batch.Blocks)-1].Header.Hash()
// Wait until daHeight is updated
time.Sleep(time.Millisecond * 500)
batchs = append(batchs, batch)
}

// Initially sync target is 0
Expand All @@ -198,9 +197,9 @@ func TestProduceOnlyAfterSynced(t *testing.T) {
require.NoError(t, err)
}()
<-ctx.Done()
assert.Equal(t, batchs[1].EndHeight(), manager.LastSubmittedHeight.Load())
assert.Equal(t, batch.EndHeight(), manager.LastSubmittedHeight.Load())
// validate that we produced blocks
assert.Greater(t, manager.State.Height(), batchs[1].EndHeight())
assert.Greater(t, manager.State.Height(), batch.EndHeight())
}

// TestApplyCachedBlocks checks the flow that happens when we are receiving blocks from p2p and some of the blocks
Expand All @@ -225,8 +224,7 @@ func TestApplyCachedBlocks_WithFraudCheck(t *testing.T) {
proxyApp := proxy.NewAppConns(clientCreator)
err := proxyApp.Start()
require.NoError(t, err)
chainId := "test"
manager, err := testutil.GetManager(chainId, testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
require.NoError(t, err)
require.NotNil(t, manager)

Expand Down Expand Up @@ -325,7 +323,7 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {
numBatchesToAdd := 2
nextBatchStartHeight := manager.NextHeightToSubmit()

var batchs []*types.Batch
var batch *types.Batch
for i := 0; i < numBatchesToAdd; i++ {
batch, err = testutil.GenerateBatch(
nextBatchStartHeight,
Expand All @@ -347,15 +345,14 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {

nextBatchStartHeight = batch.EndHeight() + 1

batchs = append(batchs, batch)
time.Sleep(time.Millisecond * 500)
}

mockExecutor := &blockmocks.MockExecutorI{}
manager.Executor = mockExecutor
mockExecutor.On("InitChain", mock.Anything, mock.Anything).Return(&abci.ResponseInitChain{}, nil)
mockExecutor.On("GetAppInfo").Return(&abci.ResponseInfo{
LastBlockHeight: int64(batchs[1].EndHeight()),
LastBlockHeight: int64(batch.EndHeight()),
}, nil)
mockExecutor.On("UpdateStateAfterInitChain", mock.Anything, mock.Anything).Return(nil)
mockExecutor.On("UpdateMempoolAfterInitChain", mock.Anything).Return(nil)
Expand Down Expand Up @@ -391,8 +388,7 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {
}

func TestRetrieveDaBatchesFailed(t *testing.T) {
chainId := "test"
manager, err := testutil.GetManager(chainId, testutil.GetManagerConfig(), nil, 1, 1, 0, nil, nil)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, nil, nil)
require.NoError(t, err)
require.NotNil(t, manager)
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
Expand Down Expand Up @@ -431,8 +427,7 @@ func TestProduceNewBlock(t *testing.T) {
err := proxyApp.Start()
require.NoError(t, err)
// Init manager
chainId := "test"
manager, err := testutil.GetManager(chainId, testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
require.NoError(t, err)
// Produce block
_, _, err = manager.ProduceApplyGossipBlock(context.Background(), true)
Expand Down Expand Up @@ -503,8 +498,7 @@ func TestProduceBlockFailAfterCommit(t *testing.T) {
// Create a new mock store which should succeed to save the first block
mockStore := testutil.NewMockStore()
// Init manager
chainId := "test"
manager, err := testutil.GetManager(chainId, testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, mockStore)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, mockStore)
require.NoError(err)

cases := []struct {
Expand Down Expand Up @@ -613,8 +607,7 @@ func TestCreateNextDABatchWithBytesLimit(t *testing.T) {
// Init manager
managerConfig := testutil.GetManagerConfig()
managerConfig.BatchSubmitBytes = batchLimitBytes // enough for 2 block, not enough for 10 blocks
chainId := "test"
manager, err := testutil.GetManager(chainId, managerConfig, nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(managerConfig, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -696,8 +689,7 @@ func TestDAFetch(t *testing.T) {
// Create a new mock store which should succeed to save the first block
mockStore := testutil.NewMockStore()
// Init manager
chainId := "test"
manager, err := testutil.GetManager(chainId, testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, mockStore)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, mockStore)
require.NoError(err)
commitHash := [32]byte{}

Expand Down Expand Up @@ -782,8 +774,7 @@ func TestManager_ProcessNextDABatch_FraudHandling(t *testing.T) {
// Create a new mock store which should succeed to save the first block
mockStore := testutil.NewMockStore()
// Init manager
chainId := "test"
manager, err := testutil.GetManager(chainId, testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, mockStore)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, mockStore)
require.NoError(err)
commitHash := [32]byte{1}
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
Expand Down
8 changes: 4 additions & 4 deletions block/production_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ func TestCreateEmptyBlocksEnableDisable(t *testing.T) {
managerConfigCreatesEmptyBlocks := testutil.GetManagerConfig()
managerConfigCreatesEmptyBlocks.BlockTime = blockTime
managerConfigCreatesEmptyBlocks.MaxIdleTime = 0 * time.Second
managerWithEmptyBlocks, err := testutil.GetManager("test", managerConfigCreatesEmptyBlocks, nil, 1, 1, 0, proxyApp, nil)
managerWithEmptyBlocks, err := testutil.GetManager(managerConfigCreatesEmptyBlocks, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

// Init manager with empty blocks feature enabled
managerConfig := testutil.GetManagerConfig()
managerConfig.BlockTime = blockTime
managerConfig.MaxIdleTime = MaxIdleTime
managerConfig.MaxProofTime = MaxIdleTime
manager, err := testutil.GetManager("test", managerConfig, nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(managerConfig, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

// Check initial height
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestCreateEmptyBlocksNew(t *testing.T) {
managerConfig := testutil.GetManagerConfig()
managerConfig.BlockTime = 200 * time.Millisecond
managerConfig.MaxIdleTime = 1 * time.Second
manager, err := testutil.GetManager("test", managerConfig, nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(managerConfig, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

abciClient, err := clientCreator.NewABCIClient()
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestStopBlockProduction(t *testing.T) {

managerConfig := testutil.GetManagerConfig()
managerConfig.BatchSubmitBytes = 1000 // small batch size to fill up quickly
manager, err := testutil.GetManager("test", managerConfig, nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(managerConfig, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

assert.Equal(manager.State.Height(), uint64(0))
Expand Down
2 changes: 1 addition & 1 deletion block/pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPruningRetainHeight(t *testing.T) {
err := proxyApp.Start()
require.NoError(err)

manager, err := testutil.GetManager("test", testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)
Expand Down
11 changes: 5 additions & 6 deletions block/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import (
// 1. single block with single large tx
// 2. single block with multiple small tx
func TestBatchOverhead(t *testing.T) {
chanId := "test"
manager, err := testutil.GetManager(chanId, testutil.GetManagerConfig(), nil, 1, 1, 0, nil, nil)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, nil, nil)
require.NoError(t, err)
require.NotNil(t, manager)

Expand Down Expand Up @@ -124,7 +123,7 @@ func TestBatchSubmissionHappyFlow(t *testing.T) {
},
},
})
manager, err := testutil.GetManager("test", testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
Expand Down Expand Up @@ -183,7 +182,7 @@ func TestBatchSubmissionFailedSubmission(t *testing.T) {
slmock.On("Start").Return(nil)
slmock.On("GetProposer").Return(proposer)

manager, err := testutil.GetManagerWithProposerKey("test", testutil.GetManagerConfig(), lib2pPrivKey, slmock, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManagerWithProposerKey(testutil.GetManagerConfig(), lib2pPrivKey, slmock, 1, 1, 0, proxyApp, nil)
require.NoError(err)

manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
Expand Down Expand Up @@ -248,7 +247,7 @@ func TestSubmissionByTime(t *testing.T) {
BatchSubmitBytes: 1000,
}

manager, err := testutil.GetManager("test", managerConfig, nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(managerConfig, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
Expand Down Expand Up @@ -324,7 +323,7 @@ func TestSubmissionByBatchSize(t *testing.T) {

managerConfig := testutil.GetManagerConfig()
managerConfig.BatchSubmitBytes = c.blockBatchMaxSizeBytes
manager, err := testutil.GetManager("test", managerConfig, nil, 1, 1, 0, proxyApp, nil)
manager, err := testutil.GetManager(managerConfig, nil, 1, 1, 0, proxyApp, nil)
require.NoError(err)

manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
Expand Down
4 changes: 2 additions & 2 deletions da/avail/avail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestRetrieveBatches(t *testing.T) {
// Set the mock functions
mockSubstrateApiClient.On("GetBlockHash", mock.Anything).Return(availtypes.NewHash([]byte("123")), nil)
// Build batches for the block extrinsics
batch1 := testutil.MustGenerateBatchAndKey(0, 1, "test", [32]byte{})
batch2 := testutil.MustGenerateBatchAndKey(2, 3, "test", [32]byte{})
batch1 := testutil.MustGenerateBatchAndKey(0, 1)
batch2 := testutil.MustGenerateBatchAndKey(2, 3)
batch1bytes, err := batch1.MarshalBinary()
require.NoError(err)
batch2bytes, err := batch2.MarshalBinary()
Expand Down
2 changes: 1 addition & 1 deletion da/celestia/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestSubmitBatch(t *testing.T) {
require := require.New(t)
configBytes, err := json.Marshal(celestia.TestConfig)
require.NoError(err)
batch := testutil.MustGenerateBatchAndKey(0, 1, "test", [32]byte{})
batch := testutil.MustGenerateBatchAndKey(0, 1)
nIDSize := 1

tree := exampleNMT(nIDSize, true, 1, 2, 3, 4)
Expand Down
4 changes: 2 additions & 2 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestStartup(t *testing.T) {
require := require.New(t)

// TODO(omritoptix): Test with and without sequencer mode.
node, err := testutil.CreateNode(false, nil, testutil.GenerateGenesis("test", 0))
node, err := testutil.CreateNode(false, nil, testutil.GenerateGenesis(0))
require.NoError(err)
require.NotNil(node)

Expand Down Expand Up @@ -87,7 +87,7 @@ func TestMempoolDirectly(t *testing.T) {
key,
signingKey,
proxy.NewLocalClientCreator(app),
testutil.GenerateGenesis("test", 0),
testutil.GenerateGenesis(0),
log.TestingLogger(),
mempool.NopMetrics(),
)
Expand Down
2 changes: 1 addition & 1 deletion p2p/block_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestBlockSync(t *testing.T) {
logger := log.TestingLogger()
ctx := context.Background()

manager, err := testutil.GetManager("test", testutil.GetManagerConfig(), nil, 1, 1, 0, nil, nil)
manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, nil, nil)

require.NoError(t, err)
require.NotNil(t, manager)
Expand Down
8 changes: 4 additions & 4 deletions rpc/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestCheckTx(t *testing.T) {
func TestGenesisChunked(t *testing.T) {
assert := assert.New(t)

genDoc := testutil.GenerateGenesis("test", 1)
genDoc := testutil.GenerateGenesis(1)

mockApp := &tmmocks.MockApplication{}
mockApp.On("InitChain", mock.Anything).Return(abci.ResponseInitChain{})
Expand Down Expand Up @@ -803,7 +803,7 @@ func TestValidatorSetHandling(t *testing.T) {
key,
signingKey,
proxy.NewLocalClientCreator(app),
testutil.GenerateGenesis("test", 0),
testutil.GenerateGenesis(0),
log.TestingLogger(),
mempool.NopMetrics(),
)
Expand Down Expand Up @@ -964,7 +964,7 @@ func getRPCInternal(t *testing.T, sequencer bool) (*tmmocks.MockApplication, *cl
key,
localKey, // this is where sequencer mode is set. if same key as in settlement.Config, it's sequencer
proxy.NewLocalClientCreator(app),
testutil.GenerateGenesis("test", 0),
testutil.GenerateGenesis(0),
log.TestingLogger(),
mempool.NopMetrics(),
)
Expand Down Expand Up @@ -1038,7 +1038,7 @@ func TestMempool2Nodes(t *testing.T) {
id1, err := peer.IDFromPrivateKey(key1)
require.NoError(err)

genesis := testutil.GenerateGenesis("test", 0)
genesis := testutil.GenerateGenesis(0)
node1, err := node.NewNode(context.Background(), config.NodeConfig{
SettlementLayer: "mock",
SettlementConfig: settlement.Config{
Expand Down
10 changes: 5 additions & 5 deletions testutil/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const (
/* utils */
/* -------------------------------------------------------------------------- */

func GetManagerWithProposerKey(chainId string, conf config.BlockManagerConfig, proposerKey crypto.PrivKey, settlementlc settlement.ClientI, genesisHeight, storeInitialHeight, storeLastBlockHeight int64, proxyAppConns proxy.AppConns, mockStore store.Store) (*block.Manager, error) {
genesis := GenerateGenesis(chainId, genesisHeight)
func GetManagerWithProposerKey(conf config.BlockManagerConfig, proposerKey crypto.PrivKey, settlementlc settlement.ClientI, genesisHeight, storeInitialHeight, storeLastBlockHeight int64, proxyAppConns proxy.AppConns, mockStore store.Store) (*block.Manager, error) {
genesis := GenerateGenesis(genesisHeight)
// Change the LastBlockHeight to avoid calling InitChainSync within the manager
// And updating the state according to the genesis.
raw, _ := proposerKey.GetPublic().Raw()
Expand Down Expand Up @@ -99,7 +99,7 @@ func GetManagerWithProposerKey(chainId string, conf config.BlockManagerConfig, p
GossipSubCacheSize: 50,
BootstrapRetryTime: 30 * time.Second,
BlockSyncRequestIntervalTime: 30 * time.Second,
}, p2pKey, chainId, managerStore, pubsubServer, datastore.NewMapDatastore(), logger)
}, p2pKey, "TestChain", managerStore, pubsubServer, datastore.NewMapDatastore(), logger)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,12 +132,12 @@ func GetManagerWithProposerKey(chainId string, conf config.BlockManagerConfig, p
return manager, nil
}

func GetManager(chainId string, conf config.BlockManagerConfig, settlementlc settlement.ClientI, genesisHeight, storeInitialHeight, storeLastBlockHeight int64, proxyAppConns proxy.AppConns, mockStore store.Store) (*block.Manager, error) {
func GetManager(conf config.BlockManagerConfig, settlementlc settlement.ClientI, genesisHeight, storeInitialHeight, storeLastBlockHeight int64, proxyAppConns proxy.AppConns, mockStore store.Store) (*block.Manager, error) {
proposerKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return nil, err
}
return GetManagerWithProposerKey(chainId, conf, proposerKey, settlementlc, genesisHeight, storeInitialHeight, storeLastBlockHeight, proxyAppConns, mockStore)
return GetManagerWithProposerKey(conf, proposerKey, settlementlc, genesisHeight, storeInitialHeight, storeLastBlockHeight, proxyAppConns, mockStore)
}

func GetMockDALC(logger log.Logger) da.DataAvailabilityLayerClient {
Expand Down
2 changes: 1 addition & 1 deletion testutil/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func CreateLocalServer(t *testing.T) (*rpc.Server, net.Listener) {
var server *rpc.Server
// Start server with listener
go func() {
node, err := CreateNode(true, nil, GenerateGenesis("test", 0))
node, err := CreateNode(true, nil, GenerateGenesis(0))
require.NoError(t, err)
err = node.Start()
require.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions testutil/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func generateBlock(height uint64, proposerHash []byte, lastHeaderHash [32]byte)
ConsensusHash: h[2],
AppHash: [32]byte{},
LastResultsHash: GetEmptyLastResultsHash(),
ProposerAddress: proposerAddress,
ProposerAddress: []byte{4, 3, 2, 1},
SequencerHash: [32]byte(proposerHash),
NextSequencersHash: [32]byte(proposerHash),
ChainID: "test-chain",
Expand Down Expand Up @@ -214,12 +214,12 @@ func MustGenerateBatch(startHeight uint64, endHeight uint64, proposerKey crypto.
}
}

func MustGenerateBatchAndKey(startHeight uint64, endHeight uint64, chainId string, lastHeaderHash [32]byte) *types.Batch {
func MustGenerateBatchAndKey(startHeight uint64, endHeight uint64) *types.Batch {
proposerKey, _, err := crypto.GenerateEd25519Key(nil)
if err != nil {
panic(err)
}
return MustGenerateBatch(startHeight, endHeight, proposerKey, chainId, lastHeaderHash)
return MustGenerateBatch(startHeight, endHeight, proposerKey)
}

// GenerateRandomValidatorSet generates random validator sets
Expand Down Expand Up @@ -260,9 +260,9 @@ func GenerateStateWithSequencer(initialHeight int64, lastBlockHeight int64, pubk
}

// GenerateGenesis generates a genesis for testing.
func GenerateGenesis(chainId string, initialHeight int64) *tmtypes.GenesisDoc {
func GenerateGenesis(initialHeight int64) *tmtypes.GenesisDoc {
return &tmtypes.GenesisDoc{
ChainID: chainId,
ChainID: "test-chain",
InitialHeight: initialHeight,
ConsensusParams: &tmproto.ConsensusParams{
Block: tmproto.BlockParams{
Expand Down

0 comments on commit 5552b77

Please sign in to comment.