From c1f2abb07c1fb4d2c296ec6424ab5593279109c9 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 18 Oct 2023 14:27:18 +0530 Subject: [PATCH] addressed comments, moved the functions to consensus/bor --- consensus/bor/bor.go | 61 +++++++++++++++++++++++++++++++++++---- consensus/bor/snapshot.go | 2 +- core/types/block.go | 48 ------------------------------ core/types/block_test.go | 32 ++++++++++++++++++-- 4 files changed, 86 insertions(+), 57 deletions(-) diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index c812877d9cb..0bc8d2b1f90 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -474,7 +474,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.GetValidatorBytes(c.config)) + signersBytes := len(GetValidatorBytes(header, c.config)) if !isSprintEnd && signersBytes != 0 { return errExtraValidators } @@ -595,7 +595,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t sort.Sort(valset.ValidatorsByAddress(producerSet)) - headerVals, err := valset.ParseValidators(header.GetValidatorBytes(c.config)) + headerVals, err := valset.ParseValidators(GetValidatorBytes(header, c.config)) if err != nil { return err @@ -619,7 +619,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t // verify the validator list in the last sprint block if isSprintStart(number, sprintLength) { // Retrieve the snapshot needed to verify this header and cache it - parentValidatorBytes := parent.GetValidatorBytes(c.config) + parentValidatorBytes := GetValidatorBytes(parent, c.config) validatorsBytes := make([]byte, len(snap.ValidatorSet.Validators)*validatorHeaderBytesLength) currentValidators := snap.ValidatorSet.Copy().Validators @@ -893,7 +893,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header, s tempValidatorBytes = append(tempValidatorBytes, validator.HeaderBytes()...) } - blockExtraData := &types.BlockExtraData{ + blockExtraData := &BlockExtraData{ ValidatorBytes: tempValidatorBytes, TxDependency: nil, } @@ -911,7 +911,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header, s } } } else if c.config.IsParallelUniverse(header.Number.Uint64()) { - blockExtraData := &types.BlockExtraData{ + blockExtraData := &BlockExtraData{ ValidatorBytes: nil, TxDependency: nil, } @@ -1530,3 +1530,54 @@ func getUpdatedValidatorSet(oldValidatorSet *valset.ValidatorSet, newVals []*val func isSprintStart(number, sprint uint64) bool { return number%sprint == 0 } + +// In bor, RLP encoding of BlockExtraData will be stored in the Extra field in the header +type BlockExtraData struct { + // Validator bytes of bor + 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 +} + +// Returns the Block-STM Transaction Dependency from the block header +func GetTxDependency(b *types.Block) [][]uint64 { + tempExtra := b.Extra() + + if len(tempExtra) < types.ExtraVanityLength+types.ExtraSealLength { + log.Error("length of extra less is than vanity and seal") + return nil + } + + var blockExtraData BlockExtraData + + if err := rlp.DecodeBytes(tempExtra[types.ExtraVanityLength:len(tempExtra)-types.ExtraSealLength], &blockExtraData); err != nil { + log.Error("error while decoding block extra data", "err", err) + return nil + } + + return blockExtraData.TxDependency +} + +func GetValidatorBytes(h *types.Header, config *chain.BorConfig) []byte { + tempExtra := h.Extra + + if !config.IsParallelUniverse(h.Number.Uint64()) { + return tempExtra[types.ExtraVanityLength : len(tempExtra)-types.ExtraSealLength] + } + + if len(tempExtra) < types.ExtraVanityLength+types.ExtraSealLength { + log.Error("length of extra less is than vanity and seal") + return nil + } + + var blockExtraData BlockExtraData + if err := rlp.DecodeBytes(tempExtra[types.ExtraVanityLength:len(tempExtra)-types.ExtraSealLength], &blockExtraData); err != nil { + log.Error("error while decoding block extra data", "err", err) + return nil + } + + return blockExtraData.ValidatorBytes +} diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index a0d2717f34b..40612097f9c 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -169,7 +169,7 @@ func (s *Snapshot) apply(headers []*types.Header, logger log.Logger) (*Snapshot, if err := validateHeaderExtraField(header.Extra); err != nil { return nil, err } - validatorBytes := header.GetValidatorBytes(s.config) + validatorBytes := GetValidatorBytes(header, s.config) // 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 2938f1116bf..d132c0b155c 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -29,11 +29,9 @@ import ( "sync/atomic" "github.com/gballet/go-verkle" - "github.com/ledgerwatch/erigon-lib/chain" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/hexutility" rlp2 "github.com/ledgerwatch/erigon-lib/rlp" - "github.com/ledgerwatch/log/v3" "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/common/hexutil" @@ -48,17 +46,6 @@ var ( ExtraSealLength = 65 // Fixed number of extra-data suffix bytes reserved for signer seal ) -// In bor, RLP encoding of BlockExtraData will be stored in the Extra field in the header -type BlockExtraData struct { - // Validator bytes of bor - 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 -} - // 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. @@ -1508,41 +1495,6 @@ func (b *Block) ParentBeaconBlockRoot() *libcommon.Hash { return b.header.Parent func (b *Block) Header() *Header { return CopyHeader(b.header) } func (b *Block) HeaderNoCopy() *Header { return b.header } -// Returns the Block-STM Transaction Dependency from the block header -func (b *Block) GetTxDependency() [][]uint64 { - 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[ExtraVanityLength:len(b.header.Extra)-ExtraSealLength], &blockExtraData); err != nil { - log.Error("error while decoding block extra data", "err", err) - return nil - } - - return blockExtraData.TxDependency -} - -func (h *Header) GetValidatorBytes(config *chain.BorConfig) []byte { - if !config.IsParallelUniverse(h.Number.Uint64()) { - 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 - } - - 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) - return nil - } - - return blockExtraData.ValidatorBytes -} - // Body returns the non-header content of the block. func (b *Block) Body() *Body { bd := &Body{Transactions: b.transactions, Uncles: b.uncles, Withdrawals: b.withdrawals} diff --git a/core/types/block_test.go b/core/types/block_test.go index cb81ca06d83..afd1d801cb2 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -40,7 +40,8 @@ import ( "github.com/ledgerwatch/erigon/rlp" ) -// This is a replica of `(h *Header) GetValidatorBytes` function +// the following 2 functions are replica for the test +// This is a replica of `bor.GetValidatorBytes` function // This was needed because currently, `IsParallelUniverse` will always return false. func GetValidatorBytesTest(h *Header) []byte { if len(h.Extra) < ExtraVanityLength+ExtraSealLength { @@ -48,7 +49,7 @@ func GetValidatorBytesTest(h *Header) []byte { return nil } - var blockExtraData BlockExtraData + var blockExtraData BlockExtraDataTest 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 @@ -57,6 +58,31 @@ func GetValidatorBytesTest(h *Header) []byte { return blockExtraData.ValidatorBytes } +func GetTxDependencyTest(b *Block) [][]uint64 { + if len(b.header.Extra) < ExtraVanityLength+ExtraSealLength { + log.Error("length of extra less is than vanity and seal") + return nil + } + + var blockExtraData BlockExtraDataTest + 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 + } + + return blockExtraData.TxDependency +} + +type BlockExtraDataTest struct { + // Validator bytes of bor + 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 +} + func TestTxDependencyBlockDecoding(t *testing.T) { t.Parallel() @@ -79,7 +105,7 @@ func TestTxDependencyBlockDecoding(t *testing.T) { check("Time", block.Time(), uint64(1426516743)) validatorBytes := GetValidatorBytesTest(block.header) - txDependency := block.GetTxDependency() + txDependency := GetTxDependencyTest(&block) check("validatorBytes", validatorBytes, []byte("val set")) check("txDependency", txDependency, [][]uint64{{2, 1}, {1, 0}})