From 0c847d1e1811c1e99432e8e4a4ce505717bc61de Mon Sep 17 00:00:00 2001 From: Adam Szkoda Date: Wed, 6 Mar 2024 09:25:41 +0100 Subject: [PATCH] Convert GetBlock to Beacon API --- cmd/root.go | 2 +- go.mod | 20 +++---- go.sum | 20 +++++++ pkg/monitoring.go | 144 +++++++++++++++++++--------------------------- pkg/slashings.go | 6 +- 5 files changed, 93 insertions(+), 99 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 7ffcaf3..e79425c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -97,7 +97,7 @@ var ( var wg sync.WaitGroup wg.Add(2) go pkg.SubscribeToEpochs(ctx, s, true, &wg) - go pkg.MonitorSlashings(ctx, s, beacon, &wg) + go pkg.MonitorSlashings(ctx, beacon, &wg) defer wg.Wait() }, } diff --git a/go.mod b/go.mod index b019377..2f9cd34 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,11 @@ module eth2-monitor go 1.19 require ( - github.com/attestantio/go-eth2-client v0.19.5 + github.com/attestantio/go-eth2-client v0.19.10 github.com/dghubble/go-twitter v0.0.0-20220716041154-837915ec2f79 github.com/dghubble/oauth1 v0.7.1 github.com/ethereum/go-ethereum v1.13.2 - github.com/mattn/go-isatty v0.0.18 + github.com/mattn/go-isatty v0.0.20 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.16.0 github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 @@ -31,7 +31,7 @@ require ( github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect github.com/dghubble/sling v1.4.0 // indirect github.com/ethereum/c-kzg-4844 v0.3.1 // indirect - github.com/fatih/color v1.10.0 // indirect + github.com/fatih/color v1.16.0 // indirect github.com/ferranbt/fastssz v0.1.3 // indirect github.com/go-logr/logr v1.2.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect @@ -40,13 +40,13 @@ require ( github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.2 // indirect - github.com/holiman/uint256 v1.2.3 // indirect + github.com/holiman/uint256 v1.2.4 // indirect github.com/huandu/go-clone v1.6.0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect - github.com/klauspost/cpuid/v2 v2.2.5 // indirect + github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect - github.com/minio/sha256-simd v1.0.0 // indirect + github.com/minio/sha256-simd v1.0.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/prometheus/client_model v0.3.0 // indirect @@ -61,12 +61,12 @@ require ( go.opentelemetry.io/otel v1.16.0 // indirect go.opentelemetry.io/otel/metric v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.16.0 // indirect - golang.org/x/crypto v0.12.0 // indirect + golang.org/x/crypto v0.18.0 // indirect golang.org/x/net v0.10.0 // indirect golang.org/x/sync v0.3.0 // indirect - golang.org/x/sys v0.13.0 // indirect - golang.org/x/text v0.12.0 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index 1e2cbc3..39b2db2 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/attestantio/go-eth2-client v0.19.5 h1:4V+vhXsCYji5jWrlONbr03GV7qoLRdzq96dLgXaqmek= github.com/attestantio/go-eth2-client v0.19.5/go.mod h1:mZve1kV9Ctj0I1HH9gdg+MnI8lZ+Cb2EktEtOYrBlsM= +github.com/attestantio/go-eth2-client v0.19.10 h1:NLs9mcBvZpBTZ3du7Ey2NHQoj8d3UePY7pFBXX6C6qs= +github.com/attestantio/go-eth2-client v0.19.10/go.mod h1:TTz7YF6w4z6ahvxKiHuGPn6DbQn7gH6HPuWm/DEQeGE= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -116,6 +118,8 @@ github.com/ethereum/go-ethereum v1.13.2/go.mod h1:gkQ5Ygi64ZBh9M/4iXY1R8WqoNCx1E github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/ferranbt/fastssz v0.1.3 h1:ZI+z3JH05h4kgmFXdHuR1aWYsgrg7o+Fw7/NCzM16Mo= github.com/ferranbt/fastssz v0.1.3/go.mod h1:0Y9TEd/9XuFlh7mskMPfXiI2Dkw4Ddg9EyXt1W7MRvE= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -242,6 +246,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/holiman/uint256 v1.2.3 h1:K8UWO1HUJpRMXBxbmaY1Y8IAMZC/RsKB+ArEnnK4l5o= github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw= +github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU= +github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c= github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U= github.com/huandu/go-clone v1.6.0 h1:HMo5uvg4wgfiy5FoGOqlFLQED/VGRm2D9Pi8g1FXPGc= @@ -265,6 +271,8 @@ github.com/klauspost/compress v1.16.4 h1:91KN02FnsOYhuunwU4ssRe8lc2JosWmizWa91B5 github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= +github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= @@ -289,6 +297,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= @@ -296,6 +306,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfr github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= +github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= +github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -443,6 +455,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -574,6 +588,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -583,6 +599,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -639,6 +657,8 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= diff --git a/pkg/monitoring.go b/pkg/monitoring.go index 1161ac4..7fca442 100644 --- a/pkg/monitoring.go +++ b/pkg/monitoring.go @@ -18,13 +18,13 @@ import ( eth2client "github.com/attestantio/go-eth2-client" "github.com/attestantio/go-eth2-client/api" + eth2spec "github.com/attestantio/go-eth2-client/spec" + "github.com/attestantio/go-eth2-client/spec/bellatrix" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" bitfield "github.com/prysmaticlabs/go-bitfield" primitives "github.com/prysmaticlabs/prysm/v4/consensus-types/primitives" - ethpbservice "github.com/prysmaticlabs/prysm/v4/proto/eth/service" - ethpbv1 "github.com/prysmaticlabs/prysm/v4/proto/eth/v1" - ethpbv2 "github.com/prysmaticlabs/prysm/v4/proto/eth/v2" ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1" "github.com/rs/zerolog/log" "golang.org/x/exp/maps" @@ -86,12 +86,12 @@ func IndexPubkeys(ctx context.Context, s *prysmgrpc.Service, pubkeys []string) ( return result, reversed, nil } -func processDeposits(ctx context.Context, s *prysmgrpc.Service, hashedKeys map[string]interface{}, deposits []*ethpbv1.Deposit) (map[string]spec.ValidatorIndex, map[spec.ValidatorIndex]string, error) { +func processDeposits(ctx context.Context, s *prysmgrpc.Service, hashedKeys map[string]interface{}, deposits []*phase0.Deposit) (map[string]spec.ValidatorIndex, map[spec.ValidatorIndex]string, error) { var pubkeys []string for _, deposit := range deposits { - binPubkey := deposit.GetData().GetPubkey() - pubkey := hex.EncodeToString(binPubkey) + binPubKey := deposit.Data.PublicKey + pubkey := hex.EncodeToString(binPubKey[:]) pubkey = strings.ToLower(pubkey) if _, ok := hashedKeys[pubkey]; !ok { @@ -205,38 +205,24 @@ type ChainBlock struct { Slot spec.Slot ProposerIndex spec.ValidatorIndex ChainAttestations []*ChainAttestation - BlockContainer *ethpbv2.SignedBeaconBlockContainer - Attestations []*ethpbv1.Attestation - Deposits []*ethpbv1.Deposit - AttesterSlashings []*ethpbv1.AttesterSlashing - ProposerSlashings []*ethpbv1.ProposerSlashing - VoluntaryExits []*ethpbv1.SignedVoluntaryExit + + BlockContainer *eth2spec.VersionedSignedBeaconBlock + Attestations []*phase0.Attestation + Deposits []*phase0.Deposit + AttesterSlashings []*phase0.AttesterSlashing + ProposerSlashings []*phase0.ProposerSlashing + VoluntaryExits []*phase0.SignedVoluntaryExit Transactions []types.Transaction } type ChainAttestation struct { + AggregationBits []byte Slot spec.Slot InclusionSlot spec.Slot CommitteeIndex spec.CommitteeIndex - AggregationBits []byte } -// getBlock retrieves the block for a specific block root. -func getBlock(ctx context.Context, s *prysmgrpc.Service, root []byte) (*ethpbv2.SignedBeaconBlockContainer, error) { - conn := ethpbservice.NewBeaconChainClient(s.Connection()) - opCtx, cancel := context.WithTimeout(ctx, s.Timeout()) - req := ðpbv2.BlockRequestV2{ - BlockId: root, - } - resp, err := conn.GetBlockV2(opCtx, req) - cancel() - if err != nil { - return nil, errors.Wrapf(err, "rpc call GetBlockV2 failed, root=%q", root) - } - return resp.GetData(), nil -} - -func unmarshallTransactions(rlpEncodedTxs [][]byte) (txs []types.Transaction, err error) { +func unmarshallTransactions(rlpEncodedTxs []bellatrix.Transaction) (txs []types.Transaction, err error) { for _, rlpEncodedTx := range rlpEncodedTxs { var tx types.Transaction if err := tx.UnmarshalBinary(rlpEncodedTx); err != nil { @@ -248,18 +234,15 @@ func unmarshallTransactions(rlpEncodedTxs [][]byte) (txs []types.Transaction, er } // ListBlocks lists blocks and attestations for a specific epoch. -func ListBlocks(ctx context.Context, s *prysmgrpc.Service, beacon *beaconchain.BeaconChain, epoch spec.Epoch) (map[spec.Slot][]*ChainBlock, error) { - provider, isProvider := beacon.Service().(eth2client.BeaconBlockHeadersProvider) - if !isProvider { - log.Fatal().Msg("Unable to cast to BeaconBlockHeadersProvider") - } +func ListBlocks(ctx context.Context, beacon *beaconchain.BeaconChain, epoch spec.Epoch) (map[spec.Slot][]*ChainBlock, error) { + blockHeadersProvider := beacon.Service().(eth2client.BeaconBlockHeadersProvider) + blockProvider := beacon.Service().(eth2client.SignedBeaconBlockProvider) result := make(map[spec.Slot][]*ChainBlock) lastSlot := (epoch + 1) * spec.SLOTS_PER_EPOCH for slot := epoch * spec.SLOTS_PER_EPOCH; slot < lastSlot; slot++ { opCtx, cancel := context.WithTimeout(ctx, beacon.Timeout()) - - resp, err := provider.BeaconBlockHeader(opCtx, &api.BeaconBlockHeaderOpts{ + resp, err := blockHeadersProvider.BeaconBlockHeader(opCtx, &api.BeaconBlockHeaderOpts{ Common: api.CommonOpts{}, Block: fmt.Sprint(slot), }) @@ -276,63 +259,54 @@ func ListBlocks(ctx context.Context, s *prysmgrpc.Service, beacon *beaconchain.B proposerIndex := spec.ValidatorIndex(resp.Data.Header.Message.ProposerIndex) - signedBeaconBlockContainer, err := getBlock(ctx, s, resp.Data.Root[:]) + opCtx, cancel = context.WithTimeout(ctx, beacon.Timeout()) + signedBeaconBlock, err := blockProvider.SignedBeaconBlock(opCtx, &api.SignedBeaconBlockOpts{ + Common: api.CommonOpts{}, + Block: resp.Data.Root.String(), + }) + cancel() if err != nil { return nil, err } - var blockAttestations []*ethpbv1.Attestation - var attesterSlashings []*ethpbv1.AttesterSlashing - var proposerSlashings []*ethpbv1.ProposerSlashing - var marshalledTransactions [][]byte - var deposits []*ethpbv1.Deposit - switch signedBeaconBlockContainer.GetMessage().(type) { - case *ethpbv2.SignedBeaconBlockContainer_Phase0Block: - phase0Block := signedBeaconBlockContainer.GetPhase0Block() - blockAttestations = phase0Block.GetBody().GetAttestations() - attesterSlashings = phase0Block.GetBody().GetAttesterSlashings() - proposerSlashings = phase0Block.GetBody().GetProposerSlashings() - deposits = phase0Block.GetBody().GetDeposits() - case *ethpbv2.SignedBeaconBlockContainer_AltairBlock: - altairBlock := signedBeaconBlockContainer.GetAltairBlock() - blockAttestations = altairBlock.GetBody().GetAttestations() - attesterSlashings = altairBlock.GetBody().GetAttesterSlashings() - proposerSlashings = altairBlock.GetBody().GetProposerSlashings() - deposits = altairBlock.GetBody().GetDeposits() - case *ethpbv2.SignedBeaconBlockContainer_BellatrixBlock: - bellatrixBlock := signedBeaconBlockContainer.GetBellatrixBlock() - blockAttestations = bellatrixBlock.GetBody().GetAttestations() - attesterSlashings = bellatrixBlock.GetBody().GetAttesterSlashings() - proposerSlashings = bellatrixBlock.GetBody().GetProposerSlashings() - deposits = bellatrixBlock.GetBody().GetDeposits() - marshalledTransactions = bellatrixBlock.GetBody().GetExecutionPayload().GetTransactions() - case *ethpbv2.SignedBeaconBlockContainer_CapellaBlock: - capellaBlock := signedBeaconBlockContainer.GetCapellaBlock() - blockAttestations = capellaBlock.GetBody().GetAttestations() - attesterSlashings = capellaBlock.GetBody().GetAttesterSlashings() - proposerSlashings = capellaBlock.GetBody().GetProposerSlashings() - - // https://github.com/ethereum/annotated-spec/blob/98c63ebcdfee6435e8b2a76e1fca8549722f6336/merge/beacon-chain.md#custom-types%C2%B6 - // [...] execution blocks are stored in SSZ form, but the - // transactions inside them are encoded with RLP, and so - // to software that only understands SSZ they are - // presented as "opaque" byte arrays. - marshalledTransactions = capellaBlock.GetBody().GetExecutionPayload().GetTransactions() - default: - fmt.Println("Unknown Hardfork, exiting...") - log.Fatal() + blockAttestations, err := signedBeaconBlock.Data.Attestations() + if err != nil { + return nil, err } - var chainAttestations []*ChainAttestation for _, att := range blockAttestations { chainAttestations = append(chainAttestations, &ChainAttestation{ - AggregationBits: att.GetAggregationBits(), - CommitteeIndex: spec.CommitteeIndex(att.GetData().GetIndex()), - Slot: spec.Slot(att.GetData().GetSlot()), + AggregationBits: att.AggregationBits, + CommitteeIndex: spec.CommitteeIndex(att.Data.Index), + Slot: spec.Slot(att.Data.Slot), InclusionSlot: slot, }) } + attesterSlashings, err := signedBeaconBlock.Data.AttesterSlashings() + if err != nil { + return nil, err + } + + proposerSlashings, err := signedBeaconBlock.Data.ProposerSlashings() + if err != nil { + return nil, err + } + + deposits, err := signedBeaconBlock.Data.Deposits() + if err != nil { + return nil, err + } + + // https://github.com/ethereum/annotated-spec/blob/98c63ebcdfee6435e8b2a76e1fca8549722f6336/merge/beacon-chain.md#custom-types%C2%B6 + // [...] execution blocks are stored in SSZ form, but the + // transactions inside them are encoded with RLP, and so + // to software that only understands SSZ they are + // presented as "opaque" byte arrays. + marshalledTransactions, err := signedBeaconBlock.Data.ExecutionTransactions() + if err != nil { + return nil, err + } var unmarshalledTransactions []types.Transaction if marshalledTransactions != nil { txs, err := unmarshallTransactions(marshalledTransactions) @@ -347,7 +321,7 @@ func ListBlocks(ctx context.Context, s *prysmgrpc.Service, beacon *beaconchain.B Slot: slot, AttesterSlashings: attesterSlashings, ProposerSlashings: proposerSlashings, - BlockContainer: signedBeaconBlockContainer, + BlockContainer: signedBeaconBlock.Data, ChainAttestations: chainAttestations, Deposits: deposits, Transactions: unmarshalledTransactions, @@ -679,7 +653,7 @@ func MonitorAttestationsAndProposals(ctx context.Context, s *prysmgrpc.Service, Must(err) }, "ListBeaconCommittees(epoch=%v)", epoch) Measure(func() { - epochBlocks, err = ListBlocks(ctx, s, beacon, spec.Epoch(epoch)) + epochBlocks, err = ListBlocks(ctx, beacon, spec.Epoch(epoch)) Must(err) }, "ListBlocks(epoch=%v)", epoch) Measure(func() { @@ -865,7 +839,7 @@ func MonitorAttestationsAndProposals(ctx context.Context, s *prysmgrpc.Service, } // MonitorSlashings listens to the beacon chain head changes and checks for slashings. -func MonitorSlashings(ctx context.Context, s *prysmgrpc.Service, beacon *beaconchain.BeaconChain, wg *sync.WaitGroup) { +func MonitorSlashings(ctx context.Context, beacon *beaconchain.BeaconChain, wg *sync.WaitGroup) { defer wg.Done() for justifiedEpoch := range epochsChan { @@ -876,7 +850,7 @@ func MonitorSlashings(ctx context.Context, s *prysmgrpc.Service, beacon *beaconc epoch := justifiedEpoch Measure(func() { - blocks, err = ListBlocks(ctx, s, beacon, spec.Epoch(epoch)) + blocks, err = ListBlocks(ctx, beacon, spec.Epoch(epoch)) Must(err) }, "ListBlocks(epoch=%v)", epoch) diff --git a/pkg/slashings.go b/pkg/slashings.go index 76b0083..4d61f98 100644 --- a/pkg/slashings.go +++ b/pkg/slashings.go @@ -49,7 +49,7 @@ func ProcessSlashings(ctx context.Context, blocks map[spec.Slot][]*ChainBlock) ( attesterSlashings := chainBlock.AttesterSlashings for _, proposerSlashing := range proposerSlashings { - slashee := spec.ValidatorIndex(proposerSlashing.GetSignedHeader_1().GetMessage().GetProposerIndex()) + slashee := spec.ValidatorIndex(proposerSlashing.SignedHeader1.Message.ProposerIndex) ReportSlashing(ctx, "🚫 🧱", "proposed two conflicting blocks", slot, slasher, slashee) @@ -58,11 +58,11 @@ func ProcessSlashings(ctx context.Context, blocks map[spec.Slot][]*ChainBlock) ( for _, attesterSlashing := range attesterSlashings { var slashee spec.ValidatorIndex attestation1Validators := make(map[spec.ValidatorIndex]interface{}) - for _, index := range attesterSlashing.GetAttestation_1().GetAttestingIndices() { + for _, index := range attesterSlashing.Attestation1.AttestingIndices { attestation1Validators[index] = nil } - for _, index := range attesterSlashing.GetAttestation_2().GetAttestingIndices() { + for _, index := range attesterSlashing.Attestation2.AttestingIndices { if _, ok := attestation1Validators[index]; ok { slashee = index break