Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(genesis): separate genesis param and chain param #1458

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,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 Expand Up @@ -440,7 +440,7 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis {
}

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

Expand Down
12 changes: 6 additions & 6 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,24 @@ 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.DefaultGenParams()
params.CommitteeSize = 4

// To prevent triggering timers before starting the tests and
// avoid double entries for new heights in some tests.
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 @@ -412,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
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 *GenParams `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() *GenParams {
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 *GenParams,
) *Genesis {
genAccs := make([]genAccount, len(accounts))
for addr, acc := range accounts {
Expand Down
45 changes: 45 additions & 0 deletions genesis/genesis_param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package genesis

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
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{
BlockVersion: 1,
BlockIntervalInSecond: 10,
CommitteeSize: 51,
BlockReward: 1000000000,
TransactionToLiveInterval: 8640, // one day
BondInterval: 360, // one hour
UnbondInterval: 181440, // 21 days
SortitionInterval: 17,
FeeFraction: 0.0,
MinimumFee: 0,
MaximumFee: 0,
MinimumStake: 1000000000,
MaximumStake: 1000000000000,
}
}

func (p *GenParams) 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.DefaultGenParams())
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.DefaultGenParams())

for addr, acc := range gen.Accounts() {
assert.Equal(t, accs[addr], acc)
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you pass params when you can get it from genesis?

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
4 changes: 2 additions & 2 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}, genesis.DefaultGenParams())
conf := config.DefaultConfigMainnet()
conf.GRPC.Enable = true
conf.GRPC.Listen = "0.0.0.0:0"
Expand All @@ -49,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
2 changes: 1 addition & 1 deletion state/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func MockingState(ts *testsuite.TestSuite) *MockState {
TestPool: txpool.MockingTxPool(),
TestCommittee: cmt,
TestValKeys: valKeys,
TestParams: genDoc.Params(),
TestParams: param.DefaultParams(),
}
}

Expand Down
7 changes: 3 additions & 4 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import (
"github.com/pactus-project/pactus/www/nanomsg/event"
)

var maxTransactionsPerBlock = 1000

type state struct {
lk sync.RWMutex

Expand All @@ -57,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 @@ -65,7 +64,7 @@ func LoadOrNewState(
valKeys: valKeys,
genDoc: genDoc,
txPool: txPool,
params: genDoc.Params(),
params: chainParam,
store: str,
lastInfo: lastinfo.NewLastInfo(),
accountMerkle: persistentmerkle.New(),
Expand Down Expand Up @@ -323,7 +322,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
20 changes: 11 additions & 9 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func setup(t *testing.T) *testData {

genTime := util.RoundNow(10).Add(-8640 * time.Second)

params := param.DefaultParams()
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 @@ -70,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 @@ -538,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 Expand Up @@ -585,10 +587,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)
Expand All @@ -598,7 +600,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) {
Expand Down
2 changes: 1 addition & 1 deletion sync/firewall/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 12 additions & 8 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,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,17 +121,19 @@ 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.MinimumStake = 1000
params := genesis.DefaultGenParams()
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
Loading
Loading