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

419 lines
8.1 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-08 05:03:37 +00:00
type Slot common.Slot
type Root common.Root
type Eth1Data common.Eth1Data
type SignedBeaconBlock struct {
2022-09-08 15:59:47 +00:00
spec *common.Spec
2022-09-08 04:50:17 +00:00
bellatrix *bellatrix.SignedBeaconBlock
altair *altair.SignedBeaconBlock
phase0 *phase0.SignedBeaconBlock
}
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 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
}
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 04:50:17 +00:00
func (s *SignedBeaconBlock) Signature() [96]byte {
if s.IsBellatrix() {
2022-09-08 04:50:17 +00:00
return s.bellatrix.Signature
}
if s.IsAltair() {
2022-09-08 04:50:17 +00:00
return s.altair.Signature
}
if s.IsPhase0() {
2022-09-08 04:50:17 +00:00
return s.phase0.Signature
}
2022-09-08 04:50:17 +00:00
return [96]byte{}
}
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 04:50:17 +00:00
return &BeaconBlockBody{bellatrix: &b.bellatrix.Body}
}
if b.IsAltair() {
2022-09-08 04:50:17 +00:00
return &BeaconBlockBody{altair: &b.altair.Body}
}
if b.IsPhase0() {
2022-09-08 04:50:17 +00:00
return &BeaconBlockBody{phase0: &b.phase0.Body}
}
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-08 05:03:37 +00:00
func (b *BeaconBlock) HashTreeRoot() Root {
2022-09-08 15:59:47 +00:00
spec := chooseSpec(b.spec)
if b.IsBellatrix() {
2022-09-08 15:59:47 +00:00
return Root(b.bellatrix.HashTreeRoot(spec, tree.Hash))
}
if b.IsAltair() {
2022-09-08 15:59:47 +00:00
return Root(b.altair.HashTreeRoot(spec, tree.Hash))
}
if b.IsPhase0() {
2022-09-08 15:59:47 +00:00
return Root(b.phase0.HashTreeRoot(spec, tree.Hash))
}
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 05:03:37 +00:00
func (s *BeaconState) Slot() Slot {
if s.IsBellatrix() {
2022-09-08 05:03:37 +00:00
return Slot(s.bellatrix.Slot)
}
if s.IsAltair() {
2022-09-08 05:03:37 +00:00
return Slot(s.altair.Slot)
}
if s.IsPhase0() {
2022-09-08 05:03:37 +00:00
return Slot(s.phase0.Slot)
}
// TODO(telackey): Something better than 0?
return 0
}
2022-09-08 15:59:47 +00:00
func (s *BeaconState) HashTreeRoot() Root {
spec := chooseSpec(s.spec)
if s.IsBellatrix() {
return Root(s.bellatrix.HashTreeRoot(spec, tree.Hash))
}
2022-09-08 15:59:47 +00:00
if s.IsAltair() {
return Root(s.altair.HashTreeRoot(spec, tree.Hash))
}
2022-09-08 15:59:47 +00:00
if s.IsPhase0() {
return Root(s.phase0.HashTreeRoot(spec, tree.Hash))
}
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)))
}