Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sentry): Shift beacon block helpers #205

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist
GeoLite2-ASN.mmdb
GeoLite2-City.mmdb
__debug_bin
.vscode/launch.json
15 changes: 0 additions & 15 deletions .vscode/launch.json

This file was deleted.

202 changes: 202 additions & 0 deletions pkg/proto/eth/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package eth

import (
"fmt"

"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
v1 "github.com/ethpandaops/xatu/pkg/proto/eth/v1"
v2 "github.com/ethpandaops/xatu/pkg/proto/eth/v2"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
)

func NewEventBlockV2FromVersionSignedBeaconBlock(block *spec.VersionedSignedBeaconBlock) (*v2.EventBlockV2, error) {
var data *v2.EventBlockV2

switch block.Version {
case spec.DataVersionPhase0:
data = NewEventBlockFromPhase0(block)
case spec.DataVersionAltair:
data = NewEventBlockFromAltair(block)
case spec.DataVersionBellatrix:
data = NewEventBlockFromBellatrix(block)
case spec.DataVersionCapella:
data = NewEventBlockFromCapella(block)
default:
return nil, fmt.Errorf("unsupported block version: %v", block.Version)
}

return data, nil
}

func getTransactions(data []bellatrix.Transaction) []string {
transactions := []string{}

for _, tx := range data {
transactions = append(transactions, fmt.Sprintf("0x%x", tx))
}

return transactions
}

func NewEventBlockFromPhase0(block *spec.VersionedSignedBeaconBlock) *v2.EventBlockV2 {
return &v2.EventBlockV2{
Version: v2.BlockVersion_PHASE0,
Message: &v2.EventBlockV2_Phase0Block{
Phase0Block: &v1.BeaconBlockV2{
Slot: &wrapperspb.UInt64Value{Value: uint64(block.Phase0.Message.Slot)},
ProposerIndex: &wrapperspb.UInt64Value{Value: uint64(block.Phase0.Message.ProposerIndex)},
ParentRoot: block.Phase0.Message.ParentRoot.String(),
StateRoot: block.Phase0.Message.StateRoot.String(),
Body: &v1.BeaconBlockBody{
RandaoReveal: block.Phase0.Message.Body.RANDAOReveal.String(),
Eth1Data: &v1.Eth1Data{
DepositRoot: block.Phase0.Message.Body.ETH1Data.DepositRoot.String(),
DepositCount: block.Phase0.Message.Body.ETH1Data.DepositCount,
BlockHash: fmt.Sprintf("0x%x", block.Phase0.Message.Body.ETH1Data.BlockHash),
},
Graffiti: fmt.Sprintf("0x%x", block.Phase0.Message.Body.Graffiti[:]),
ProposerSlashings: v1.NewProposerSlashingsFromPhase0(block.Phase0.Message.Body.ProposerSlashings),
AttesterSlashings: v1.NewAttesterSlashingsFromPhase0(block.Phase0.Message.Body.AttesterSlashings),
Attestations: v1.NewAttestationsFromPhase0(block.Phase0.Message.Body.Attestations),
Deposits: v1.NewDepositsFromPhase0(block.Phase0.Message.Body.Deposits),
VoluntaryExits: v1.NewSignedVoluntaryExitsFromPhase0(block.Phase0.Message.Body.VoluntaryExits),
},
},
},
Signature: block.Phase0.Signature.String(),
}
}

func NewEventBlockFromAltair(block *spec.VersionedSignedBeaconBlock) *v2.EventBlockV2 {
return &v2.EventBlockV2{
Version: v2.BlockVersion_ALTAIR,
Message: &v2.EventBlockV2_AltairBlock{
AltairBlock: &v2.BeaconBlockAltairV2{
Slot: &wrapperspb.UInt64Value{Value: uint64(block.Altair.Message.Slot)},
ProposerIndex: &wrapperspb.UInt64Value{Value: uint64(block.Altair.Message.ProposerIndex)},
ParentRoot: block.Altair.Message.ParentRoot.String(),
StateRoot: block.Altair.Message.StateRoot.String(),
Body: &v2.BeaconBlockBodyAltairV2{
RandaoReveal: block.Altair.Message.Body.RANDAOReveal.String(),
Eth1Data: &v1.Eth1Data{
DepositRoot: block.Altair.Message.Body.ETH1Data.DepositRoot.String(),
DepositCount: block.Altair.Message.Body.ETH1Data.DepositCount,
BlockHash: fmt.Sprintf("0x%x", block.Altair.Message.Body.ETH1Data.BlockHash),
},
Graffiti: fmt.Sprintf("0x%x", block.Altair.Message.Body.Graffiti[:]),
ProposerSlashings: v1.NewProposerSlashingsFromPhase0(block.Altair.Message.Body.ProposerSlashings),
AttesterSlashings: v1.NewAttesterSlashingsFromPhase0(block.Altair.Message.Body.AttesterSlashings),
Attestations: v1.NewAttestationsFromPhase0(block.Altair.Message.Body.Attestations),
Deposits: v1.NewDepositsFromPhase0(block.Altair.Message.Body.Deposits),
VoluntaryExits: v1.NewSignedVoluntaryExitsFromPhase0(block.Altair.Message.Body.VoluntaryExits),
SyncAggregate: &v1.SyncAggregate{
SyncCommitteeBits: fmt.Sprintf("0x%x", block.Altair.Message.Body.SyncAggregate.SyncCommitteeBits),
SyncCommitteeSignature: block.Altair.Message.Body.SyncAggregate.SyncCommitteeSignature.String(),
},
},
},
},
Signature: block.Altair.Signature.String(),
}
}

func NewEventBlockFromBellatrix(block *spec.VersionedSignedBeaconBlock) *v2.EventBlockV2 {
return &v2.EventBlockV2{
Version: v2.BlockVersion_BELLATRIX,
Message: &v2.EventBlockV2_BellatrixBlock{
BellatrixBlock: &v2.BeaconBlockBellatrixV2{
Slot: &wrapperspb.UInt64Value{Value: uint64(block.Bellatrix.Message.Slot)},
ProposerIndex: &wrapperspb.UInt64Value{Value: uint64(block.Bellatrix.Message.ProposerIndex)},
ParentRoot: block.Bellatrix.Message.ParentRoot.String(),
StateRoot: block.Bellatrix.Message.StateRoot.String(),
Body: &v2.BeaconBlockBodyBellatrixV2{
RandaoReveal: block.Bellatrix.Message.Body.RANDAOReveal.String(),
Eth1Data: &v1.Eth1Data{
DepositRoot: block.Bellatrix.Message.Body.ETH1Data.DepositRoot.String(),
DepositCount: block.Bellatrix.Message.Body.ETH1Data.DepositCount,
BlockHash: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.ETH1Data.BlockHash),
},
Graffiti: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.Graffiti[:]),
ProposerSlashings: v1.NewProposerSlashingsFromPhase0(block.Bellatrix.Message.Body.ProposerSlashings),
AttesterSlashings: v1.NewAttesterSlashingsFromPhase0(block.Bellatrix.Message.Body.AttesterSlashings),
Attestations: v1.NewAttestationsFromPhase0(block.Bellatrix.Message.Body.Attestations),
Deposits: v1.NewDepositsFromPhase0(block.Bellatrix.Message.Body.Deposits),
VoluntaryExits: v1.NewSignedVoluntaryExitsFromPhase0(block.Bellatrix.Message.Body.VoluntaryExits),
SyncAggregate: &v1.SyncAggregate{
SyncCommitteeBits: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.SyncAggregate.SyncCommitteeBits),
SyncCommitteeSignature: block.Bellatrix.Message.Body.SyncAggregate.SyncCommitteeSignature.String(),
},
ExecutionPayload: &v1.ExecutionPayloadV2{
ParentHash: block.Bellatrix.Message.Body.ExecutionPayload.ParentHash.String(),
FeeRecipient: block.Bellatrix.Message.Body.ExecutionPayload.FeeRecipient.String(),
StateRoot: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.ExecutionPayload.StateRoot[:]),
ReceiptsRoot: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.ExecutionPayload.ReceiptsRoot[:]),
LogsBloom: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.ExecutionPayload.LogsBloom[:]),
PrevRandao: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.ExecutionPayload.PrevRandao[:]),
BlockNumber: &wrapperspb.UInt64Value{Value: block.Bellatrix.Message.Body.ExecutionPayload.BlockNumber},
GasLimit: &wrapperspb.UInt64Value{Value: block.Bellatrix.Message.Body.ExecutionPayload.GasLimit},
GasUsed: &wrapperspb.UInt64Value{Value: block.Bellatrix.Message.Body.ExecutionPayload.GasUsed},
Timestamp: &wrapperspb.UInt64Value{Value: block.Bellatrix.Message.Body.ExecutionPayload.Timestamp},
ExtraData: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.ExecutionPayload.ExtraData),
BaseFeePerGas: fmt.Sprintf("0x%x", block.Bellatrix.Message.Body.ExecutionPayload.BaseFeePerGas[:]),
BlockHash: block.Bellatrix.Message.Body.ExecutionPayload.BlockHash.String(),
Transactions: getTransactions(block.Bellatrix.Message.Body.ExecutionPayload.Transactions),
},
},
},
},
Signature: block.Bellatrix.Signature.String(),
}
}

func NewEventBlockFromCapella(block *spec.VersionedSignedBeaconBlock) *v2.EventBlockV2 {
return &v2.EventBlockV2{
Version: v2.BlockVersion_CAPELLA,
Message: &v2.EventBlockV2_CapellaBlock{
CapellaBlock: &v2.BeaconBlockCapellaV2{
Slot: &wrapperspb.UInt64Value{Value: uint64(block.Capella.Message.Slot)},
ProposerIndex: &wrapperspb.UInt64Value{Value: uint64(block.Capella.Message.ProposerIndex)},
ParentRoot: block.Capella.Message.ParentRoot.String(),
StateRoot: block.Capella.Message.StateRoot.String(),
Body: &v2.BeaconBlockBodyCapellaV2{
RandaoReveal: block.Capella.Message.Body.RANDAOReveal.String(),
Eth1Data: &v1.Eth1Data{
DepositRoot: block.Capella.Message.Body.ETH1Data.DepositRoot.String(),
DepositCount: block.Capella.Message.Body.ETH1Data.DepositCount,
BlockHash: fmt.Sprintf("0x%x", block.Capella.Message.Body.ETH1Data.BlockHash),
},
Graffiti: fmt.Sprintf("0x%x", block.Capella.Message.Body.Graffiti[:]),
ProposerSlashings: v1.NewProposerSlashingsFromPhase0(block.Capella.Message.Body.ProposerSlashings),
AttesterSlashings: v1.NewAttesterSlashingsFromPhase0(block.Capella.Message.Body.AttesterSlashings),
Attestations: v1.NewAttestationsFromPhase0(block.Capella.Message.Body.Attestations),
Deposits: v1.NewDepositsFromPhase0(block.Capella.Message.Body.Deposits),
VoluntaryExits: v1.NewSignedVoluntaryExitsFromPhase0(block.Capella.Message.Body.VoluntaryExits),
SyncAggregate: &v1.SyncAggregate{
SyncCommitteeBits: fmt.Sprintf("0x%x", block.Capella.Message.Body.SyncAggregate.SyncCommitteeBits),
SyncCommitteeSignature: block.Capella.Message.Body.SyncAggregate.SyncCommitteeSignature.String(),
},
ExecutionPayload: &v1.ExecutionPayloadCapellaV2{
ParentHash: block.Capella.Message.Body.ExecutionPayload.ParentHash.String(),
FeeRecipient: block.Capella.Message.Body.ExecutionPayload.FeeRecipient.String(),
StateRoot: fmt.Sprintf("0x%x", block.Capella.Message.Body.ExecutionPayload.StateRoot[:]),
ReceiptsRoot: fmt.Sprintf("0x%x", block.Capella.Message.Body.ExecutionPayload.ReceiptsRoot[:]),
LogsBloom: fmt.Sprintf("0x%x", block.Capella.Message.Body.ExecutionPayload.LogsBloom[:]),
PrevRandao: fmt.Sprintf("0x%x", block.Capella.Message.Body.ExecutionPayload.PrevRandao[:]),
BlockNumber: &wrapperspb.UInt64Value{Value: block.Capella.Message.Body.ExecutionPayload.BlockNumber},
GasLimit: &wrapperspb.UInt64Value{Value: block.Capella.Message.Body.ExecutionPayload.GasLimit},
GasUsed: &wrapperspb.UInt64Value{Value: block.Capella.Message.Body.ExecutionPayload.GasUsed},
Timestamp: &wrapperspb.UInt64Value{Value: block.Capella.Message.Body.ExecutionPayload.Timestamp},
ExtraData: fmt.Sprintf("0x%x", block.Capella.Message.Body.ExecutionPayload.ExtraData),
BaseFeePerGas: fmt.Sprintf("0x%x", block.Capella.Message.Body.ExecutionPayload.BaseFeePerGas[:]),
BlockHash: block.Capella.Message.Body.ExecutionPayload.BlockHash.String(),
Transactions: getTransactions(block.Capella.Message.Body.ExecutionPayload.Transactions),
Withdrawals: v1.NewWithdrawalsFromCapella(block.Capella.Message.Body.ExecutionPayload.Withdrawals),
},
BlsToExecutionChanges: v2.NewBLSToExecutionChangesFromCapella(block.Capella.Message.Body.BLSToExecutionChanges),
},
},
},
Signature: block.Capella.Signature.String(),
}
}
Loading
Loading