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

396 lines
8.0 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"
)
type SignedBeaconBlock struct {
2022-09-08 04:50:17 +00:00
bellatrix *bellatrix.SignedBeaconBlock
altair *altair.SignedBeaconBlock
phase0 *phase0.SignedBeaconBlock
}
type BeaconBlock struct {
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 04:50:17 +00:00
bellatrix *bellatrix.BeaconState
altair *altair.BeaconState
phase0 *phase0.BeaconState
}
func (s *SignedBeaconBlock) UnmarshalSSZ(ssz []byte) error {
2022-09-08 04:50:17 +00:00
var bellatrix bellatrix.SignedBeaconBlock
decodingReader := codec.NewDecodingReader(bytes.NewReader(ssz), uint64(len(ssz)))
err := bellatrix.Deserialize(configs.Mainnet, decodingReader)
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
decodingReader = codec.NewDecodingReader(bytes.NewReader(ssz), uint64(len(ssz)))
err = altair.Deserialize(configs.Mainnet, decodingReader)
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
decodingReader = codec.NewDecodingReader(bytes.NewReader(ssz), uint64(len(ssz)))
err = phase0.Deserialize(configs.Mainnet, decodingReader)
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 04:50:17 +00:00
var err error
var buf bytes.Buffer
encodingWriter := codec.NewEncodingWriter(&buf)
if s.IsBellatrix() {
2022-09-08 04:50:17 +00:00
err = s.bellatrix.Serialize(configs.Mainnet, encodingWriter)
}
if s.IsAltair() {
2022-09-08 04:50:17 +00:00
err = s.altair.Serialize(configs.Mainnet, encodingWriter)
}
if s.IsPhase0() {
2022-09-08 04:50:17 +00:00
err = s.phase0.Serialize(configs.Mainnet, encodingWriter)
}
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 04:50:17 +00:00
return &BeaconBlock{bellatrix: &s.bellatrix.Message}
}
if s.IsAltair() {
2022-09-08 04:50:17 +00:00
return &BeaconBlock{altair: &s.altair.Message}
}
if s.IsPhase0() {
2022-09-08 04:50:17 +00:00
return &BeaconBlock{phase0: &s.phase0.Message}
}
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 04:50:17 +00:00
func (b *BeaconBlock) ParentRoot() *common.Root {
if b.IsBellatrix() {
2022-09-08 04:50:17 +00:00
return &b.bellatrix.ParentRoot
}
if b.IsAltair() {
2022-09-08 04:50:17 +00:00
return &b.altair.ParentRoot
}
if b.IsPhase0() {
2022-09-08 04:50:17 +00:00
return &b.phase0.ParentRoot
}
return nil
}
2022-09-08 04:50:17 +00:00
func (b *BeaconBlock) StateRoot() *common.Root {
if b.IsBellatrix() {
2022-09-08 04:50:17 +00:00
return &b.bellatrix.StateRoot
}
if b.IsAltair() {
2022-09-08 04:50:17 +00:00
return &b.altair.StateRoot
}
if b.IsPhase0() {
2022-09-08 04:50:17 +00:00
return &b.phase0.StateRoot
}
return nil
}
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 04:50:17 +00:00
func (b *BeaconBlockBody) Eth1Data() *common.Eth1Data {
if b.IsBellatrix() {
2022-09-08 04:50:17 +00:00
return &b.bellatrix.Eth1Data
}
if b.IsAltair() {
2022-09-08 04:50:17 +00:00
return &b.altair.Eth1Data
}
if b.IsPhase0() {
2022-09-08 04:50:17 +00:00
return &b.phase0.Eth1Data
}
return nil
}
2022-09-08 04:50:17 +00:00
func (b *BeaconBlock) HashTreeRoot() common.Root {
if b.IsBellatrix() {
2022-09-08 04:50:17 +00:00
return b.bellatrix.HashTreeRoot(configs.Mainnet, tree.Hash)
}
if b.IsAltair() {
2022-09-08 04:50:17 +00:00
return b.altair.HashTreeRoot(configs.Mainnet, tree.Hash)
}
if b.IsPhase0() {
2022-09-08 04:50:17 +00:00
return b.phase0.HashTreeRoot(configs.Mainnet, tree.Hash)
}
2022-09-08 04:50:17 +00:00
return common.Root{}
}
func (s *BeaconState) UnmarshalSSZ(ssz []byte) error {
2022-09-08 04:50:17 +00:00
var bellatrix bellatrix.BeaconState
decodingReader := codec.NewDecodingReader(bytes.NewReader(ssz), uint64(len(ssz)))
err := bellatrix.Deserialize(configs.Mainnet, decodingReader)
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
decodingReader = codec.NewDecodingReader(bytes.NewReader(ssz), uint64(len(ssz)))
err = altair.Deserialize(configs.Mainnet, decodingReader)
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
decodingReader = codec.NewDecodingReader(bytes.NewReader(ssz), uint64(len(ssz)))
err = phase0.Deserialize(configs.Mainnet, decodingReader)
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 04:50:17 +00:00
var err error
var buf bytes.Buffer
encodingWriter := codec.NewEncodingWriter(&buf)
if s.IsBellatrix() {
2022-09-08 04:50:17 +00:00
err = s.bellatrix.Serialize(configs.Mainnet, encodingWriter)
} else if s.IsAltair() {
err = s.altair.Serialize(configs.Mainnet, encodingWriter)
} else if s.IsPhase0() {
err = s.phase0.Serialize(configs.Mainnet, encodingWriter)
} 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 04:50:17 +00:00
func (s *BeaconState) Slot() common.Slot {
if s.IsBellatrix() {
2022-09-08 04:50:17 +00:00
return s.bellatrix.Slot
}
if s.IsAltair() {
2022-09-08 04:50:17 +00:00
return s.altair.Slot
}
if s.IsPhase0() {
2022-09-08 04:50:17 +00:00
return s.phase0.Slot
}
// TODO(telackey): Something better than 0?
return 0
}
2022-09-08 04:50:17 +00:00
func (b *BeaconState) HashTreeRoot() common.Root {
if b.IsBellatrix() {
2022-09-08 04:50:17 +00:00
return b.bellatrix.HashTreeRoot(configs.Mainnet, tree.Hash)
}
if b.IsAltair() {
2022-09-08 04:50:17 +00:00
return b.altair.HashTreeRoot(configs.Mainnet, tree.Hash)
}
if b.IsPhase0() {
2022-09-08 04:50:17 +00:00
return b.phase0.HashTreeRoot(configs.Mainnet, tree.Hash)
}
2022-09-08 04:50:17 +00:00
return common.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
}