Skip to content

Commit

Permalink
feat(genesis): separating chain param from genesis param (#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Aug 12, 2024
1 parent aee4482 commit 9991001
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 55 deletions.
3 changes: 1 addition & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -440,7 +439,7 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis {
}

// create genesis
params := param.DefaultParams()
params := genesis.DefaultGenesisParams()
params.BlockVersion = 0
gen := genesis.MakeGenesis(util.RoundNow(60), accs, vals, params)

Expand Down
3 changes: 1 addition & 2 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.DefaultParams()
params := genesis.DefaultGenesisParams()
params.CommitteeSize = 4

// To prevent triggering timers before starting the tests and
Expand Down
7 changes: 3 additions & 4 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -57,7 +56,7 @@ type Genesis struct {

type genesisData struct {
GenesisTime time.Time `cbor:"1,keyasint" json:"genesis_time"`
Params *param.Params `cbor:"2,keyasint" json:"params"`
Params *GenesisParams `cbor:"2,keyasint" json:"params"`
Accounts []genAccount `cbor:"3,keyasint" json:"accounts"`
Validators []genValidator `cbor:"4,keyasint" json:"validators"`
}
Expand All @@ -72,7 +71,7 @@ func (gen *Genesis) GenesisTime() time.Time {
return gen.data.GenesisTime
}

func (gen *Genesis) Params() *param.Params {
func (gen *Genesis) Params() *GenesisParams {
return gen.data.Params
}

Expand Down Expand Up @@ -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.Params,
validators []*validator.Validator, params *GenesisParams,
) *Genesis {
genAccs := make([]genAccount, len(accounts))
for addr, acc := range accounts {
Expand Down
18 changes: 10 additions & 8 deletions types/param/param.go → genesis/genesis_params.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package param
package genesis

import (
"time"

"github.com/pactus-project/pactus/types/amount"
)

type Params struct {
type GenesisParams 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"`
Expand All @@ -22,8 +22,8 @@ type Params struct {
MaximumStake amount.Amount `cbor:"13,keyasint" json:"maximum_stake"`
}

func DefaultParams() *Params {
return &Params{
func DefaultGenesisParams() *GenesisParams {
return &GenesisParams{
BlockVersion: 1,
BlockIntervalInSecond: 10,
CommitteeSize: 51,
Expand All @@ -32,14 +32,16 @@ func DefaultParams() *Params {
BondInterval: 360, // one hour
UnbondInterval: 181440, // 21 days
SortitionInterval: 17,
FeeFraction: 0.0001,
MinimumFee: 1000,
MaximumFee: 1000000,
MinimumStake: 1000000000,
MaximumStake: 1000000000000,

// Deprecated: Replaced by fix fee
FeeFraction: 0.0,
MinimumFee: 0,
MaximumFee: 0,
}
}

func (p *Params) BlockInterval() time.Duration {
func (p *GenesisParams) BlockInterval() time.Duration {
return time.Duration(p.BlockIntervalInSecond) * time.Second
}
5 changes: 2 additions & 3 deletions genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.DefaultParams())
[]*validator.Validator{val}, genesis.DefaultGenesisParams())
gen2 := new(genesis.Genesis)

assert.Equal(t, 10, gen1.Params().BlockIntervalInSecond)
Expand Down Expand Up @@ -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.DefaultParams())
gen := genesis.MakeGenesis(time.Now(), accs, vals, genesis.DefaultGenesisParams())

for addr, acc := range gen.Accounts() {
assert.Equal(t, accs[addr], acc)
Expand Down
2 changes: 1 addition & 1 deletion genesis/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
"public_key": "tpublic1pjy7z27zm6795xhj8rln65cve88dtqjkg7gca6scxzwwfxpncnmkhg35n2rw3fa9e04cqcc8c9fyqvpm7y39wj0x2e5qrl0he60ms6v79vga3le2uvgn566p7x9exjnyhh3klycqddstj7339afnm73k09v8k52uy"
}
]
}
}
3 changes: 1 addition & 2 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.DefaultParams())
[]*validator.Validator{val}, genesis.DefaultGenesisParams())
conf := config.DefaultConfigMainnet()
conf.GRPC.Enable = true
conf.GRPC.Listen = "0.0.0.0:0"
Expand Down
2 changes: 1 addition & 1 deletion sandbox/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/sortition"
"github.com/pactus-project/pactus/state/param"
"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/tx"
"github.com/pactus-project/pactus/types/validator"
)
Expand Down
5 changes: 3 additions & 2 deletions sandbox/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"github.com/pactus-project/pactus/committee"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/sortition"
"github.com/pactus-project/pactus/state/param"
"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/tx"
"github.com/pactus-project/pactus/types/validator"
"github.com/pactus-project/pactus/util/testsuite"
Expand All @@ -34,7 +35,7 @@ func MockingSandbox(ts *testsuite.TestSuite) *MockSandbox {

sb := &MockSandbox{
ts: ts,
TestParams: param.DefaultParams(),
TestParams: param.FromGenesis(genesis.DefaultGenesisParams()),
TestStore: store.MockingStore(ts),
TestCommittee: cmt,
TestJoinedValidators: make(map[crypto.Address]bool),
Expand Down
2 changes: 1 addition & 1 deletion sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/sortition"
"github.com/pactus-project/pactus/state/param"
"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/tx"
"github.com/pactus-project/pactus/types/validator"
"github.com/pactus-project/pactus/util/logger"
Expand Down
9 changes: 5 additions & 4 deletions sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/sortition"
"github.com/pactus-project/pactus/state/param"
"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/testsuite"
"github.com/stretchr/testify/assert"
Expand All @@ -28,7 +29,7 @@ func setup(t *testing.T) *testData {

ts := testsuite.NewTestSuite(t)
mockStore := store.MockingStore(ts)
params := param.DefaultParams()
params := genesis.DefaultGenesisParams()
params.TransactionToLiveInterval = 64

cmt, valKeys := ts.GenerateTestCommittee(21)
Expand All @@ -54,9 +55,9 @@ func setup(t *testing.T) *testData {
mockStore.SaveBlock(blk, cert)
}
sandbox := NewSandbox(mockStore.LastHeight,
mockStore, params, cmt, totalPower).(*sandbox)
mockStore, param.FromGenesis(params), cmt, totalPower).(*sandbox)
assert.Equal(t, lastHeight, sandbox.CurrentHeight())
assert.Equal(t, params, sandbox.Params())
assert.Equal(t, param.FromGenesis(params), sandbox.Params())

return &testData{
TestSuite: ts,
Expand Down
2 changes: 1 addition & 1 deletion state/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/state/param"
"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/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
4 changes: 2 additions & 2 deletions state/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/state/param"
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/txpool"
"github.com/pactus-project/pactus/types/account"
"github.com/pactus-project/pactus/types/amount"
"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 @@ -51,7 +51,7 @@ func MockingState(ts *testsuite.TestSuite) *MockState {
TestPool: txpool.MockingTxPool(),
TestCommittee: cmt,
TestValKeys: valKeys,
TestParams: genDoc.Params(),
TestParams: param.FromGenesis(genDoc.Params()),
}
}

Expand Down
45 changes: 45 additions & 0 deletions state/param/param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package param

import (
"time"

"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/types/amount"
)

type Params struct {
BlockVersion uint8
BlockIntervalInSecond int
MaxTransactionsPerBlock int
CommitteeSize int
BlockReward amount.Amount
TransactionToLiveInterval uint32
BondInterval uint32
UnbondInterval uint32
SortitionInterval uint32
MinimumStake amount.Amount
MaximumStake amount.Amount
}

func FromGenesis(genDoc *genesis.GenesisParams) *Params {
return &Params{
// genesis parameters
BlockVersion: genDoc.BlockVersion,
BlockIntervalInSecond: genDoc.BlockIntervalInSecond,
CommitteeSize: genDoc.CommitteeSize,
BlockReward: genDoc.BlockReward,
TransactionToLiveInterval: genDoc.TransactionToLiveInterval,
BondInterval: genDoc.BondInterval,
UnbondInterval: genDoc.UnbondInterval,
SortitionInterval: genDoc.SortitionInterval,
MaximumStake: genDoc.MaximumStake,
MinimumStake: genDoc.MinimumStake,

// chain parameters
MaxTransactionsPerBlock: 1000,
}
}

func (p *Params) BlockInterval() time.Duration {
return time.Duration(p.BlockIntervalInSecond) * time.Second
}
8 changes: 3 additions & 5 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
"github.com/pactus-project/pactus/sandbox"
"github.com/pactus-project/pactus/sortition"
"github.com/pactus-project/pactus/state/lastinfo"
"github.com/pactus-project/pactus/state/param"
"github.com/pactus-project/pactus/state/score"
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/txpool"
"github.com/pactus-project/pactus/types/account"
"github.com/pactus-project/pactus/types/amount"
"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 All @@ -35,8 +35,6 @@ import (
"github.com/pactus-project/pactus/www/nanomsg/event"
)

var maxTransactionsPerBlock = 1000

type state struct {
lk sync.RWMutex

Expand Down Expand Up @@ -65,7 +63,7 @@ func LoadOrNewState(
valKeys: valKeys,
genDoc: genDoc,
txPool: txPool,
params: genDoc.Params(),
params: param.FromGenesis(genDoc.Params()),
store: str,
lastInfo: lastinfo.NewLastInfo(),
accountMerkle: persistentmerkle.New(),
Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 9991001

Please sign in to comment.