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

ProcessPreGenesisDeposits: Remove context.TODO() usage #8705

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions beacon-chain/core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
// state.compact_committees_roots[index] = committee_root
// return state
// This method differs from the spec so as to process deposits beforehand instead of the end of the function.
func GenesisBeaconState(deposits []*ethpb.Deposit, genesisTime uint64, eth1Data *ethpb.Eth1Data) (iface.BeaconState, error) {
func GenesisBeaconState(ctx context.Context, deposits []*ethpb.Deposit, genesisTime uint64, eth1Data *ethpb.Eth1Data) (iface.BeaconState, error) {
state, err := EmptyGenesisState()
if err != nil {
return nil, err
Expand All @@ -67,7 +67,7 @@ func GenesisBeaconState(deposits []*ethpb.Deposit, genesisTime uint64, eth1Data
return nil, err
}

state, err = b.ProcessPreGenesisDeposits(context.TODO(), state, deposits)
state, err = b.ProcessPreGenesisDeposits(ctx, state, deposits)
if err != nil {
return nil, errors.Wrap(err, "could not process validator deposits")
}
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/core/state/state_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state

import (
"context"
"testing"

fuzz "github.com/google/gofuzz"
Expand All @@ -20,7 +21,7 @@ func TestGenesisBeaconState_1000(t *testing.T) {
fuzzer.Fuzz(&deposits)
fuzzer.Fuzz(&genesisTime)
fuzzer.Fuzz(eth1Data)
gs, err := GenesisBeaconState(deposits, genesisTime, eth1Data)
gs, err := GenesisBeaconState(context.Background(), deposits, genesisTime, eth1Data)
if err != nil {
if gs != nil {
t.Fatalf("Genesis state should be nil on err. found: %v on error: %v for inputs deposit: %v "+
Expand Down
11 changes: 6 additions & 5 deletions beacon-chain/core/state/state_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state_test

import (
"context"
"testing"

"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestGenesisBeaconState_OK(t *testing.T) {
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
newState, err := state.GenesisBeaconState(deposits, genesisTime, eth1Data)
newState, err := state.GenesisBeaconState(context.Background(), deposits, genesisTime, eth1Data)
require.NoError(t, err, "Could not execute GenesisBeaconState")

// Misc fields checks.
Expand Down Expand Up @@ -93,9 +94,9 @@ func TestGenesisBeaconState_OK(t *testing.T) {
func TestGenesisState_HashEquality(t *testing.T) {
deposits, _, err := testutil.DeterministicDepositsAndKeys(100)
require.NoError(t, err)
state1, err := state.GenesisBeaconState(deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
state1, err := state.GenesisBeaconState(context.Background(), deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
require.NoError(t, err)
state2, err := state.GenesisBeaconState(deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
state2, err := state.GenesisBeaconState(context.Background(), deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
require.NoError(t, err)

pbState1, err := stateV0.ProtobufBeaconState(state1.CloneInnerState())
Expand All @@ -113,7 +114,7 @@ func TestGenesisState_HashEquality(t *testing.T) {
}

func TestGenesisState_InitializesLatestBlockHashes(t *testing.T) {
s, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{})
s, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{})
require.NoError(t, err)
got, want := uint64(len(s.BlockRoots())), uint64(params.BeaconConfig().SlotsPerHistoricalRoot)
assert.Equal(t, want, got, "Wrong number of recent block hashes")
Expand All @@ -127,6 +128,6 @@ func TestGenesisState_InitializesLatestBlockHashes(t *testing.T) {
}

func TestGenesisState_FailsWithoutEth1data(t *testing.T) {
_, err := state.GenesisBeaconState(nil, 0, nil)
_, err := state.GenesisBeaconState(context.Background(), nil, 0, nil)
assert.ErrorContains(t, "no eth1data provided for genesis state", err)
}
2 changes: 1 addition & 1 deletion beacon-chain/interop-cold-start/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewService(ctx context.Context, cfg *Config) *Service {
}

// Save genesis state in db
genesisState, _, err := interop.GenerateGenesisState(s.cfg.GenesisTime, s.cfg.NumValidators)
genesisState, _, err := interop.GenerateGenesisState(ctx, s.cfg.GenesisTime, s.cfg.NumValidators)
if err != nil {
log.Fatalf("Could not generate interop genesis state: %v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/rpc/validator/assignments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGetDuties_OK(t *testing.T) {
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
bs, err := state.GenesisBeaconState(deposits, 0, eth1Data)
bs, err := state.GenesisBeaconState(context.Background(), deposits, 0, eth1Data)
require.NoError(t, err, "Could not setup genesis bs")
genesisRoot, err := genesis.Block.HashTreeRoot()
require.NoError(t, err, "Could not get signing root")
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestGetDuties_CurrentEpoch_ShouldNotFail(t *testing.T) {
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
bState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
bState, err := state.GenesisBeaconState(context.Background(), deposits, 0, eth1Data)
require.NoError(t, err, "Could not setup genesis state")
// Set state to non-epoch start slot.
require.NoError(t, bState.SetSlot(5))
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestGetDuties_MultipleKeys_OK(t *testing.T) {
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
bs, err := state.GenesisBeaconState(deposits, 0, eth1Data)
bs, err := state.GenesisBeaconState(context.Background(), deposits, 0, eth1Data)
require.NoError(t, err, "Could not setup genesis bs")
genesisRoot, err := genesis.Block.HashTreeRoot()
require.NoError(t, err, "Could not get signing root")
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestStreamDuties_OK(t *testing.T) {
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
bs, err := state.GenesisBeaconState(deposits, 0, eth1Data)
bs, err := state.GenesisBeaconState(context.Background(), deposits, 0, eth1Data)
require.NoError(t, err, "Could not setup genesis bs")
genesisRoot, err := genesis.Block.HashTreeRoot()
require.NoError(t, err, "Could not get signing root")
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestStreamDuties_OK_ChainReorg(t *testing.T) {
require.NoError(t, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(t, err)
bs, err := state.GenesisBeaconState(deposits, 0, eth1Data)
bs, err := state.GenesisBeaconState(context.Background(), deposits, 0, eth1Data)
require.NoError(t, err, "Could not setup genesis bs")
genesisRoot, err := genesis.Block.HashTreeRoot()
require.NoError(t, err, "Could not get signing root")
Expand Down Expand Up @@ -384,7 +384,7 @@ func BenchmarkCommitteeAssignment(b *testing.B) {
require.NoError(b, err)
eth1Data, err := testutil.DeterministicEth1Data(len(deposits))
require.NoError(b, err)
bs, err := state.GenesisBeaconState(deposits, 0, eth1Data)
bs, err := state.GenesisBeaconState(context.Background(), deposits, 0, eth1Data)
require.NoError(b, err, "Could not setup genesis bs")
genesisRoot, err := genesis.Block.HashTreeRoot()
require.NoError(b, err, "Could not get signing root")
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/rpc/validator/exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestProposeExit_Notification(t *testing.T) {
testutil.ResetCache()
deposits, keys, err := testutil.DeterministicDepositsAndKeys(params.BeaconConfig().MinGenesisActiveValidatorCount)
require.NoError(t, err)
beaconState, err := state.GenesisBeaconState(deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
beaconState, err := state.GenesisBeaconState(ctx, deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
require.NoError(t, err)
epoch := types.Epoch(2048)
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(epoch))))
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestProposeExit_NoPanic(t *testing.T) {
testutil.ResetCache()
deposits, keys, err := testutil.DeterministicDepositsAndKeys(params.BeaconConfig().MinGenesisActiveValidatorCount)
require.NoError(t, err)
beaconState, err := state.GenesisBeaconState(deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
beaconState, err := state.GenesisBeaconState(ctx, deposits, 0, &ethpb.Eth1Data{BlockHash: make([]byte, 32)})
require.NoError(t, err)
epoch := types.Epoch(2048)
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(epoch))))
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/stateV0/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestBeaconState_ProtoBeaconStateCompatibility(t *testing.T) {
}

func setupGenesisState(tb testing.TB, count uint64) *pb.BeaconState {
genesisState, _, err := interop.GenerateGenesisState(0, count)
genesisState, _, err := interop.GenerateGenesisState(context.Background(), 0, count)
require.NoError(tb, err, "Could not generate genesis beacon state")
for i := uint64(1); i < count; i++ {
someRoot := [32]byte{}
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/state/stateutil/state_root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stateutil_test

import (
"context"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -58,7 +59,7 @@ func BenchmarkHashTreeRoot_Generic_300000(b *testing.B) {
}

func setupGenesisState(tb testing.TB, count uint64) *pb.BeaconState {
genesisState, _, err := interop.GenerateGenesisState(0, 1)
genesisState, _, err := interop.GenerateGenesisState(context.Background(), 0, 1)
require.NoError(tb, err, "Could not generate genesis beacon state")
for i := uint64(1); i < count; i++ {
someRoot := [32]byte{}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/sync/rpc_beacon_blocks_by_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestRecentBeaconBlocks_RPCRequestSent(t *testing.T) {
require.NoError(t, err)
blockBRoot, err := blockB.Block.HashTreeRoot()
require.NoError(t, err)
genesisState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{})
genesisState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{})
require.NoError(t, err)
require.NoError(t, genesisState.SetSlot(111))
require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), blockARoot))
Expand Down
14 changes: 7 additions & 7 deletions beacon-chain/sync/rpc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestStatusRPCHandler_ReturnsHelloMessage(t *testing.T) {
finalized.Block.Slot = blkSlot
finalizedRoot, err := finalized.Block.HashTreeRoot()
require.NoError(t, err)
genesisState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{})
genesisState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{})
require.NoError(t, err)
require.NoError(t, genesisState.SetSlot(111))
require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), headRoot))
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestStatusRPCRequest_RequestSent(t *testing.T) {
finalized.Block.Slot = 40
finalizedRoot, err := finalized.Block.HashTreeRoot()
require.NoError(t, err)
genesisState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{})
genesisState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{})
require.NoError(t, err)
require.NoError(t, genesisState.SetSlot(111))
require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), headRoot))
Expand Down Expand Up @@ -471,7 +471,7 @@ func TestStatusRPCRequest_FinalizedBlockExists(t *testing.T) {
finalized.Block.Slot = blkSlot
finalizedRoot, err := finalized.Block.HashTreeRoot()
require.NoError(t, err)
genesisState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{DepositRoot: make([]byte, 32), BlockHash: make([]byte, 32)})
genesisState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{DepositRoot: make([]byte, 32), BlockHash: make([]byte, 32)})
require.NoError(t, err)
require.NoError(t, genesisState.SetSlot(111))
require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), headRoot))
Expand Down Expand Up @@ -550,7 +550,7 @@ func TestStatusRPCRequest_FinalizedBlockExists(t *testing.T) {
func TestStatusRPCRequest_FinalizedBlockSkippedSlots(t *testing.T) {
db, err := kv.NewKVStore(context.Background(), t.TempDir(), &kv.Config{})
require.NoError(t, err)
bState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{DepositRoot: make([]byte, 32), BlockHash: make([]byte, 32)})
bState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{DepositRoot: make([]byte, 32), BlockHash: make([]byte, 32)})
require.NoError(t, err)

blk := testutil.NewBeaconBlock()
Expand Down Expand Up @@ -736,7 +736,7 @@ func TestStatusRPCRequest_BadPeerHandshake(t *testing.T) {
finalized := testutil.NewBeaconBlock()
finalizedRoot, err := finalized.Block.HashTreeRoot()
require.NoError(t, err)
genesisState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{})
genesisState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{})
require.NoError(t, err)
require.NoError(t, genesisState.SetSlot(111))
require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), headRoot))
Expand Down Expand Up @@ -817,7 +817,7 @@ func TestStatusRPC_ValidGenesisMessage(t *testing.T) {
finalized.Block.Slot = blkSlot
finalizedRoot, err := finalized.Block.HashTreeRoot()
require.NoError(t, err)
genesisState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{})
genesisState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{})
require.NoError(t, err)
require.NoError(t, genesisState.SetSlot(111))
require.NoError(t, genesisState.UpdateBlockRootAtIndex(111%uint64(params.BeaconConfig().SlotsPerHistoricalRoot), headRoot))
Expand Down Expand Up @@ -904,7 +904,7 @@ func TestShouldResync(t *testing.T) {
},
}
for _, tt := range tests {
headState, err := state.GenesisBeaconState(nil, 0, &ethpb.Eth1Data{})
headState, err := state.GenesisBeaconState(context.Background(), nil, 0, &ethpb.Eth1Data{})
require.NoError(t, err)
require.NoError(t, headState.SetSlot(tt.args.headSlot))
r := &Service{
Expand Down
9 changes: 5 additions & 4 deletions shared/interop/generate_genesis_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package interop

import (
"context"
"sync"

"github.com/pkg/errors"
Expand All @@ -27,7 +28,7 @@ var (

// GenerateGenesisState deterministically given a genesis time and number of validators.
// If a genesis time of 0 is supplied it is set to the current time.
func GenerateGenesisState(genesisTime, numValidators uint64) (*pb.BeaconState, []*ethpb.Deposit, error) {
func GenerateGenesisState(ctx context.Context, genesisTime, numValidators uint64) (*pb.BeaconState, []*ethpb.Deposit, error) {
privKeys, pubKeys, err := DeterministicallyGenerateKeys(0 /*startIndex*/, numValidators)
if err != nil {
return nil, nil, errors.Wrapf(err, "could not deterministically generate keys for %d validators", numValidators)
Expand All @@ -36,13 +37,13 @@ func GenerateGenesisState(genesisTime, numValidators uint64) (*pb.BeaconState, [
if err != nil {
return nil, nil, errors.Wrap(err, "could not generate deposit data from keys")
}
return GenerateGenesisStateFromDepositData(genesisTime, depositDataItems, depositDataRoots)
return GenerateGenesisStateFromDepositData(ctx, genesisTime, depositDataItems, depositDataRoots)
}

// GenerateGenesisStateFromDepositData creates a genesis state given a list of
// deposit data items and their corresponding roots.
func GenerateGenesisStateFromDepositData(
genesisTime uint64, depositData []*ethpb.Deposit_Data, depositDataRoots [][]byte,
ctx context.Context, genesisTime uint64, depositData []*ethpb.Deposit_Data, depositDataRoots [][]byte,
) (*pb.BeaconState, []*ethpb.Deposit, error) {
trie, err := trieutil.GenerateTrieFromItems(depositDataRoots, params.BeaconConfig().DepositContractTreeDepth)
if err != nil {
Expand All @@ -56,7 +57,7 @@ func GenerateGenesisStateFromDepositData(
if genesisTime == 0 {
genesisTime = uint64(timeutils.Now().Unix())
}
beaconState, err := coreState.GenesisBeaconState(deposits, genesisTime, &ethpb.Eth1Data{
beaconState, err := coreState.GenesisBeaconState(ctx, deposits, genesisTime, &ethpb.Eth1Data{
DepositRoot: root[:],
DepositCount: uint64(len(deposits)),
BlockHash: mockEth1BlockHash,
Expand Down
3 changes: 2 additions & 1 deletion shared/interop/generate_genesis_state_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package interop_test

import (
"context"
"testing"

eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
Expand All @@ -23,7 +24,7 @@ func TestGenerateGenesisState(t *testing.T) {
deposits, err := interop.GenerateDepositsFromData(depositDataItems, trie)
require.NoError(t, err)
root := trie.Root()
genesisState, err := state.GenesisBeaconState(deposits, 0, &eth.Eth1Data{
genesisState, err := state.GenesisBeaconState(context.Background(), deposits, 0, &eth.Eth1Data{
DepositRoot: root[:],
DepositCount: uint64(len(deposits)),
})
Expand Down
3 changes: 2 additions & 1 deletion shared/testutil/deposits.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutil

import (
"context"
"sync"
"testing"

Expand Down Expand Up @@ -254,7 +255,7 @@ func DeterministicGenesisState(t testing.TB, numValidators uint64) (iface.Beacon
if err != nil {
t.Fatal(errors.Wrapf(err, "failed to get eth1data for %d deposits", numValidators))
}
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), eth1Data)
beaconState, err := state.GenesisBeaconState(context.Background(), deposits, uint64(0), eth1Data)
if err != nil {
t.Fatal(errors.Wrapf(err, "failed to get genesis beacon state of %d validators", numValidators))
}
Expand Down
2 changes: 1 addition & 1 deletion tools/benchmark-files-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
}

func generateGenesisBeaconState() error {
genesisState, _, err := interop.GenerateGenesisState(0, benchutil.ValidatorCount)
genesisState, _, err := interop.GenerateGenesisState(context.Background(), 0, benchutil.ValidatorCount)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions tools/genesis-state-gen/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/hex"
"encoding/json"
"flag"
Expand Down Expand Up @@ -85,7 +86,7 @@ func main() {
return
}
// If no JSON input is specified, we create the state deterministically from interop keys.
genesisState, _, err = interop.GenerateGenesisState(*genesisTime, uint64(*numValidators))
genesisState, _, err = interop.GenerateGenesisState(context.Background(), *genesisTime, uint64(*numValidators))
if err != nil {
log.Printf("Could not generate genesis beacon state: %v", err)
return
Expand Down Expand Up @@ -149,7 +150,7 @@ func genesisStateFromJSONValidators(r io.Reader, genesisTime uint64) (*pb.Beacon
depositDataList[i] = data
depositDataRoots[i] = dataRootBytes
}
beaconState, _, err := interop.GenerateGenesisStateFromDepositData(genesisTime, depositDataList, depositDataRoots)
beaconState, _, err := interop.GenerateGenesisStateFromDepositData(context.Background(), genesisTime, depositDataList, depositDataRoots)
if err != nil {
return nil, err
}
Expand Down