Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 923653f

Browse files
authored
evm: add whitelist deployer v2 (#344)
* evm: add whitelist deployer v2 * statedb-utils: Add unit testing * Update Antennablock for testnet * Add params and update block in genesis * Update check compatibility and params config
1 parent 83c8375 commit 923653f

File tree

7 files changed

+283
-24
lines changed

7 files changed

+283
-24
lines changed

core/state/statedb.go

+4
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,10 @@ func (s *StateDB) ValidDeployer(addr common.Address) bool {
646646
return IsWhitelistedDeployer(s, addr)
647647
}
648648

649+
func (s *StateDB) ValidDeployerV2(addr common.Address, blockTime uint64, whiteListContract *common.Address) bool {
650+
return IsWhitelistedDeployerV2(s, addr, blockTime, whiteListContract)
651+
}
652+
649653
func (s *StateDB) Blacklisted(contractAddr *common.Address, addr *common.Address) bool {
650654
return IsAddressBlacklisted(s, contractAddr, addr)
651655
}

core/state/statedb_utils.go

+38
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ var (
2020
WHITELISTED: 1,
2121
WHITELIST_ALL: 2,
2222
}
23+
slotWhitelistDeployerMappingV2 = map[string]uint64{
24+
WHITELISTED: 53,
25+
WHITELIST_ALL: 58,
26+
} // Contract Infinity
2327
slotBlacklistContractMapping = map[string]uint64{
2428
BLACKLISTED: 1,
2529
DISABLED: 2,
@@ -32,11 +36,45 @@ var (
3236
}
3337
)
3438

39+
// IsWhitelistedDeployer reads the contract storage to check if an address is allow to deploy
40+
func IsWhitelistedDeployerV2(statedb *StateDB, address common.Address, blockTime uint64, whiteListContract *common.Address) bool {
41+
contract := *whiteListContract
42+
whitelistAllSlot := slotWhitelistDeployerMappingV2[WHITELIST_ALL]
43+
whitelistAll := statedb.GetState(contract, GetLocSimpleVariable(whitelistAllSlot))
44+
45+
if whitelistAll.Big().Cmp(common.Big1) == 0 {
46+
return true
47+
}
48+
49+
whitelistedSlot := slotWhitelistDeployerMappingV2[WHITELISTED]
50+
// WhiteListInfo have 2 fields, so we need to plus 1.
51+
// struct WhiteListInfo {
52+
// uint256 expiryTimestamp;
53+
// bool activated;
54+
// }
55+
expiredLoc := GetLocMappingAtKey(address.Hash(), whitelistedSlot)
56+
activatedLoc := common.BigToHash(expiredLoc.Big().Add(expiredLoc.Big(), common.Big1))
57+
expiredHash := statedb.GetState(contract, expiredLoc)
58+
59+
activatedHash := statedb.GetState(contract, activatedLoc)
60+
61+
// (whiteListInfo.activated && block.timestamp < whiteListInfo.expiryTimestamp)
62+
// Compare expiredHash with Blockheader timestamp.
63+
if activatedHash.Big().Cmp(common.Big1) == 0 {
64+
if expiredHash.Big().Cmp(big.NewInt(int64(blockTime))) > 0 {
65+
// Block time still is in expiredTime
66+
return true
67+
}
68+
}
69+
return false
70+
}
71+
3572
// IsWhitelistedDeployer reads the contract storage to check if an address is allow to deploy
3673
func IsWhitelistedDeployer(statedb *StateDB, address common.Address) bool {
3774
contract := common.HexToAddress(common.WhitelistDeployerSC)
3875
whitelistAllSlot := slotWhitelistDeployerMapping[WHITELIST_ALL]
3976
whitelistAll := statedb.GetState(contract, GetLocSimpleVariable(whitelistAllSlot))
77+
4078
if whitelistAll.Big().Cmp(big.NewInt(1)) == 0 {
4179
return true
4280
}

core/vm/evm.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,17 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
500500
}
501501
}
502502
}
503-
504-
if evm.chainRules.IsOdysseusFork && !evm.StateDB.ValidDeployer(caller.Address()) {
505-
captureTraceEarly(ErrExecutionReverted)
506-
return nil, common.Address{}, gas, ErrExecutionReverted
503+
// Handle latest hardfork firstly.
504+
if evm.chainRules.IsAntenna {
505+
if !evm.StateDB.ValidDeployerV2(caller.Address(), evm.Context.Time, evm.ChainConfig().WhiteListDeployerContractV2Address) {
506+
captureTraceEarly(ErrExecutionReverted)
507+
return nil, common.Address{}, gas, ErrExecutionReverted
508+
}
509+
} else if evm.chainRules.IsOdysseusFork {
510+
if !evm.StateDB.ValidDeployer(caller.Address()) {
511+
captureTraceEarly(ErrExecutionReverted)
512+
return nil, common.Address{}, gas, ErrExecutionReverted
513+
}
507514
}
508515

509516
// Depth check execution. Fail if we're trying to execute above the
@@ -517,6 +524,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
517524
return nil, common.Address{}, gas, ErrInsufficientBalance
518525
}
519526
nonce := evm.StateDB.GetNonce(caller.Address())
527+
520528
if nonce+1 < nonce {
521529
captureTraceEarly(ErrNonceUintOverflow)
522530
return nil, common.Address{}, gas, ErrNonceUintOverflow

core/vm/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type StateDB interface {
7676
ForEachStorage(common.Address, func(common.Hash, common.Hash) bool) error
7777

7878
ValidDeployer(common.Address) bool
79+
ValidDeployerV2(common.Address, uint64, *common.Address) bool
7980
Blacklisted(*common.Address, *common.Address) bool
8081
}
8182

core/vm/statedb_utils_test.go

+187
Large diffs are not rendered by default.

genesis/testnet.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
"puffyBlock": 12254000,
3030
"bubaBlock": 14260600,
3131
"olekBlock": 16849000,
32-
"shillinBlock": 20268000
32+
"shillinBlock": 20268000,
33+
"antennaBlock": 20737258,
34+
"whiteListDeployerContractV2Address": "0x50a7e07Aa75eB9C04281713224f50403cA79851F"
3335
},
3436
"alloc": {
3537
"0x0000000000000000000000000000000000000011": {

params/config.go

+38-19
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,23 @@ var (
283283
RoninTestnetStakingContractAddress = common.HexToAddress("0x9C245671791834daf3885533D24dce516B763B28")
284284
RoninTestnetProfileContractAddress = common.HexToAddress("0x3b67c8D22a91572a6AB18acC9F70787Af04A4043")
285285
RoninTestnetFinalityTrackingAddress = common.HexToAddress("0x41aCDFe786171824a037f2Cd6224c5916A58969a")
286+
RoninWhiteListDeployerContractV2Address = common.HexToAddress("0x50a7e07Aa75eB9C04281713224f50403cA79851F")
286287

287288
RoninTestnetChainConfig = &ChainConfig{
288-
ChainID: big.NewInt(2021),
289-
HomesteadBlock: big.NewInt(0),
290-
EIP150Block: big.NewInt(0),
291-
EIP155Block: big.NewInt(0),
292-
EIP158Block: big.NewInt(0),
293-
ByzantiumBlock: big.NewInt(0),
294-
ConstantinopleBlock: big.NewInt(0),
295-
PetersburgBlock: big.NewInt(0),
296-
IstanbulBlock: big.NewInt(0),
297-
OdysseusBlock: big.NewInt(3315095),
298-
FenixBlock: big.NewInt(6770400),
299-
BlacklistContractAddress: &RoninTestnetBlacklistContract,
300-
FenixValidatorContractAddress: &RoninTestnetFenixValidatorContractAddress,
289+
ChainID: big.NewInt(2021),
290+
HomesteadBlock: big.NewInt(0),
291+
EIP150Block: big.NewInt(0),
292+
EIP155Block: big.NewInt(0),
293+
EIP158Block: big.NewInt(0),
294+
ByzantiumBlock: big.NewInt(0),
295+
ConstantinopleBlock: big.NewInt(0),
296+
PetersburgBlock: big.NewInt(0),
297+
IstanbulBlock: big.NewInt(0),
298+
OdysseusBlock: big.NewInt(3315095),
299+
FenixBlock: big.NewInt(6770400),
300+
BlacklistContractAddress: &RoninTestnetBlacklistContract,
301+
FenixValidatorContractAddress: &RoninTestnetFenixValidatorContractAddress,
302+
WhiteListDeployerContractV2Address: &RoninWhiteListDeployerContractV2Address,
301303
Consortium: &ConsortiumConfig{
302304
Period: 3,
303305
Epoch: 30,
@@ -315,6 +317,7 @@ var (
315317
BubaBlock: big.NewInt(14260600),
316318
OlekBlock: big.NewInt(16849000),
317319
ShillinBlock: big.NewInt(20268000),
320+
AntennaBlock: big.NewInt(20737258),
318321
}
319322

320323
// GoerliTrustedCheckpoint contains the light client trusted checkpoint for the Görli test network.
@@ -528,9 +531,10 @@ type ChainConfig struct {
528531
// Shillin hardfork introduces fast finality
529532
ShillinBlock *big.Int `json:"shillinBlock,omitempty"` // Shillin switch block (nil = no fork, 0 = already on activated)
530533

531-
BlacklistContractAddress *common.Address `json:"blacklistContractAddress,omitempty"` // Address of Blacklist Contract (nil = no blacklist)
532-
FenixValidatorContractAddress *common.Address `json:"fenixValidatorContractAddress,omitempty"` // Address of Ronin Contract in the Fenix hardfork (nil = no blacklist)
533-
534+
AntennaBlock *big.Int `json:"antennaBlock,omitempty"` // AntennaBlock switch block (nil = no fork, 0 = already on activated)
535+
BlacklistContractAddress *common.Address `json:"blacklistContractAddress,omitempty"` // Address of Blacklist Contract (nil = no blacklist)
536+
FenixValidatorContractAddress *common.Address `json:"fenixValidatorContractAddress,omitempty"` // Address of Ronin Contract in the Fenix hardfork (nil = no blacklist)
537+
WhiteListDeployerContractV2Address *common.Address `json:"whiteListDeployerContractV2Address,omitempty"` // Address of Whitelist Ronin Contract V2 (nil = no blacklist)
534538
// TerminalTotalDifficulty is the amount of total difficulty reached by
535539
// the network that triggers the consensus upgrade.
536540
TerminalTotalDifficulty *big.Int `json:"terminalTotalDifficulty,omitempty"`
@@ -629,12 +633,16 @@ func (c *ChainConfig) String() string {
629633
if c.ConsortiumV2Contracts != nil {
630634
finalityTrackingContract = c.ConsortiumV2Contracts.FinalityTracking
631635
}
636+
whiteListDeployerContractV2Address := common.HexToAddress("")
637+
if c.WhiteListDeployerContractV2Address != nil {
638+
whiteListDeployerContractV2Address = *c.WhiteListDeployerContractV2Address
639+
}
632640

633641
chainConfigFmt := "{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v "
634642
chainConfigFmt += "Petersburg: %v Istanbul: %v, Odysseus: %v, Fenix: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, "
635643
chainConfigFmt += "Engine: %v, Blacklist Contract: %v, Fenix Validator Contract: %v, ConsortiumV2: %v, ConsortiumV2.RoninValidatorSet: %v, "
636-
chainConfigFmt += "ConsortiumV2.SlashIndicator: %v, ConsortiumV2.StakingContract: %v, Puffy: %v, Buba: %v, Olek: %v, Shillin: %v, "
637-
chainConfigFmt += "ConsortiumV2.ProfileContract: %v, ConsortiumV2.FinalityTracking: %v}"
644+
chainConfigFmt += "ConsortiumV2.SlashIndicator: %v, ConsortiumV2.StakingContract: %v, Puffy: %v, Buba: %v, Olek: %v, Shillin: %v, Antenna: %v, "
645+
chainConfigFmt += "ConsortiumV2.ProfileContract: %v, ConsortiumV2.FinalityTracking: %v, whiteListDeployerContractV2Address: %v}"
638646

639647
return fmt.Sprintf(chainConfigFmt,
640648
c.ChainID,
@@ -665,8 +673,10 @@ func (c *ChainConfig) String() string {
665673
c.BubaBlock,
666674
c.OlekBlock,
667675
c.ShillinBlock,
676+
c.AntennaBlock,
668677
profileContract.Hex(),
669678
finalityTrackingContract.Hex(),
679+
whiteListDeployerContractV2Address.Hex(),
670680
)
671681
}
672682

@@ -780,6 +790,11 @@ func (c *ChainConfig) IsOlek(num *big.Int) bool {
780790
return isForked(c.OlekBlock, num)
781791
}
782792

793+
// IsAntenna returns whether the num is equals to or larger than the Antenna fork block.
794+
func (c *ChainConfig) IsAntenna(num *big.Int) bool {
795+
return isForked(c.AntennaBlock, num)
796+
}
797+
783798
// IsShillin returns whether the num is equals to or larger than the shillin fork block.
784799
func (c *ChainConfig) IsShillin(num *big.Int) bool {
785800
return isForked(c.ShillinBlock, num)
@@ -919,6 +934,9 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *Confi
919934
if isForkIncompatible(c.ShillinBlock, newcfg.ShillinBlock, head) {
920935
return newCompatError("Shillin fork block", c.ShillinBlock, newcfg.ShillinBlock)
921936
}
937+
if isForkIncompatible(c.AntennaBlock, newcfg.AntennaBlock, head) {
938+
return newCompatError("Antenna fork block", c.AntennaBlock, newcfg.AntennaBlock)
939+
}
922940
return nil
923941
}
924942

@@ -987,7 +1005,7 @@ type Rules struct {
9871005
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
9881006
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
9891007
IsBerlin, IsLondon bool
990-
IsOdysseusFork, IsFenix, IsConsortiumV2 bool
1008+
IsOdysseusFork, IsFenix, IsConsortiumV2, IsAntenna bool
9911009
}
9921010

9931011
// Rules ensures c's ChainID is not nil.
@@ -1011,5 +1029,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
10111029
IsOdysseusFork: c.IsOdysseus(num),
10121030
IsFenix: c.IsFenix(num),
10131031
IsConsortiumV2: c.IsConsortiumV2(num),
1032+
IsAntenna: c.IsAntenna(num),
10141033
}
10151034
}

0 commit comments

Comments
 (0)