Add more information to "epoch summary".

This commit is contained in:
Jim McDonald
2022-10-01 22:52:20 +01:00
parent 290413f115
commit fe0bfd4f87
7 changed files with 438 additions and 63 deletions

80
util/attestations.go Normal file
View File

@@ -0,0 +1,80 @@
// Copyright © 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"bytes"
"context"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/wealdtech/ethdo/services/chaintime"
)
// AttestationHeadCorrect returns true if the given attestation had the correct head.
func AttestationHeadCorrect(ctx context.Context,
headersCache *BeaconBlockHeaderCache,
attestation *phase0.Attestation,
) (
bool,
error,
) {
slot := attestation.Data.Slot
for {
header, err := headersCache.Fetch(ctx, slot)
if err != nil {
return false, nil
}
if header == nil {
// No block.
slot--
continue
}
if !header.Canonical {
// Not canonical.
slot--
continue
}
return bytes.Equal(header.Root[:], attestation.Data.BeaconBlockRoot[:]), nil
}
}
// AttestationTargetCorrect returns true if the given attestation had the correct target.
func AttestationTargetCorrect(ctx context.Context,
headersCache *BeaconBlockHeaderCache,
chainTime chaintime.Service,
attestation *phase0.Attestation,
) (
bool,
error,
) {
// Start with first slot of the target epoch.
slot := chainTime.FirstSlotOfEpoch(attestation.Data.Target.Epoch)
for {
header, err := headersCache.Fetch(ctx, slot)
if err != nil {
return false, nil
}
if header == nil {
// No block.
slot--
continue
}
if !header.Canonical {
// Not canonical.
slot--
continue
}
return bytes.Equal(header.Root[:], attestation.Data.Target.Root[:]), nil
}
}

70
util/beaconheadercache.go Normal file
View File

@@ -0,0 +1,70 @@
// Copyright © 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"context"
"fmt"
eth2client "github.com/attestantio/go-eth2-client"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
)
// BeaconBlockHeaderCache is a cache of beacon block headers.
type BeaconBlockHeaderCache struct {
beaconBlockHeadersProvider eth2client.BeaconBlockHeadersProvider
entries map[phase0.Slot]*beaconBlockHeaderEntry
}
// NewBeaconBlockHeaderCache makes a new beacon block header cache.
func NewBeaconBlockHeaderCache(provider eth2client.BeaconBlockHeadersProvider) *BeaconBlockHeaderCache {
return &BeaconBlockHeaderCache{
beaconBlockHeadersProvider: provider,
entries: make(map[phase0.Slot]*beaconBlockHeaderEntry),
}
}
type beaconBlockHeaderEntry struct {
present bool
value *apiv1.BeaconBlockHeader
}
// Fetch the beacon block header for the given slot.
func (b *BeaconBlockHeaderCache) Fetch(ctx context.Context,
slot phase0.Slot,
) (
*apiv1.BeaconBlockHeader,
error,
) {
entry, exists := b.entries[slot]
if !exists {
header, err := b.beaconBlockHeadersProvider.BeaconBlockHeader(ctx, fmt.Sprintf("%d", slot))
if err != nil {
return nil, err
}
if header == nil {
entry = &beaconBlockHeaderEntry{
present: false,
}
} else {
entry = &beaconBlockHeaderEntry{
present: true,
value: header,
}
}
b.entries[slot] = entry
}
return entry.value, nil
}

132
util/validators.go Normal file
View File

@@ -0,0 +1,132 @@
// Copyright © 2022 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"context"
"encoding/hex"
"fmt"
"strconv"
"strings"
eth2client "github.com/attestantio/go-eth2-client"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
)
// ParseValidators parses input to obtain the list of validators.
func ParseValidators(ctx context.Context, validatorsProvider eth2client.ValidatorsProvider, validatorsStr []string, stateID string) ([]*apiv1.Validator, error) {
validators := make([]*apiv1.Validator, 0, len(validatorsStr))
for i := range validatorsStr {
if strings.Contains(validatorsStr[i], "-") {
// Range.
bits := strings.Split(validatorsStr[i], "-")
if len(bits) != 2 {
return nil, fmt.Errorf("invalid range %s", validatorsStr[i])
}
low, err := strconv.ParseUint(bits[0], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "invalid range start")
}
high, err := strconv.ParseUint(bits[1], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "invalid range end")
}
indices := make([]phase0.ValidatorIndex, 0)
for index := low; index <= high; index++ {
indices = append(indices, phase0.ValidatorIndex(index))
}
rangeValidators, err := validatorsProvider.Validators(ctx, stateID, indices)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to obtain validators %s", validatorsStr[i]))
}
for _, validator := range rangeValidators {
validators = append(validators, validator)
}
} else {
validator, err := ParseValidator(ctx, validatorsProvider, validatorsStr[i], stateID)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("unknown validator %s", validatorsStr[i]))
}
validators = append(validators, validator)
}
}
return validators, nil
}
// ParseValidator parses input to obtain the validator.
func ParseValidator(ctx context.Context,
validatorsProvider eth2client.ValidatorsProvider,
validatorStr string,
stateID string,
) (
*apiv1.Validator,
error,
) {
var validators map[phase0.ValidatorIndex]*apiv1.Validator
switch {
case strings.HasPrefix(validatorStr, "0x"):
// A public key.
data, err := hex.DecodeString(strings.TrimPrefix(validatorStr, "0x"))
if err != nil {
return nil, errors.Wrap(err, "failed to parse validator public key")
}
pubKey := phase0.BLSPubKey{}
copy(pubKey[:], data)
validators, err = validatorsProvider.ValidatorsByPubKey(ctx,
stateID,
[]phase0.BLSPubKey{pubKey},
)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain validator information")
}
case strings.Contains(validatorStr, "/"):
// An account.
_, account, err := WalletAndAccountFromPath(ctx, validatorStr)
if err != nil {
return nil, errors.Wrap(err, "unable to obtain account")
}
accPubKey, err := BestPublicKey(account)
if err != nil {
return nil, errors.Wrap(err, "unable to obtain public key for account")
}
pubKey := phase0.BLSPubKey{}
copy(pubKey[:], accPubKey.Marshal())
validators, err = validatorsProvider.ValidatorsByPubKey(ctx,
stateID,
[]phase0.BLSPubKey{pubKey},
)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain validator information")
}
default:
// An index.
index, err := strconv.ParseUint(validatorStr, 10, 64)
if err != nil {
return nil, errors.Wrap(err, "failed to parse validator index")
}
validators, err = validatorsProvider.Validators(ctx, stateID, []phase0.ValidatorIndex{phase0.ValidatorIndex(index)})
if err != nil {
return nil, errors.Wrap(err, "failed to obtain validator information")
}
}
// Validator is first entry in the map.
for _, validator := range validators {
return validator, nil
}
return nil, errors.New("unknown validator")
}