-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ETH1 Block SSZ Hashing to CL (#6549)
more of SSZ again.
- Loading branch information
1 parent
a4751aa
commit 4bee05d
Showing
20 changed files
with
397 additions
and
643 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package cltypes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ledgerwatch/erigon/cl/clparams" | ||
"github.com/ledgerwatch/erigon/cl/cltypes/ssz_utils" | ||
"github.com/ledgerwatch/erigon/cl/merkle_tree" | ||
"github.com/ledgerwatch/erigon/common" | ||
"github.com/ledgerwatch/erigon/core/types" | ||
) | ||
|
||
// ETH1Block represents a block structure CL-side. | ||
type Eth1Block struct { | ||
Header *types.Header | ||
// Transactions can be kept in bytes. | ||
Body *types.RawBody | ||
} | ||
|
||
func (b *Eth1Block) NumberU64() uint64 { | ||
return b.Header.Number.Uint64() | ||
} | ||
|
||
func (b *Eth1Block) Withdrawals() types.Withdrawals { | ||
return types.Withdrawals(b.Body.Withdrawals) | ||
} | ||
|
||
func (b *Eth1Block) EncodingSizeSSZ() (size int) { | ||
size = 508 | ||
|
||
if b.Header == nil { | ||
return | ||
} | ||
// Field (10) 'ExtraData' | ||
size += len(b.Header.Extra) | ||
// Field (13) 'Transactions' | ||
for _, tx := range b.Body.Transactions { | ||
size += 4 | ||
size += len(tx) | ||
} | ||
|
||
if b.Header.WithdrawalsHash != nil { | ||
size += len(b.Body.Withdrawals) * 44 | ||
} | ||
|
||
return | ||
} | ||
|
||
func (b *Eth1Block) DecodeSSZ(buf []byte, version clparams.StateVersion) error { | ||
if len(buf) < b.EncodingSizeSSZ() { | ||
return ssz_utils.ErrLowBufferSize | ||
} | ||
b.Header = new(types.Header) | ||
|
||
pos := b.Header.DecodeHeaderMetadataForSSZ(buf) | ||
// Compute block SSZ offsets. | ||
extraDataOffset := ssz_utils.BaseExtraDataSSZOffsetBlock | ||
transactionsOffset := ssz_utils.DecodeOffset(buf[pos : pos+4]) | ||
pos += 4 | ||
var withdrawalOffset *uint32 | ||
if version >= clparams.CapellaVersion { | ||
withdrawalOffset = new(uint32) | ||
*withdrawalOffset = ssz_utils.DecodeOffset(buf[pos : pos+4]) | ||
} | ||
// Compute extra data. | ||
b.Header.Extra = common.CopyBytes(buf[extraDataOffset:transactionsOffset]) | ||
if len(b.Header.Extra) > 32 { | ||
return fmt.Errorf("Decode(SSZ): Extra data field length should be less or equal to 32, got %d", len(b.Header.Extra)) | ||
} | ||
// Compute transactions | ||
var transactionsBuffer []byte | ||
if withdrawalOffset == nil { | ||
transactionsBuffer = buf[transactionsOffset:] | ||
} else { | ||
transactionsBuffer = buf[transactionsOffset:*withdrawalOffset] | ||
} | ||
|
||
length := uint32(0) | ||
transactionsPosition := 4 | ||
var txOffset uint32 | ||
if len(transactionsBuffer) == 0 { | ||
length = 0 | ||
} else { | ||
if len(transactionsBuffer) < 4 { | ||
return ssz_utils.ErrLowBufferSize | ||
} | ||
txOffset = ssz_utils.DecodeOffset(transactionsBuffer) | ||
length = txOffset / 4 | ||
// Retrieve tx length | ||
if txOffset%4 != 0 { | ||
return ssz_utils.ErrBadDynamicLength | ||
} | ||
} | ||
|
||
b.Body = new(types.RawBody) | ||
b.Body.Transactions = make([][]byte, length) | ||
txIdx := 0 | ||
// Loop through each transaction | ||
for length > 0 { | ||
var txEndOffset uint32 | ||
if length == 1 { | ||
txEndOffset = uint32(len(transactionsBuffer)) | ||
} else { | ||
txEndOffset = ssz_utils.DecodeOffset(transactionsBuffer[transactionsPosition:]) | ||
} | ||
transactionsPosition += 4 | ||
if txOffset > txEndOffset { | ||
return ssz_utils.ErrBadOffset | ||
} | ||
b.Body.Transactions[txIdx] = transactionsBuffer[txOffset:txEndOffset] | ||
// Decode RLP and put it in the tx list. | ||
// Update parameters for next iteration | ||
txOffset = txEndOffset | ||
txIdx++ | ||
length-- | ||
} | ||
// Cache transaction ssz root. | ||
var err error | ||
b.Header.TxHashSSZ, err = merkle_tree.TransactionsListRoot(b.Body.Transactions) | ||
if err != nil { | ||
return err | ||
} | ||
// Cache transaction rlp hash. | ||
b.Header.TxHash = types.DeriveSha(types.BinaryTransactions(b.Body.Transactions)) | ||
// If withdrawals are enabled, process them. | ||
if withdrawalOffset != nil { | ||
withdrawalsCount := (uint32(len(buf)) - *withdrawalOffset) / 44 | ||
if withdrawalsCount > 16 { | ||
return fmt.Errorf("Decode(SSZ): Withdrawals field length should be less or equal to 16, got %d", withdrawalsCount) | ||
} | ||
b.Body.Withdrawals = make([]*types.Withdrawal, withdrawalsCount) | ||
for i := range b.Body.Withdrawals { | ||
b.Body.Withdrawals[i].DecodeSSZ(buf[(*withdrawalOffset)+uint32(i)*44:]) | ||
} | ||
// Cache withdrawal root. | ||
b.Header.WithdrawalsHash = new(common.Hash) | ||
withdrawalRoot, err := b.Withdrawals().HashSSZ(16) | ||
if err != nil { | ||
return err | ||
} | ||
*b.Header.WithdrawalsHash = withdrawalRoot | ||
} | ||
return nil | ||
} | ||
|
||
func (b *Eth1Block) EncodeSSZ(dst []byte) ([]byte, error) { | ||
buf := dst | ||
var err error | ||
currentOffset := ssz_utils.BaseExtraDataSSZOffsetBlock | ||
|
||
if b.Header.WithdrawalsHash != nil { | ||
currentOffset += 4 | ||
} | ||
buf, err = b.Header.EncodeHeaderMetadataForSSZ(buf, currentOffset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
currentOffset += len(b.Header.Extra) | ||
// use raw body for encoded txs and offsets. | ||
body := b.Body | ||
// Write transaction offset | ||
buf = append(buf, ssz_utils.OffsetSSZ(uint32(currentOffset))...) | ||
|
||
for _, tx := range body.Transactions { | ||
currentOffset += len(tx) + 4 | ||
} | ||
// Write withdrawals offset if exist | ||
if b.Header.WithdrawalsHash != nil { | ||
buf = append(buf, ssz_utils.OffsetSSZ(uint32(currentOffset))...) | ||
} | ||
// Sanity check for extra data then write it. | ||
if len(b.Header.Extra) > 32 { | ||
return nil, fmt.Errorf("Encode(SSZ): Extra data field length should be less or equal to 32, got %d", len(b.Header.Extra)) | ||
} | ||
buf = append(buf, b.Header.Extra...) | ||
// Write all tx offsets | ||
txOffset := len(body.Transactions) * 4 | ||
for _, tx := range body.Transactions { | ||
buf = append(buf, ssz_utils.OffsetSSZ(uint32(txOffset))...) | ||
txOffset += len(tx) | ||
} | ||
// Write all transactions | ||
for _, tx := range body.Transactions { | ||
buf = append(buf, tx...) | ||
} | ||
|
||
if b.Header.WithdrawalsHash != nil { | ||
// Append all withdrawals SSZ | ||
for _, withdrawal := range body.Withdrawals { | ||
buf = append(buf, withdrawal.EncodeSSZ()...) | ||
} | ||
} | ||
return buf, nil | ||
} | ||
|
||
func (b *Eth1Block) HashSSZ() ([32]byte, error) { | ||
return b.Header.HashSSZ() | ||
} | ||
|
||
func (b *Eth1Block) Hash() common.Hash { | ||
return b.Header.Hash() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ssz_utils | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrLowBufferSize = errors.New("ssz(DecodeSSZ): bad encoding size") | ||
ErrBadDynamicLength = errors.New("ssz(DecodeSSZ): bad dynamic length") | ||
ErrBadOffset = errors.New("ssz(DecodeSSZ): invalid offset") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.