From e5e713fb8ded1cfdd0217ce6504fd6d8cb66a767 Mon Sep 17 00:00:00 2001 From: kehiy Date: Mon, 5 Aug 2024 18:29:01 +0330 Subject: [PATCH 1/5] fix(genesis): sparate genesis params and chain params --- cmd/cmd.go | 2 +- consensus/consensus_test.go | 2 +- genesis/genesis.go | 12 +++++----- genesis/genesis_test.go | 4 ++-- node/node_test.go | 2 +- state/mock.go | 2 +- state/state.go | 6 ++--- state/state_test.go | 8 +++---- tests/main_test.go | 2 +- types/param/genesis_param.go | 45 ++++++++++++++++++++++++++++++++++++ types/param/param.go | 12 ++++------ 11 files changed, 68 insertions(+), 29 deletions(-) create mode 100644 types/param/genesis_param.go diff --git a/cmd/cmd.go b/cmd/cmd.go index e1cf0fbf9..c0be69f8e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -440,7 +440,7 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis { } // create genesis - params := param.DefaultParams() + params := param.DefaultGenParams() params.BlockVersion = 0 gen := genesis.MakeGenesis(util.RoundNow(60), accs, vals, params) diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index 416c40ce2..aaa1e1768 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -86,7 +86,7 @@ func setupWithSeed(t *testing.T, seed int64) *testData { acc := account.NewAccount(0) acc.AddToBalance(21 * 1e14) accs := map[crypto.Address]*account.Account{crypto.TreasuryAddress: acc} - params := param.DefaultParams() + params := param.DefaultGenParams() params.CommitteeSize = 4 // To prevent triggering timers before starting the tests and diff --git a/genesis/genesis.go b/genesis/genesis.go index a3d017489..9857e438d 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -56,10 +56,10 @@ type Genesis struct { } type genesisData struct { - GenesisTime time.Time `cbor:"1,keyasint" json:"genesis_time"` - Params *param.Params `cbor:"2,keyasint" json:"params"` - Accounts []genAccount `cbor:"3,keyasint" json:"accounts"` - Validators []genValidator `cbor:"4,keyasint" json:"validators"` + GenesisTime time.Time `cbor:"1,keyasint" json:"genesis_time"` + Params *param.GenParams `cbor:"2,keyasint" json:"params"` + Accounts []genAccount `cbor:"3,keyasint" json:"accounts"` + Validators []genValidator `cbor:"4,keyasint" json:"validators"` } func (gen *Genesis) Hash() hash.Hash { @@ -72,7 +72,7 @@ func (gen *Genesis) GenesisTime() time.Time { return gen.data.GenesisTime } -func (gen *Genesis) Params() *param.Params { +func (gen *Genesis) Params() *param.GenParams { return gen.data.Params } @@ -124,7 +124,7 @@ func makeGenesisValidator(val *validator.Validator) genValidator { } func MakeGenesis(genesisTime time.Time, accounts map[crypto.Address]*account.Account, - validators []*validator.Validator, params *param.Params, + validators []*validator.Validator, params *param.GenParams, ) *Genesis { genAccs := make([]genAccount, len(accounts)) for addr, acc := range accounts { diff --git a/genesis/genesis_test.go b/genesis/genesis_test.go index 56e3284fc..3be4efd46 100644 --- a/genesis/genesis_test.go +++ b/genesis/genesis_test.go @@ -26,7 +26,7 @@ func TestMarshaling(t *testing.T) { val, _ := ts.GenerateTestValidator(0) gen1 := genesis.MakeGenesis(util.RoundNow(10), map[crypto.Address]*account.Account{prv: acc}, - []*validator.Validator{val}, param.DefaultParams()) + []*validator.Validator{val}, param.DefaultGenParams()) gen2 := new(genesis.Genesis) assert.Equal(t, 10, gen1.Params().BlockIntervalInSecond) @@ -95,7 +95,7 @@ func TestCheckGenesisAccountAndValidator(t *testing.T) { accs[pub.AccountAddress()] = acc vals = append(vals, val) } - gen := genesis.MakeGenesis(time.Now(), accs, vals, param.DefaultParams()) + gen := genesis.MakeGenesis(time.Now(), accs, vals, param.DefaultGenParams()) for addr, acc := range gen.Accounts() { assert.Equal(t, accs[addr], acc) diff --git a/node/node_test.go b/node/node_test.go index 2cfc4f107..e183aa3f5 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -30,7 +30,7 @@ func TestRunningNode(t *testing.T) { val := validator.NewValidator(pub, 0) gen := genesis.MakeGenesis(time.Now(), map[crypto.Address]*account.Account{crypto.TreasuryAddress: acc}, - []*validator.Validator{val}, param.DefaultParams()) + []*validator.Validator{val}, param.DefaultGenParams()) conf := config.DefaultConfigMainnet() conf.GRPC.Enable = true conf.GRPC.Listen = "0.0.0.0:0" diff --git a/state/mock.go b/state/mock.go index 4a2849edf..09a816bd3 100644 --- a/state/mock.go +++ b/state/mock.go @@ -51,7 +51,7 @@ func MockingState(ts *testsuite.TestSuite) *MockState { TestPool: txpool.MockingTxPool(), TestCommittee: cmt, TestValKeys: valKeys, - TestParams: genDoc.Params(), + TestParams: param.DefaultParams(), } } diff --git a/state/state.go b/state/state.go index e9ab9ad46..129c960b6 100644 --- a/state/state.go +++ b/state/state.go @@ -35,8 +35,6 @@ import ( "github.com/pactus-project/pactus/www/nanomsg/event" ) -var maxTransactionsPerBlock = 1000 - type state struct { lk sync.RWMutex @@ -65,7 +63,7 @@ func LoadOrNewState( valKeys: valKeys, genDoc: genDoc, txPool: txPool, - params: genDoc.Params(), + params: param.DefaultParams(), store: str, lastInfo: lastinfo.NewLastInfo(), accountMerkle: persistentmerkle.New(), @@ -323,7 +321,7 @@ func (st *state) ProposeBlock(valKey *bls.ValidatorKey, rewardAddr crypto.Addres // Re-check all transactions strictly and remove invalid ones txs := st.txPool.PrepareBlockTransactions() - txs = util.Trim(txs, maxTransactionsPerBlock-1) + txs = util.Trim(txs, st.params.MaxTransactionsPerBlock-1) for i := 0; i < txs.Len(); i++ { // Only one subsidy transaction per blk if txs[i].IsSubsidyTx() { diff --git a/state/state_test.go b/state/state_test.go index 2ea3fbcc2..30bd88170 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -55,7 +55,7 @@ func setup(t *testing.T) *testData { genTime := util.RoundNow(10).Add(-8640 * time.Second) - params := param.DefaultParams() + params := param.DefaultGenParams() params.CommitteeSize = 7 params.BondInterval = 10 @@ -585,10 +585,10 @@ func TestCalculateFee(t *testing.T) { func TestCheckMaximumTransactionPerBlock(t *testing.T) { td := setup(t) - maxTransactionsPerBlock = 10 + td.state.params.MaxTransactionsPerBlock = 10 lockTime := td.state.LastBlockHeight() senderAddr := td.genAccKey.PublicKeyNative().AccountAddress() - for i := 0; i < maxTransactionsPerBlock+2; i++ { + for i := 0; i < td.state.params.MaxTransactionsPerBlock+2; i++ { amt := td.RandAmount() fee := td.state.CalculateFee(amt, payload.TypeTransfer) trx := tx.NewTransferTx(lockTime, senderAddr, td.RandAccAddress(), amt, fee) @@ -598,7 +598,7 @@ func TestCheckMaximumTransactionPerBlock(t *testing.T) { blk, err := td.state.ProposeBlock(td.state.valKeys[0], td.RandAccAddress()) assert.NoError(t, err) - assert.Equal(t, maxTransactionsPerBlock, blk.Transactions().Len()) + assert.Equal(t, td.state.params.MaxTransactionsPerBlock, blk.Transactions().Len()) } func TestCommittedBlock(t *testing.T) { diff --git a/tests/main_test.go b/tests/main_test.go index 5578a48d6..0db31f44e 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -119,7 +119,7 @@ func TestMain(m *testing.M) { vals[1] = validator.NewValidator(tValKeys[tNodeIdx2][0].PublicKey(), 1) vals[2] = validator.NewValidator(tValKeys[tNodeIdx3][0].PublicKey(), 2) vals[3] = validator.NewValidator(tValKeys[tNodeIdx4][0].PublicKey(), 3) - params := param.DefaultParams() + params := param.DefaultGenParams() params.MinimumStake = 1000 params.BlockIntervalInSecond = 2 params.BondInterval = 8 diff --git a/types/param/genesis_param.go b/types/param/genesis_param.go new file mode 100644 index 000000000..1baa63801 --- /dev/null +++ b/types/param/genesis_param.go @@ -0,0 +1,45 @@ +package param + +import ( + "time" + + "github.com/pactus-project/pactus/types/amount" +) + +type GenParams struct { + BlockVersion uint8 `cbor:"1,keyasint" json:"block_version"` + BlockIntervalInSecond int `cbor:"2,keyasint" json:"block_interval_in_second"` + CommitteeSize int `cbor:"3,keyasint" json:"committee_size"` + BlockReward amount.Amount `cbor:"4,keyasint" json:"block_reward"` + TransactionToLiveInterval uint32 `cbor:"5,keyasint" json:"transaction_to_live_interval"` + BondInterval uint32 `cbor:"6,keyasint" json:"bond_interval"` + UnbondInterval uint32 `cbor:"7,keyasint" json:"unbond_interval"` + SortitionInterval uint32 `cbor:"8,keyasint" json:"sortition_interval"` + FeeFraction float64 `cbor:"9,keyasint" json:"fee_fraction"` // Deprecated: Replaced by fix fee + MinimumFee amount.Amount `cbor:"10,keyasint" json:"minimum_fee"` // Deprecated: Replaced by fix fee + MaximumFee amount.Amount `cbor:"11,keyasint" json:"maximum_fee"` // Deprecated: Replaced by fix fee + MinimumStake amount.Amount `cobr:"12,keyasint" json:"minimum_stake"` + MaximumStake amount.Amount `cbor:"13,keyasint" json:"maximum_stake"` +} + +func DefaultGenParams() *GenParams { + return &GenParams{ + BlockVersion: 1, + BlockIntervalInSecond: 10, + CommitteeSize: 51, + BlockReward: 1000000000, + TransactionToLiveInterval: 8640, // one day + BondInterval: 360, // one hour + UnbondInterval: 181440, // 21 days + SortitionInterval: 17, + FeeFraction: 0.0001, + MinimumFee: 1000, + MaximumFee: 1000000, + MinimumStake: 1000000000, + MaximumStake: 1000000000000, + } +} + +func (p *GenParams) BlockInterval() time.Duration { + return time.Duration(p.BlockIntervalInSecond) * time.Second +} diff --git a/types/param/param.go b/types/param/param.go index f4f319212..fe14a31cb 100644 --- a/types/param/param.go +++ b/types/param/param.go @@ -15,11 +15,9 @@ type Params struct { BondInterval uint32 `cbor:"6,keyasint" json:"bond_interval"` UnbondInterval uint32 `cbor:"7,keyasint" json:"unbond_interval"` SortitionInterval uint32 `cbor:"8,keyasint" json:"sortition_interval"` - FeeFraction float64 `cbor:"9,keyasint" json:"fee_fraction"` // Deprecated: Replaced by fix fee - MinimumFee amount.Amount `cbor:"10,keyasint" json:"minimum_fee"` // Deprecated: Replaced by fix fee - MaximumFee amount.Amount `cbor:"11,keyasint" json:"maximum_fee"` // Deprecated: Replaced by fix fee - MinimumStake amount.Amount `cobr:"12,keyasint" json:"minimum_stake"` - MaximumStake amount.Amount `cbor:"13,keyasint" json:"maximum_stake"` + MaxTransactionsPerBlock int `cbor:"9,keyasint" json:"max_transaction_per_block"` + MinimumStake amount.Amount `cobr:"10,keyasint" json:"minimum_stake"` + MaximumStake amount.Amount `cbor:"11,keyasint" json:"maximum_stake"` } func DefaultParams() *Params { @@ -32,9 +30,7 @@ func DefaultParams() *Params { BondInterval: 360, // one hour UnbondInterval: 181440, // 21 days SortitionInterval: 17, - FeeFraction: 0.0001, - MinimumFee: 1000, - MaximumFee: 1000000, + MaxTransactionsPerBlock: 1000, MinimumStake: 1000000000, MaximumStake: 1000000000000, } From 38eaf62b4cb29cc8259c9191780799040eeb255f Mon Sep 17 00:00:00 2001 From: kehiy Date: Wed, 7 Aug 2024 18:56:36 +0330 Subject: [PATCH 2/5] fix(genesis,params): moving genesis params to genesis module --- cmd/cmd.go | 3 +-- consensus/consensus_test.go | 3 +-- genesis/genesis.go | 13 ++++++------- {types/param => genesis}/genesis_param.go | 2 +- genesis/genesis_test.go | 5 ++--- node/node_test.go | 3 +-- state/state_test.go | 3 +-- sync/firewall/firewall_test.go | 2 +- tests/main_test.go | 3 +-- 9 files changed, 15 insertions(+), 22 deletions(-) rename {types/param => genesis}/genesis_param.go (99%) diff --git a/cmd/cmd.go b/cmd/cmd.go index c0be69f8e..f8e5d066e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -24,7 +24,6 @@ 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" @@ -440,7 +439,7 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis { } // create genesis - params := param.DefaultGenParams() + params := genesis.DefaultGenParams() params.BlockVersion = 0 gen := genesis.MakeGenesis(util.RoundNow(60), accs, vals, params) diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index aaa1e1768..2dcd96215 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -16,7 +16,6 @@ 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" @@ -86,7 +85,7 @@ func setupWithSeed(t *testing.T, seed int64) *testData { acc := account.NewAccount(0) acc.AddToBalance(21 * 1e14) accs := map[crypto.Address]*account.Account{crypto.TreasuryAddress: acc} - params := param.DefaultGenParams() + params := genesis.DefaultGenParams() params.CommitteeSize = 4 // To prevent triggering timers before starting the tests and diff --git a/genesis/genesis.go b/genesis/genesis.go index 9857e438d..4be991501 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -11,7 +11,6 @@ import ( "github.com/pactus-project/pactus/crypto/hash" "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" ) @@ -56,10 +55,10 @@ type Genesis struct { } type genesisData struct { - GenesisTime time.Time `cbor:"1,keyasint" json:"genesis_time"` - Params *param.GenParams `cbor:"2,keyasint" json:"params"` - Accounts []genAccount `cbor:"3,keyasint" json:"accounts"` - Validators []genValidator `cbor:"4,keyasint" json:"validators"` + GenesisTime time.Time `cbor:"1,keyasint" json:"genesis_time"` + Params *GenParams `cbor:"2,keyasint" json:"params"` + Accounts []genAccount `cbor:"3,keyasint" json:"accounts"` + Validators []genValidator `cbor:"4,keyasint" json:"validators"` } func (gen *Genesis) Hash() hash.Hash { @@ -72,7 +71,7 @@ func (gen *Genesis) GenesisTime() time.Time { return gen.data.GenesisTime } -func (gen *Genesis) Params() *param.GenParams { +func (gen *Genesis) Params() *GenParams { return gen.data.Params } @@ -124,7 +123,7 @@ func makeGenesisValidator(val *validator.Validator) genValidator { } func MakeGenesis(genesisTime time.Time, accounts map[crypto.Address]*account.Account, - validators []*validator.Validator, params *param.GenParams, + validators []*validator.Validator, params *GenParams, ) *Genesis { genAccs := make([]genAccount, len(accounts)) for addr, acc := range accounts { diff --git a/types/param/genesis_param.go b/genesis/genesis_param.go similarity index 99% rename from types/param/genesis_param.go rename to genesis/genesis_param.go index 1baa63801..620038f10 100644 --- a/types/param/genesis_param.go +++ b/genesis/genesis_param.go @@ -1,4 +1,4 @@ -package param +package genesis import ( "time" diff --git a/genesis/genesis_test.go b/genesis/genesis_test.go index 3be4efd46..00b0c6464 100644 --- a/genesis/genesis_test.go +++ b/genesis/genesis_test.go @@ -10,7 +10,6 @@ import ( "github.com/pactus-project/pactus/genesis" "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/testsuite" @@ -26,7 +25,7 @@ func TestMarshaling(t *testing.T) { val, _ := ts.GenerateTestValidator(0) gen1 := genesis.MakeGenesis(util.RoundNow(10), map[crypto.Address]*account.Account{prv: acc}, - []*validator.Validator{val}, param.DefaultGenParams()) + []*validator.Validator{val}, genesis.DefaultGenParams()) gen2 := new(genesis.Genesis) assert.Equal(t, 10, gen1.Params().BlockIntervalInSecond) @@ -95,7 +94,7 @@ func TestCheckGenesisAccountAndValidator(t *testing.T) { accs[pub.AccountAddress()] = acc vals = append(vals, val) } - gen := genesis.MakeGenesis(time.Now(), accs, vals, param.DefaultGenParams()) + gen := genesis.MakeGenesis(time.Now(), accs, vals, genesis.DefaultGenParams()) for addr, acc := range gen.Accounts() { assert.Equal(t, accs[addr], acc) diff --git a/node/node_test.go b/node/node_test.go index e183aa3f5..05584f9f7 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -10,7 +10,6 @@ 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" @@ -30,7 +29,7 @@ func TestRunningNode(t *testing.T) { val := validator.NewValidator(pub, 0) gen := genesis.MakeGenesis(time.Now(), map[crypto.Address]*account.Account{crypto.TreasuryAddress: acc}, - []*validator.Validator{val}, param.DefaultGenParams()) + []*validator.Validator{val}, genesis.DefaultGenParams()) conf := config.DefaultConfigMainnet() conf.GRPC.Enable = true conf.GRPC.Listen = "0.0.0.0:0" diff --git a/state/state_test.go b/state/state_test.go index 30bd88170..8bffa1cda 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -13,7 +13,6 @@ 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" @@ -55,7 +54,7 @@ func setup(t *testing.T) *testData { genTime := util.RoundNow(10).Add(-8640 * time.Second) - params := param.DefaultGenParams() + params := genesis.DefaultGenParams() params.CommitteeSize = 7 params.BondInterval = 10 diff --git a/sync/firewall/firewall_test.go b/sync/firewall/firewall_test.go index 4c25a7e55..6e2a53697 100644 --- a/sync/firewall/firewall_test.go +++ b/sync/firewall/firewall_test.go @@ -354,7 +354,7 @@ func TestNetworkFlagsTestnet(t *testing.T) { func TestNetworkFlagsLocalnet(t *testing.T) { td := setup(t, nil) - td.state.TestParams.BlockVersion = 0x3f // changing genesis hash + td.state.Genesis().Params().BlockVersion = 0x3f // changing genesis hash bdl := bundle.NewBundle(message.NewQueryVoteMessage(td.RandHeight(), td.RandRound(), td.RandValAddress())) bdl.Flags = util.SetFlag(bdl.Flags, bundle.BundleFlagNetworkTestnet) diff --git a/tests/main_test.go b/tests/main_test.go index 0db31f44e..6e659669d 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -16,7 +16,6 @@ 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" @@ -119,7 +118,7 @@ func TestMain(m *testing.M) { vals[1] = validator.NewValidator(tValKeys[tNodeIdx2][0].PublicKey(), 1) vals[2] = validator.NewValidator(tValKeys[tNodeIdx3][0].PublicKey(), 2) vals[3] = validator.NewValidator(tValKeys[tNodeIdx4][0].PublicKey(), 3) - params := param.DefaultGenParams() + params := genesis.DefaultGenParams() params.MinimumStake = 1000 params.BlockIntervalInSecond = 2 params.BondInterval = 8 From 3bb1f5efcc1f4fda7152aa47d9be24e37b3b75ee Mon Sep 17 00:00:00 2001 From: kehiy Date: Wed, 7 Aug 2024 18:57:49 +0330 Subject: [PATCH 3/5] fix(genesis,params): making genesis params private --- genesis/genesis.go | 6 +++--- genesis/genesis_param.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/genesis/genesis.go b/genesis/genesis.go index 4be991501..3d6cdac32 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -56,7 +56,7 @@ type Genesis struct { type genesisData struct { GenesisTime time.Time `cbor:"1,keyasint" json:"genesis_time"` - Params *GenParams `cbor:"2,keyasint" json:"params"` + Params *genParams `cbor:"2,keyasint" json:"params"` Accounts []genAccount `cbor:"3,keyasint" json:"accounts"` Validators []genValidator `cbor:"4,keyasint" json:"validators"` } @@ -71,7 +71,7 @@ func (gen *Genesis) GenesisTime() time.Time { return gen.data.GenesisTime } -func (gen *Genesis) Params() *GenParams { +func (gen *Genesis) Params() *genParams { return gen.data.Params } @@ -123,7 +123,7 @@ func makeGenesisValidator(val *validator.Validator) genValidator { } func MakeGenesis(genesisTime time.Time, accounts map[crypto.Address]*account.Account, - validators []*validator.Validator, params *GenParams, + validators []*validator.Validator, params *genParams, ) *Genesis { genAccs := make([]genAccount, len(accounts)) for addr, acc := range accounts { diff --git a/genesis/genesis_param.go b/genesis/genesis_param.go index 620038f10..1ccd64203 100644 --- a/genesis/genesis_param.go +++ b/genesis/genesis_param.go @@ -6,7 +6,7 @@ import ( "github.com/pactus-project/pactus/types/amount" ) -type GenParams struct { +type genParams struct { BlockVersion uint8 `cbor:"1,keyasint" json:"block_version"` BlockIntervalInSecond int `cbor:"2,keyasint" json:"block_interval_in_second"` CommitteeSize int `cbor:"3,keyasint" json:"committee_size"` @@ -22,8 +22,8 @@ type GenParams struct { MaximumStake amount.Amount `cbor:"13,keyasint" json:"maximum_stake"` } -func DefaultGenParams() *GenParams { - return &GenParams{ +func DefaultGenParams() *genParams { + return &genParams{ BlockVersion: 1, BlockIntervalInSecond: 10, CommitteeSize: 51, @@ -40,6 +40,6 @@ func DefaultGenParams() *GenParams { } } -func (p *GenParams) BlockInterval() time.Duration { +func (p *genParams) BlockInterval() time.Duration { return time.Duration(p.BlockIntervalInSecond) * time.Second } From 83fe68bd3163afeafdf8c02c3ad36f85b902e817 Mon Sep 17 00:00:00 2001 From: kehiy Date: Sat, 10 Aug 2024 12:15:33 +0330 Subject: [PATCH 4/5] refactor: removing json and cbor tags from chain params --- genesis/genesis.go | 6 +++--- genesis/genesis_param.go | 20 ++++++++++---------- types/param/param.go | 22 +++++++++++----------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/genesis/genesis.go b/genesis/genesis.go index 3d6cdac32..4be991501 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -56,7 +56,7 @@ type Genesis struct { type genesisData struct { GenesisTime time.Time `cbor:"1,keyasint" json:"genesis_time"` - Params *genParams `cbor:"2,keyasint" json:"params"` + Params *GenParams `cbor:"2,keyasint" json:"params"` Accounts []genAccount `cbor:"3,keyasint" json:"accounts"` Validators []genValidator `cbor:"4,keyasint" json:"validators"` } @@ -71,7 +71,7 @@ func (gen *Genesis) GenesisTime() time.Time { return gen.data.GenesisTime } -func (gen *Genesis) Params() *genParams { +func (gen *Genesis) Params() *GenParams { return gen.data.Params } @@ -123,7 +123,7 @@ func makeGenesisValidator(val *validator.Validator) genValidator { } func MakeGenesis(genesisTime time.Time, accounts map[crypto.Address]*account.Account, - validators []*validator.Validator, params *genParams, + validators []*validator.Validator, params *GenParams, ) *Genesis { genAccs := make([]genAccount, len(accounts)) for addr, acc := range accounts { diff --git a/genesis/genesis_param.go b/genesis/genesis_param.go index 1ccd64203..baf43d6f0 100644 --- a/genesis/genesis_param.go +++ b/genesis/genesis_param.go @@ -6,7 +6,7 @@ import ( "github.com/pactus-project/pactus/types/amount" ) -type genParams struct { +type GenParams struct { BlockVersion uint8 `cbor:"1,keyasint" json:"block_version"` BlockIntervalInSecond int `cbor:"2,keyasint" json:"block_interval_in_second"` CommitteeSize int `cbor:"3,keyasint" json:"committee_size"` @@ -15,15 +15,15 @@ type genParams struct { BondInterval uint32 `cbor:"6,keyasint" json:"bond_interval"` UnbondInterval uint32 `cbor:"7,keyasint" json:"unbond_interval"` SortitionInterval uint32 `cbor:"8,keyasint" json:"sortition_interval"` - FeeFraction float64 `cbor:"9,keyasint" json:"fee_fraction"` // Deprecated: Replaced by fix fee - MinimumFee amount.Amount `cbor:"10,keyasint" json:"minimum_fee"` // Deprecated: Replaced by fix fee - MaximumFee amount.Amount `cbor:"11,keyasint" json:"maximum_fee"` // Deprecated: Replaced by fix fee + FeeFraction float64 `cbor:"9,keyasint" json:"fee_fraction"` // Deprecated + MinimumFee amount.Amount `cbor:"10,keyasint" json:"minimum_fee"` // Deprecated + MaximumFee amount.Amount `cbor:"11,keyasint" json:"maximum_fee"` // Deprecated MinimumStake amount.Amount `cobr:"12,keyasint" json:"minimum_stake"` MaximumStake amount.Amount `cbor:"13,keyasint" json:"maximum_stake"` } -func DefaultGenParams() *genParams { - return &genParams{ +func DefaultGenParams() *GenParams { + return &GenParams{ BlockVersion: 1, BlockIntervalInSecond: 10, CommitteeSize: 51, @@ -32,14 +32,14 @@ func DefaultGenParams() *genParams { BondInterval: 360, // one hour UnbondInterval: 181440, // 21 days SortitionInterval: 17, - FeeFraction: 0.0001, - MinimumFee: 1000, - MaximumFee: 1000000, + FeeFraction: 0.0, + MinimumFee: 0, + MaximumFee: 0, MinimumStake: 1000000000, MaximumStake: 1000000000000, } } -func (p *genParams) BlockInterval() time.Duration { +func (p *GenParams) BlockInterval() time.Duration { return time.Duration(p.BlockIntervalInSecond) * time.Second } diff --git a/types/param/param.go b/types/param/param.go index fe14a31cb..0aff0c0c6 100644 --- a/types/param/param.go +++ b/types/param/param.go @@ -7,17 +7,17 @@ import ( ) type Params struct { - BlockVersion uint8 `cbor:"1,keyasint" json:"block_version"` - BlockIntervalInSecond int `cbor:"2,keyasint" json:"block_interval_in_second"` - CommitteeSize int `cbor:"3,keyasint" json:"committee_size"` - BlockReward amount.Amount `cbor:"4,keyasint" json:"block_reward"` - TransactionToLiveInterval uint32 `cbor:"5,keyasint" json:"transaction_to_live_interval"` - BondInterval uint32 `cbor:"6,keyasint" json:"bond_interval"` - UnbondInterval uint32 `cbor:"7,keyasint" json:"unbond_interval"` - SortitionInterval uint32 `cbor:"8,keyasint" json:"sortition_interval"` - MaxTransactionsPerBlock int `cbor:"9,keyasint" json:"max_transaction_per_block"` - MinimumStake amount.Amount `cobr:"10,keyasint" json:"minimum_stake"` - MaximumStake amount.Amount `cbor:"11,keyasint" json:"maximum_stake"` + BlockVersion uint8 + BlockIntervalInSecond int + CommitteeSize int + BlockReward amount.Amount + TransactionToLiveInterval uint32 + BondInterval uint32 + UnbondInterval uint32 + SortitionInterval uint32 + MaxTransactionsPerBlock int + MinimumStake amount.Amount + MaximumStake amount.Amount } func DefaultParams() *Params { From c2f553328584c723086079f7c816d37c5be426d0 Mon Sep 17 00:00:00 2001 From: kehiy Date: Sat, 10 Aug 2024 15:26:48 +0330 Subject: [PATCH 5/5] 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(),