diff --git a/consensus/forks/cancunfork.go b/consensus/forks/cancunfork.go index a5de4ed4..32894b69 100644 --- a/consensus/forks/cancunfork.go +++ b/consensus/forks/cancunfork.go @@ -1,28 +1,13 @@ package forks import ( - "github.com/Qitmeer/qng/core/protocol" "github.com/Qitmeer/qng/params" "github.com/ethereum/go-ethereum/common" - "math" "math/big" ) -const ( - // TODO:Future decision on whether to start - // Support cancun height - CancunForkEvmHeight = math.MaxInt64 -) - -func IsCancunForkHeight(height int64) bool { - if params.ActiveNetParams.Net == protocol.PrivNet { - return true - } - return height >= CancunForkEvmHeight -} - -func GetCancunForkDifficulty(height int64) *big.Int { - if IsCancunForkHeight(height) { +func GetCancunForkDifficulty(number *big.Int) *big.Int { + if params.ActiveNetParams.IsCancunFork(number) { return common.Big0 } return common.Big1 diff --git a/consensus/forks/emptyblockfork.go b/consensus/forks/emptyblockfork.go deleted file mode 100644 index ddf94ad4..00000000 --- a/consensus/forks/emptyblockfork.go +++ /dev/null @@ -1,20 +0,0 @@ -package forks - -import ( - "github.com/Qitmeer/qng/common/math" - "github.com/Qitmeer/qng/core/protocol" - "github.com/Qitmeer/qng/params" -) - -const ( - // TODO:Future decision on whether to start - // Should we abolish the consensus restriction strategy when empty blocks appear - EmptyBlockForkHeight = math.MaxInt64 -) - -func IsEmptyBlockForkHeight(mainHeight int64) bool { - if params.ActiveNetParams.Net != protocol.MainNet { - return true - } - return mainHeight >= EmptyBlockForkHeight -} diff --git a/consensus/forks/gaslimitfork.go b/consensus/forks/gaslimitfork.go deleted file mode 100644 index 9adee6bc..00000000 --- a/consensus/forks/gaslimitfork.go +++ /dev/null @@ -1,24 +0,0 @@ -package forks - -import ( - mparams "github.com/Qitmeer/qng/meerevm/params" -) - -const ( - // New gas limit for evm block - GasLimitForkEVMHeight = 606567 -) - -func IsGasLimitForkHeight(number int64, chainID int64) bool { - if chainID != mparams.QngMainnetChainConfig.ChainID.Int64() { - return false - } - return number >= GasLimitForkEVMHeight -} - -func NeedFixedGasLimit(number int64, chainID int64) bool { - if chainID != mparams.QngMainnetChainConfig.ChainID.Int64() { - return false - } - return !IsGasLimitForkHeight(number, chainID) -} diff --git a/consensus/forks/meerchangefork.go b/consensus/forks/meerchangefork.go deleted file mode 100644 index 1d7d5490..00000000 --- a/consensus/forks/meerchangefork.go +++ /dev/null @@ -1,27 +0,0 @@ -package forks - -import ( - "github.com/Qitmeer/qng/core/protocol" - "github.com/Qitmeer/qng/params" - "math" -) - -const ( - // TODO:Future decision on whether to start - // Support MeerChange system contract at evm height - MeerChangeForkEvmHeight = math.MaxInt64 -) - -func IsMeerChangeForkHeight(height int64) bool { - if params.ActiveNetParams.Net == protocol.PrivNet { - return true - } - return height >= MeerChangeForkEvmHeight -} - -func GetMeerChangeForkHeight() uint64 { - if params.ActiveNetParams.Net == protocol.PrivNet { - return 0 - } - return MeerChangeForkEvmHeight -} diff --git a/consensus/forks/meerevmfork.go b/consensus/forks/meerevmfork.go index 1951551f..a2e1200e 100644 --- a/consensus/forks/meerevmfork.go +++ b/consensus/forks/meerevmfork.go @@ -1,20 +1,12 @@ package forks import ( - "github.com/Qitmeer/qng/core/protocol" "github.com/Qitmeer/qng/core/types" "github.com/Qitmeer/qng/engine/txscript" "github.com/Qitmeer/qng/params" ) const ( - // MeerEVM is enabled and new subsidy calculation - MeerEVMForkMainHeight = 951100 - - // What main height can transfer the locked utxo in genesis to MeerEVM - // Must after MeerEVMForkMainHeight - MeerEVMUTXOUnlockMainHeight = 1200000 - // 21024000000000000 (Total)-5051813000000000 (locked genesis)-1215912000000000 (meerevm genesis) = 14756275000000000 MeerEVMForkTotalSubsidy = 14756275000000000 @@ -32,13 +24,11 @@ const ( ) func IsVaildEVMUTXOUnlockTx(tx *types.Transaction, ip *types.TxInput, mainHeight int64) bool { - if params.ActiveNetParams.Net != protocol.MainNet { - return false - } - if mainHeight < MeerEVMForkMainHeight || - mainHeight < MeerEVMUTXOUnlockMainHeight { + if !params.ActiveNetParams.IsMeerEVMFork(mainHeight) || + !params.ActiveNetParams.IsMeerUTXOFork(mainHeight) { return false } + if !types.IsCrossChainExportTx(tx) { return false } @@ -68,24 +58,3 @@ func IsMaxLockUTXOInGenesis(op *types.TxOutPoint) bool { } return false } - -func IsMeerEVMForkHeight(mainHeight int64) bool { - if params.ActiveNetParams.Net != protocol.MainNet { - return false - } - return mainHeight >= MeerEVMForkMainHeight -} - -func IsMeerEVMUTXOHeight(mainHeight int64) bool { - if params.ActiveNetParams.Net != protocol.MainNet { - return false - } - return mainHeight >= MeerEVMUTXOUnlockMainHeight -} - -func IsBeforeMeerEVMForkHeight(mainHeight int64) bool { - if params.ActiveNetParams.Net != protocol.MainNet { - return false - } - return mainHeight < MeerEVMForkMainHeight -} diff --git a/core/blockchain/subsidy.go b/core/blockchain/subsidy.go index 691e4620..1e0c220f 100644 --- a/core/blockchain/subsidy.go +++ b/core/blockchain/subsidy.go @@ -66,7 +66,7 @@ func NewSubsidyCache(blocks int64, params *params.Params) *SubsidyCache { // // Safe for concurrent access. func (s *SubsidyCache) CalcBlockSubsidy(bi *meerdag.BlueInfo) int64 { - if forks.IsMeerEVMForkHeight(bi.GetHeight()) { + if params.ActiveNetParams.IsMeerEVMFork(bi.GetHeight()) { return s.CalcBlockSubsidyByMeerEVMFork(bi) } if s.params.TargetTotalSubsidy > 0 { @@ -91,7 +91,7 @@ func (s *SubsidyCache) CalcTotalControlBlockSubsidy(bi *meerdag.BlueInfo) int64 } func (s *SubsidyCache) GetMode(height int64) string { - if forks.IsMeerEVMForkHeight(height) { + if params.ActiveNetParams.IsMeerEVMFork(height) { return "meerevmfork" } if s.params.TargetTotalSubsidy > 0 { @@ -149,7 +149,7 @@ func (s *SubsidyCache) CalcBlockSubsidyByMeerEVMFork(bi *meerdag.BlueInfo) int64 if bi.GetWeight() >= targetTotalSubsidy { return 0 } - realHeight := bi.GetHeight() - forks.MeerEVMForkMainHeight + realHeight := bi.GetHeight() - params.ActiveNetParams.MeerEVMForkBlock.Int64() iteration := uint64(realHeight) / forks.SubsidyReductionInterval blockSubsidy := s.estimateSupply(iteration, forks.MulSubsidy, forks.DivSubsidy) if bi.GetWeight()+blockSubsidy > targetTotalSubsidy { diff --git a/core/blockchain/subsidy_test.go b/core/blockchain/subsidy_test.go index ab6a3dc9..adaf7a61 100644 --- a/core/blockchain/subsidy_test.go +++ b/core/blockchain/subsidy_test.go @@ -14,7 +14,7 @@ func TestEstimateSupplyByMeerEVMFork(t *testing.T) { params.ActiveNetParams = ¶ms.MainNetParam param := params.MainNetParam.Params maxTestInterval := int64(28) - endBlockHeight := int64(forks.MeerEVMForkMainHeight + forks.SubsidyReductionInterval*maxTestInterval + 1) + endBlockHeight := int64(param.MeerEVMForkBlock.Int64() + forks.SubsidyReductionInterval*maxTestInterval + 1) bis := map[int64]*meerdag.BlueInfo{} subsidyCache := NewSubsidyCache(0, param) @@ -52,7 +52,7 @@ func TestEstimateSupplyByMeerEVMFork(t *testing.T) { blockTwoSubsidy := calcBlockSubsidy(2) baseSubsidy := int64(1000000000) - forkSubsidy := int64(forks.MeerEVMForkMainHeight * baseSubsidy) + forkSubsidy := int64(param.MeerEVMForkBlock.Int64() * baseSubsidy) type testData struct { height int64 @@ -68,7 +68,7 @@ func TestEstimateSupplyByMeerEVMFork(t *testing.T) { firstIndex := len(tests) for i := int64(0); i < maxTestInterval; i++ { - td := testData{height: forks.MeerEVMForkMainHeight + forks.SubsidyReductionInterval*i, expectSubsidy: subsidyCache.subsidyCache[uint64(i)], expectMode: "meerevmfork"} + td := testData{height: param.MeerEVMForkBlock.Int64() + forks.SubsidyReductionInterval*i, expectSubsidy: subsidyCache.subsidyCache[uint64(i)], expectMode: "meerevmfork"} if i == 0 { td.expectTotalSubsidy = forkSubsidy td.expectSubsidy = baseSubsidy diff --git a/core/blockchain/validate.go b/core/blockchain/validate.go index 50210674..bd769e13 100644 --- a/core/blockchain/validate.go +++ b/core/blockchain/validate.go @@ -1447,7 +1447,7 @@ func (b *BlockChain) IsValidTxType(tt types.TxType) bool { } ok := true if params.ActiveNetParams.Net == protocol.MainNet { - if !forks.IsMeerEVMForkHeight(int64(b.BestSnapshot().GraphState.GetMainHeight())) { + if !params.ActiveNetParams.IsMeerEVMFork(int64(b.BestSnapshot().GraphState.GetMainHeight())) { ok = false } } diff --git a/meerdag/meerdag.go b/meerdag/meerdag.go index 81930eba..4d26c627 100644 --- a/meerdag/meerdag.go +++ b/meerdag/meerdag.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/Qitmeer/qng/common/hash" "github.com/Qitmeer/qng/common/roughtime" - "github.com/Qitmeer/qng/consensus/forks" "github.com/Qitmeer/qng/consensus/model" l "github.com/Qitmeer/qng/log" "github.com/Qitmeer/qng/meerdag/anticone" @@ -927,7 +926,7 @@ func (bd *MeerDAG) checkLegality(parentsNode []IBlock) bool { // Checking the priority of block legitimacy func (bd *MeerDAG) checkPriority(parents []IBlock, b IBlockData) bool { - if forks.IsEmptyBlockForkHeight(int64(parents[0].GetHeight()) + 1) { + if params.ActiveNetParams.IsEmptyBlockFork(int64(parents[0].GetHeight()) + 1) { return true } if b.GetPriority() <= 0 { diff --git a/meerdag/tips.go b/meerdag/tips.go index 84316cbe..93790de0 100644 --- a/meerdag/tips.go +++ b/meerdag/tips.go @@ -7,8 +7,8 @@ package meerdag import ( "fmt" "github.com/Qitmeer/qng/common/hash" - "github.com/Qitmeer/qng/consensus/forks" "github.com/Qitmeer/qng/core/merkle" + "github.com/Qitmeer/qng/params" "math" ) @@ -48,7 +48,7 @@ func (bd *MeerDAG) GetValidTips(expectPriority int) []*hash.Hash { result := []*hash.Hash{tips[0].GetHash()} epNum := expectPriority - if forks.IsEmptyBlockForkHeight(int64(tips[0].GetHeight()) + 1) { + if params.ActiveNetParams.IsEmptyBlockFork(int64(tips[0].GetHeight()) + 1) { epNum = MaxPriority } for k, v := range tips { diff --git a/meerevm/amana/genesis.go b/meerevm/amana/genesis.go index e8234e7d..5bfcc8aa 100644 --- a/meerevm/amana/genesis.go +++ b/meerevm/amana/genesis.go @@ -1,7 +1,6 @@ package amana import ( - mparams "github.com/Qitmeer/qng/meerevm/params" qparams "github.com/Qitmeer/qng/params" qcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -17,7 +16,7 @@ var ( func AmanaGenesis() *core.Genesis { return &core.Genesis{ - Config: mparams.AmanaChainConfig, + Config: qparams.ActiveNetParams.AmanaConfig, Nonce: 0, Number: 0, ExtraData: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000071bc4403af41634cda7c32600a8024d54e7f64990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), diff --git a/meerevm/cmd/mkalloc.go b/meerevm/cmd/mkalloc.go index 5a0311e0..7ae32ecd 100644 --- a/meerevm/cmd/mkalloc.go +++ b/meerevm/cmd/mkalloc.go @@ -32,7 +32,7 @@ func main() { fileContent = fileHeader for _, np := range params.AllNetParams { alloc := meer.DoDecodeAlloc(np.Params, genesisJ, burnListJ) - genesis := meer.Genesis(np.Net, alloc) + genesis := meer.Genesis(np.Params, alloc) genesisHash := genesis.ToBlock().Hash() log.Printf("network = %s, genesisHash= %s\n", np.Name, genesisHash.String()) fileContent += fmt.Sprintf("\nconst %sGenesisHash = \"%s\"", np.Net.String(), genesisHash.String()) diff --git a/meerevm/meer/api.go b/meerevm/meer/api.go index 121b096c..64de0f44 100644 --- a/meerevm/meer/api.go +++ b/meerevm/meer/api.go @@ -3,9 +3,10 @@ package meer import ( "encoding/hex" "fmt" - "github.com/Qitmeer/qng/consensus/forks" "github.com/Qitmeer/qng/meerevm/meer/meerchange" + "github.com/Qitmeer/qng/params" "github.com/ethereum/go-ethereum/common" + "math" ) type MeerChainInfo struct { @@ -67,7 +68,11 @@ func (api *PublicMeerChainAPI) GetMeerChainInfo() (interface{}, error) { header := api.mc.GetCurHeader() if header != nil { - mci.Fork = fmt.Sprintf("%d/%d", header.Number.Uint64(), forks.GetMeerChangeForkHeight()) + forkNumber := int64(math.MaxInt64) + if params.ActiveNetParams.MeerChangeForkBlock != nil { + forkNumber = params.ActiveNetParams.MeerChangeForkBlock.Int64() + } + mci.Fork = fmt.Sprintf("%d/%d", header.Number.Uint64(), forkNumber) } return mi, nil } diff --git a/meerevm/meer/config.go b/meerevm/meer/config.go index 21868f1b..fb28eabf 100644 --- a/meerevm/meer/config.go +++ b/meerevm/meer/config.go @@ -7,10 +7,10 @@ import ( mcommon "github.com/Qitmeer/qng/meerevm/common" "github.com/Qitmeer/qng/meerevm/eth" mconsensus "github.com/Qitmeer/qng/meerevm/meer/consensus" - mparams "github.com/Qitmeer/qng/meerevm/params" "github.com/Qitmeer/qng/p2p/common" qparams "github.com/Qitmeer/qng/params" "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" @@ -20,6 +20,7 @@ import ( "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/params" + "math/big" "path/filepath" "time" ) @@ -118,36 +119,28 @@ func createConsensusEngine(config *params.ChainConfig, db ethdb.Database) (conse return mconsensus.New(), nil } -func ChainConfig() *params.ChainConfig { - switch qparams.ActiveNetParams.Net { - case protocol.MainNet: - return mparams.QngMainnetChainConfig - case protocol.TestNet: - return mparams.QngTestnetChainConfig - case protocol.MixNet: - return mparams.QngMixnetChainConfig - case protocol.PrivNet: - return mparams.QngPrivnetChainConfig +func Genesis(net *qparams.Params, alloc types.GenesisAlloc) *core.Genesis { + if alloc == nil { + alloc = DecodeAlloc(net) } - return nil -} - -func Genesis(net protocol.Network, alloc types.GenesisAlloc) *core.Genesis { - switch net { - case protocol.MainNet: - return QngGenesis(alloc) - case protocol.TestNet: - return QngTestnetGenesis(alloc) - case protocol.MixNet: - return QngMixnetGenesis(alloc) - case protocol.PrivNet: - return QngPrivnetGenesis(alloc) + gen := &core.Genesis{ + Config: net.MeerConfig, + Nonce: 0, + Number: 0, + ExtraData: hexutil.MustDecode("0x00"), + GasLimit: 100000000, + Difficulty: big.NewInt(0), + Alloc: alloc, + Timestamp: uint64(net.GenesisBlock.Block().Header.Timestamp.Unix()), + } + if net.Net == protocol.TestNet { + gen.GasLimit = 8000000 } - return nil + return gen } func CurrentGenesis() *core.Genesis { - return Genesis(qparams.ActiveNetParams.Net, nil) + return Genesis(qparams.ActiveNetParams.Params, nil) } func getBootstrapNodes(port int) []*enode.Node { diff --git a/meerevm/meer/consensus/consensus.go b/meerevm/meer/consensus/consensus.go index 3fa2736b..29dfd57e 100644 --- a/meerevm/meer/consensus/consensus.go +++ b/meerevm/meer/consensus/consensus.go @@ -10,6 +10,7 @@ import ( "github.com/Qitmeer/qng/consensus/forks" qtypes "github.com/Qitmeer/qng/core/types" qcommon "github.com/Qitmeer/qng/meerevm/common" + qparams "github.com/Qitmeer/qng/params" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" @@ -109,7 +110,7 @@ func (me *MeerEngine) verifyHeader(chain consensus.ChainHeaderReader, header, pa if header.BaseFee != nil { return fmt.Errorf("invalid baseFee before fork: have %d, expected 'nil'", header.BaseFee) } - if !forks.NeedFixedGasLimit(parent.Number.Int64(), chain.Config().ChainID.Int64()) { + if qparams.ActiveNetParams.IsGasLimitFork(parent.Number) { if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil { return err } @@ -158,7 +159,7 @@ func (me *MeerEngine) verifyHeader(chain consensus.ChainHeaderReader, header, pa } func (me *MeerEngine) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int { - return forks.GetCancunForkDifficulty(parent.Number.Int64()) + return forks.GetCancunForkDifficulty(parent.Number) } func (me *MeerEngine) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error { @@ -166,7 +167,7 @@ func (me *MeerEngine) Prepare(chain consensus.ChainHeaderReader, header *types.H if number > 0 { number-- } - header.Difficulty = forks.GetCancunForkDifficulty(number) + header.Difficulty = forks.GetCancunForkDifficulty(big.NewInt(number)) return nil } diff --git a/meerevm/meer/genesis.go b/meerevm/meer/genesis.go index d65daa07..88f8ef64 100644 --- a/meerevm/meer/genesis.go +++ b/meerevm/meer/genesis.go @@ -6,80 +6,14 @@ import ( "math/big" "strings" - mparams "github.com/Qitmeer/qng/meerevm/params" qparams "github.com/Qitmeer/qng/params" "github.com/ethereum/go-ethereum/common" qcommon "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" ) -func QngGenesis(alloc types.GenesisAlloc) *core.Genesis { - if alloc == nil { - alloc = DecodeAlloc(qparams.MainNetParam.Params) - } - return &core.Genesis{ - Config: mparams.QngMainnetChainConfig, - Nonce: 0, - Number: 0, - ExtraData: hexutil.MustDecode("0x00"), - GasLimit: 100000000, - Difficulty: big.NewInt(0), - Alloc: alloc, - Timestamp: uint64(qparams.MainNetParams.GenesisBlock.Block().Header.Timestamp.Unix()), - } -} - -func QngTestnetGenesis(alloc types.GenesisAlloc) *core.Genesis { - if alloc == nil { - alloc = DecodeAlloc(qparams.TestNetParam.Params) - } - return &core.Genesis{ - Config: mparams.QngTestnetChainConfig, - Nonce: 0, - Number: 0, - ExtraData: hexutil.MustDecode("0x00"), - GasLimit: 8000000, - Difficulty: big.NewInt(0), - Alloc: alloc, - Timestamp: uint64(qparams.TestNetParams.GenesisBlock.Block().Header.Timestamp.Unix()), - } -} - -func QngMixnetGenesis(alloc types.GenesisAlloc) *core.Genesis { - if alloc == nil { - alloc = DecodeAlloc(qparams.MixNetParam.Params) - } - return &core.Genesis{ - Config: mparams.QngMixnetChainConfig, - Nonce: 0, - Number: 0, - ExtraData: hexutil.MustDecode("0x00"), - GasLimit: 100000000, - Difficulty: big.NewInt(0), - Alloc: alloc, - Timestamp: uint64(qparams.MixNetParams.GenesisBlock.Block().Header.Timestamp.Unix()), - } -} - -func QngPrivnetGenesis(alloc types.GenesisAlloc) *core.Genesis { - if alloc == nil { - alloc = DecodeAlloc(qparams.PrivNetParam.Params) - } - return &core.Genesis{ - Config: mparams.QngPrivnetChainConfig, - Nonce: 0, - Number: 0, - ExtraData: hexutil.MustDecode("0x00"), - GasLimit: 100000000, - Difficulty: big.NewInt(0), - Alloc: alloc, - Timestamp: uint64(qparams.PrivNetParams.GenesisBlock.Block().Header.Timestamp.Unix()), - } -} - func DecodePrealloc(data string) types.GenesisAlloc { if len(data) <= 0 { return types.GenesisAlloc{} @@ -164,7 +98,7 @@ func DoDecodeAlloc(network *qparams.Params, genesisStr string, burnStr string) t } burnList := BuildBurnBalance(burnStr) - genesis := Genesis(network.Net, types.GenesisAlloc{}) + genesis := Genesis(network, types.GenesisAlloc{}) genesis.Alloc = ngd.Data.Genesis.Alloc releaseConAddr := common.HexToAddress(RELEASE_CONTRACT_ADDR) if releaseAccount, ok := genesis.Alloc[releaseConAddr]; ok { diff --git a/meerevm/meer/meer_test.go b/meerevm/meer/meer_test.go index 8e8dd2fb..1888a50b 100644 --- a/meerevm/meer/meer_test.go +++ b/meerevm/meer/meer_test.go @@ -8,12 +8,12 @@ import ( ) func TestGenesisHash(t *testing.T) { - assert.Equal(t, MainNetGenesisHash, Genesis(params.MainNetParams.Net, nil).ToBlock().Hash().String(), + assert.Equal(t, MainNetGenesisHash, Genesis(¶ms.MainNetParams, nil).ToBlock().Hash().String(), params.MainNetParams.Name+" genesis hash not equal latest") - assert.Equal(t, MixNetGenesisHash, Genesis(params.MixNetParams.Net, nil).ToBlock().Hash().String(), + assert.Equal(t, MixNetGenesisHash, Genesis(¶ms.MixNetParams, nil).ToBlock().Hash().String(), params.MixNetParams.Name+" genesis hash not equal latest") - assert.Equal(t, TestNetGenesisHash, Genesis(params.TestNetParams.Net, nil).ToBlock().Hash().String(), + assert.Equal(t, TestNetGenesisHash, Genesis(¶ms.TestNetParams, nil).ToBlock().Hash().String(), params.TestNetParams.Name+" genesis hash not equal latest") - assert.Equal(t, PrivNetGenesisHash, Genesis(params.PrivNetParams.Net, nil).ToBlock().Hash().String(), + assert.Equal(t, PrivNetGenesisHash, Genesis(¶ms.PrivNetParams, nil).ToBlock().Hash().String(), params.PrivNetParam.Name+" genesis hash not equal latest") } diff --git a/meerevm/meer/meerchain.go b/meerevm/meer/meerchain.go index fa090fc1..313d3a00 100644 --- a/meerevm/meer/meerchain.go +++ b/meerevm/meer/meerchain.go @@ -193,11 +193,11 @@ func (b *MeerChain) buildBlock(parent *types.Header, qtxs []model.Tx, timestamp } gaslimit := core.CalcGasLimit(parentBlock.GasLimit(), b.chain.Config().Eth.Miner.GasCeil) - if forks.NeedFixedGasLimit(parent.Number.Int64(), config.ChainID.Int64()) { + if !params.ActiveNetParams.IsGasLimitFork(parent.Number) { gaslimit = 0x10000000000000 } - header := makeHeader(&b.chain.Config().Eth, parentBlock, statedb, timestamp, gaslimit, forks.GetCancunForkDifficulty(parent.Number.Int64())) + header := makeHeader(&b.chain.Config().Eth, parentBlock, statedb, timestamp, gaslimit, forks.GetCancunForkDifficulty(parent.Number)) if config.DAOForkSupport && config.DAOForkBlock != nil && config.DAOForkBlock.Cmp(header.Number) == 0 { misc.ApplyDAOHardFork(statedb) @@ -641,7 +641,7 @@ func (b *MeerChain) checkMeerChange() error { if curBlockHeader == nil { return nil } - if !forks.IsMeerChangeForkHeight(curBlockHeader.Number.Int64()) { + if !params.ActiveNetParams.IsMeerChangeFork(curBlockHeader.Number) { return nil } if meerchange.ContractAddr != (common.Address{}) { diff --git a/meerevm/meer/meerchange/utils.go b/meerevm/meer/meerchange/utils.go index f8c18ea9..4ebc2ed2 100644 --- a/meerevm/meer/meerchange/utils.go +++ b/meerevm/meer/meerchange/utils.go @@ -1,11 +1,9 @@ package meerchange import ( - "github.com/Qitmeer/qng/core/protocol" "github.com/Qitmeer/qng/params" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - eparams "github.com/ethereum/go-ethereum/params" "math/big" ) @@ -48,17 +46,7 @@ func IsMeerChangeTx(tx *types.Transaction) bool { } func GetChainID() *big.Int { - switch params.ActiveNetParams.Net { - case protocol.MainNet: - return eparams.QngMainnetChainConfig.ChainID - case protocol.TestNet: - return eparams.QngTestnetChainConfig.ChainID - case protocol.MixNet: - return eparams.QngMixnetChainConfig.ChainID - case protocol.PrivNet: - return eparams.QngPrivnetChainConfig.ChainID - } - return nil + return params.ActiveNetParams.MeerConfig.ChainID } func EnableContractAddr() { diff --git a/meerevm/params/config.go b/meerevm/params/config.go deleted file mode 100644 index 5f0b42ab..00000000 --- a/meerevm/params/config.go +++ /dev/null @@ -1,114 +0,0 @@ -package params - -import ( - eparams "github.com/ethereum/go-ethereum/params" - "math/big" -) - -func newUint64(val uint64) *uint64 { return &val } - -var ( - // QNG - QngMainnetChainConfig = &eparams.ChainConfig{ - ChainID: eparams.QngMainnetChainConfig.ChainID, - HomesteadBlock: big.NewInt(0), - DAOForkBlock: big.NewInt(0), - DAOForkSupport: false, - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(0), - MuirGlacierBlock: big.NewInt(0), - BerlinBlock: big.NewInt(0), - LondonBlock: nil, - Ethash: new(eparams.EthashConfig), - } - - QngTestnetChainConfig = &eparams.ChainConfig{ - ChainID: eparams.QngTestnetChainConfig.ChainID, - HomesteadBlock: big.NewInt(0), - DAOForkBlock: big.NewInt(0), - DAOForkSupport: false, - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(0), - MuirGlacierBlock: big.NewInt(0), - BerlinBlock: big.NewInt(0), - LondonBlock: big.NewInt(22500), - Ethash: new(eparams.EthashConfig), - } - - QngMixnetChainConfig = &eparams.ChainConfig{ - ChainID: eparams.QngMixnetChainConfig.ChainID, - HomesteadBlock: big.NewInt(0), - DAOForkBlock: big.NewInt(0), - DAOForkSupport: false, - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(0), - MuirGlacierBlock: big.NewInt(0), - BerlinBlock: big.NewInt(0), - LondonBlock: nil, - Ethash: new(eparams.EthashConfig), - } - - QngPrivnetChainConfig = &eparams.ChainConfig{ - ChainID: eparams.QngPrivnetChainConfig.ChainID, - HomesteadBlock: big.NewInt(0), - DAOForkBlock: big.NewInt(0), - DAOForkSupport: false, - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(0), - MuirGlacierBlock: big.NewInt(0), - BerlinBlock: big.NewInt(0), - LondonBlock: big.NewInt(0), - ArrowGlacierBlock: big.NewInt(0), - GrayGlacierBlock: big.NewInt(0), - TerminalTotalDifficulty: big.NewInt(0), - TerminalTotalDifficultyPassed: true, - ShanghaiTime: newUint64(0), - CancunTime: newUint64(0), - } - - // Amana - AmanaChainConfig = &eparams.ChainConfig{ - ChainID: eparams.AmanaChainConfig.ChainID, - HomesteadBlock: big.NewInt(0), - DAOForkBlock: big.NewInt(0), - DAOForkSupport: false, - EIP150Block: big.NewInt(0), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: big.NewInt(0), - MuirGlacierBlock: big.NewInt(0), - BerlinBlock: big.NewInt(0), - LondonBlock: big.NewInt(0), - ArrowGlacierBlock: big.NewInt(0), - GrayGlacierBlock: big.NewInt(0), - ShanghaiTime: newUint64(0), - CancunTime: newUint64(0), - Clique: &eparams.CliqueConfig{ - Period: 3, - Epoch: 100, - }, - } -) diff --git a/node/api.go b/node/api.go index 5733c01f..c6397f8e 100644 --- a/node/api.go +++ b/node/api.go @@ -94,8 +94,8 @@ func (api *PublicBlockChainAPI) GetNodeInfo() (interface{}, error) { ret.ConsensusDeployment = make(map[string]*json.ConsensusDeploymentDesc) ret.ConsensusDeployment["token"] = &json.ConsensusDeploymentDesc{Status: "active"} if params.ActiveNetParams.Net == protocol.MainNet { - cdd := json.ConsensusDeploymentDesc{Status: "inactive", StartHeight: forks.MeerEVMForkMainHeight} - if forks.IsMeerEVMForkHeight(int64(best.GraphState.GetMainHeight())) { + cdd := json.ConsensusDeploymentDesc{Status: "inactive", StartHeight: params.ActiveNetParams.MeerEVMForkBlock.Int64()} + if params.ActiveNetParams.IsMeerEVMFork(int64(best.GraphState.GetMainHeight())) { cdd.Status = "active" } ret.ConsensusDeployment["meerevm"] = &cdd @@ -166,7 +166,7 @@ func (api *PublicBlockChainAPI) GetSubsidy() (interface{}, error) { info := &json.SubsidyInfo{Mode: sc.GetMode(mainHeight), TotalSubsidy: best.TotalSubsidy, BaseSubsidy: params.ActiveNetParams.BaseSubsidy} - if forks.IsMeerEVMForkHeight(mainHeight) { + if params.ActiveNetParams.IsMeerEVMFork(mainHeight) { info.TargetTotalSubsidy = forks.MeerEVMForkTotalSubsidy - binfo.GetWeight() info.LeftTotalSubsidy = info.TargetTotalSubsidy - int64(info.TotalSubsidy) if info.LeftTotalSubsidy < 0 { diff --git a/params/params.go b/params/params.go index 0d88cf24..cfeeba2d 100644 --- a/params/params.go +++ b/params/params.go @@ -8,6 +8,8 @@ package params import ( "encoding/hex" "errors" + eparams "github.com/ethereum/go-ethereum/params" + "math/big" "strings" "time" @@ -239,6 +241,18 @@ type Params struct { LedgerParams ledger.LedgerParams CoinbaseConfig CoinbaseConfigs + + // evm + MeerConfig *eparams.ChainConfig + + AmanaConfig *eparams.ChainConfig + + MeerEVMForkBlock *big.Int // MeerEVM is enabled and new subsidy calculation + MeerUTXOForkBlock *big.Int // What main height can transfer the locked utxo in genesis to MeerEVM, Must after MeerEVMForkMainHeight + EmptyBlockForkBlock *big.Int // main height + GasLimitForkBlock *big.Int // gaslimit fork meerevm block number + CancunForkBlock *big.Int // use custom diff when cancun fork + MeerChangeForkBlock *big.Int // MeerChange system contract } type CoinbaseConfig struct { @@ -294,6 +308,64 @@ func (p *Params) IsDevelopDiff() bool { return p.PowConfig.DifficultyMode == pow.DIFFICULTY_MODE_DEVELOP } +func (p *Params) IsMeerEVMFork(height int64) bool { + return isBlockForked(p.MeerEVMForkBlock, big.NewInt(height)) +} + +func (p *Params) IsMeerUTXOFork(height int64) bool { + return isBlockForked(p.MeerUTXOForkBlock, big.NewInt(height)) +} + +func (p *Params) IsEmptyBlockFork(height int64) bool { + return isBlockForked(p.EmptyBlockForkBlock, big.NewInt(height)) +} + +func (p *Params) IsGasLimitFork(number *big.Int) bool { + return isBlockForked(p.GasLimitForkBlock, number) +} + +func (p *Params) IsCancunFork(number *big.Int) bool { + return isBlockForked(p.CancunForkBlock, number) +} + +func (p *Params) IsMeerChangeFork(number *big.Int) bool { + return isBlockForked(p.MeerChangeForkBlock, number) +} + +// Rules wraps Params and is merely syntactic sugar or can be used for functions +// that do not have or require information about the block. +// +// Rules is a one time interface meaning that it shouldn't be used in between transition +// phases. +type Rules struct { + Net protocol.Network + ChainID *big.Int + IsMeerEVMFork bool + IsMeerUTXOFork bool + IsEmptyBlockFork bool + IsGasLimitFork bool + IsCancunFork bool + IsMeerChangeFork bool +} + +// Rules ensures p's ChainID is not nil. +func (p *Params) Rules(height int64, num *big.Int) Rules { + chainID := p.MeerConfig.ChainID + if chainID == nil { + chainID = new(big.Int) + } + return Rules{ + Net: p.Net, + ChainID: new(big.Int).Set(chainID), + IsMeerEVMFork: p.IsMeerEVMFork(height), + IsMeerUTXOFork: p.IsMeerUTXOFork(height), + IsEmptyBlockFork: p.IsEmptyBlockFork(height), + IsGasLimitFork: p.IsGasLimitFork(num), + IsCancunFork: p.IsCancunFork(num), + IsMeerChangeFork: p.IsMeerChangeFork(num), + } +} + var ( // ErrDuplicateNet describes an error where the parameters for a network // could not be set due to the network already being a standard @@ -371,3 +443,40 @@ func IsBech32SegwitPrefix(prefix string) bool { _, ok := bech32SegwitPrefixes[prefix] return ok } + +// isBlockForked returns whether a fork scheduled at block s is active at the +// given head block. +func isBlockForked(s, head *big.Int) bool { + if s == nil || head == nil { + return false + } + return s.Cmp(head) <= 0 +} + +func newUint64(val uint64) *uint64 { return &val } + +// Amana +var amanaChainConfig = &eparams.ChainConfig{ + ChainID: eparams.AmanaChainConfig.ChainID, + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: false, + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + ArrowGlacierBlock: big.NewInt(0), + GrayGlacierBlock: big.NewInt(0), + ShanghaiTime: newUint64(0), + CancunTime: newUint64(0), + Clique: &eparams.CliqueConfig{ + Period: 3, + Epoch: 100, + }, +} diff --git a/params/params_mainnet.go b/params/params_mainnet.go index 88e1b0b9..6236f6c6 100644 --- a/params/params_mainnet.go +++ b/params/params_mainnet.go @@ -12,6 +12,7 @@ import ( "github.com/Qitmeer/qng/core/types" "github.com/Qitmeer/qng/core/types/pow" "github.com/Qitmeer/qng/ledger" + eparams "github.com/ethereum/go-ethereum/params" "math/big" "time" ) @@ -128,4 +129,27 @@ var MainNetParams = Params{ GuardAddrPkScript: hexMustDecode("76a9143846e53e5e952b5cd6023e3ad3cfc75cb93fce0388ac"), // MmQitmeerMainNetHonorAddressXY9JH2y HonorAddrPkScript: hexMustDecode("76a9143846e53e5e952b5cd60240ad9c4cf6164dd5090988ac"), + + MeerConfig: &eparams.ChainConfig{ + ChainID: eparams.QngMainnetChainConfig.ChainID, + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: false, + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: nil, + Ethash: new(eparams.EthashConfig), + }, + MeerEVMForkBlock: big.NewInt(951100), + MeerUTXOForkBlock: big.NewInt(1200000), + GasLimitForkBlock: big.NewInt(606567), + + AmanaConfig: amanaChainConfig, } diff --git a/params/params_mixnet.go b/params/params_mixnet.go index 39f95e8c..a75299da 100644 --- a/params/params_mixnet.go +++ b/params/params_mixnet.go @@ -7,6 +7,7 @@ package params import ( + eparams "github.com/ethereum/go-ethereum/params" "math/big" "time" @@ -125,4 +126,26 @@ var MixNetParams = Params{ CoinbaseMaturity: 16, OrganizationPkScript: hexMustDecode("76a91429209320e66d96839785dd07e643a7f1592edc5a88ac"), TokenAdminPkScript: hexMustDecode("00000000c96d6d76a914b8834294977b26a44094fe2216f8a7d59af1130888ac"), + + MeerConfig: &eparams.ChainConfig{ + ChainID: eparams.QngMixnetChainConfig.ChainID, + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: false, + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: nil, + Ethash: new(eparams.EthashConfig), + }, + EmptyBlockForkBlock: big.NewInt(0), + GasLimitForkBlock: big.NewInt(0), + + AmanaConfig: amanaChainConfig, } diff --git a/params/params_privnet.go b/params/params_privnet.go index 875e3d1e..d70ac5cf 100644 --- a/params/params_privnet.go +++ b/params/params_privnet.go @@ -12,6 +12,7 @@ import ( "github.com/Qitmeer/qng/core/types" "github.com/Qitmeer/qng/core/types/pow" "github.com/Qitmeer/qng/ledger" + eparams "github.com/ethereum/go-ethereum/params" "math/big" "time" ) @@ -162,4 +163,33 @@ var PrivNetParams = Params{ TokenAdminPkScript: hexMustDecode("00000000c96d6d76a914785bfbf4ecad8b72f2582be83616c5d364a3244288ac"), CoinbaseMaturity: 16, + + MeerConfig: &eparams.ChainConfig{ + ChainID: eparams.QngPrivnetChainConfig.ChainID, + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: false, + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + ArrowGlacierBlock: big.NewInt(0), + GrayGlacierBlock: big.NewInt(0), + TerminalTotalDifficulty: big.NewInt(0), + TerminalTotalDifficultyPassed: true, + ShanghaiTime: newUint64(0), + CancunTime: newUint64(0), + }, + EmptyBlockForkBlock: big.NewInt(0), + GasLimitForkBlock: big.NewInt(0), + CancunForkBlock: big.NewInt(0), + MeerChangeForkBlock: big.NewInt(0), + + AmanaConfig: amanaChainConfig, } diff --git a/params/params_testnet.go b/params/params_testnet.go index 18510321..091bdafa 100644 --- a/params/params_testnet.go +++ b/params/params_testnet.go @@ -7,6 +7,7 @@ package params import ( + eparams "github.com/ethereum/go-ethereum/params" "math/big" "time" @@ -123,4 +124,26 @@ var TestNetParams = Params{ LegacyCoinType: 223, OrganizationPkScript: hexMustDecode("76a91429209320e66d96839785dd07e643a7f1592edc5a88ac"), TokenAdminPkScript: hexMustDecode("00000000c96d6d76a914b8834294977b26a44094fe2216f8a7d59af1130888ac"), + + MeerConfig: &eparams.ChainConfig{ + ChainID: eparams.QngTestnetChainConfig.ChainID, + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: false, + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(22500), + Ethash: new(eparams.EthashConfig), + }, + EmptyBlockForkBlock: big.NewInt(0), + GasLimitForkBlock: big.NewInt(0), + + AmanaConfig: amanaChainConfig, } diff --git a/services/acct/cltvwatcher.go b/services/acct/cltvwatcher.go index 606a50f4..fdd649b0 100644 --- a/services/acct/cltvwatcher.go +++ b/services/acct/cltvwatcher.go @@ -2,8 +2,8 @@ package acct import ( "fmt" - "github.com/Qitmeer/qng/consensus/forks" "github.com/Qitmeer/qng/engine/txscript" + "github.com/Qitmeer/qng/params" ) type CLTVWatcher struct { @@ -21,7 +21,7 @@ func (cw *CLTVWatcher) Update(am *AccountManager) error { if mainTip == nil { return fmt.Errorf("No main tip") } - if forks.IsMeerEVMUTXOHeight(int64(mainTip.GetHeight())) && cw.isForkGenUTXO { + if params.ActiveNetParams.IsMeerUTXOFork(int64(mainTip.GetHeight())) && cw.isForkGenUTXO { cw.unlocked = true return nil } diff --git a/testutils/helps.go b/testutils/helps.go index 537b5c60..aa27cefe 100644 --- a/testutils/helps.go +++ b/testutils/helps.go @@ -9,7 +9,6 @@ import ( "github.com/Qitmeer/qng/core/json" "github.com/Qitmeer/qng/core/types" "github.com/Qitmeer/qng/core/types/pow" - "github.com/Qitmeer/qng/meerevm/params" qparams "github.com/Qitmeer/qng/params" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -315,7 +314,7 @@ func createLegacyTx(node *MockNode, fromPkByte []byte, to *common.Address, nonce Data: d, } tx := etype.NewTx(data) - signedTx, err := etype.SignTx(tx, etype.NewEIP155Signer(params.QngPrivnetChainConfig.ChainID), privateKey) + signedTx, err := etype.SignTx(tx, etype.NewEIP155Signer(qparams.ActiveNetParams.MeerConfig.ChainID), privateKey) if err != nil { return "", err } @@ -335,7 +334,7 @@ func MeerTransfer(node *MockNode, fromAccountIdx int, to common.Address, val *bi } func AuthTrans(privatekeybyte []byte) (*bind.TransactOpts, error) { privateKey := crypto.ToECDSAUnsafe(privatekeybyte) - authCaller, err := bind.NewKeyedTransactorWithChainID(privateKey, params.QngPrivnetChainConfig.ChainID) + authCaller, err := bind.NewKeyedTransactorWithChainID(privateKey, qparams.ActiveNetParams.MeerConfig.ChainID) if err != nil { return nil, err }