Skip to content

Commit

Permalink
Merge branch 'testnet-update-new-pos-contracts' into 'master'
Browse files Browse the repository at this point in the history
testnet: add new improvement for updating new pos contracts

See merge request bbt-blockchain/bitkub-chain/bkc!41
  • Loading branch information
kongrath committed Aug 2, 2023
2 parents 91df6fa + 518d52d commit 8ee5455
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
7 changes: 4 additions & 3 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ var (
func (c *Clique) isToSystemContract(to common.Address, snap *Snapshot) bool {
// Map system contracts
systemContracts := map[common.Address]bool{
c.config.Clique.ValidatorContract: true,
snap.SystemContracts.StakeManager: true,
snap.SystemContracts.SlashManager: true,
c.config.Clique.ValidatorContractV2: true,
c.config.Clique.ValidatorContract: true,
snap.SystemContracts.StakeManager: true,
snap.SystemContracts.SlashManager: true,
}
return systemContracts[to]
}
Expand Down
18 changes: 14 additions & 4 deletions consensus/clique/contract/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (cc *ContractClient) GetCurrentSpan(ctx context.Context, header *types.Head
}

msgData := (hexutil.Bytes)(data)
toAddress := cc.config.Clique.ValidatorContract
toAddress := cc.getValidatorContract(header.Number)
gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2))
result, err := cc.ethAPI.Call(ctx, ethapi.TransactionArgs{
Gas: &gas,
Expand Down Expand Up @@ -147,8 +147,9 @@ func (cc *ContractClient) CommitSpan(val common.Address, state *state.StateDB, h
log.Error("Unable to pack tx for commitspan", "error", err)
return err
}
validatorContract := cc.getValidatorContract(header.Number)
// get system message
msg := getSystemMessage(header.Coinbase, cc.config.Clique.ValidatorContract, data, common.Big0)
msg := getSystemMessage(header.Coinbase, validatorContract, data, common.Big0)
// apply message
return cc.applyTransaction(msg, state, header, chain, txs, receipts, receivedTxs, usedGas, mining)
}
Expand Down Expand Up @@ -210,9 +211,10 @@ func (cc *ContractClient) GetCurrentValidators(headerHash common.Hash, blockNumb
log.Error("Unable to pack tx for getValidators", "error", err)
return nil, nil, err
}

// call
msgData := (hexutil.Bytes)(data)
toAddress := cc.config.Clique.ValidatorContract
toAddress := cc.getValidatorContract(blockNumber)
gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2))
result, err := cc.ethAPI.Call(ctx, ethapi.TransactionArgs{
Gas: &gas,
Expand Down Expand Up @@ -273,7 +275,7 @@ func (cc *ContractClient) GetEligibleValidators(headerHash common.Hash, blockNum

// call
msgData := (hexutil.Bytes)(data)
toAddress := cc.config.Clique.ValidatorContract
toAddress := cc.getValidatorContract(big.NewInt(int64(blockNumber)))
gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2))
result, err := cc.ethAPI.Call(ctx, ethapi.TransactionArgs{
Gas: &gas,
Expand Down Expand Up @@ -303,6 +305,14 @@ func (cc *ContractClient) GetEligibleValidators(headerHash common.Hash, blockNum
return valz, nil
}

func (cc *ContractClient) getValidatorContract(number *big.Int) common.Address {
validatorContract := cc.config.Clique.ValidatorContract
if cc.config.ChaophrayaBangkokBlock != nil && cc.config.IsChaophrayaBangkok(number) {
validatorContract = cc.config.Clique.ValidatorContractV2
}
return validatorContract
}

// Transaction handler functions vvv

// get system message
Expand Down
46 changes: 28 additions & 18 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,16 @@ var (
//
// This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields.
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, new(EthashConfig), nil}
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, new(EthashConfig), nil}

// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers into the Clique consensus.
//
// This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields.
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}

TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil}
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, new(EthashConfig), nil}
TestRules = TestChainConfig.Rules(new(big.Int), false)
)

Expand Down Expand Up @@ -343,17 +343,18 @@ type ChainConfig struct {
EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block

ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
PetersburgBlock *big.Int `json:"petersburgBlock,omitempty"` // Petersburg switch block (nil = same as Constantinople)
IstanbulBlock *big.Int `json:"istanbulBlock,omitempty"` // Istanbul switch block (nil = no fork, 0 = already on istanbul)
ErawanBlock *big.Int `json:"erawanBlock,omitempty"` // IsErawan switch block (nil = no fork, 0 = already on Erawan)
ChaophrayaBlock *big.Int `json:"chaophrayaBlock,omitempty"` // IsChaophraya switch block (nil = no fork, 0 = already on Chaophraya)
MuirGlacierBlock *big.Int `json:"muirGlacierBlock,omitempty"` // Eip-2384 (bomb delay) switch block (nil = no fork, 0 = already activated)
BerlinBlock *big.Int `json:"berlinBlock,omitempty"` // Berlin switch block (nil = no fork, 0 = already on berlin)
LondonBlock *big.Int `json:"londonBlock,omitempty"` // London switch block (nil = no fork, 0 = already on london)
ArrowGlacierBlock *big.Int `json:"arrowGlacierBlock,omitempty"` // Eip-4345 (bomb delay) switch block (nil = no fork, 0 = already activated)
MergeForkBlock *big.Int `json:"mergeForkBlock,omitempty"` // EIP-3675 (TheMerge) switch block (nil = no fork, 0 = already in merge proceedings)
ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
PetersburgBlock *big.Int `json:"petersburgBlock,omitempty"` // Petersburg switch block (nil = same as Constantinople)
IstanbulBlock *big.Int `json:"istanbulBlock,omitempty"` // Istanbul switch block (nil = no fork, 0 = already on istanbul)
ErawanBlock *big.Int `json:"erawanBlock,omitempty"` // IsErawan switch block (nil = no fork, 0 = already on Erawan)
ChaophrayaBlock *big.Int `json:"chaophrayaBlock,omitempty"` // IsChaophraya switch block (nil = no fork, 0 = already on Chaophraya)
ChaophrayaBangkokBlock *big.Int `json:"chaophrayaBangkokBlock,omitempty"` // IsChaophraya Testnet switch block (nil = no fork, 0 = already on Chaophraya Testnet)
MuirGlacierBlock *big.Int `json:"muirGlacierBlock,omitempty"` // Eip-2384 (bomb delay) switch block (nil = no fork, 0 = already activated)
BerlinBlock *big.Int `json:"berlinBlock,omitempty"` // Berlin switch block (nil = no fork, 0 = already on berlin)
LondonBlock *big.Int `json:"londonBlock,omitempty"` // London switch block (nil = no fork, 0 = already on london)
ArrowGlacierBlock *big.Int `json:"arrowGlacierBlock,omitempty"` // Eip-4345 (bomb delay) switch block (nil = no fork, 0 = already activated)
MergeForkBlock *big.Int `json:"mergeForkBlock,omitempty"` // EIP-3675 (TheMerge) switch block (nil = no fork, 0 = already in merge proceedings)

// TerminalTotalDifficulty is the amount of total difficulty reached by
// the network that triggers the consensus upgrade.
Expand All @@ -374,10 +375,11 @@ func (c *EthashConfig) String() string {

// CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
type CliqueConfig struct {
Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
Span uint64 `json:"span"` // Size of each span in block
ValidatorContract common.Address `json:"validatorContract"`
Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
Span uint64 `json:"span"` // Size of each span in block
ValidatorContract common.Address `json:"validatorContract"`
ValidatorContractV2 common.Address `json:"validatorContractV2"`
}

// String implements the stringer interface, returning the consensus engine details.
Expand Down Expand Up @@ -491,6 +493,11 @@ func (c *ChainConfig) IsChaophraya(num *big.Int) bool {
return isForked(c.ChaophrayaBlock, num)
}

// IsChaophrayaBangkok
func (c *ChainConfig) IsChaophrayaBangkok(num *big.Int) bool {
return isForked(c.ChaophrayaBangkokBlock, num)
}

// IsArrowGlacier returns whether num is either equal to the Arrow Glacier (EIP-4345) fork block or greater.
func (c *ChainConfig) IsArrowGlacier(num *big.Int) bool {
return isForked(c.ArrowGlacierBlock, num)
Expand Down Expand Up @@ -614,6 +621,9 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *Confi
if isForkIncompatible(c.ChaophrayaBlock, newcfg.ChaophrayaBlock, head) {
return newCompatError("ChaophrayaBlock fork block", c.ChaophrayaBlock, newcfg.ChaophrayaBlock)
}
if isForkIncompatible(c.ChaophrayaBangkokBlock, newcfg.ChaophrayaBangkokBlock, head) {
return newCompatError("ChaophrayaBangkokBlock fork block", c.ChaophrayaBangkokBlock, newcfg.ChaophrayaBangkokBlock)
}
if isForkIncompatible(c.MuirGlacierBlock, newcfg.MuirGlacierBlock, head) {
return newCompatError("Muir Glacier fork block", c.MuirGlacierBlock, newcfg.MuirGlacierBlock)
}
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
VersionMajor = 2 // Major version component of the current release
VersionMinor = 0 // Minor version component of the current release
VersionMinor = 1 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionMeta = "stable" // Version metadata to append to the version string
)
Expand Down

0 comments on commit 8ee5455

Please sign in to comment.