mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 12:40:05 +00:00
cafe0bd1f8
* fork * types * cloners * getters * remove CapellaBlind from fork * hasher * setters * spec params, config tests * generate ssz * executionPayloadHeaderCapella * proto state * BeaconStateCapella SSZ * saving state * configfork * BUILD files * fix RealPosition * fix hasher * SetLatestExecutionPayloadHeaderCapella * fix error message * reduce complexity of saveStatesEfficientInternal * add latestExecutionPayloadHeaderCapella to minimal state * halway done interface * remove withdrawal methods * merge setters * change signatures for v1 and v2 * fixing errors pt. 1 * paylod_test fixes * fix everything * remove unused func * fix tests * state_trie_test improvements * in progress... * hasher test * fix configs * simplify hashing * Revert "fix configs" This reverts commit bcae2825fcc8ba45a2b43d68ad0ab57f8eac8952. * remove capella from config test * unify locking * review * hashing * fixes Co-authored-by: terencechain <terence@prysmaticlabs.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
180 lines
6.7 KiB
Go
180 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, 3)
|
|
if w != nil {
|
|
indexBuf := make([]byte, 32)
|
|
binary.LittleEndian.PutUint64(indexBuf, w.WithdrawalIndex)
|
|
fieldRoots[0] = bytesutil.ToBytes32(indexBuf)
|
|
fieldRoots[1] = bytesutil.ToBytes32(w.ExecutionAddress)
|
|
amountBuf := make([]byte, 8)
|
|
binary.LittleEndian.PutUint64(amountBuf, w.Amount)
|
|
fieldRoots[2] = bytesutil.ToBytes32(amountBuf)
|
|
}
|
|
return BitwiseMerkleize(hasher, fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
|
|
}
|