From c2f553328584c723086079f7c816d37c5be426d0 Mon Sep 17 00:00:00 2001 From: kehiy Date: Sat, 10 Aug 2024 15:26:48 +0330 Subject: [PATCH] fix: failing tests on transaction test and state test --- cmd/cmd.go | 3 ++- consensus/consensus_test.go | 11 ++++++----- node/node.go | 5 +++-- node/node_test.go | 3 ++- state/state.go | 3 ++- state/state_test.go | 15 +++++++++------ tests/main_test.go | 19 ++++++++++++------- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index f8e5d066e..a9d5aa56f 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -24,6 +24,7 @@ import ( "github.com/pactus-project/pactus/genesis" "github.com/pactus-project/pactus/node" "github.com/pactus-project/pactus/types/account" + "github.com/pactus-project/pactus/types/param" "github.com/pactus-project/pactus/types/validator" "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/wallet" @@ -408,7 +409,7 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, return nil, nil, err } - nd, err := node.NewNode(gen, conf, valKeys, rewardAddrs) + nd, err := node.NewNode(gen, param.DefaultParams(), conf, valKeys, rewardAddrs) if err != nil { return nil, nil, err } diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index 2dcd96215..c6a3b829d 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -16,6 +16,7 @@ import ( "github.com/pactus-project/pactus/types/account" "github.com/pactus-project/pactus/types/block" "github.com/pactus-project/pactus/types/certificate" + "github.com/pactus-project/pactus/types/param" "github.com/pactus-project/pactus/types/proposal" "github.com/pactus-project/pactus/types/tx" "github.com/pactus-project/pactus/types/validator" @@ -93,16 +94,16 @@ func setupWithSeed(t *testing.T, seed int64) *testData { getTime := util.RoundNow(params.BlockIntervalInSecond). Add(time.Duration(params.BlockIntervalInSecond) * time.Second) genDoc := genesis.MakeGenesis(getTime, accs, vals, params) - stX, err := state.LoadOrNewState(genDoc, []*bls.ValidatorKey{valKeys[tIndexX]}, + stX, err := state.LoadOrNewState(genDoc, param.DefaultParams(), []*bls.ValidatorKey{valKeys[tIndexX]}, store.MockingStore(ts), txPool, nil) require.NoError(t, err) - stY, err := state.LoadOrNewState(genDoc, []*bls.ValidatorKey{valKeys[tIndexY]}, + stY, err := state.LoadOrNewState(genDoc, param.DefaultParams(), []*bls.ValidatorKey{valKeys[tIndexY]}, store.MockingStore(ts), txPool, nil) require.NoError(t, err) - stB, err := state.LoadOrNewState(genDoc, []*bls.ValidatorKey{valKeys[tIndexB]}, + stB, err := state.LoadOrNewState(genDoc, param.DefaultParams(), []*bls.ValidatorKey{valKeys[tIndexB]}, store.MockingStore(ts), txPool, nil) require.NoError(t, err) - stP, err := state.LoadOrNewState(genDoc, []*bls.ValidatorKey{valKeys[tIndexP]}, + stP, err := state.LoadOrNewState(genDoc, param.DefaultParams(), []*bls.ValidatorKey{valKeys[tIndexP]}, store.MockingStore(ts), txPool, nil) require.NoError(t, err) @@ -411,7 +412,7 @@ func TestNotInCommittee(t *testing.T) { valKey := td.RandValKey() str := store.MockingStore(td.TestSuite) - st, _ := state.LoadOrNewState(td.genDoc, []*bls.ValidatorKey{valKey}, str, td.txPool, nil) + st, _ := state.LoadOrNewState(td.genDoc, param.DefaultParams(), []*bls.ValidatorKey{valKey}, str, td.txPool, nil) consInst := NewConsensus(testConfig(), st, valKey, valKey.Address(), make(chan message.Message, 100), newConcreteMediator()) cons := consInst.(*consensus) diff --git a/node/node.go b/node/node.go index 6d88ae884..ae1f673de 100644 --- a/node/node.go +++ b/node/node.go @@ -15,6 +15,7 @@ import ( "github.com/pactus-project/pactus/sync/bundle/message" "github.com/pactus-project/pactus/sync/peerset/peer/service" "github.com/pactus-project/pactus/txpool" + "github.com/pactus-project/pactus/types/param" "github.com/pactus-project/pactus/util/logger" "github.com/pactus-project/pactus/version" "github.com/pactus-project/pactus/wallet" @@ -41,7 +42,7 @@ type Node struct { nanomsg *nanomsg.Server } -func NewNode(genDoc *genesis.Genesis, conf *config.Config, +func NewNode(genDoc *genesis.Genesis, chainParam *param.Params, conf *config.Config, valKeys []*bls.ValidatorKey, rewardAddrs []crypto.Address, ) (*Node, error) { // Initialize the logger @@ -66,7 +67,7 @@ func NewNode(genDoc *genesis.Genesis, conf *config.Config, return nil, err } - st, err := state.LoadOrNewState(genDoc, valKeys, str, txPool, eventCh) + st, err := state.LoadOrNewState(genDoc, chainParam, valKeys, str, txPool, eventCh) if err != nil { return nil, err } diff --git a/node/node_test.go b/node/node_test.go index 05584f9f7..9fe8ad058 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -10,6 +10,7 @@ import ( "github.com/pactus-project/pactus/crypto/hash" "github.com/pactus-project/pactus/genesis" "github.com/pactus-project/pactus/types/account" + "github.com/pactus-project/pactus/types/param" "github.com/pactus-project/pactus/types/validator" "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/util/logger" @@ -48,7 +49,7 @@ func TestRunningNode(t *testing.T) { valKeys := []*bls.ValidatorKey{ts.RandValKey(), ts.RandValKey()} rewardAddrs := []crypto.Address{ts.RandAccAddress(), ts.RandAccAddress()} - nd, err := NewNode(gen, conf, valKeys, rewardAddrs) + nd, err := NewNode(gen, param.DefaultParams(), conf, valKeys, rewardAddrs) assert.True(t, conf.Sync.Services.IsFullNode()) assert.True(t, conf.Sync.Services.IsPrunedNode()) diff --git a/state/state.go b/state/state.go index 129c960b6..fd088ef60 100644 --- a/state/state.go +++ b/state/state.go @@ -55,6 +55,7 @@ type state struct { func LoadOrNewState( genDoc *genesis.Genesis, + chainParam *param.Params, valKeys []*bls.ValidatorKey, str store.Store, txPool txpool.TxPool, eventCh chan event.Event, @@ -63,7 +64,7 @@ func LoadOrNewState( valKeys: valKeys, genDoc: genDoc, txPool: txPool, - params: param.DefaultParams(), + params: chainParam, store: str, lastInfo: lastinfo.NewLastInfo(), accountMerkle: persistentmerkle.New(), diff --git a/state/state_test.go b/state/state_test.go index 8bffa1cda..c1241d919 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -13,6 +13,7 @@ import ( "github.com/pactus-project/pactus/types/account" "github.com/pactus-project/pactus/types/block" "github.com/pactus-project/pactus/types/certificate" + "github.com/pactus-project/pactus/types/param" "github.com/pactus-project/pactus/types/tx" "github.com/pactus-project/pactus/types/tx/payload" "github.com/pactus-project/pactus/types/validator" @@ -54,10 +55,6 @@ func setup(t *testing.T) *testData { genTime := util.RoundNow(10).Add(-8640 * time.Second) - params := genesis.DefaultGenParams() - params.CommitteeSize = 7 - params.BondInterval = 10 - genAcc1 := account.NewAccount(0) genAcc1.AddToBalance(21 * 1e15) // 21,000,000.000,000,000 genAcc2 := account.NewAccount(1) @@ -69,11 +66,17 @@ func setup(t *testing.T) *testData { genAccPubKey.AccountAddress(): genAcc2, } + params := genesis.DefaultGenParams() gnDoc := genesis.MakeGenesis(genTime, genAccs, genVals, params) // First validator is in the committee valKeys := []*bls.ValidatorKey{genValKeys[0], ts.RandValKey()} - st1, err := LoadOrNewState(gnDoc, valKeys, mockStore, mockTxPool, nil) + + chainParams := param.DefaultParams() + chainParams.CommitteeSize = 7 + chainParams.BondInterval = 10 + + st1, err := LoadOrNewState(gnDoc, chainParams, valKeys, mockStore, mockTxPool, nil) require.NoError(t, err) state, _ := st1.(*state) @@ -537,7 +540,7 @@ func TestLoadState(t *testing.T) { blk6, cert6 := td.makeBlockAndCertificate(t, 0) // Load last state info - newState, err := LoadOrNewState(td.state.genDoc, td.state.valKeys, + newState, err := LoadOrNewState(td.state.genDoc, td.state.params, td.state.valKeys, td.state.store, td.commonTxPool, nil) require.NoError(t, err) diff --git a/tests/main_test.go b/tests/main_test.go index 6e659669d..e2cfae8b8 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -16,6 +16,7 @@ import ( "github.com/pactus-project/pactus/store" "github.com/pactus-project/pactus/types/account" "github.com/pactus-project/pactus/types/amount" + "github.com/pactus-project/pactus/types/param" "github.com/pactus-project/pactus/types/validator" "github.com/pactus-project/pactus/util" "github.com/pactus-project/pactus/util/logger" @@ -35,14 +36,16 @@ var ( tTransaction pactus.TransactionClient tNetwork pactus.NetworkClient tCtx context.Context + tParam *param.Params ) const ( - tNodeIdx1 = 0 - tNodeIdx2 = 1 - tNodeIdx3 = 2 - tNodeIdx4 = 3 - tTotalNodes = 4 // each node has 3 validators + tNodeIdx1 = iota + tNodeIdx2 + tNodeIdx3 + tNodeIdx4 + tTotalNodes // each node has 3 validators + tCommitteeSize = 7 ) @@ -119,16 +122,18 @@ func TestMain(m *testing.M) { vals[2] = validator.NewValidator(tValKeys[tNodeIdx3][0].PublicKey(), 2) vals[3] = validator.NewValidator(tValKeys[tNodeIdx4][0].PublicKey(), 3) params := genesis.DefaultGenParams() - params.MinimumStake = 1000 params.BlockIntervalInSecond = 2 params.BondInterval = 8 params.CommitteeSize = tCommitteeSize params.TransactionToLiveInterval = 8 tGenDoc = genesis.MakeGenesis(time.Now(), accs, vals, params) + tParam = param.DefaultParams() + tParam.MinimumStake = 1000 + for i := 0; i < tTotalNodes; i++ { tNodes[i], _ = node.NewNode( - tGenDoc, tConfigs[i], + tGenDoc, tParam, tConfigs[i], tValKeys[i], []crypto.Address{ tValKeys[i][0].PublicKey().AccountAddress(),