refactor: move vm.Rand to rand.Rand

This commit is contained in:
Aayush 2023-08-22 11:14:17 -04:00
parent c90faf0754
commit 7e6ed09628
12 changed files with 55 additions and 32 deletions

View File

@ -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,

View File

@ -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{}

View File

@ -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

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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.

View File

@ -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

View File

@ -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{}
}

View File

@ -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

View File

@ -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