Skip to content

Commit

Permalink
fix: failing tests on transaction test and state test
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Aug 10, 2024
1 parent 83fe68b commit c2f5533
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand Down
3 changes: 2 additions & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
15 changes: 9 additions & 6 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
19 changes: 12 additions & 7 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
)

Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit c2f5533

Please sign in to comment.