Skip to content

Commit

Permalink
addressed comments, moved the functions to consensus/bor
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikspatil024 committed Oct 18, 2023
1 parent c690cb2 commit c1f2abb
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 57 deletions.
61 changes: 56 additions & 5 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion consensus/bor/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 0 additions & 48 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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}
Expand Down
32 changes: 29 additions & 3 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ 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 {
log.Error("length of extra is less than vanity and seal")
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
Expand All @@ -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()

Expand All @@ -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}})
Expand Down

0 comments on commit c1f2abb

Please sign in to comment.