2023-06-11 21:50:02 +00:00
|
|
|
package eth2
|
2023-05-13 19:52:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2023-07-28 10:12:05 +00:00
|
|
|
|
2023-05-13 19:52:49 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2023-07-28 10:12:05 +00:00
|
|
|
|
2023-07-19 22:20:33 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/abstract"
|
2023-05-13 19:52:49 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/utils"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const VERSIONED_HASH_VERSION_KZG byte = byte(1)
|
|
|
|
|
|
|
|
func kzgCommitmentToVersionedHash(kzgCommitment *cltypes.KZGCommitment) (libcommon.Hash, error) {
|
|
|
|
versionedHash := [32]byte{}
|
2023-11-22 12:24:35 +00:00
|
|
|
kzgCommitmentHash := utils.Sha256(kzgCommitment[:])
|
2023-05-13 19:52:49 +00:00
|
|
|
|
|
|
|
buf := append([]byte{}, VERSIONED_HASH_VERSION_KZG)
|
|
|
|
buf = append(buf, kzgCommitmentHash[1:]...)
|
|
|
|
copy(versionedHash[:], buf)
|
|
|
|
|
|
|
|
return versionedHash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func txPeekBlobVersionedHashes(txBytes []byte) []libcommon.Hash {
|
|
|
|
if txBytes[0] != types.BlobTxType {
|
|
|
|
return []libcommon.Hash{}
|
|
|
|
}
|
|
|
|
|
|
|
|
messageOffset := 1 + binary.LittleEndian.Uint32(txBytes[1:5])
|
|
|
|
|
|
|
|
/*
|
|
|
|
https://gist.github.com/protolambda/23bd106b66f6d4bb854ce46044aa3ca3
|
|
|
|
chain_id: 32 bytes
|
|
|
|
nonce: 8 bytes
|
|
|
|
priority_fee_per_gas: 32 bytes
|
|
|
|
max_basefee_per_gas: 32 bytes
|
|
|
|
gas: 8 bytes
|
|
|
|
to: 4 bytes - offset to B (relative to A)
|
|
|
|
value: 32 bytes
|
|
|
|
data: 4 bytes - offset to C (relative to A)
|
|
|
|
access_list: 4 bytes - offset to D (relative to A)
|
2023-07-28 10:12:05 +00:00
|
|
|
max_fee_per_blob_gas: 32 bytes
|
2023-05-13 19:52:49 +00:00
|
|
|
blob_versioned_hashes: 4 bytes - offset to E (relative to A)
|
|
|
|
*/
|
|
|
|
// field offset: 32 + 8 + 32 + 32 + 8 + 4 + 32 + 4 + 4 + 32 = 188
|
|
|
|
blobVersionedHashes := messageOffset + binary.LittleEndian.Uint32(txBytes[messageOffset+188:messageOffset+192])
|
|
|
|
|
|
|
|
versionedHashes := make([]libcommon.Hash, len(txBytes[blobVersionedHashes:])/32)
|
|
|
|
for pos, i := blobVersionedHashes, 0; int(pos) < len(txBytes) && i < len(versionedHashes); pos += 32 {
|
|
|
|
versionedHash := libcommon.BytesToHash(txBytes[pos : pos+32])
|
|
|
|
versionedHashes[i] = versionedHash
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return versionedHashes
|
|
|
|
}
|
|
|
|
|
2023-06-11 21:50:02 +00:00
|
|
|
func computeSigningRootEpoch(epoch uint64, domain []byte) (libcommon.Hash, error) {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
binary.LittleEndian.PutUint64(b, epoch)
|
2023-11-22 12:24:35 +00:00
|
|
|
return utils.Sha256(b, domain), nil
|
2023-06-11 21:50:02 +00:00
|
|
|
}
|
2023-05-13 19:52:49 +00:00
|
|
|
|
2023-06-11 21:50:02 +00:00
|
|
|
// transitionSlot is called each time there is a new slot to process
|
2023-07-19 22:20:33 +00:00
|
|
|
func transitionSlot(s abstract.BeaconState) error {
|
2023-06-11 21:50:02 +00:00
|
|
|
slot := s.Slot()
|
|
|
|
previousStateRoot := s.PreviousStateRoot()
|
2023-05-23 18:58:34 +00:00
|
|
|
var err error
|
2023-06-11 21:50:02 +00:00
|
|
|
if previousStateRoot == (libcommon.Hash{}) {
|
|
|
|
previousStateRoot, err = s.HashSSZ()
|
2023-05-13 19:52:49 +00:00
|
|
|
if err != nil {
|
2023-06-11 21:50:02 +00:00
|
|
|
return err
|
2023-05-13 19:52:49 +00:00
|
|
|
}
|
2023-06-11 21:50:02 +00:00
|
|
|
}
|
2023-05-13 19:52:49 +00:00
|
|
|
|
2023-06-11 21:50:02 +00:00
|
|
|
beaconConfig := s.BeaconConfig()
|
|
|
|
|
|
|
|
s.SetStateRootAt(int(slot%beaconConfig.SlotsPerHistoricalRoot), previousStateRoot)
|
|
|
|
|
|
|
|
latestBlockHeader := s.LatestBlockHeader()
|
|
|
|
if latestBlockHeader.Root == [32]byte{} {
|
|
|
|
latestBlockHeader.Root = previousStateRoot
|
|
|
|
s.SetLatestBlockHeader(&latestBlockHeader)
|
2023-05-13 19:52:49 +00:00
|
|
|
}
|
2023-06-11 21:50:02 +00:00
|
|
|
blockHeader := s.LatestBlockHeader()
|
2023-05-13 19:52:49 +00:00
|
|
|
|
2023-06-11 21:50:02 +00:00
|
|
|
previousBlockRoot, err := (&blockHeader).HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.SetBlockRootAt(int(slot%beaconConfig.SlotsPerHistoricalRoot), previousBlockRoot)
|
|
|
|
return nil
|
2023-05-13 19:52:49 +00:00
|
|
|
}
|