From 8d3800a4406a071d6d9f20df8bc6210e3b5ae202 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 12 Jul 2023 13:15:28 +0800 Subject: [PATCH 01/13] moved TxDependency Metadata to ExtraData in Block Header --- consensus/bor/bor.go | 50 +++++++++++++++------- consensus/bor/snapshot.go | 2 +- core/parallel_state_processor.go | 6 +-- core/types/block.go | 56 ++++++++++++++++++------- core/types/block_test.go | 2 +- core/types/gen_header_json.go | 72 +++++++++++++++----------------- core/types/gen_header_rlp.go | 14 +------ miner/worker.go | 28 +++++++++++-- miner/worker_test.go | 4 +- 9 files changed, 142 insertions(+), 92 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 44dd48f867..ff28c61987 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -54,9 +54,6 @@ var ( "0": 64, } // Default number of blocks after which to checkpoint and reset the pending votes - extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity - extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal - uncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. validatorHeaderBytesLength = common.AddressLength + 20 // address + power @@ -120,11 +117,11 @@ func ecrecover(header *types.Header, sigcache *lru.ARCCache, c *params.BorConfig return address.(common.Address), nil } // Retrieve the signature from the header extra-data - if len(header.Extra) < extraSeal { + if len(header.Extra) < types.GetExtraSeal() { return common.Address{}, errMissingSignature } - signature := header.Extra[len(header.Extra)-extraSeal:] + signature := header.Extra[len(header.Extra)-types.GetExtraSeal():] // Recover the public key and the Ethereum address pubkey, err := crypto.Ecrecover(SealHash(header, c).Bytes(), signature) @@ -355,7 +352,7 @@ func (c *Bor) verifyHeader(chain consensus.ChainHeaderReader, header *types.Head isSprintEnd := IsSprintStart(number+1, c.config.CalculateSprint(number)) // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise - signersBytes := len(header.Extra) - extraVanity - extraSeal + signersBytes := len(header.Extra) - types.GetExtraVanity() - types.GetExtraSeal() if !isSprintEnd && signersBytes != 0 { return errExtraValidators } @@ -400,11 +397,11 @@ func (c *Bor) verifyHeader(chain consensus.ChainHeaderReader, header *types.Head // validateHeaderExtraField validates that the extra-data contains both the vanity and signature. // header.Extra = header.Vanity + header.ProducerBytes (optional) + header.Seal func validateHeaderExtraField(extraBytes []byte) error { - if len(extraBytes) < extraVanity { + if len(extraBytes) < types.GetExtraVanity() { return errMissingVanity } - if len(extraBytes) < extraVanity+extraSeal { + if len(extraBytes) < types.GetExtraVanity()+types.GetExtraSeal() { return errMissingSignature } @@ -475,7 +472,13 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t sort.Sort(valset.ValidatorsByAddress(newValidators)) - headerVals, err := valset.ParseValidators(header.Extra[extraVanity : len(header.Extra)-extraSeal]) + var blockExtraData types.BlockExtraData + if err := rlp.DecodeBytes(header.Extra[types.GetExtraVanity():len(header.Extra)-types.GetExtraSeal()], &blockExtraData); err != nil { + log.Error("error while decoding block extra data: %v", err) + return fmt.Errorf("error while decoding block extra data: %v", err) + } + + headerVals, err := valset.ParseValidators(blockExtraData.ValidatorBytes) if err != nil { return err @@ -494,7 +497,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t // verify the validator list in the last sprint block if IsSprintStart(number, c.config.CalculateSprint(number)) { - parentValidatorBytes := parent.Extra[extraVanity : len(parent.Extra)-extraSeal] + parentValidatorBytes := parent.GetValidatorBytes() validatorsBytes := make([]byte, len(snap.ValidatorSet.Validators)*validatorHeaderBytesLength) currentValidators := snap.ValidatorSet.Copy().Validators @@ -731,11 +734,11 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e header.Difficulty = new(big.Int).SetUint64(Difficulty(snap.ValidatorSet, currentSigner.signer)) // Ensure the extra data has all it's components - if len(header.Extra) < extraVanity { - header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...) + if len(header.Extra) < types.GetExtraVanity() { + header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, types.GetExtraVanity()-len(header.Extra))...) } - header.Extra = header.Extra[:extraVanity] + header.Extra = header.Extra[:types.GetExtraVanity()] // get validator set if number if IsSprintStart(number+1, c.config.CalculateSprint(number)) { @@ -747,13 +750,28 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e // sort validator by address sort.Sort(valset.ValidatorsByAddress(newValidators)) + var tempValidatorBytes []byte + for _, validator := range newValidators { - header.Extra = append(header.Extra, validator.HeaderBytes()...) + tempValidatorBytes = append(tempValidatorBytes, validator.HeaderBytes()...) + } + + blockExtraData := &types.BlockExtraData{ + ValidatorBytes: tempValidatorBytes, + TxDependency: nil, } + + blockExtraDataBytes, err := rlp.EncodeToBytes(blockExtraData) + if err != nil { + log.Error("error while encoding block extra data: %v", err) + return fmt.Errorf("error while encoding block extra data: %v", err) + } + + header.Extra = append(header.Extra, blockExtraDataBytes...) } // add extra seal space - header.Extra = append(header.Extra, make([]byte, extraSeal)...) + header.Extra = append(header.Extra, make([]byte, types.GetExtraSeal())...) // Mix digest is reserved for now, set to empty header.MixDigest = common.Hash{} @@ -1053,7 +1071,7 @@ func Sign(signFn SignerFn, signer common.Address, header *types.Header, c *param return err } - copy(header.Extra[len(header.Extra)-extraSeal:], sighash) + copy(header.Extra[len(header.Extra)-types.GetExtraSeal():], sighash) return nil } diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index b61851262e..a421b8ccd9 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -150,7 +150,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { return nil, err } - validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal] + validatorBytes := header.GetValidatorBytes() // get validators from headers and use that for new validator set newVals, _ := valset.ParseValidators(validatorBytes) diff --git a/core/parallel_state_processor.go b/core/parallel_state_processor.go index 8956f6f4d2..6eea4225ec 100644 --- a/core/parallel_state_processor.go +++ b/core/parallel_state_processor.go @@ -286,9 +286,9 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat coinbase, _ := p.bc.Engine().Author(header) - deps := GetDeps(block.Header().TxDependency) + deps := GetDeps(block.GetTxDependency()) - if block.Header().TxDependency != nil { + if block.GetTxDependency() != nil { metadata = true } @@ -308,7 +308,7 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat shouldDelayFeeCal = false } - if len(header.TxDependency) != len(block.Transactions()) { + if len(block.GetTxDependency()) != len(block.Transactions()) { task := &ExecutionTask{ msg: msg, config: p.config, diff --git a/core/types/block.go b/core/types/block.go index 0af6a35501..0069c6e04a 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -28,14 +28,26 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" ) var ( EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") EmptyUncleHash = rlpHash([]*Header(nil)) + + extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity + extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal ) +func GetExtraVanity() int { + return extraVanity +} + +func GetExtraSeal() int { + return extraSeal +} + // A BlockNonce is a 64-bit hash which proves (combined with the // mix-hash) that a sufficient amount of computation has been carried // out on a block. @@ -87,11 +99,6 @@ type Header struct { // BaseFee was added by EIP-1559 and is ignored in legacy headers. BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"` - // length of TxDependency -> n (n = number of transactions in the block) - // length of TxDependency[i] -> k (k = a whole number) - // k elements in TxDependency[i] -> transaction indexes on which transaction i is dependent on - TxDependency [][]uint64 `json:"txDependency" rlp:"optional"` - /* TODO (MariusVanDerWijden) Add this field once needed // Random was added during the merge and contains the BeaconState randomness @@ -99,6 +106,16 @@ type Header struct { */ } +// Used for Encoding and Decoding of the Extra Data Field +type BlockExtraData struct { + ValidatorBytes []byte + + // length of TxDependency -> n (n = number of transactions in the block) + // length of TxDependency[i] -> k (k = a whole number) + // k elements in TxDependency[i] -> transaction indexes on which transaction i is dependent on + TxDependency [][]uint64 +} + // field type overrides for gencodec type headerMarshaling struct { Difficulty *hexutil.Big @@ -258,14 +275,6 @@ func CopyHeader(h *Header) *Header { copy(cpy.Extra, h.Extra) } - if len(h.TxDependency) > 0 { - cpy.TxDependency = make([][]uint64, len(h.TxDependency)) - - for i, dep := range h.TxDependency { - cpy.TxDependency[i] = make([]uint64, len(dep)) - copy(cpy.TxDependency[i], dep) - } - } return &cpy } @@ -321,7 +330,26 @@ func (b *Block) TxHash() common.Hash { return b.header.TxHash } func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash } func (b *Block) UncleHash() common.Hash { return b.header.UncleHash } func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) } -func (b *Block) TxDependency() [][]uint64 { return b.header.TxDependency } + +func (b *Block) GetTxDependency() [][]uint64 { + var blockExtraData BlockExtraData + if err := rlp.DecodeBytes(b.header.Extra[extraVanity:len(b.header.Extra)-extraSeal], &blockExtraData); err != nil { + log.Error("error while decoding block extra data: %v", err) + return nil + } + + return blockExtraData.TxDependency +} + +func (h *Header) GetValidatorBytes() []byte { + var blockExtraData BlockExtraData + if err := rlp.DecodeBytes(h.Extra[extraVanity:len(h.Extra)-extraSeal], &blockExtraData); err != nil { + log.Error("error while decoding block extra data: %v", err) + return nil + } + + return blockExtraData.ValidatorBytes +} func (b *Block) BaseFee() *big.Int { if b.header.BaseFee == nil { diff --git a/core/types/block_test.go b/core/types/block_test.go index dede213bf6..95fa12d450 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -95,7 +95,7 @@ func TestTxDependencyBlockEncoding(t *testing.T) { check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4)) check("Time", block.Time(), uint64(1426516743)) check("Size", block.Size(), common.StorageSize(len(blockEnc))) - check("TxDependency", block.TxDependency(), [][]uint64{{2, 1}, {1, 0}}) + check("TxDependency", block.GetTxDependency(), [][]uint64{{2, 1}, {1, 0}}) tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil) tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100")) diff --git a/core/types/gen_header_json.go b/core/types/gen_header_json.go index 10f7156749..75e24b34d6 100644 --- a/core/types/gen_header_json.go +++ b/core/types/gen_header_json.go @@ -16,24 +16,23 @@ var _ = (*headerMarshaling)(nil) // MarshalJSON marshals as JSON. func (h Header) MarshalJSON() ([]byte, error) { type Header struct { - ParentHash common.Hash `json:"parentHash" gencodec:"required"` - UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"` - Coinbase common.Address `json:"miner" gencodec:"required"` - Root common.Hash `json:"stateRoot" gencodec:"required"` - TxHash common.Hash `json:"transactionsRoot" gencodec:"required"` - ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"` - Bloom Bloom `json:"logsBloom" gencodec:"required"` - Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` - Number *hexutil.Big `json:"number" gencodec:"required"` - GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"` - GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"` - Time hexutil.Uint64 `json:"timestamp" gencodec:"required"` - Extra hexutil.Bytes `json:"extraData" gencodec:"required"` - MixDigest common.Hash `json:"mixHash"` - Nonce BlockNonce `json:"nonce"` - BaseFee *hexutil.Big `json:"baseFeePerGas" rlp:"optional"` - TxDependency [][]uint64 `json:"txDependency" rlp:"optional"` - Hash common.Hash `json:"hash"` + ParentHash common.Hash `json:"parentHash" gencodec:"required"` + UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"` + Coinbase common.Address `json:"miner" gencodec:"required"` + Root common.Hash `json:"stateRoot" gencodec:"required"` + TxHash common.Hash `json:"transactionsRoot" gencodec:"required"` + ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"` + Bloom Bloom `json:"logsBloom" gencodec:"required"` + Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` + Number *hexutil.Big `json:"number" gencodec:"required"` + GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"` + GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"` + Time hexutil.Uint64 `json:"timestamp" gencodec:"required"` + Extra hexutil.Bytes `json:"extraData" gencodec:"required"` + MixDigest common.Hash `json:"mixHash"` + Nonce BlockNonce `json:"nonce"` + BaseFee *hexutil.Big `json:"baseFeePerGas" rlp:"optional"` + Hash common.Hash `json:"hash"` } var enc Header enc.ParentHash = h.ParentHash @@ -52,7 +51,6 @@ func (h Header) MarshalJSON() ([]byte, error) { enc.MixDigest = h.MixDigest enc.Nonce = h.Nonce enc.BaseFee = (*hexutil.Big)(h.BaseFee) - enc.TxDependency = h.TxDependency enc.Hash = h.Hash() return json.Marshal(&enc) } @@ -60,23 +58,22 @@ func (h Header) MarshalJSON() ([]byte, error) { // UnmarshalJSON unmarshals from JSON. func (h *Header) UnmarshalJSON(input []byte) error { type Header struct { - ParentHash *common.Hash `json:"parentHash" gencodec:"required"` - UncleHash *common.Hash `json:"sha3Uncles" gencodec:"required"` - Coinbase *common.Address `json:"miner" gencodec:"required"` - Root *common.Hash `json:"stateRoot" gencodec:"required"` - TxHash *common.Hash `json:"transactionsRoot" gencodec:"required"` - ReceiptHash *common.Hash `json:"receiptsRoot" gencodec:"required"` - Bloom *Bloom `json:"logsBloom" gencodec:"required"` - Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` - Number *hexutil.Big `json:"number" gencodec:"required"` - GasLimit *hexutil.Uint64 `json:"gasLimit" gencodec:"required"` - GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"` - Time *hexutil.Uint64 `json:"timestamp" gencodec:"required"` - Extra *hexutil.Bytes `json:"extraData" gencodec:"required"` - MixDigest *common.Hash `json:"mixHash"` - Nonce *BlockNonce `json:"nonce"` - BaseFee *hexutil.Big `json:"baseFeePerGas" rlp:"optional"` - TxDependency [][]uint64 `json:"txDependency" rlp:"optional"` + ParentHash *common.Hash `json:"parentHash" gencodec:"required"` + UncleHash *common.Hash `json:"sha3Uncles" gencodec:"required"` + Coinbase *common.Address `json:"miner" gencodec:"required"` + Root *common.Hash `json:"stateRoot" gencodec:"required"` + TxHash *common.Hash `json:"transactionsRoot" gencodec:"required"` + ReceiptHash *common.Hash `json:"receiptsRoot" gencodec:"required"` + Bloom *Bloom `json:"logsBloom" gencodec:"required"` + Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"` + Number *hexutil.Big `json:"number" gencodec:"required"` + GasLimit *hexutil.Uint64 `json:"gasLimit" gencodec:"required"` + GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"` + Time *hexutil.Uint64 `json:"timestamp" gencodec:"required"` + Extra *hexutil.Bytes `json:"extraData" gencodec:"required"` + MixDigest *common.Hash `json:"mixHash"` + Nonce *BlockNonce `json:"nonce"` + BaseFee *hexutil.Big `json:"baseFeePerGas" rlp:"optional"` } var dec Header if err := json.Unmarshal(input, &dec); err != nil { @@ -143,8 +140,5 @@ func (h *Header) UnmarshalJSON(input []byte) error { if dec.BaseFee != nil { h.BaseFee = (*big.Int)(dec.BaseFee) } - if dec.TxDependency != nil { - h.TxDependency = dec.TxDependency - } return nil } diff --git a/core/types/gen_header_rlp.go b/core/types/gen_header_rlp.go index 10377e2ad2..e1a6873318 100644 --- a/core/types/gen_header_rlp.go +++ b/core/types/gen_header_rlp.go @@ -41,8 +41,7 @@ func (obj *Header) EncodeRLP(_w io.Writer) error { w.WriteBytes(obj.MixDigest[:]) w.WriteBytes(obj.Nonce[:]) _tmp1 := obj.BaseFee != nil - _tmp2 := len(obj.TxDependency) > 0 - if _tmp1 || _tmp2 { + if _tmp1 { if obj.BaseFee == nil { w.Write(rlp.EmptyString) } else { @@ -52,17 +51,6 @@ func (obj *Header) EncodeRLP(_w io.Writer) error { w.WriteBigInt(obj.BaseFee) } } - if _tmp2 { - _tmp3 := w.List() - for _, _tmp4 := range obj.TxDependency { - _tmp5 := w.List() - for _, _tmp6 := range _tmp4 { - w.WriteUint64(_tmp6) - } - w.ListEnd(_tmp5) - } - w.ListEnd(_tmp3) - } w.ListEnd(_tmp0) return w.Flush() } diff --git a/miner/worker.go b/miner/worker.go index c46d0b2c8d..9808055e14 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -51,6 +51,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) @@ -1170,6 +1171,11 @@ mainloop: close(chDeps) depsWg.Wait() + var blockExtraData types.BlockExtraData + + tempVanity := env.header.Extra[:types.GetExtraVanity()] + tempSeal := env.header.Extra[types.GetExtraVanity() : len(env.header.Extra)-types.GetExtraSeal()] + if len(mvReadMapList) > 0 { tempDeps := make([][]uint64, len(mvReadMapList)) @@ -1194,14 +1200,30 @@ mainloop: } } + if err := rlp.DecodeBytes(env.header.Extra[types.GetExtraVanity():len(env.header.Extra)-types.GetExtraSeal()], &blockExtraData); err != nil { + log.Error("error while decoding block extra data: %v", err) + return false + } + if delayFlag { - env.header.TxDependency = tempDeps + blockExtraData.TxDependency = tempDeps } else { - env.header.TxDependency = nil + blockExtraData.TxDependency = nil } } else { - env.header.TxDependency = nil + blockExtraData.TxDependency = nil } + + blockExtraDataBytes, err := rlp.EncodeToBytes(blockExtraData) + if err != nil { + log.Error("error while encoding block extra data: %v", err) + return false + } + + env.header.Extra = []byte{} + env.header.Extra = append(tempVanity, blockExtraDataBytes...) + env.header.Extra = append(env.header.Extra, tempSeal...) + } if !w.isRunning() && len(coalescedLogs) > 0 { diff --git a/miner/worker_test.go b/miner/worker_test.go index 81743dd59d..a6324f0c3a 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -949,11 +949,11 @@ func BenchmarkBorMiningBlockSTMMetadata(b *testing.B) { // check for dependencies for block number > 4 if block.NumberU64() <= 4 { - if block.TxDependency() != nil { + if block.GetTxDependency() != nil { b.Fatalf("dependency not nil") } } else { - deps := block.TxDependency() + deps := block.GetTxDependency() if len(deps[0]) != 0 { b.Fatalf("wrong dependency") } From 3aa102a94f356733276eea559dbe49fb9c1652b4 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 12 Jul 2023 13:45:17 +0800 Subject: [PATCH 02/13] updated unit tests --- core/types/block_test.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/core/types/block_test.go b/core/types/block_test.go index 95fa12d450..055109f467 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -68,10 +68,8 @@ func TestBlockEncoding(t *testing.T) { } } -func TestTxDependencyBlockEncoding(t *testing.T) { - t.Parallel() - - blockEnc := common.FromHex("f90268f90201a083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c480c6c20201c20180f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1c0") +func TestTxDependencyBlockDecoding(t *testing.T) { + blockEnc := common.FromHex("f90270f9026ba00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8825208845506eb07b8710000000000000000000000000000000000000000000000000000000000000000cf8776616c20736574c6c20201c201800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498880000000000000000c0c0") var block Block @@ -91,17 +89,14 @@ func TestTxDependencyBlockEncoding(t *testing.T) { check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1")) check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498")) check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017")) - check("Hash", block.Hash(), common.HexToHash("0xc6d8dc8995c0a4374bb9f87bd0dd8c0761e6e026a71edbfed5e961c9e55dbd6a")) - check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4)) check("Time", block.Time(), uint64(1426516743)) - check("Size", block.Size(), common.StorageSize(len(blockEnc))) - check("TxDependency", block.GetTxDependency(), [][]uint64{{2, 1}, {1, 0}}) - tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil) - tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100")) + validatorBytes := block.header.GetValidatorBytes() + txDependency := block.GetTxDependency() + + check("validatorBytes", validatorBytes, []byte("val set")) + check("txDependency", txDependency, [][]uint64{{2, 1}, {1, 0}}) - check("len(Transactions)", len(block.Transactions()), 1) - check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash()) ourBlockEnc, err := rlp.EncodeToBytes(&block) if err != nil { From c40c1ddf25fee8c5e16f1c86df80e5eacda7df4b Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 12 Jul 2023 14:28:28 +0800 Subject: [PATCH 03/13] lints --- core/types/block_test.go | 2 ++ miner/worker.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/core/types/block_test.go b/core/types/block_test.go index 055109f467..82f4046099 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -69,6 +69,8 @@ func TestBlockEncoding(t *testing.T) { } func TestTxDependencyBlockDecoding(t *testing.T) { + t.Parallel() + blockEnc := common.FromHex("f90270f9026ba00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8825208845506eb07b8710000000000000000000000000000000000000000000000000000000000000000cf8776616c20736574c6c20201c201800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498880000000000000000c0c0") var block Block diff --git a/miner/worker.go b/miner/worker.go index 9808055e14..01540d9e2c 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1221,7 +1221,9 @@ mainloop: } env.header.Extra = []byte{} + env.header.Extra = append(tempVanity, blockExtraDataBytes...) + env.header.Extra = append(env.header.Extra, tempSeal...) } From 69cbe7a590ee88ed5922ae6138ebb079406757b6 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Thu, 13 Jul 2023 17:20:31 +0800 Subject: [PATCH 04/13] small bug fix --- consensus/bor/bor.go | 10 ++-------- core/types/block.go | 4 ++-- miner/worker.go | 2 +- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index ff28c61987..ac61afd2b1 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -352,7 +352,7 @@ func (c *Bor) verifyHeader(chain consensus.ChainHeaderReader, header *types.Head isSprintEnd := IsSprintStart(number+1, c.config.CalculateSprint(number)) // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise - signersBytes := len(header.Extra) - types.GetExtraVanity() - types.GetExtraSeal() + signersBytes := len(header.GetValidatorBytes()) if !isSprintEnd && signersBytes != 0 { return errExtraValidators } @@ -472,13 +472,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t sort.Sort(valset.ValidatorsByAddress(newValidators)) - var blockExtraData types.BlockExtraData - if err := rlp.DecodeBytes(header.Extra[types.GetExtraVanity():len(header.Extra)-types.GetExtraSeal()], &blockExtraData); err != nil { - log.Error("error while decoding block extra data: %v", err) - return fmt.Errorf("error while decoding block extra data: %v", err) - } - - headerVals, err := valset.ParseValidators(blockExtraData.ValidatorBytes) + headerVals, err := valset.ParseValidators(header.GetValidatorBytes()) if err != nil { return err diff --git a/core/types/block.go b/core/types/block.go index 0069c6e04a..8d0e4a09a0 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -334,7 +334,7 @@ func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Ext func (b *Block) GetTxDependency() [][]uint64 { var blockExtraData BlockExtraData if err := rlp.DecodeBytes(b.header.Extra[extraVanity:len(b.header.Extra)-extraSeal], &blockExtraData); err != nil { - log.Error("error while decoding block extra data: %v", err) + log.Error("error while decoding block extra data", "err", err) return nil } @@ -344,7 +344,7 @@ func (b *Block) GetTxDependency() [][]uint64 { func (h *Header) GetValidatorBytes() []byte { var blockExtraData BlockExtraData if err := rlp.DecodeBytes(h.Extra[extraVanity:len(h.Extra)-extraSeal], &blockExtraData); err != nil { - log.Error("error while decoding block extra data: %v", err) + log.Error("error while decoding block extra data", "err", err) return nil } diff --git a/miner/worker.go b/miner/worker.go index 01540d9e2c..5d402141d4 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1201,7 +1201,7 @@ mainloop: } if err := rlp.DecodeBytes(env.header.Extra[types.GetExtraVanity():len(env.header.Extra)-types.GetExtraSeal()], &blockExtraData); err != nil { - log.Error("error while decoding block extra data: %v", err) + log.Error("error while decoding block extra data", "err", err) return false } From e1f262ac1f3b31fc47ee1d816f455ff632c06f02 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Thu, 13 Jul 2023 17:23:26 +0800 Subject: [PATCH 05/13] small improvement --- core/parallel_state_processor.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/parallel_state_processor.go b/core/parallel_state_processor.go index 6eea4225ec..6bb372f298 100644 --- a/core/parallel_state_processor.go +++ b/core/parallel_state_processor.go @@ -286,9 +286,11 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat coinbase, _ := p.bc.Engine().Author(header) - deps := GetDeps(block.GetTxDependency()) + blockTxDependency := block.GetTxDependency() - if block.GetTxDependency() != nil { + deps := GetDeps(blockTxDependency) + + if blockTxDependency != nil { metadata = true } @@ -308,7 +310,7 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat shouldDelayFeeCal = false } - if len(block.GetTxDependency()) != len(block.Transactions()) { + if len(blockTxDependency) != len(block.Transactions()) { task := &ExecutionTask{ msg: msg, config: p.config, From e0036d96ff42445fa0f9dc0de630503cf064d137 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 14 Jul 2023 12:31:57 +0800 Subject: [PATCH 06/13] minor bug fix --- core/types/block.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/types/block.go b/core/types/block.go index 8d0e4a09a0..3e7c7217f6 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -332,6 +332,11 @@ func (b *Block) UncleHash() common.Hash { return b.header.UncleHash } func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) } func (b *Block) GetTxDependency() [][]uint64 { + if len(b.header.Extra) < extraVanity+extraSeal { + log.Error("length of extra less is than vanity and seal") + return nil + } + var blockExtraData BlockExtraData if err := rlp.DecodeBytes(b.header.Extra[extraVanity:len(b.header.Extra)-extraSeal], &blockExtraData); err != nil { log.Error("error while decoding block extra data", "err", err) @@ -342,6 +347,11 @@ func (b *Block) GetTxDependency() [][]uint64 { } func (h *Header) GetValidatorBytes() []byte { + if len(h.Extra) < extraVanity+extraSeal { + log.Error("length of extra less is than vanity and seal") + return nil + } + var blockExtraData BlockExtraData if err := rlp.DecodeBytes(h.Extra[extraVanity:len(h.Extra)-extraSeal], &blockExtraData); err != nil { log.Error("error while decoding block extra data", "err", err) From 9953790c4f714a2028b29e62c6470fe883f1890e Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Fri, 14 Jul 2023 12:38:19 +0800 Subject: [PATCH 07/13] addressed comments --- consensus/bor/bor.go | 18 +++++++++--------- core/types/block.go | 20 ++++++-------------- miner/worker.go | 6 +++--- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index ac61afd2b1..e720837e90 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -117,11 +117,11 @@ func ecrecover(header *types.Header, sigcache *lru.ARCCache, c *params.BorConfig return address.(common.Address), nil } // Retrieve the signature from the header extra-data - if len(header.Extra) < types.GetExtraSeal() { + if len(header.Extra) < types.ExtraSealLength { return common.Address{}, errMissingSignature } - signature := header.Extra[len(header.Extra)-types.GetExtraSeal():] + signature := header.Extra[len(header.Extra)-types.ExtraSealLength:] // Recover the public key and the Ethereum address pubkey, err := crypto.Ecrecover(SealHash(header, c).Bytes(), signature) @@ -397,11 +397,11 @@ func (c *Bor) verifyHeader(chain consensus.ChainHeaderReader, header *types.Head // validateHeaderExtraField validates that the extra-data contains both the vanity and signature. // header.Extra = header.Vanity + header.ProducerBytes (optional) + header.Seal func validateHeaderExtraField(extraBytes []byte) error { - if len(extraBytes) < types.GetExtraVanity() { + if len(extraBytes) < types.ExtraVanityLength { return errMissingVanity } - if len(extraBytes) < types.GetExtraVanity()+types.GetExtraSeal() { + if len(extraBytes) < types.ExtraVanityLength+types.ExtraSealLength { return errMissingSignature } @@ -728,11 +728,11 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e header.Difficulty = new(big.Int).SetUint64(Difficulty(snap.ValidatorSet, currentSigner.signer)) // Ensure the extra data has all it's components - if len(header.Extra) < types.GetExtraVanity() { - header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, types.GetExtraVanity()-len(header.Extra))...) + if len(header.Extra) < types.ExtraVanityLength { + header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, types.ExtraVanityLength-len(header.Extra))...) } - header.Extra = header.Extra[:types.GetExtraVanity()] + header.Extra = header.Extra[:types.ExtraVanityLength] // get validator set if number if IsSprintStart(number+1, c.config.CalculateSprint(number)) { @@ -765,7 +765,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e } // add extra seal space - header.Extra = append(header.Extra, make([]byte, types.GetExtraSeal())...) + header.Extra = append(header.Extra, make([]byte, types.ExtraSealLength)...) // Mix digest is reserved for now, set to empty header.MixDigest = common.Hash{} @@ -1065,7 +1065,7 @@ func Sign(signFn SignerFn, signer common.Address, header *types.Header, c *param return err } - copy(header.Extra[len(header.Extra)-types.GetExtraSeal():], sighash) + copy(header.Extra[len(header.Extra)-types.ExtraSealLength:], sighash) return nil } diff --git a/core/types/block.go b/core/types/block.go index 3e7c7217f6..db622b24d7 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -36,18 +36,10 @@ var ( EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") EmptyUncleHash = rlpHash([]*Header(nil)) - extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity - extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal + ExtraVanityLength = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity + ExtraSealLength = 65 // Fixed number of extra-data suffix bytes reserved for signer seal ) -func GetExtraVanity() int { - return extraVanity -} - -func GetExtraSeal() int { - return extraSeal -} - // A BlockNonce is a 64-bit hash which proves (combined with the // mix-hash) that a sufficient amount of computation has been carried // out on a block. @@ -332,13 +324,13 @@ func (b *Block) UncleHash() common.Hash { return b.header.UncleHash } func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) } func (b *Block) GetTxDependency() [][]uint64 { - if len(b.header.Extra) < extraVanity+extraSeal { + if len(b.header.Extra) < ExtraVanityLength+ExtraSealLength { log.Error("length of extra less is than vanity and seal") return nil } var blockExtraData BlockExtraData - if err := rlp.DecodeBytes(b.header.Extra[extraVanity:len(b.header.Extra)-extraSeal], &blockExtraData); err != nil { + if err := rlp.DecodeBytes(b.header.Extra[ExtraVanityLength:len(b.header.Extra)-ExtraSealLength], &blockExtraData); err != nil { log.Error("error while decoding block extra data", "err", err) return nil } @@ -347,13 +339,13 @@ func (b *Block) GetTxDependency() [][]uint64 { } func (h *Header) GetValidatorBytes() []byte { - if len(h.Extra) < extraVanity+extraSeal { + if len(h.Extra) < ExtraVanityLength+ExtraSealLength { log.Error("length of extra less is than vanity and seal") return nil } var blockExtraData BlockExtraData - if err := rlp.DecodeBytes(h.Extra[extraVanity:len(h.Extra)-extraSeal], &blockExtraData); err != nil { + if err := rlp.DecodeBytes(h.Extra[ExtraVanityLength:len(h.Extra)-ExtraSealLength], &blockExtraData); err != nil { log.Error("error while decoding block extra data", "err", err) return nil } diff --git a/miner/worker.go b/miner/worker.go index 5d402141d4..635c579369 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1173,8 +1173,8 @@ mainloop: var blockExtraData types.BlockExtraData - tempVanity := env.header.Extra[:types.GetExtraVanity()] - tempSeal := env.header.Extra[types.GetExtraVanity() : len(env.header.Extra)-types.GetExtraSeal()] + tempVanity := env.header.Extra[:types.ExtraVanityLength] + tempSeal := env.header.Extra[types.ExtraVanityLength : len(env.header.Extra)-types.ExtraSealLength] if len(mvReadMapList) > 0 { tempDeps := make([][]uint64, len(mvReadMapList)) @@ -1200,7 +1200,7 @@ mainloop: } } - if err := rlp.DecodeBytes(env.header.Extra[types.GetExtraVanity():len(env.header.Extra)-types.GetExtraSeal()], &blockExtraData); err != nil { + if err := rlp.DecodeBytes(env.header.Extra[types.ExtraVanityLength:len(env.header.Extra)-types.ExtraSealLength], &blockExtraData); err != nil { log.Error("error while decoding block extra data", "err", err) return false } From 75b96bb84c9b6195f1efe5a59af06a72214fec43 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 18 Jul 2023 10:07:49 +0530 Subject: [PATCH 08/13] bug fix, only using the BlockExtraData structure after the Parallel Universe HF --- consensus/bor/bor.go | 56 +++++++++++++++++++++++++++------------ consensus/bor/snapshot.go | 7 ++++- core/types/block.go | 4 +-- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index e720837e90..0881b24fee 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -352,7 +352,12 @@ func (c *Bor) verifyHeader(chain consensus.ChainHeaderReader, header *types.Head isSprintEnd := IsSprintStart(number+1, c.config.CalculateSprint(number)) // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise - signersBytes := len(header.GetValidatorBytes()) + var signersBytes int + if c.config.IsParallelUniverse(header.Number) { + signersBytes = len(header.GetValidatorBytes()) + } else { + signersBytes = len(header.Extra) - types.ExtraVanityLength - types.ExtraSealLength + } if !isSprintEnd && signersBytes != 0 { return errExtraValidators } @@ -472,7 +477,12 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t sort.Sort(valset.ValidatorsByAddress(newValidators)) - headerVals, err := valset.ParseValidators(header.GetValidatorBytes()) + var headerVals []*valset.Validator + if c.config.IsParallelUniverse(header.Number) { + headerVals, err = valset.ParseValidators(header.GetValidatorBytes()) + } else { + headerVals, err = valset.ParseValidators(header.Extra[types.ExtraVanityLength : len(header.Extra)-types.ExtraSealLength]) + } if err != nil { return err @@ -491,7 +501,13 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t // verify the validator list in the last sprint block if IsSprintStart(number, c.config.CalculateSprint(number)) { - parentValidatorBytes := parent.GetValidatorBytes() + var parentValidatorBytes []byte + if c.config.IsParallelUniverse(parent.Number) { + parentValidatorBytes = parent.GetValidatorBytes() + } else { + parentValidatorBytes = parent.Extra[types.ExtraVanityLength : len(parent.Extra)-types.ExtraSealLength] + } + validatorsBytes := make([]byte, len(snap.ValidatorSet.Validators)*validatorHeaderBytesLength) currentValidators := snap.ValidatorSet.Copy().Validators @@ -744,24 +760,30 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e // sort validator by address sort.Sort(valset.ValidatorsByAddress(newValidators)) - var tempValidatorBytes []byte + if c.config.IsParallelUniverse(header.Number) { + var tempValidatorBytes []byte - for _, validator := range newValidators { - tempValidatorBytes = append(tempValidatorBytes, validator.HeaderBytes()...) - } + for _, validator := range newValidators { + tempValidatorBytes = append(tempValidatorBytes, validator.HeaderBytes()...) + } - blockExtraData := &types.BlockExtraData{ - ValidatorBytes: tempValidatorBytes, - TxDependency: nil, - } + blockExtraData := &types.BlockExtraData{ + ValidatorBytes: tempValidatorBytes, + TxDependency: nil, + } - blockExtraDataBytes, err := rlp.EncodeToBytes(blockExtraData) - if err != nil { - log.Error("error while encoding block extra data: %v", err) - return fmt.Errorf("error while encoding block extra data: %v", err) - } + blockExtraDataBytes, err := rlp.EncodeToBytes(blockExtraData) + if err != nil { + log.Error("error while encoding block extra data: %v", err) + return fmt.Errorf("error while encoding block extra data: %v", err) + } - header.Extra = append(header.Extra, blockExtraDataBytes...) + header.Extra = append(header.Extra, blockExtraDataBytes...) + } else { + for _, validator := range newValidators { + header.Extra = append(header.Extra, validator.HeaderBytes()...) + } + } } // add extra seal space diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index a421b8ccd9..40f5938125 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -150,7 +150,12 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { return nil, err } - validatorBytes := header.GetValidatorBytes() + var validatorBytes []byte + if s.config.IsParallelUniverse(header.Number) { + validatorBytes = header.GetValidatorBytes() + } else { + validatorBytes = header.Extra[types.ExtraVanityLength : len(header.Extra)-types.ExtraSealLength] + } // get validators from headers and use that for new validator set newVals, _ := valset.ParseValidators(validatorBytes) diff --git a/core/types/block.go b/core/types/block.go index db622b24d7..87af32d4ed 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -331,7 +331,7 @@ func (b *Block) GetTxDependency() [][]uint64 { var blockExtraData BlockExtraData if err := rlp.DecodeBytes(b.header.Extra[ExtraVanityLength:len(b.header.Extra)-ExtraSealLength], &blockExtraData); err != nil { - log.Error("error while decoding block extra data", "err", err) + log.Debug("error while decoding block extra data", "err", err) return nil } @@ -346,7 +346,7 @@ func (h *Header) GetValidatorBytes() []byte { var blockExtraData BlockExtraData if err := rlp.DecodeBytes(h.Extra[ExtraVanityLength:len(h.Extra)-ExtraSealLength], &blockExtraData); err != nil { - log.Error("error while decoding block extra data", "err", err) + log.Debug("error while decoding block extra data", "err", err) return nil } From 646a421232dcfedfdcd1731e932f19455f16cc84 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 18 Jul 2023 12:50:24 +0530 Subject: [PATCH 09/13] addressed comments --- consensus/bor/bor.go | 24 ++++-------------------- core/types/block.go | 7 ++++++- core/types/block_test.go | 4 +++- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 0881b24fee..8d46768d80 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -352,12 +352,8 @@ func (c *Bor) verifyHeader(chain consensus.ChainHeaderReader, header *types.Head isSprintEnd := IsSprintStart(number+1, c.config.CalculateSprint(number)) // Ensure that the extra-data contains a signer list on checkpoint, but none otherwise - var signersBytes int - if c.config.IsParallelUniverse(header.Number) { - signersBytes = len(header.GetValidatorBytes()) - } else { - signersBytes = len(header.Extra) - types.ExtraVanityLength - types.ExtraSealLength - } + signersBytes := len(header.GetValidatorBytes(c.config)) + if !isSprintEnd && signersBytes != 0 { return errExtraValidators } @@ -477,13 +473,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t sort.Sort(valset.ValidatorsByAddress(newValidators)) - var headerVals []*valset.Validator - if c.config.IsParallelUniverse(header.Number) { - headerVals, err = valset.ParseValidators(header.GetValidatorBytes()) - } else { - headerVals, err = valset.ParseValidators(header.Extra[types.ExtraVanityLength : len(header.Extra)-types.ExtraSealLength]) - } - + headerVals, err := valset.ParseValidators(header.GetValidatorBytes(c.config)) if err != nil { return err } @@ -501,13 +491,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t // verify the validator list in the last sprint block if IsSprintStart(number, c.config.CalculateSprint(number)) { - var parentValidatorBytes []byte - if c.config.IsParallelUniverse(parent.Number) { - parentValidatorBytes = parent.GetValidatorBytes() - } else { - parentValidatorBytes = parent.Extra[types.ExtraVanityLength : len(parent.Extra)-types.ExtraSealLength] - } - + parentValidatorBytes := parent.GetValidatorBytes(c.config) validatorsBytes := make([]byte, len(snap.ValidatorSet.Validators)*validatorHeaderBytesLength) currentValidators := snap.ValidatorSet.Copy().Validators diff --git a/core/types/block.go b/core/types/block.go index 87af32d4ed..6b7d074f30 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" ) @@ -338,7 +339,11 @@ func (b *Block) GetTxDependency() [][]uint64 { return blockExtraData.TxDependency } -func (h *Header) GetValidatorBytes() []byte { +func (h *Header) GetValidatorBytes(config *params.BorConfig) []byte { + if config.IsParallelUniverse(h.Number) { + return h.Extra[ExtraVanityLength : len(h.Extra)-ExtraSealLength] + } + if len(h.Extra) < ExtraVanityLength+ExtraSealLength { log.Error("length of extra less is than vanity and seal") return nil diff --git a/core/types/block_test.go b/core/types/block_test.go index 82f4046099..2f2addcab4 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -93,7 +93,9 @@ func TestTxDependencyBlockDecoding(t *testing.T) { check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017")) check("Time", block.Time(), uint64(1426516743)) - validatorBytes := block.header.GetValidatorBytes() + validatorBytes := block.header.GetValidatorBytes(¶ms.BorConfig{ + ParallelUniverseBlock: big.NewInt(0), + }) txDependency := block.GetTxDependency() check("validatorBytes", validatorBytes, []byte("val set")) From c90a2b321cc739c2827b68aeff9fee8f1509ba6d Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 18 Jul 2023 16:39:21 +0530 Subject: [PATCH 10/13] fixed an error --- consensus/bor/snapshot.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index 40f5938125..7ced09c3b8 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -150,12 +150,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { return nil, err } - var validatorBytes []byte - if s.config.IsParallelUniverse(header.Number) { - validatorBytes = header.GetValidatorBytes() - } else { - validatorBytes = header.Extra[types.ExtraVanityLength : len(header.Extra)-types.ExtraSealLength] - } + validatorBytes := header.GetValidatorBytes(s.config) // get validators from headers and use that for new validator set newVals, _ := valset.ParseValidators(validatorBytes) From ff002f97617b64cb46c5a6b2a7ce9dca2ca525e8 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Tue, 18 Jul 2023 17:41:08 +0530 Subject: [PATCH 11/13] bug fix --- core/types/block.go | 2 +- core/types/block_test.go | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 6b7d074f30..e60de10539 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -340,7 +340,7 @@ func (b *Block) GetTxDependency() [][]uint64 { } func (h *Header) GetValidatorBytes(config *params.BorConfig) []byte { - if config.IsParallelUniverse(h.Number) { + if !config.IsParallelUniverse(h.Number) { return h.Extra[ExtraVanityLength : len(h.Extra)-ExtraSealLength] } diff --git a/core/types/block_test.go b/core/types/block_test.go index 2f2addcab4..eeca572d7a 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/crypto/sha3" @@ -68,6 +69,23 @@ func TestBlockEncoding(t *testing.T) { } } +// This is a replica of `(h *Header) GetValidatorBytes` function +// This was needed because currently, `IsParallelUniverse` will always return false. +func GetValidatorBytesTest(h *Header) []byte { + if len(h.Extra) < ExtraVanityLength+ExtraSealLength { + log.Error("length of extra less is than vanity and seal") + return nil + } + + var blockExtraData BlockExtraData + if err := rlp.DecodeBytes(h.Extra[ExtraVanityLength:len(h.Extra)-ExtraSealLength], &blockExtraData); err != nil { + log.Debug("error while decoding block extra data", "err", err) + return nil + } + + return blockExtraData.ValidatorBytes +} + func TestTxDependencyBlockDecoding(t *testing.T) { t.Parallel() @@ -93,9 +111,7 @@ func TestTxDependencyBlockDecoding(t *testing.T) { check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017")) check("Time", block.Time(), uint64(1426516743)) - validatorBytes := block.header.GetValidatorBytes(¶ms.BorConfig{ - ParallelUniverseBlock: big.NewInt(0), - }) + validatorBytes := GetValidatorBytesTest(block.header) txDependency := block.GetTxDependency() check("validatorBytes", validatorBytes, []byte("val set")) From ca6223f975042fbcf50dd4526da23b6b71c97f8e Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 19 Jul 2023 10:35:43 +0530 Subject: [PATCH 12/13] encoding nil ValidatorBytes and TxDependency in prepare if the current block is not the last block of the sprint --- consensus/bor/bor.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 8d46768d80..fc6b1a0b62 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -768,6 +768,19 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e header.Extra = append(header.Extra, validator.HeaderBytes()...) } } + } else if c.config.IsParallelUniverse(header.Number) { + blockExtraData := &types.BlockExtraData{ + ValidatorBytes: nil, + TxDependency: nil, + } + + blockExtraDataBytes, err := rlp.EncodeToBytes(blockExtraData) + if err != nil { + log.Error("error while encoding block extra data: %v", err) + return fmt.Errorf("error while encoding block extra data: %v", err) + } + + header.Extra = append(header.Extra, blockExtraDataBytes...) } // add extra seal space From b2eb50f1c31f5a44c1cedfabe3abc1cfc05e44cf Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 19 Jul 2023 16:50:02 +0530 Subject: [PATCH 13/13] bug fix --- miner/worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miner/worker.go b/miner/worker.go index 635c579369..7c726f1209 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1174,7 +1174,7 @@ mainloop: var blockExtraData types.BlockExtraData tempVanity := env.header.Extra[:types.ExtraVanityLength] - tempSeal := env.header.Extra[types.ExtraVanityLength : len(env.header.Extra)-types.ExtraSealLength] + tempSeal := env.header.Extra[len(env.header.Extra)-types.ExtraSealLength:] if len(mvReadMapList) > 0 { tempDeps := make([][]uint64, len(mvReadMapList))