ipld-eth-beacon-indexer/pkg/beaconclient/consensus.go

442 lines
8.8 KiB
Go
Raw Normal View History

package beaconclient
import (
2022-09-08 04:50:17 +00:00
"bytes"
"errors"
2022-09-08 04:50:17 +00:00
"github.com/protolambda/zrnt/eth2/beacon/altair"
"github.com/protolambda/zrnt/eth2/beacon/bellatrix"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/beacon/phase0"
"github.com/protolambda/zrnt/eth2/configs"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/tree"
log "github.com/sirupsen/logrus"
2022-09-28 18:52:13 +00:00
"strconv"
)
2022-09-08 05:03:37 +00:00
type Eth1Data common.Eth1Data
2022-09-08 16:22:30 +00:00
type Root common.Root
type Signature common.BLSSignature
2022-09-28 18:52:13 +00:00
type Slot uint64
type Epoch uint64
2022-09-21 21:27:22 +00:00
type ExecutionPayloadHeader common.ExecutionPayloadHeader
2022-09-28 18:52:13 +00:00
func ParseSlot(v string) (Slot, error) {
slotNum, err := strconv.ParseUint(v, 10, 64)
return Slot(slotNum), err
}
func (s *Slot) Format() string {
return strconv.FormatUint(uint64(*s), 10)
}
func (s *Slot) Number() uint64 {
return uint64(*s)
}
func (s *Slot) Plus(v uint64) Slot {
return Slot(v + s.Number())
}
func (s *Slot) PlusInt(v int) Slot {
return s.Plus(uint64(v))
}
func (e *Epoch) Format() string {
return strconv.FormatUint(uint64(*e), 10)
}
type BeaconBlock struct {
2022-09-08 15:59:47 +00:00
spec *common.Spec
2022-09-08 04:50:17 +00:00
bellatrix *bellatrix.BeaconBlock
altair *altair.BeaconBlock
phase0 *phase0.BeaconBlock
}
type BeaconBlockBody struct {
2022-09-08 16:22:30 +00:00
spec *common.Spec
2022-09-08 04:50:17 +00:00
bellatrix *bellatrix.BeaconBlockBody
altair *altair.BeaconBlockBody
phase0 *phase0.BeaconBlockBody
}
type BeaconState struct {
2022-09-08 15:59:47 +00:00
spec *common.Spec
2022-09-08 04:50:17 +00:00
bellatrix *bellatrix.BeaconState
altair *altair.BeaconState
phase0 *phase0.BeaconState
}
2022-09-08 16:22:30 +00:00
type SignedBeaconBlock struct {
spec *common.Spec
bellatrix *bellatrix.SignedBeaconBlock
altair *altair.SignedBeaconBlock
phase0 *phase0.SignedBeaconBlock
}
func (s *SignedBeaconBlock) UnmarshalSSZ(ssz []byte) error {
2022-09-08 15:59:47 +00:00
spec := chooseSpec(s.spec)
2022-09-08 04:50:17 +00:00
var bellatrix bellatrix.SignedBeaconBlock
2022-09-08 15:59:47 +00:00
err := bellatrix.Deserialize(spec, makeDecodingReader(ssz))
if nil == err {
2022-09-08 04:50:17 +00:00
s.bellatrix = &bellatrix
s.altair = nil
s.phase0 = nil
log.Info("Unmarshalled Bellatrix SignedBeaconBlock")
return nil
}
2022-09-08 04:50:17 +00:00
var altair altair.SignedBeaconBlock
2022-09-08 15:59:47 +00:00
err = altair.Deserialize(spec, makeDecodingReader(ssz))
if nil == err {
2022-09-08 04:50:17 +00:00
s.bellatrix = nil
s.altair = &altair
s.phase0 = nil
log.Info("Unmarshalled Altair SignedBeaconBlock")
return nil
}
2022-09-08 04:50:17 +00:00
var phase0 phase0.SignedBeaconBlock
2022-09-08 15:59:47 +00:00
err = phase0.Deserialize(spec, makeDecodingReader(ssz))
if nil == err {
2022-09-08 04:50:17 +00:00
s.bellatrix = nil
s.altair = nil
s.phase0 = &phase0
log.Info("Unmarshalled Phase0 SignedBeaconBlock")
return nil
}
2022-09-08 04:50:17 +00:00
s.bellatrix = nil
s.altair = nil
s.phase0 = nil
log.Warning("Unable to unmarshal SignedBeaconBlock")
return err
}
func (s *SignedBeaconBlock) MarshalSSZ() ([]byte, error) {
2022-09-08 15:59:47 +00:00
spec := chooseSpec(s.spec)
2022-09-08 04:50:17 +00:00
var err error
var buf bytes.Buffer
encodingWriter := codec.NewEncodingWriter(&buf)
if s.IsBellatrix() {
2022-09-08 15:59:47 +00:00
err = s.bellatrix.Serialize(spec, encodingWriter)
}
if s.IsAltair() {
2022-09-08 15:59:47 +00:00
err = s.altair.Serialize(spec, encodingWriter)
}
if s.IsPhase0() {
2022-09-08 15:59:47 +00:00
err = s.phase0.Serialize(spec, encodingWriter)
2022-09-08 04:50:17 +00:00
}
if err != nil {
return nil, err
}
2022-09-08 04:50:17 +00:00
return buf.Bytes(), err
}
func (s *SignedBeaconBlock) IsBellatrix() bool {
2022-09-08 04:50:17 +00:00
return s.bellatrix != nil
}
func (s *SignedBeaconBlock) IsAltair() bool {
2022-09-08 04:50:17 +00:00
return s.altair != nil
}
func (s *SignedBeaconBlock) IsPhase0() bool {
2022-09-08 04:50:17 +00:00
return s.phase0 != nil
}
2022-09-08 04:50:17 +00:00
func (s *SignedBeaconBlock) GetBellatrix() *bellatrix.SignedBeaconBlock {
return s.bellatrix
}
2022-09-08 04:50:17 +00:00
func (s *SignedBeaconBlock) GetAltair() *altair.SignedBeaconBlock {
return s.altair
}
2022-09-08 04:50:17 +00:00
func (s *SignedBeaconBlock) GetPhase0() *phase0.SignedBeaconBlock {
return s.phase0
}
2022-09-08 16:22:30 +00:00
func (s *SignedBeaconBlock) Signature() Signature {
if s.IsBellatrix() {
2022-09-08 16:22:30 +00:00
return Signature(s.bellatrix.Signature)
}
if s.IsAltair() {
2022-09-08 16:22:30 +00:00
return Signature(s.altair.Signature)
}
if s.IsPhase0() {
2022-09-08 16:22:30 +00:00
return Signature(s.phase0.Signature)
}
2022-09-08 16:22:30 +00:00
return Signature{}
}
func (s *SignedBeaconBlock) Block() *BeaconBlock {
if s.IsBellatrix() {
2022-09-08 15:59:47 +00:00
return &BeaconBlock{bellatrix: &s.bellatrix.Message, spec: s.spec}
}
if s.IsAltair() {
2022-09-08 15:59:47 +00:00
return &BeaconBlock{altair: &s.altair.Message, spec: s.spec}
}
if s.IsPhase0() {
2022-09-08 15:59:47 +00:00
return &BeaconBlock{phase0: &s.phase0.Message, spec: s.spec}
}
return nil
}
func (b *BeaconBlock) IsBellatrix() bool {
2022-09-08 04:50:17 +00:00
return b.bellatrix != nil
}
func (b *BeaconBlock) IsAltair() bool {
2022-09-08 04:50:17 +00:00
return b.altair != nil
}
func (b *BeaconBlock) IsPhase0() bool {
2022-09-08 04:50:17 +00:00
return b.phase0 != nil
}
2022-09-08 04:50:17 +00:00
func (s *BeaconBlock) GetBellatrix() *bellatrix.BeaconBlock {
return s.bellatrix
}
2022-09-08 04:50:17 +00:00
func (s *BeaconBlock) GetAltair() *altair.BeaconBlock {
return s.altair
}
2022-09-08 04:50:17 +00:00
func (s *BeaconBlock) GetPhase0() *phase0.BeaconBlock {
return s.phase0
}
2022-09-08 05:03:37 +00:00
func (b *BeaconBlock) ParentRoot() Root {
if b.IsBellatrix() {
2022-09-08 05:03:37 +00:00
return Root(b.bellatrix.ParentRoot)
}
if b.IsAltair() {
2022-09-08 05:03:37 +00:00
return Root(b.altair.ParentRoot)
}
if b.IsPhase0() {
2022-09-08 05:03:37 +00:00
return Root(b.phase0.ParentRoot)
}
2022-09-08 05:03:37 +00:00
return Root{}
}
2022-09-08 05:03:37 +00:00
func (b *BeaconBlock) StateRoot() Root {
if b.IsBellatrix() {
2022-09-08 05:03:37 +00:00
return Root(b.bellatrix.StateRoot)
}
if b.IsAltair() {
2022-09-08 05:03:37 +00:00
return Root(b.altair.StateRoot)
}
if b.IsPhase0() {
2022-09-08 05:03:37 +00:00
return Root(b.phase0.StateRoot)
}
2022-09-08 05:03:37 +00:00
return Root{}
}
func (b *BeaconBlock) Body() *BeaconBlockBody {
if b.IsBellatrix() {
2022-09-08 16:22:30 +00:00
return &BeaconBlockBody{bellatrix: &b.bellatrix.Body, spec: b.spec}
}
if b.IsAltair() {
2022-09-08 16:22:30 +00:00
return &BeaconBlockBody{altair: &b.altair.Body, spec: b.spec}
}
if b.IsPhase0() {
2022-09-08 16:22:30 +00:00
return &BeaconBlockBody{phase0: &b.phase0.Body, spec: b.spec}
}
return nil
}
func (b *BeaconBlockBody) IsBellatrix() bool {
2022-09-08 04:50:17 +00:00
return b.bellatrix != nil
}
func (b *BeaconBlockBody) IsAltair() bool {
2022-09-08 04:50:17 +00:00
return b.altair != nil
}
func (b *BeaconBlockBody) IsPhase0() bool {
2022-09-08 04:50:17 +00:00
return b.phase0 != nil
}
2022-09-08 05:03:37 +00:00
func (b *BeaconBlockBody) Eth1Data() Eth1Data {
if b.IsBellatrix() {
2022-09-08 05:03:37 +00:00
return Eth1Data(b.bellatrix.Eth1Data)
}
if b.IsAltair() {
2022-09-08 05:03:37 +00:00
return Eth1Data(b.altair.Eth1Data)
}
if b.IsPhase0() {
2022-09-08 05:03:37 +00:00
return Eth1Data(b.phase0.Eth1Data)
}
2022-09-08 05:03:37 +00:00
return Eth1Data{}
}
2022-09-21 21:27:22 +00:00
func (b *BeaconBlockBody) ExecutionPayloadHeader() *ExecutionPayloadHeader {
if b.IsBellatrix() {
payloadHeader := b.bellatrix.ExecutionPayload.Header(chooseSpec(b.spec))
return (*ExecutionPayloadHeader)(payloadHeader)
}
return nil
}
2022-09-08 05:03:37 +00:00
func (b *BeaconBlock) HashTreeRoot() Root {
2022-09-08 15:59:47 +00:00
spec := chooseSpec(b.spec)
2022-09-08 16:26:05 +00:00
hashFn := tree.GetHashFn()
2022-09-08 15:59:47 +00:00
if b.IsBellatrix() {
2022-09-08 16:26:05 +00:00
return Root(b.bellatrix.HashTreeRoot(spec, hashFn))
}
if b.IsAltair() {
2022-09-08 16:26:05 +00:00
return Root(b.altair.HashTreeRoot(spec, hashFn))
}
if b.IsPhase0() {
2022-09-08 16:26:05 +00:00
return Root(b.phase0.HashTreeRoot(spec, hashFn))
}
2022-09-08 05:03:37 +00:00
return Root{}
}
func (s *BeaconState) UnmarshalSSZ(ssz []byte) error {
2022-09-08 15:59:47 +00:00
spec := chooseSpec(s.spec)
2022-09-08 04:50:17 +00:00
var bellatrix bellatrix.BeaconState
2022-09-08 15:59:47 +00:00
err := bellatrix.Deserialize(spec, makeDecodingReader(ssz))
if nil == err {
2022-09-08 04:50:17 +00:00
s.bellatrix = &bellatrix
s.altair = nil
s.phase0 = nil
log.Info("Unmarshalled Bellatrix BeaconState")
return nil
}
2022-09-08 04:50:17 +00:00
var altair altair.BeaconState
2022-09-08 15:59:47 +00:00
err = altair.Deserialize(spec, makeDecodingReader(ssz))
if nil == err {
2022-09-08 04:50:17 +00:00
s.bellatrix = nil
s.altair = &altair
s.phase0 = nil
log.Info("Unmarshalled Altair BeaconState")
return nil
}
2022-09-08 04:50:17 +00:00
var phase0 phase0.BeaconState
2022-09-08 15:59:47 +00:00
err = phase0.Deserialize(spec, makeDecodingReader(ssz))
if nil == err {
2022-09-08 04:50:17 +00:00
s.bellatrix = nil
s.altair = nil
s.phase0 = &phase0
log.Info("Unmarshalled Phase0 BeaconState")
return nil
}
2022-09-08 04:50:17 +00:00
s.bellatrix = nil
s.altair = nil
s.phase0 = nil
log.Warning("Unable to unmarshal BeaconState")
return err
}
func (s *BeaconState) MarshalSSZ() ([]byte, error) {
2022-09-08 15:59:47 +00:00
spec := chooseSpec(s.spec)
2022-09-08 04:50:17 +00:00
var err error
var buf bytes.Buffer
encodingWriter := codec.NewEncodingWriter(&buf)
if s.IsBellatrix() {
2022-09-08 15:59:47 +00:00
err = s.bellatrix.Serialize(spec, encodingWriter)
2022-09-08 04:50:17 +00:00
} else if s.IsAltair() {
2022-09-08 15:59:47 +00:00
err = s.altair.Serialize(spec, encodingWriter)
2022-09-08 04:50:17 +00:00
} else if s.IsPhase0() {
2022-09-08 15:59:47 +00:00
err = s.phase0.Serialize(spec, encodingWriter)
2022-09-08 04:50:17 +00:00
} else {
err = errors.New("BeaconState not set")
}
2022-09-08 04:50:17 +00:00
if nil != err {
return nil, err
}
2022-09-08 04:50:17 +00:00
return buf.Bytes(), nil
}
func (s *BeaconState) IsBellatrix() bool {
2022-09-08 04:50:17 +00:00
return s.bellatrix != nil
}
func (s *BeaconState) IsAltair() bool {
2022-09-08 04:50:17 +00:00
return s.altair != nil
}
func (s *BeaconState) IsPhase0() bool {
2022-09-08 04:50:17 +00:00
return s.phase0 != nil
}
2022-09-08 15:59:47 +00:00
func (s *BeaconState) HashTreeRoot() Root {
spec := chooseSpec(s.spec)
2022-09-08 16:26:05 +00:00
hashFn := tree.GetHashFn()
2022-09-08 15:59:47 +00:00
if s.IsBellatrix() {
2022-09-08 16:26:05 +00:00
return Root(s.bellatrix.HashTreeRoot(spec, hashFn))
}
2022-09-08 15:59:47 +00:00
if s.IsAltair() {
2022-09-08 16:26:05 +00:00
return Root(s.altair.HashTreeRoot(spec, hashFn))
}
2022-09-08 15:59:47 +00:00
if s.IsPhase0() {
2022-09-08 16:26:05 +00:00
return Root(s.phase0.HashTreeRoot(spec, hashFn))
}
2022-09-08 05:03:37 +00:00
return Root{}
}
2022-09-08 04:50:17 +00:00
func (s *BeaconState) GetBellatrix() *bellatrix.BeaconState {
return s.bellatrix
}
2022-09-08 04:50:17 +00:00
func (s *BeaconState) GetAltair() *altair.BeaconState {
return s.altair
}
2022-09-08 04:50:17 +00:00
func (s *BeaconState) GetPhase0() *phase0.BeaconState {
return s.phase0
}
2022-09-08 15:59:47 +00:00
func chooseSpec(spec *common.Spec) *common.Spec {
if nil == spec {
return configs.Mainnet
}
return spec
}
func makeDecodingReader(ssz []byte) *codec.DecodingReader {
return codec.NewDecodingReader(bytes.NewReader(ssz), uint64(len(ssz)))
}