From e2b938562a3856db77c091e8dd8e693994d17d3f Mon Sep 17 00:00:00 2001 From: Arpit Temani Date: Wed, 15 Dec 2021 17:28:11 +0530 Subject: [PATCH] jaipur fork (#269) * jaipur fork * add check * review comments * test case * fix few changes * Change condition * review comments * type conversion * Update mumbai.go * Update config.go * Update config.go Co-authored-by: Ferran Borreguero --- command/server/chains/mainnet.go | 12 ++++--- command/server/chains/mumbai.go | 26 +++++++++++--- consensus/bor/bor.go | 35 ++++++++++-------- consensus/bor/bor_test.go | 36 +++++++++++++++++++ consensus/bor/snapshot.go | 2 +- params/config.go | 8 ++++- tests/bor/bor_test.go | 62 ++++++++++++++++++++++++++++++-- tests/bor/helper.go | 15 ++++++-- tests/bor/testdata/genesis.json | 3 +- 9 files changed, 168 insertions(+), 31 deletions(-) diff --git a/command/server/chains/mainnet.go b/command/server/chains/mainnet.go index ac074ed8cb04..631b6e664705 100644 --- a/command/server/chains/mainnet.go +++ b/command/server/chains/mainnet.go @@ -28,10 +28,14 @@ var mainnetBor = &Chain{ MuirGlacierBlock: big.NewInt(3395000), BerlinBlock: big.NewInt(14750000), Bor: ¶ms.BorConfig{ - Period: 2, - ProducerDelay: 6, - Sprint: 64, - BackupMultiplier: 2, + Period: map[string]uint64{ + "0": 2, + }, + ProducerDelay: 6, + Sprint: 64, + BackupMultiplier: map[string]uint64{ + "0": 2, + }, ValidatorContract: "0x0000000000000000000000000000000000001000", StateReceiverContract: "0x0000000000000000000000000000000000001001", OverrideStateSyncRecords: map[string]int{ diff --git a/command/server/chains/mumbai.go b/command/server/chains/mumbai.go index 1003380d104f..4d250dbf2b0e 100644 --- a/command/server/chains/mumbai.go +++ b/command/server/chains/mumbai.go @@ -27,13 +27,31 @@ var mumbaiTestnet = &Chain{ IstanbulBlock: big.NewInt(2722000), MuirGlacierBlock: big.NewInt(2722000), BerlinBlock: big.NewInt(13996000), + LondonBlock: big.NewInt(22640000), Bor: ¶ms.BorConfig{ - Period: 2, - ProducerDelay: 6, - Sprint: 64, - BackupMultiplier: 2, + JaipurBlock: 22770000, + Period: map[string]uint64{ + "0": 2, + }, + ProducerDelay: 6, + Sprint: 64, + BackupMultiplier: map[string]uint64{ + "0": 2, + }, ValidatorContract: "0x0000000000000000000000000000000000001000", StateReceiverContract: "0x0000000000000000000000000000000000001001", + BurntContract: map[string]string{ + "22640000": "0x70bcA57F4579f58670aB2d18Ef16e02C17553C38", + }, + BlockAlloc: map[string]interface{}{ + // write as interface since that is how it is decoded in genesis + "22244000": map[string]interface{}{ + "0000000000000000000000000000000000001010": map[string]interface{}{ + "balance": "0x0", + "code": "0x60806040526004361061019c5760003560e01c806377d32e94116100ec578063acd06cb31161008a578063e306f77911610064578063e306f77914610a7b578063e614d0d614610aa6578063f2fde38b14610ad1578063fc0c546a14610b225761019c565b8063acd06cb31461097a578063b789543c146109cd578063cc79f97b14610a505761019c565b80639025e64c116100c65780639025e64c146107c957806395d89b4114610859578063a9059cbb146108e9578063abceeba21461094f5761019c565b806377d32e94146106315780638da5cb5b146107435780638f32d59b1461079a5761019c565b806347e7ef24116101595780637019d41a116101335780637019d41a1461053357806370a082311461058a578063715018a6146105ef578063771282f6146106065761019c565b806347e7ef2414610410578063485cc9551461046b57806360f96a8f146104dc5761019c565b806306fdde03146101a15780631499c5921461023157806318160ddd1461028257806319d27d9c146102ad5780632e1a7d4d146103b1578063313ce567146103df575b600080fd5b3480156101ad57600080fd5b506101b6610b79565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101f65780820151818401526020810190506101db565b50505050905090810190601f1680156102235780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561023d57600080fd5b506102806004803603602081101561025457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610bb6565b005b34801561028e57600080fd5b50610297610c24565b6040518082815260200191505060405180910390f35b3480156102b957600080fd5b5061036f600480360360a08110156102d057600080fd5b81019080803590602001906401000000008111156102ed57600080fd5b8201836020820111156102ff57600080fd5b8035906020019184600183028401116401000000008311171561032157600080fd5b9091929391929390803590602001909291908035906020019092919080359060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610c3a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6103dd600480360360208110156103c757600080fd5b8101908080359060200190929190505050610caa565b005b3480156103eb57600080fd5b506103f4610dfc565b604051808260ff1660ff16815260200191505060405180910390f35b34801561041c57600080fd5b506104696004803603604081101561043357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610e05565b005b34801561047757600080fd5b506104da6004803603604081101561048e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610fc1565b005b3480156104e857600080fd5b506104f1611090565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561053f57600080fd5b506105486110b6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561059657600080fd5b506105d9600480360360208110156105ad57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506110dc565b6040518082815260200191505060405180910390f35b3480156105fb57600080fd5b506106046110fd565b005b34801561061257600080fd5b5061061b6111cd565b6040518082815260200191505060405180910390f35b34801561063d57600080fd5b506107016004803603604081101561065457600080fd5b81019080803590602001909291908035906020019064010000000081111561067b57600080fd5b82018360208201111561068d57600080fd5b803590602001918460018302840111640100000000831117156106af57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506111d3565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561074f57600080fd5b50610758611358565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156107a657600080fd5b506107af611381565b604051808215151515815260200191505060405180910390f35b3480156107d557600080fd5b506107de6113d8565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561081e578082015181840152602081019050610803565b50505050905090810190601f16801561084b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561086557600080fd5b5061086e611411565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156108ae578082015181840152602081019050610893565b50505050905090810190601f1680156108db5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610935600480360360408110156108ff57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061144e565b604051808215151515815260200191505060405180910390f35b34801561095b57600080fd5b50610964611474565b6040518082815260200191505060405180910390f35b34801561098657600080fd5b506109b36004803603602081101561099d57600080fd5b8101908080359060200190929190505050611501565b604051808215151515815260200191505060405180910390f35b3480156109d957600080fd5b50610a3a600480360360808110156109f057600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291908035906020019092919080359060200190929190505050611521565b6040518082815260200191505060405180910390f35b348015610a5c57600080fd5b50610a65611541565b6040518082815260200191505060405180910390f35b348015610a8757600080fd5b50610a90611548565b6040518082815260200191505060405180910390f35b348015610ab257600080fd5b50610abb61154e565b6040518082815260200191505060405180910390f35b348015610add57600080fd5b50610b2060048036036020811015610af457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506115db565b005b348015610b2e57600080fd5b50610b376115f8565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60606040518060400160405280600b81526020017f4d6174696320546f6b656e000000000000000000000000000000000000000000815250905090565b6040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260108152602001807f44697361626c656420666561747572650000000000000000000000000000000081525060200191505060405180910390fd5b6000601260ff16600a0a6402540be40002905090565b60006040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260108152602001807f44697361626c656420666561747572650000000000000000000000000000000081525060200191505060405180910390fd5b60003390506000610cba826110dc565b9050610cd18360065461161e90919063ffffffff16565b600681905550600083118015610ce657508234145b610d58576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f496e73756666696369656e7420616d6f756e740000000000000000000000000081525060200191505060405180910390fd5b8173ffffffffffffffffffffffffffffffffffffffff16600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167febff2602b3f468259e1e99f613fed6691f3a6526effe6ef3e768ba7ae7a36c4f8584610dd4876110dc565b60405180848152602001838152602001828152602001935050505060405180910390a3505050565b60006012905090565b610e0d611381565b610e1657600080fd5b600081118015610e535750600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614155b610ea8576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526023815260200180611da96023913960400191505060405180910390fd5b6000610eb3836110dc565b905060008390508073ffffffffffffffffffffffffffffffffffffffff166108fc849081150290604051600060405180830381858888f19350505050158015610f00573d6000803e3d6000fd5b50610f168360065461163e90919063ffffffff16565b6006819055508373ffffffffffffffffffffffffffffffffffffffff16600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f4e2ca0515ed1aef1395f66b5303bb5d6f1bf9d61a353fa53f73f8ac9973fa9f68585610f98896110dc565b60405180848152602001838152602001828152602001935050505060405180910390a350505050565b600760009054906101000a900460ff1615611027576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526023815260200180611d866023913960400191505060405180910390fd5b6001600760006101000a81548160ff02191690831515021790555080600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061108c8261165d565b5050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008173ffffffffffffffffffffffffffffffffffffffff16319050919050565b611105611381565b61110e57600080fd5b600073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a360008060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b60065481565b60008060008060418551146111ee5760009350505050611352565b602085015192506040850151915060ff6041860151169050601b8160ff16101561121957601b810190505b601b8160ff16141580156112315750601c8160ff1614155b156112425760009350505050611352565b60018682858560405160008152602001604052604051808581526020018460ff1660ff1681526020018381526020018281526020019450505050506020604051602081039080840390855afa15801561129f573d6000803e3d6000fd5b505050602060405103519350600073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16141561134e576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260128152602001807f4572726f7220696e2065637265636f766572000000000000000000000000000081525060200191505060405180910390fd5b5050505b92915050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614905090565b6040518060400160405280600381526020017f013881000000000000000000000000000000000000000000000000000000000081525081565b60606040518060400160405280600581526020017f4d41544943000000000000000000000000000000000000000000000000000000815250905090565b6000813414611460576000905061146e565b61146b338484611755565b90505b92915050565b6040518060800160405280605b8152602001611e1e605b91396040516020018082805190602001908083835b602083106114c357805182526020820191506020810190506020830392506114a0565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040528051906020012081565b60056020528060005260406000206000915054906101000a900460ff1681565b600061153761153286868686611b12565b611be8565b9050949350505050565b6201388181565b60015481565b604051806080016040528060528152602001611dcc605291396040516020018082805190602001908083835b6020831061159d578051825260208201915060208101905060208303925061157a565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040516020818303038152906040528051906020012081565b6115e3611381565b6115ec57600080fd5b6115f58161165d565b50565b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008282111561162d57600080fd5b600082840390508091505092915050565b60008082840190508381101561165357600080fd5b8091505092915050565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16141561169757600080fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6000803073ffffffffffffffffffffffffffffffffffffffff166370a08231866040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b1580156117d557600080fd5b505afa1580156117e9573d6000803e3d6000fd5b505050506040513d60208110156117ff57600080fd5b8101908080519060200190929190505050905060003073ffffffffffffffffffffffffffffffffffffffff166370a08231866040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b15801561189157600080fd5b505afa1580156118a5573d6000803e3d6000fd5b505050506040513d60208110156118bb57600080fd5b810190808051906020019092919050505090506118d9868686611c32565b8473ffffffffffffffffffffffffffffffffffffffff168673ffffffffffffffffffffffffffffffffffffffff16600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167fe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c48786863073ffffffffffffffffffffffffffffffffffffffff166370a082318e6040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b1580156119e157600080fd5b505afa1580156119f5573d6000803e3d6000fd5b505050506040513d6020811015611a0b57600080fd5b81019080805190602001909291905050503073ffffffffffffffffffffffffffffffffffffffff166370a082318e6040518263ffffffff1660e01b8152600401808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060206040518083038186803b158015611a9957600080fd5b505afa158015611aad573d6000803e3d6000fd5b505050506040513d6020811015611ac357600080fd5b8101908080519060200190929190505050604051808681526020018581526020018481526020018381526020018281526020019550505050505060405180910390a46001925050509392505050565b6000806040518060800160405280605b8152602001611e1e605b91396040516020018082805190602001908083835b60208310611b645780518252602082019150602081019050602083039250611b41565b6001836020036101000a03801982511681845116808217855250505050505090500191505060405160208183030381529060405280519060200120905060405181815273ffffffffffffffffffffffffffffffffffffffff8716602082015285604082015284606082015283608082015260a0812092505081915050949350505050565b60008060015490506040517f190100000000000000000000000000000000000000000000000000000000000081528160028201528360228201526042812092505081915050919050565b3073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415611cd4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f63616e27742073656e6420746f204d524332300000000000000000000000000081525060200191505060405180910390fd5b8173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050158015611d1a573d6000803e3d6000fd5b508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a350505056fe54686520636f6e747261637420697320616c726561647920696e697469616c697a6564496e73756666696369656e7420616d6f756e74206f7220696e76616c69642075736572454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429546f6b656e5472616e736665724f726465722861646472657373207370656e6465722c75696e7432353620746f6b656e49644f72416d6f756e742c6279746573333220646174612c75696e743235362065787069726174696f6e29a265627a7a72315820ccd6c2a9c259832bbb367986ee06cd87af23022681b0cb22311a864b701d939564736f6c63430005100032", + }, + }, + }, }, }, Nonce: 0, diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 94774d503234..7f3c422dfc7b 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -123,7 +123,7 @@ var ( type SignerFn func(accounts.Account, string, []byte) ([]byte, error) // ecrecover extracts the Ethereum account address from a signed header. -func ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, error) { +func ecrecover(header *types.Header, sigcache *lru.ARCCache, c *params.BorConfig) (common.Address, error) { // If the signature's already cached, return that hash := header.Hash() if address, known := sigcache.Get(hash); known { @@ -136,7 +136,7 @@ func ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, er signature := header.Extra[len(header.Extra)-extraSeal:] // Recover the public key and the Ethereum address - pubkey, err := crypto.Ecrecover(SealHash(header).Bytes(), signature) + pubkey, err := crypto.Ecrecover(SealHash(header, c).Bytes(), signature) if err != nil { return common.Address{}, err } @@ -148,15 +148,15 @@ func ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, er } // SealHash returns the hash of a block prior to it being sealed. -func SealHash(header *types.Header) (hash common.Hash) { +func SealHash(header *types.Header, c *params.BorConfig) (hash common.Hash) { hasher := sha3.NewLegacyKeccak256() - encodeSigHeader(hasher, header) + encodeSigHeader(hasher, header, c) hasher.Sum(hash[:0]) return hash } -func encodeSigHeader(w io.Writer, header *types.Header) { - err := rlp.Encode(w, []interface{}{ +func encodeSigHeader(w io.Writer, header *types.Header, c *params.BorConfig) { + enc := []interface{}{ header.ParentHash, header.UncleHash, header.Coinbase, @@ -172,8 +172,13 @@ func encodeSigHeader(w io.Writer, header *types.Header) { header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short header.MixDigest, header.Nonce, - }) - if err != nil { + } + if c.IsJaipur(header.Number.Uint64()) { + if header.BaseFee != nil { + enc = append(enc, header.BaseFee) + } + } + if err := rlp.Encode(w, enc); err != nil { panic("can't encode: " + err.Error()) } } @@ -199,9 +204,9 @@ func CalcProducerDelay(number uint64, succession int, c *params.BorConfig) uint6 // Note, the method requires the extra data to be at least 65 bytes, otherwise it // panics. This is done to avoid accidentally using both forms (signature present // or not), which could be abused to produce different hashes for the same header. -func BorRLP(header *types.Header) []byte { +func BorRLP(header *types.Header, c *params.BorConfig) []byte { b := new(bytes.Buffer) - encodeSigHeader(b, header) + encodeSigHeader(b, header, c) return b.Bytes() } @@ -280,7 +285,7 @@ func New( // Author implements consensus.Engine, returning the Ethereum address recovered // from the signature in the header's extra-data section. func (c *Bor) Author(header *types.Header) (common.Address, error) { - return ecrecover(header, c.signatures) + return ecrecover(header, c.signatures, c.config) } // VerifyHeader checks whether a header conforms to the consensus rules. @@ -578,7 +583,7 @@ func (c *Bor) verifySeal(chain consensus.ChainHeaderReader, header *types.Header } // Resolve the authorization key and check against signers - signer, err := ecrecover(header, c.signatures) + signer, err := ecrecover(header, c.signatures, c.config) if err != nil { return err } @@ -844,7 +849,7 @@ func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, result wiggle := time.Duration(successionNumber) * time.Duration(c.config.CalculateBackupMultiplier(number)) * time.Second // Sign all the things! - sighash, err := signFn(accounts.Account{Address: signer}, accounts.MimetypeBor, BorRLP(header)) + sighash, err := signFn(accounts.Account{Address: signer}, accounts.MimetypeBor, BorRLP(header, c.config)) if err != nil { return err } @@ -876,7 +881,7 @@ func (c *Bor) Seal(chain consensus.ChainHeaderReader, block *types.Block, result select { case results <- block.WithSeal(header): default: - log.Warn("Sealing result was not read by miner", "number", number, "sealhash", SealHash(header)) + log.Warn("Sealing result was not read by miner", "number", number, "sealhash", SealHash(header, c.config)) } }() return nil @@ -895,7 +900,7 @@ func (c *Bor) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, par // SealHash returns the hash of a block prior to it being sealed. func (c *Bor) SealHash(header *types.Header) common.Hash { - return SealHash(header) + return SealHash(header, c.config) } // APIs implements consensus.Engine, returning the user facing RPC API to allow diff --git a/consensus/bor/bor_test.go b/consensus/bor/bor_test.go index 75beea36d8eb..b0ebc9686131 100644 --- a/consensus/bor/bor_test.go +++ b/consensus/bor/bor_test.go @@ -99,3 +99,39 @@ func TestGenesisContractChange(t *testing.T) { // make sure balance change DOES NOT take effect assert.Equal(t, statedb.GetBalance(addr0), big.NewInt(0)) } + +func TestEncodeSigHeaderJaipur(t *testing.T) { + // As part of the EIP-1559 fork in mumbai, an incorrect seal hash + // was used for Bor that did not included the BaseFee. The Jaipur + // block is a hard fork to fix that. + h := &types.Header{ + Difficulty: new(big.Int), + Number: big.NewInt(1), + Extra: make([]byte, 32+65), + } + + var ( + // hash for the block without the BaseFee + hashWithoutBaseFee = common.HexToHash("0x1be13e83939b3c4701ee57a34e10c9290ce07b0e53af0fe90b812c6881826e36") + // hash for the block with the baseFee + hashWithBaseFee = common.HexToHash("0xc55b0cac99161f71bde1423a091426b1b5b4d7598e5981ad802cce712771965b") + ) + + // Jaipur NOT enabled and BaseFee not set + hash := SealHash(h, ¶ms.BorConfig{JaipurBlock: 10}) + assert.Equal(t, hash, hashWithoutBaseFee) + + // Jaipur enabled (Jaipur=0) and BaseFee not set + hash = SealHash(h, ¶ms.BorConfig{JaipurBlock: 0}) + assert.Equal(t, hash, hashWithoutBaseFee) + + h.BaseFee = big.NewInt(2) + + // Jaipur enabled (Jaipur=Header block) and BaseFee set + hash = SealHash(h, ¶ms.BorConfig{JaipurBlock: 1}) + assert.Equal(t, hash, hashWithBaseFee) + + // Jaipur NOT enabled and BaseFee set + hash = SealHash(h, ¶ms.BorConfig{JaipurBlock: 10}) + assert.Equal(t, hash, hashWithoutBaseFee) +} diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index 8405f34fbd91..8d212f33ef08 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -131,7 +131,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { } // Resolve the authorization key and check against signers - signer, err := ecrecover(header, s.sigcache) + signer, err := ecrecover(header, s.sigcache, s.config) if err != nil { return nil, err } diff --git a/params/config.go b/params/config.go index f3a83530b960..86bf6430b88e 100644 --- a/params/config.go +++ b/params/config.go @@ -276,6 +276,7 @@ var ( BerlinBlock: big.NewInt(13996000), LondonBlock: big.NewInt(22640000), Bor: &BorConfig{ + JaipurBlock: 22770000, Period: map[string]uint64{ "0": 2, }, @@ -486,7 +487,8 @@ type BorConfig struct { StateReceiverContract string `json:"stateReceiverContract"` // State receiver contract OverrideStateSyncRecords map[string]int `json:"overrideStateSyncRecords"` // override state records count BlockAlloc map[string]interface{} `json:"blockAlloc"` - BurntContract map[string]string `json:"burntContract"` // governance contract where the token will be sent to and burnt in london fork + BurntContract map[string]string `json:"burntContract"` // governance contract where the token will be sent to and burnt in london fork + JaipurBlock uint64 `json:"jaipurBlock"` // Jaipur switch block (nil = no fork, 0 = already on jaipur) } // String implements the stringer interface, returning the consensus engine details. @@ -502,6 +504,10 @@ func (c *BorConfig) CalculatePeriod(number uint64) uint64 { return c.calculateBorConfigHelper(c.Period, number) } +func (c *BorConfig) IsJaipur(number uint64) bool { + return number >= c.JaipurBlock +} + func (c *BorConfig) calculateBorConfigHelper(field map[string]uint64, number uint64) uint64 { keys := make([]string, 0, len(field)) for k := range field { diff --git a/tests/bor/bor_test.go b/tests/bor/bor_test.go index 850d4fda6cf8..7d0ca9372b49 100644 --- a/tests/bor/bor_test.go +++ b/tests/bor/bor_test.go @@ -3,6 +3,7 @@ package bor import ( "encoding/hex" "encoding/json" + "io" "math/big" "testing" "time" @@ -16,9 +17,11 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/tests/bor/mocks" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "golang.org/x/crypto/sha3" ) var ( @@ -192,7 +195,7 @@ func TestOutOfTurnSigning(t *testing.T) { header := block.Header() header.Time += (bor.CalcProducerDelay(header.Number.Uint64(), expectedSuccessionNumber, init.genesis.Config.Bor) - bor.CalcProducerDelay(header.Number.Uint64(), 0, init.genesis.Config.Bor)) - sign(t, header, signerKey) + sign(t, header, signerKey, init.genesis.Config.Bor) block = types.NewBlockWithHeader(header) _, err = chain.InsertChain([]*types.Block{block}) assert.Equal(t, @@ -200,7 +203,7 @@ func TestOutOfTurnSigning(t *testing.T) { bor.WrongDifficultyError{Number: spanSize, Expected: expectedDifficulty, Actual: 3, Signer: addr.Bytes()}) header.Difficulty = new(big.Int).SetUint64(expectedDifficulty) - sign(t, header, signerKey) + sign(t, header, signerKey, init.genesis.Config.Bor) block = types.NewBlockWithHeader(header) _, err = chain.InsertChain([]*types.Block{block}) assert.Nil(t, err) @@ -502,3 +505,58 @@ func TestEIP1559Transition(t *testing.T) { func newGwei(n int64) *big.Int { return new(big.Int).Mul(big.NewInt(n), big.NewInt(params.GWei)) } + +func TestJaipurFork(t *testing.T) { + init := buildEthereumInstance(t, rawdb.NewMemoryDatabase()) + chain := init.ethereum.BlockChain() + engine := init.ethereum.Engine() + _bor := engine.(*bor.Bor) + db := init.ethereum.ChainDb() + block := init.genesis.ToBlock(db) + for i := uint64(1); i < sprintSize; i++ { + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor) + insertNewBlock(t, chain, block) + if block.Number().Uint64() == init.genesis.Config.Bor.JaipurBlock-1 { + assert.Equal(t, testSealHash(block.Header(), init.genesis.Config.Bor), bor.SealHash(block.Header(), init.genesis.Config.Bor)) + } + if block.Number().Uint64() == init.genesis.Config.Bor.JaipurBlock { + assert.Equal(t, testSealHash(block.Header(), init.genesis.Config.Bor), bor.SealHash(block.Header(), init.genesis.Config.Bor)) + } + } +} + +// SealHash returns the hash of a block prior to it being sealed. +func testSealHash(header *types.Header, c *params.BorConfig) (hash common.Hash) { + hasher := sha3.NewLegacyKeccak256() + testEncodeSigHeader(hasher, header, c) + hasher.Sum(hash[:0]) + return hash +} + +func testEncodeSigHeader(w io.Writer, header *types.Header, c *params.BorConfig) { + enc := []interface{}{ + header.ParentHash, + header.UncleHash, + header.Coinbase, + header.Root, + header.TxHash, + header.ReceiptHash, + header.Bloom, + header.Difficulty, + header.Number, + header.GasLimit, + header.GasUsed, + header.Time, + header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short + header.MixDigest, + header.Nonce, + } + if c.IsJaipur(header.Number.Uint64()) { + if header.BaseFee != nil { + enc = append(enc, header.BaseFee) + } + } + if err := rlp.Encode(w, enc); err != nil { + panic("can't encode: " + err.Error()) + } +} diff --git a/tests/bor/helper.go b/tests/bor/helper.go index 850d82643506..a1b6a19742f0 100644 --- a/tests/bor/helper.go +++ b/tests/bor/helper.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/bor" + "github.com/ethereum/go-ethereum/consensus/misc" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -111,6 +112,14 @@ func buildNextBlock(t *testing.T, _bor *bor.Bor, chain *core.BlockChain, block * copy(header.Extra[32:], validatorBytes) } + if chain.Config().IsLondon(header.Number) { + header.BaseFee = misc.CalcBaseFee(chain.Config(), block.Header()) + if !chain.Config().IsLondon(block.Number()) { + parentGasLimit := block.GasLimit() * params.ElasticityMultiplier + header.GasLimit = core.CalcGasLimit(parentGasLimit, parentGasLimit) + } + } + state, err := chain.State() if err != nil { t.Fatalf("%s", err) @@ -119,12 +128,12 @@ func buildNextBlock(t *testing.T, _bor *bor.Bor, chain *core.BlockChain, block * if err != nil { t.Fatalf("%s", err) } - sign(t, header, signer) + sign(t, header, signer, borConfig) return types.NewBlockWithHeader(header) } -func sign(t *testing.T, header *types.Header, signer []byte) { - sig, err := secp256k1.Sign(crypto.Keccak256(bor.BorRLP(header)), signer) +func sign(t *testing.T, header *types.Header, signer []byte, c *params.BorConfig) { + sig, err := secp256k1.Sign(crypto.Keccak256(bor.BorRLP(header, c)), signer) if err != nil { t.Fatalf("%s", err) } diff --git a/tests/bor/testdata/genesis.json b/tests/bor/testdata/genesis.json index 584f7fc91230..b5767b3ed333 100644 --- a/tests/bor/testdata/genesis.json +++ b/tests/bor/testdata/genesis.json @@ -12,8 +12,9 @@ "istanbulBlock": 0, "muirGlacierBlock": 0, "berlinBlock": 0, - "londonBlock": 10, + "londonBlock": 1, "bor": { + "jaipurBlock": 2, "period": { "0": 1 },