From 054b15bc45b9d12d25e0a7932dc6fbcb60aeed07 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 7 Mar 2020 00:06:01 +0100 Subject: [PATCH] Add SlotsPerArchivedPoint flag and a check (#5023) * Flag * Service * Tests * Tests and comments * Lint * Add to usages Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- beacon-chain/flags/base.go | 7 ++++++ beacon-chain/main.go | 1 + beacon-chain/state/stategen/BUILD.bazel | 2 ++ beacon-chain/state/stategen/service.go | 11 +++++++++ beacon-chain/state/stategen/service_test.go | 27 +++++++++++++++++++++ beacon-chain/usage.go | 1 + 6 files changed, 49 insertions(+) create mode 100644 beacon-chain/state/stategen/service_test.go diff --git a/beacon-chain/flags/base.go b/beacon-chain/flags/base.go index efd4e4051..d110eb797 100644 --- a/beacon-chain/flags/base.go +++ b/beacon-chain/flags/base.go @@ -97,4 +97,11 @@ var ( Usage: "A slasher provider string endpoint. Can either be an grpc server endpoint.", Value: "127.0.0.1:5000", } + // SlotsPerArchivedPoint specifies the number of slots between the archived points, to save beacon state in the cold + // section of DB. + SlotsPerArchivedPoint = cli.IntFlag{ + Name: "slots-per-archive-point", + Usage: "The slot durations of when an archived state gets saved in the DB.", + Value: 128, + } ) diff --git a/beacon-chain/main.go b/beacon-chain/main.go index bec514f80..b704c0f35 100644 --- a/beacon-chain/main.go +++ b/beacon-chain/main.go @@ -46,6 +46,7 @@ var appFlags = []cli.Flag{ flags.ArchiveValidatorSetChangesFlag, flags.ArchiveBlocksFlag, flags.ArchiveAttestationsFlag, + flags.SlotsPerArchivedPoint, cmd.BootstrapNode, cmd.NoDiscovery, cmd.StaticPeers, diff --git a/beacon-chain/state/stategen/BUILD.bazel b/beacon-chain/state/stategen/BUILD.bazel index 6a17e9eab..4d1d87df5 100644 --- a/beacon-chain/state/stategen/BUILD.bazel +++ b/beacon-chain/state/stategen/BUILD.bazel @@ -18,6 +18,7 @@ go_library( "//beacon-chain/state:go_default_library", "//shared/bytesutil:go_default_library", "//shared/featureconfig:go_default_library", + "//shared/params:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_go_ssz//:go_default_library", @@ -31,6 +32,7 @@ go_test( srcs = [ "epoch_boundary_root_test.go", "replay_test.go", + "service_test.go", ], embed = [":go_default_library"], deps = [ diff --git a/beacon-chain/state/stategen/service.go b/beacon-chain/state/stategen/service.go index 65f6d9b42..1e15eba78 100644 --- a/beacon-chain/state/stategen/service.go +++ b/beacon-chain/state/stategen/service.go @@ -4,6 +4,7 @@ import ( "sync" "github.com/prysmaticlabs/prysm/beacon-chain/db" + "github.com/prysmaticlabs/prysm/shared/params" ) // State represents a management object that handles the internal @@ -22,3 +23,13 @@ func New(db db.NoHeadAccessDatabase) *State { epochBoundarySlotToRoot: make(map[uint64][32]byte), } } + +// This verifies the archive point frequency is valid. It checks the interval +// is a divisor of the number of slots per epoch. This ensures we have at least one +// archive point within range of our state root history when iterating +// backwards. It also ensures the archive points align with hot state summaries +// which makes it quicker to migrate hot to cold. +func verifySlotsPerArchivePoint(slotsPerArchivePoint uint64) bool { + return slotsPerArchivePoint > 0 && + slotsPerArchivePoint%params.BeaconConfig().SlotsPerEpoch == 0 +} diff --git a/beacon-chain/state/stategen/service_test.go b/beacon-chain/state/stategen/service_test.go new file mode 100644 index 000000000..ddb958f35 --- /dev/null +++ b/beacon-chain/state/stategen/service_test.go @@ -0,0 +1,27 @@ +package stategen + +import ( + "testing" + + "github.com/prysmaticlabs/prysm/shared/params" +) + +func Test_verifySlotsPerArchivePoint(t *testing.T) { + type tc struct { + input uint64 + result bool + } + tests := []tc{ + {0, false}, + {1, false}, + {params.BeaconConfig().SlotsPerEpoch, true}, + {params.BeaconConfig().SlotsPerEpoch + 1, false}, + {params.BeaconConfig().SlotsPerHistoricalRoot, true}, + {params.BeaconConfig().SlotsPerHistoricalRoot + 1, false}, + } + for _, tt := range tests { + if got := verifySlotsPerArchivePoint(tt.input); got != tt.result { + t.Errorf("verifySlotsPerArchivePoint(%d) = %v, want %v", tt.input, got, tt.result) + } + } +} diff --git a/beacon-chain/usage.go b/beacon-chain/usage.go index 9286b1cea..868e9f58b 100644 --- a/beacon-chain/usage.go +++ b/beacon-chain/usage.go @@ -91,6 +91,7 @@ var appHelpFlagGroups = []flagGroup{ flags.HTTPWeb3ProviderFlag, flags.SetGCPercent, flags.UnsafeSync, + flags.SlotsPerArchivedPoint, }, }, {