prysm-pulse/encoding/ssz/htrutils.go
Radosław Kapka cb02a6897d
Update block Beacon APIs to Capella (#11706)
* proto+ssz

* refactor GetBlindedBlockSSZ

(cherry picked from commit 97483c339f99b0d96bd81846a979383ffd2b0cda)

# Conflicts:
#	beacon-chain/rpc/eth/beacon/blocks.go
(cherry picked from commit 9e4e82d2c55e09be7568b28eaa33cdd1141445f4)

# Conflicts:
#	beacon-chain/rpc/eth/beacon/blocks.go

* add Capella version

(cherry picked from commit 5d6fd0bbe663e5dd16df5b2e773f68982bbcd24e)
(cherry picked from commit 82f6ddb693ac9e8b4336b30fae724e478b9e8ec0)

* support SSZ lol

(cherry picked from commit 52bc2c8d617ac3e1254c493fa053cdce4a1ebd63)
(cherry picked from commit d7d70bc25b3ee8acbea10aaf77d26cd1f8c5f26f)

* update withdrawals proto

* refactor and test GetBlockV2

(cherry picked from commit c1d4eaa79d4df04bd284ec65cf261b6f5f260a97)

# Conflicts:
#	beacon-chain/rpc/eth/beacon/blocks.go

* refactor and test GetSSZBlockV2

(cherry picked from commit fbc4e73d31c2f68d55d1e2bb8e7f2d8c7458c0a0)

# Conflicts:
#	beacon-chain/rpc/eth/beacon/blocks.go

* test other functions

(cherry picked from commit 31d4a4cd1165b882d823696e5983ac6635262ec2)

* move stuff to blinded_blocks.go

(cherry picked from commit 0a9e1658ddb28f45ae5c1cb9fc2703cbb8c6708d)

# Conflicts:
#	beacon-chain/rpc/eth/beacon/blocks.go

* fix migration code

* add Capella to SubmitBlock

* custom hooks

* missing structs

* fix tests

* fix tests

* review

* fix build issues

* replace ioutil with io

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2022-12-15 23:42:07 +00:00

179 lines
6.7 KiB
Go

package ssz
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
"github.com/prysmaticlabs/prysm/v3/crypto/hash"
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
)
// Uint64Root computes the HashTreeRoot Merkleization of
// a simple uint64 value according to the Ethereum
// Simple Serialize specification.
func Uint64Root(val uint64) [32]byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, val)
root := bytesutil.ToBytes32(buf)
return root
}
// ForkRoot computes the HashTreeRoot Merkleization of
// a Fork struct value according to the Ethereum
// Simple Serialize specification.
func ForkRoot(fork *ethpb.Fork) ([32]byte, error) {
fieldRoots := make([][32]byte, 3)
if fork != nil {
fieldRoots[0] = bytesutil.ToBytes32(fork.PreviousVersion)
fieldRoots[1] = bytesutil.ToBytes32(fork.CurrentVersion)
forkEpochBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(forkEpochBuf, uint64(fork.Epoch))
fieldRoots[2] = bytesutil.ToBytes32(forkEpochBuf)
}
return BitwiseMerkleize(hash.CustomSHA256Hasher(), fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
// CheckpointRoot computes the HashTreeRoot Merkleization of
// a InitWithReset struct value according to the Ethereum
// Simple Serialize specification.
func CheckpointRoot(hasher HashFn, checkpoint *ethpb.Checkpoint) ([32]byte, error) {
fieldRoots := make([][32]byte, 2)
if checkpoint != nil {
epochBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(epochBuf, uint64(checkpoint.Epoch))
fieldRoots[0] = bytesutil.ToBytes32(epochBuf)
fieldRoots[1] = bytesutil.ToBytes32(checkpoint.Root)
}
return BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
// ByteArrayRootWithLimit computes the HashTreeRoot Merkleization of
// a list of [32]byte roots according to the Ethereum Simple Serialize
// specification.
func ByteArrayRootWithLimit(roots [][]byte, limit uint64) ([32]byte, error) {
newRoots := make([][32]byte, len(roots))
for i, r := range roots {
copy(newRoots[i][:], r)
}
result, err := BitwiseMerkleize(hash.CustomSHA256Hasher(), newRoots, uint64(len(newRoots)), limit)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute byte array merkleization")
}
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, uint64(len(newRoots))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal byte array length")
}
// We need to mix in the length of the slice.
output := make([]byte, 32)
copy(output, buf.Bytes())
mixedLen := MixInLength(result, output)
return mixedLen, nil
}
// SlashingsRoot computes the HashTreeRoot Merkleization of
// a list of uint64 slashing values according to the Ethereum
// Simple Serialize specification.
func SlashingsRoot(slashings []uint64) ([32]byte, error) {
slashingMarshaling := make([][]byte, fieldparams.SlashingsLength)
for i := 0; i < len(slashings) && i < len(slashingMarshaling); i++ {
slashBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(slashBuf, slashings[i])
slashingMarshaling[i] = slashBuf
}
slashingChunks, err := PackByChunk(slashingMarshaling)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not pack slashings into chunks")
}
return BitwiseMerkleize(hash.CustomSHA256Hasher(), slashingChunks, uint64(len(slashingChunks)), uint64(len(slashingChunks)))
}
// TransactionsRoot computes the HTR for the Transactions' property of the ExecutionPayload
// The code was largely copy/pasted from the code generated to compute the HTR of the entire
// ExecutionPayload.
func TransactionsRoot(txs [][]byte) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher()
txRoots := make([][32]byte, 0)
for i := 0; i < len(txs); i++ {
rt, err := transactionRoot(txs[i])
if err != nil {
return [32]byte{}, err
}
txRoots = append(txRoots, rt)
}
bytesRoot, err := BitwiseMerkleize(hasher, txRoots, uint64(len(txRoots)), fieldparams.MaxTxsPerPayloadLength)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute merkleization")
}
bytesRootBuf := new(bytes.Buffer)
if err := binary.Write(bytesRootBuf, binary.LittleEndian, uint64(len(txs))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal length")
}
bytesRootBufRoot := make([]byte, 32)
copy(bytesRootBufRoot, bytesRootBuf.Bytes())
return MixInLength(bytesRoot, bytesRootBufRoot), nil
}
// WithdrawalSliceRoot computes the HTR of a slice of withdrawals.
// The limit parameter is used as input to the bitwise merkleization algorithm.
func WithdrawalSliceRoot(hasher HashFn, withdrawals []*enginev1.Withdrawal, limit uint64) ([32]byte, error) {
roots := make([][32]byte, len(withdrawals))
for i := 0; i < len(withdrawals); i++ {
r, err := withdrawalRoot(hasher, withdrawals[i])
if err != nil {
return [32]byte{}, err
}
roots[i] = r
}
bytesRoot, err := BitwiseMerkleize(hasher, roots, uint64(len(roots)), limit)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute merkleization")
}
bytesRootBuf := new(bytes.Buffer)
if err := binary.Write(bytesRootBuf, binary.LittleEndian, uint64(len(withdrawals))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal length")
}
bytesRootBufRoot := make([]byte, 32)
copy(bytesRootBufRoot, bytesRootBuf.Bytes())
return MixInLength(bytesRoot, bytesRootBufRoot), nil
}
func transactionRoot(tx []byte) ([32]byte, error) {
hasher := hash.CustomSHA256Hasher()
chunkedRoots, err := PackByChunk([][]byte{tx})
if err != nil {
return [32]byte{}, err
}
maxLength := (fieldparams.MaxBytesPerTxLength + 31) / 32
bytesRoot, err := BitwiseMerkleize(hasher, chunkedRoots, uint64(len(chunkedRoots)), uint64(maxLength))
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute merkleization")
}
bytesRootBuf := new(bytes.Buffer)
if err := binary.Write(bytesRootBuf, binary.LittleEndian, uint64(len(tx))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal length")
}
bytesRootBufRoot := make([]byte, 32)
copy(bytesRootBufRoot, bytesRootBuf.Bytes())
return MixInLength(bytesRoot, bytesRootBufRoot), nil
}
func withdrawalRoot(hasher HashFn, w *enginev1.Withdrawal) ([32]byte, error) {
fieldRoots := make([][32]byte, 4)
if w != nil {
binary.LittleEndian.PutUint64(fieldRoots[0][:], w.Index)
binary.LittleEndian.PutUint64(fieldRoots[1][:], uint64(w.ValidatorIndex))
fieldRoots[2] = bytesutil.ToBytes32(w.Address)
binary.LittleEndian.PutUint64(fieldRoots[3][:], w.Amount)
}
return BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}