From 7e6ed0962846688a9558a6ba3796510cdf063d77 Mon Sep 17 00:00:00 2001 From: Aayush Date: Tue, 22 Aug 2023 11:14:17 -0400 Subject: [PATCH] refactor: move vm.Rand to rand.Rand --- chain/consensus/compute_state.go | 2 +- chain/gen/genesis/miners.go | 3 ++- chain/rand/rand.go | 11 ++++++++--- chain/stmgr/stmgr.go | 24 ++++++++++++++++++++++-- chain/vm/fvm.go | 3 ++- chain/vm/vm.go | 5 +++-- chain/vm/vmi.go | 6 ------ cmd/tvx/main.go | 4 ++-- conformance/driver.go | 13 +++++++------ conformance/rand_fixed.go | 6 +++--- conformance/rand_record.go | 4 ++-- conformance/rand_replay.go | 6 +++--- 12 files changed, 55 insertions(+), 32 deletions(-) diff --git a/chain/consensus/compute_state.go b/chain/consensus/compute_state.go index 64b9624ea..b81975f63 100644 --- a/chain/consensus/compute_state.go +++ b/chain/consensus/compute_state.go @@ -80,7 +80,7 @@ func (t *TipSetExecutor) ApplyBlocks(ctx context.Context, pstate cid.Cid, bms []FilecoinBlockMessages, epoch abi.ChainEpoch, - r vm.Rand, + r rand.Rand, em stmgr.ExecMonitor, vmTracing bool, baseFee abi.TokenAmount, diff --git a/chain/gen/genesis/miners.go b/chain/gen/genesis/miners.go index 556f5206b..2d9942464 100644 --- a/chain/gen/genesis/miners.go +++ b/chain/gen/genesis/miners.go @@ -43,6 +43,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin/reward" "github.com/filecoin-project/lotus/chain/actors/policy" "github.com/filecoin-project/lotus/chain/consensus" + lrand "github.com/filecoin-project/lotus/chain/rand" "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" @@ -590,7 +591,7 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.Syscal return c, nil } -var _ vm.Rand = new(fakeRand) +var _ lrand.Rand = new(fakeRand) // TODO: copied from actors test harness, deduplicate or remove from here type fakeRand struct{} diff --git a/chain/rand/rand.go b/chain/rand/rand.go index 7f16cbc08..40f9f593a 100644 --- a/chain/rand/rand.go +++ b/chain/rand/rand.go @@ -108,7 +108,12 @@ type stateRand struct { networkVersionGetter NetworkVersionGetter } -func NewStateRand(cs *store.ChainStore, blks []cid.Cid, b beacon.Schedule, networkVersionGetter NetworkVersionGetter) *stateRand { +type Rand interface { + GetChainRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) + GetBeaconRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) +} + +func NewStateRand(cs *store.ChainStore, blks []cid.Cid, b beacon.Schedule, networkVersionGetter NetworkVersionGetter) Rand { return &stateRand{ cs: cs, blks: blks, @@ -203,12 +208,12 @@ func (sr *stateRand) DrawBeaconRandomness(ctx context.Context, pers crypto.Domai digest, err := sr.GetBeaconRandomness(ctx, filecoinEpoch) if err != nil { - return nil, xerrors.Errorf("failed to get chain randomness: %w", err) + return nil, xerrors.Errorf("failed to get beacon randomness: %w", err) } ret, err := DrawRandomnessFromDigest(digest, pers, filecoinEpoch, entropy) if err != nil { - return nil, xerrors.Errorf("failed to draw chain randomness: %w", err) + return nil, xerrors.Errorf("failed to draw beacon randomness: %w", err) } return ret, nil diff --git a/chain/stmgr/stmgr.go b/chain/stmgr/stmgr.go index c977e3883..85c82c6f1 100644 --- a/chain/stmgr/stmgr.go +++ b/chain/stmgr/stmgr.go @@ -509,7 +509,17 @@ func (sm *StateManager) GetRandomnessFromBeacon(ctx context.Context, personaliza r := rand.NewStateRand(sm.ChainStore(), pts.Cids(), sm.beacon, sm.GetNetworkVersion) - return r.DrawBeaconRandomness(ctx, personalization, randEpoch, entropy) + digest, err := r.GetBeaconRandomness(ctx, randEpoch) + if err != nil { + return nil, xerrors.Errorf("getting beacon randomness: %w", err) + } + + ret, err := rand.DrawRandomnessFromDigest(digest, personalization, randEpoch, entropy) + if err != nil { + return nil, xerrors.Errorf("drawing beacon randomness: %w", err) + } + + return ret, nil } @@ -521,7 +531,17 @@ func (sm *StateManager) GetRandomnessFromTickets(ctx context.Context, personaliz r := rand.NewStateRand(sm.ChainStore(), pts.Cids(), sm.beacon, sm.GetNetworkVersion) - return r.DrawChainRandomness(ctx, personalization, randEpoch, entropy) + digest, err := r.GetChainRandomness(ctx, randEpoch) + if err != nil { + return nil, xerrors.Errorf("getting chain randomness: %w", err) + } + + ret, err := rand.DrawRandomnessFromDigest(digest, personalization, randEpoch, entropy) + if err != nil { + return nil, xerrors.Errorf("drawing chain randomness: %w", err) + } + + return ret, nil } func (sm *StateManager) GetRandomnessDigestFromBeacon(ctx context.Context, randEpoch abi.ChainEpoch, tsk types.TipSetKey) ([32]byte, error) { diff --git a/chain/vm/fvm.go b/chain/vm/fvm.go index ed2e40cd9..bc4c3a851 100644 --- a/chain/vm/fvm.go +++ b/chain/vm/fvm.go @@ -33,6 +33,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/aerrors" "github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/actors/policy" + "github.com/filecoin-project/lotus/chain/rand" "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/lib/sigs" @@ -43,7 +44,7 @@ var _ Interface = (*FVM)(nil) var _ ffi_cgo.Externs = (*FvmExtern)(nil) type FvmExtern struct { - Rand + rand.Rand blockstore.Blockstore epoch abi.ChainEpoch lbState LookbackStateGetter diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 9db811e94..ba404ab1f 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -31,6 +31,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/actors/builtin/account" "github.com/filecoin-project/lotus/chain/actors/builtin/reward" + "github.com/filecoin-project/lotus/chain/rand" "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/metrics" @@ -222,7 +223,7 @@ type LegacyVM struct { buf *blockstore.BufferedBlockstore blockHeight abi.ChainEpoch areg *ActorRegistry - rand Rand + rand rand.Rand circSupplyCalc CircSupplyCalculator networkVersion network.Version baseFee abi.TokenAmount @@ -236,7 +237,7 @@ type VMOpts struct { StateBase cid.Cid Epoch abi.ChainEpoch Timestamp uint64 - Rand Rand + Rand rand.Rand Bstore blockstore.Blockstore Actors *ActorRegistry Syscalls SyscallBuilder diff --git a/chain/vm/vmi.go b/chain/vm/vmi.go index d022479f1..042621ca2 100644 --- a/chain/vm/vmi.go +++ b/chain/vm/vmi.go @@ -7,7 +7,6 @@ import ( cid "github.com/ipfs/go-cid" - "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/network" "github.com/filecoin-project/lotus/chain/types" @@ -69,8 +68,3 @@ func NewVM(ctx context.Context, opts *VMOpts) (Interface, error) { return newVMExecutor(vmi, opts.ExecutionLane), nil } - -type Rand interface { - GetChainRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) - GetBeaconRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error) -} diff --git a/cmd/tvx/main.go b/cmd/tvx/main.go index b1541e4e1..ab3e37d55 100644 --- a/cmd/tvx/main.go +++ b/cmd/tvx/main.go @@ -10,13 +10,13 @@ import ( "github.com/filecoin-project/go-jsonrpc" - "github.com/filecoin-project/lotus/api/v0api" + "github.com/filecoin-project/lotus/api/v1api" lcli "github.com/filecoin-project/lotus/cli" ) // FullAPI is a JSON-RPC client targeting a full node. It's initialized in a // cli.BeforeFunc. -var FullAPI v0api.FullNode +var FullAPI v1api.FullNode // Closer is the closer for the JSON-RPC client, which must be called on // cli.AfterFunc. diff --git a/conformance/driver.go b/conformance/driver.go index eb5973f72..3c62ca7b9 100644 --- a/conformance/driver.go +++ b/conformance/driver.go @@ -23,6 +23,7 @@ import ( "github.com/filecoin-project/lotus/chain/consensus" "github.com/filecoin-project/lotus/chain/consensus/filcns" "github.com/filecoin-project/lotus/chain/index" + "github.com/filecoin-project/lotus/chain/rand" "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/store" @@ -89,9 +90,9 @@ type ExecuteTipsetParams struct { ParentEpoch abi.ChainEpoch Tipset *schema.Tipset ExecEpoch abi.ChainEpoch - // Rand is an optional vm.Rand implementation to use. If nil, the driver - // will use a vm.Rand that returns a fixed value for all calls. - Rand vm.Rand + // Rand is an optional rand.Rand implementation to use. If nil, the driver + // will use a rand.Rand that returns a fixed value for all calls. + Rand rand.Rand // BaseFee if not nil or zero, will override the basefee of the tipset. BaseFee abi.TokenAmount } @@ -200,9 +201,9 @@ type ExecuteMessageParams struct { BaseFee abi.TokenAmount NetworkVersion network.Version - // Rand is an optional vm.Rand implementation to use. If nil, the driver - // will use a vm.Rand that returns a fixed value for all calls. - Rand vm.Rand + // Rand is an optional rand.Rand implementation to use. If nil, the driver + // will use a rand.Rand that returns a fixed value for all calls. + Rand rand.Rand // Lookback is the LookbackStateGetter; returns the state tree at a given epoch. Lookback vm.LookbackStateGetter diff --git a/conformance/rand_fixed.go b/conformance/rand_fixed.go index 773cf0c65..f35f05cd4 100644 --- a/conformance/rand_fixed.go +++ b/conformance/rand_fixed.go @@ -5,16 +5,16 @@ import ( "github.com/filecoin-project/go-state-types/abi" - "github.com/filecoin-project/lotus/chain/vm" + "github.com/filecoin-project/lotus/chain/rand" ) type fixedRand struct{} -var _ vm.Rand = (*fixedRand)(nil) +var _ rand.Rand = (*fixedRand)(nil) // NewFixedRand creates a test vm.Rand that always returns fixed bytes value // of utf-8 string 'i_am_random_____i_am_random_____'. -func NewFixedRand() vm.Rand { +func NewFixedRand() rand.Rand { return &fixedRand{} } diff --git a/conformance/rand_record.go b/conformance/rand_record.go index 20d8bf81d..4dc30b28e 100644 --- a/conformance/rand_record.go +++ b/conformance/rand_record.go @@ -9,8 +9,8 @@ import ( "github.com/filecoin-project/test-vectors/schema" "github.com/filecoin-project/lotus/api/v1api" + "github.com/filecoin-project/lotus/chain/rand" "github.com/filecoin-project/lotus/chain/types" - "github.com/filecoin-project/lotus/chain/vm" ) type RecordingRand struct { @@ -26,7 +26,7 @@ type RecordingRand struct { recorded schema.Randomness } -var _ vm.Rand = (*RecordingRand)(nil) +var _ rand.Rand = (*RecordingRand)(nil) // NewRecordingRand returns a vm.Rand implementation that proxies calls to a // full Lotus node via JSON-RPC, and records matching rules and responses so diff --git a/conformance/rand_replay.go b/conformance/rand_replay.go index 13aae739b..6d78d813b 100644 --- a/conformance/rand_replay.go +++ b/conformance/rand_replay.go @@ -6,16 +6,16 @@ import ( "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/test-vectors/schema" - "github.com/filecoin-project/lotus/chain/vm" + "github.com/filecoin-project/lotus/chain/rand" ) type ReplayingRand struct { reporter Reporter recorded schema.Randomness - fallback vm.Rand + fallback rand.Rand } -var _ vm.Rand = (*ReplayingRand)(nil) +var _ rand.Rand = (*ReplayingRand)(nil) // NewReplayingRand replays recorded randomness when requested, falling back to // fixed randomness if the value cannot be found; hence this is a safe