Various code cleanups (#8889)

This commit is contained in:
terence tsao
2021-05-12 23:29:14 -07:00
committed by GitHub
parent 4ef5c9ad15
commit bc27a73600
14 changed files with 22 additions and 36 deletions

View File

@@ -61,14 +61,14 @@ func (s *Store) Backup(ctx context.Context, outputDir string) error {
}() }()
// Prefetch all keys of buckets, and inner keys in a // Prefetch all keys of buckets, and inner keys in a
// bucket to use less memory usage when backing up. // bucket to use less memory usage when backing up.
bucketKeys := [][]byte{} var bucketKeys [][]byte
bucketMap := make(map[string][][]byte) bucketMap := make(map[string][][]byte)
err = s.db.View(func(tx *bolt.Tx) error { err = s.db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, b *bolt.Bucket) error { return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
newName := make([]byte, len(name)) newName := make([]byte, len(name))
copy(newName, name) copy(newName, name)
bucketKeys = append(bucketKeys, newName) bucketKeys = append(bucketKeys, newName)
innerKeys := [][]byte{} var innerKeys [][]byte
err := b.ForEach(func(k, v []byte) error { err := b.ForEach(func(k, v []byte) error {
if k == nil { if k == nil {
return nil return nil

View File

@@ -232,7 +232,7 @@ func (vs *Server) SubscribeCommitteeSubnets(ctx context.Context, req *ethpb.Comm
} }
currEpoch = helpers.SlotToEpoch(req.Slots[i]) currEpoch = helpers.SlotToEpoch(req.Slots[i])
} }
subnet := helpers.ComputeSubnetFromCommitteeAndSlot(currValsLen, types.CommitteeIndex(req.CommitteeIds[i]), req.Slots[i]) subnet := helpers.ComputeSubnetFromCommitteeAndSlot(currValsLen, req.CommitteeIds[i], req.Slots[i])
cache.SubnetIDs.AddAttesterSubnetID(req.Slots[i], subnet) cache.SubnetIDs.AddAttesterSubnetID(req.Slots[i], subnet)
if req.IsAggregator[i] { if req.IsAggregator[i] {
cache.SubnetIDs.AddAggregatorSubnetID(req.Slots[i], subnet) cache.SubnetIDs.AddAggregatorSubnetID(req.Slots[i], subnet)

View File

@@ -62,11 +62,11 @@ func TestAppendBeyondIndicesLimit(t *testing.T) {
assert.NoError(t, st.AppendValidator(&eth.Validator{})) assert.NoError(t, st.AppendValidator(&eth.Validator{}))
} }
assert.Equal(t, false, st.rebuildTrie[validators]) assert.Equal(t, false, st.rebuildTrie[validators])
assert.NotEqual(t, len(st.dirtyIndices[validators]), int(0)) assert.NotEqual(t, len(st.dirtyIndices[validators]), 0)
for i := 0; i < indicesLimit; i++ { for i := 0; i < indicesLimit; i++ {
assert.NoError(t, st.AppendValidator(&eth.Validator{})) assert.NoError(t, st.AppendValidator(&eth.Validator{}))
} }
assert.Equal(t, true, st.rebuildTrie[validators]) assert.Equal(t, true, st.rebuildTrie[validators])
assert.Equal(t, len(st.dirtyIndices[validators]), int(0)) assert.Equal(t, len(st.dirtyIndices[validators]), 0)
} }

View File

@@ -7,22 +7,22 @@ import (
) )
var ( var (
// BeaconCertFlag defines a flag for the beacon api certificate. // ValidatorMetricsURLFlag defines a flag for the URL to the validator /metrics prometheus endpoint to scrape.
ValidatorMetricsURLFlag = &cli.StringFlag{ ValidatorMetricsURLFlag = &cli.StringFlag{
Name: "validator-metrics-url", Name: "validator-metrics-url",
Usage: "Full URL to the validator /metrics prometheus endpoint to scrape. eg http://localhost:8081/metrics", Usage: "Full URL to the validator /metrics prometheus endpoint to scrape. eg http://localhost:8081/metrics",
} }
// BeaconRPCProviderFlag defines a flag for the beacon host ip or address. // BeaconnodeMetricsURLFlag defines a flag for the URL to the beacon-node /metrics prometheus endpoint to scraps.
BeaconnodeMetricsURLFlag = &cli.StringFlag{ BeaconnodeMetricsURLFlag = &cli.StringFlag{
Name: "beacon-node-metrics-url", Name: "beacon-node-metrics-url",
Usage: "Full URL to the beacon-node /metrics prometheus endpoint to scrape. eg http://localhost:8080/metrics", Usage: "Full URL to the beacon-node /metrics prometheus endpoint to scrape. eg http://localhost:8080/metrics",
} }
// CertFlag defines a flag for the node's TLS certificate. // ClientStatsAPIURLFlag defines a flag for the URL to the client stats endpoint where collected metrics should be sent.
ClientStatsAPIURLFlag = &cli.StringFlag{ ClientStatsAPIURLFlag = &cli.StringFlag{
Name: "clientstats-api-url", Name: "clientstats-api-url",
Usage: "Full URL to the client stats endpoint where collected metrics should be sent.", Usage: "Full URL to the client stats endpoint where collected metrics should be sent.",
} }
// CertFlag defines a flag for the node's TLS certificate. // ScrapeIntervalFlag defines a flag for the frequency of scraping.
ScrapeIntervalFlag = &cli.DurationFlag{ ScrapeIntervalFlag = &cli.DurationFlag{
Name: "scrape-interval", Name: "scrape-interval",
Usage: "Frequency of scraping expressed as a duration, eg 2m or 1m5s. Default is 60s.", Usage: "Frequency of scraping expressed as a duration, eg 2m or 1m5s. Default is 60s.",

View File

@@ -191,20 +191,6 @@ var (
Usage: "Comma-separated list of public key hex strings to specify which validator accounts to delete", Usage: "Comma-separated list of public key hex strings to specify which validator accounts to delete",
Value: "", Value: "",
} }
// DisablePublicKeysFlag defines a comma-separated list of hex string public keys
// for accounts which a user desires to disable for their wallet.
DisablePublicKeysFlag = &cli.StringFlag{
Name: "disable-public-keys",
Usage: "Comma-separated list of public key hex strings to specify which validator accounts to disable",
Value: "",
}
// EnablePublicKeysFlag defines a comma-separated list of hex string public keys
// for accounts which a user desires to enable for their wallet.
EnablePublicKeysFlag = &cli.StringFlag{
Name: "enable-public-keys",
Usage: "Comma-separated list of public key hex strings to specify which validator accounts to enable",
Value: "",
}
// BackupPublicKeysFlag defines a comma-separated list of hex string public keys // BackupPublicKeysFlag defines a comma-separated list of hex string public keys
// for accounts which a user desires to backup from their wallet. // for accounts which a user desires to backup from their wallet.
BackupPublicKeysFlag = &cli.StringFlag{ BackupPublicKeysFlag = &cli.StringFlag{

View File

@@ -118,13 +118,13 @@ type metricMap map[string]*dto.MetricFamily
func (mm metricMap) getFamily(name string) (*dto.MetricFamily, error) { func (mm metricMap) getFamily(name string) (*dto.MetricFamily, error) {
f, ok := mm[name] f, ok := mm[name]
if !ok { if !ok {
return nil, fmt.Errorf("Scraper did not find metric family %s", name) return nil, fmt.Errorf("scraper did not find metric family %s", name)
} }
return f, nil return f, nil
} }
var now = time.Now // var hook for tests to overwrite var now = time.Now // var hook for tests to overwrite
var nanosPerMilli = (int64(time.Millisecond) / int64(time.Nanosecond)) var nanosPerMilli = int64(time.Millisecond) / int64(time.Nanosecond)
func populateAPIMessage(processName string) APIMessage { func populateAPIMessage(processName string) APIMessage {
return APIMessage{ return APIMessage{

View File

@@ -53,7 +53,7 @@ type Gateway struct {
allowedOrigins []string allowedOrigins []string
} }
// New returns a new gateway server which translates HTTP into gRPC. // NewValidator returns a new gateway server which translates HTTP into gRPC.
// Accepts a context. // Accepts a context.
func NewValidator( func NewValidator(
ctx context.Context, ctx context.Context,
@@ -71,7 +71,7 @@ func NewValidator(
} }
} }
// New returns a new gateway server which translates HTTP into gRPC. // NewBeacon returns a new gateway server which translates HTTP into gRPC.
// Accepts a context and optional http.ServeMux. // Accepts a context and optional http.ServeMux.
func NewBeacon( func NewBeacon(
ctx context.Context, ctx context.Context,

View File

@@ -342,7 +342,7 @@ func (bld *builder) swtch(stmt ast.Stmt, cases []ast.Stmt) {
} }
} }
parents := []*block{} var parents []*block
if c.List != nil { if c.List != nil {
parents = append(parents, list) parents = append(parents, list)
} }

View File

@@ -28,7 +28,7 @@ const (
KeymanagerConfigFileName = "keymanageropts.json" KeymanagerConfigFileName = "keymanageropts.json"
// NewWalletPasswordPromptText for wallet creation. // NewWalletPasswordPromptText for wallet creation.
NewWalletPasswordPromptText = "New wallet password" NewWalletPasswordPromptText = "New wallet password"
// WalletPasswordPromptText for wallet unlocking. // PasswordPromptText for wallet unlocking.
PasswordPromptText = "Wallet password" PasswordPromptText = "Wallet password"
// ConfirmPasswordPromptText for confirming a wallet password. // ConfirmPasswordPromptText for confirming a wallet password.
ConfirmPasswordPromptText = "Confirm password" ConfirmPasswordPromptText = "Confirm password"

View File

@@ -20,9 +20,9 @@ const (
RoleUnknown ValidatorRole = iota RoleUnknown ValidatorRole = iota
// RoleAttester means that the validator should submit an attestation. // RoleAttester means that the validator should submit an attestation.
RoleAttester RoleAttester
// RoleAttester means that the validator should propose a block. // RoleProposer means that the validator should propose a block.
RoleProposer RoleProposer
// RoleAttester means that the validator should submit an aggregation and proof. // RoleAggregator means that the validator should submit an aggregation and proof.
RoleAggregator RoleAggregator
) )

View File

@@ -39,7 +39,7 @@ func (v *validator) ProposeBlock(ctx context.Context, slot types.Slot, pubKey [4
log.Debug("Assigned to genesis slot, skipping proposal") log.Debug("Assigned to genesis slot, skipping proposal")
return return
} }
lock := mputil.NewMultilock(string(rune(iface.RoleProposer)), string(pubKey[:])) lock := mputil.NewMultilock(string(iface.RoleProposer), string(pubKey[:]))
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
ctx, span := trace.StartSpan(ctx, "validator.ProposeBlock") ctx, span := trace.StartSpan(ctx, "validator.ProposeBlock")

View File

@@ -650,7 +650,7 @@ func TestRolesAt_OK(t *testing.T) {
roleMap, err := v.RolesAt(context.Background(), 1) roleMap, err := v.RolesAt(context.Background(), 1)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, iface.ValidatorRole(iface.RoleAttester), roleMap[bytesutil.ToBytes48(validatorKey.PublicKey().Marshal())][0]) assert.Equal(t, iface.RoleAttester, roleMap[bytesutil.ToBytes48(validatorKey.PublicKey().Marshal())][0])
} }
func TestRolesAt_DoesNotAssignProposer_Slot0(t *testing.T) { func TestRolesAt_DoesNotAssignProposer_Slot0(t *testing.T) {
@@ -676,7 +676,7 @@ func TestRolesAt_DoesNotAssignProposer_Slot0(t *testing.T) {
roleMap, err := v.RolesAt(context.Background(), 0) roleMap, err := v.RolesAt(context.Background(), 0)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, iface.ValidatorRole(iface.RoleAttester), roleMap[bytesutil.ToBytes48(validatorKey.PublicKey().Marshal())][0]) assert.Equal(t, iface.RoleAttester, roleMap[bytesutil.ToBytes48(validatorKey.PublicKey().Marshal())][0])
} }
func TestCheckAndLogValidatorStatus_OK(t *testing.T) { func TestCheckAndLogValidatorStatus_OK(t *testing.T) {

View File

@@ -58,7 +58,7 @@ func emptyHistoryData() *deprecatedHistoryData {
// newDeprecatedAttestingHistory creates a new encapsulated attestation history byte array // newDeprecatedAttestingHistory creates a new encapsulated attestation history byte array
// sized by the latest epoch written. // sized by the latest epoch written.
func newDeprecatedAttestingHistory(target types.Epoch) deprecatedEncodedAttestingHistory { func newDeprecatedAttestingHistory(target types.Epoch) deprecatedEncodedAttestingHistory {
relativeTarget := types.Epoch(target % params.BeaconConfig().WeakSubjectivityPeriod) relativeTarget := target % params.BeaconConfig().WeakSubjectivityPeriod
historyDataSize := (relativeTarget + 1) * historySize historyDataSize := (relativeTarget + 1) * historySize
arraySize := latestEpochWrittenSize + historyDataSize arraySize := latestEpochWrittenSize + historyDataSize
en := make(deprecatedEncodedAttestingHistory, arraySize) en := make(deprecatedEncodedAttestingHistory, arraySize)

View File

@@ -17,7 +17,7 @@ import (
func (s *Store) PruneAttestations(ctx context.Context) error { func (s *Store) PruneAttestations(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "Validator.PruneAttestations") ctx, span := trace.StartSpan(ctx, "Validator.PruneAttestations")
defer span.End() defer span.End()
pubkeys := [][]byte{} var pubkeys [][]byte
err := s.view(func(tx *bolt.Tx) error { err := s.view(func(tx *bolt.Tx) error {
bucket := tx.Bucket(pubKeysBucket) bucket := tx.Bucket(pubKeysBucket)
return bucket.ForEach(func(pubKey []byte, _ []byte) error { return bucket.ForEach(func(pubKey []byte, _ []byte) error {