2021-05-19 00:01:30 +00:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-06-10 18:16:26 +00:00
|
|
|
"runtime"
|
2021-05-19 00:01:30 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2021-06-10 18:16:26 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2021-05-19 00:01:30 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2021-06-10 02:40:00 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
blockadt "github.com/filecoin-project/specs-actors/actors/util/adt"
|
|
|
|
|
2022-09-27 10:21:42 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/consensus"
|
2021-09-02 16:07:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/consensus/filcns"
|
2021-05-19 00:01:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-07-27 13:49:01 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-sim/simulation/mock"
|
2021-06-12 01:39:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-sim/simulation/stages"
|
2021-05-19 00:01:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("simulation")
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// config is the simulation's config, persisted to the local metadata store and loaded on start.
|
|
|
|
//
|
2021-06-12 01:39:15 +00:00
|
|
|
// See Simulation.loadConfig and Simulation.saveConfig.
|
2021-05-19 00:01:30 +00:00
|
|
|
type config struct {
|
|
|
|
Upgrades map[network.Version]abi.ChainEpoch
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// upgradeSchedule constructs an stmgr.StateManager upgrade schedule, overriding any network upgrade
|
|
|
|
// epochs as specified in the config.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (c *config) upgradeSchedule() (stmgr.UpgradeSchedule, error) {
|
2021-09-02 16:07:23 +00:00
|
|
|
upgradeSchedule := filcns.DefaultUpgradeSchedule()
|
2021-05-19 00:01:30 +00:00
|
|
|
expected := make(map[network.Version]struct{}, len(c.Upgrades))
|
|
|
|
for nv := range c.Upgrades {
|
|
|
|
expected[nv] = struct{}{}
|
|
|
|
}
|
2021-06-08 00:45:53 +00:00
|
|
|
|
|
|
|
// Update network upgrade epochs.
|
2021-05-19 00:01:30 +00:00
|
|
|
newUpgradeSchedule := upgradeSchedule[:0]
|
|
|
|
for _, upgrade := range upgradeSchedule {
|
|
|
|
if height, ok := c.Upgrades[upgrade.Network]; ok {
|
|
|
|
delete(expected, upgrade.Network)
|
|
|
|
if height < 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
upgrade.Height = height
|
|
|
|
}
|
|
|
|
newUpgradeSchedule = append(newUpgradeSchedule, upgrade)
|
|
|
|
}
|
2021-06-08 00:45:53 +00:00
|
|
|
|
|
|
|
// Make sure we didn't try to configure an unknown network version.
|
2021-05-19 00:01:30 +00:00
|
|
|
if len(expected) > 0 {
|
|
|
|
missing := make([]network.Version, 0, len(expected))
|
|
|
|
for nv := range expected {
|
|
|
|
missing = append(missing, nv)
|
|
|
|
}
|
|
|
|
return nil, xerrors.Errorf("unknown network versions %v in config", missing)
|
|
|
|
}
|
2021-06-08 00:45:53 +00:00
|
|
|
|
|
|
|
// Finally, validate it. This ensures we don't change the order of the upgrade or anything
|
|
|
|
// like that.
|
|
|
|
if err := newUpgradeSchedule.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 00:01:30 +00:00
|
|
|
return newUpgradeSchedule, nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// Simulation specifies a lotus-sim simulation.
|
2021-05-19 00:01:30 +00:00
|
|
|
type Simulation struct {
|
2021-06-21 18:41:06 +00:00
|
|
|
Node *Node
|
2021-06-08 18:22:11 +00:00
|
|
|
StateManager *stmgr.StateManager
|
2021-05-19 00:01:30 +00:00
|
|
|
|
|
|
|
name string
|
|
|
|
config config
|
2021-06-08 17:42:59 +00:00
|
|
|
start *types.TipSet
|
2021-05-19 00:01:30 +00:00
|
|
|
|
|
|
|
// head
|
|
|
|
head *types.TipSet
|
|
|
|
|
2021-06-12 01:39:15 +00:00
|
|
|
stages []stages.Stage
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// loadConfig loads a simulation's config from the datastore. This must be called on startup and may
|
|
|
|
// be called to restore the config from-disk.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (sim *Simulation) loadConfig() error {
|
2021-12-17 09:42:09 +00:00
|
|
|
configBytes, err := sim.Node.MetadataDS.Get(context.TODO(), sim.key("config"))
|
2021-05-19 00:01:30 +00:00
|
|
|
if err == nil {
|
|
|
|
err = json.Unmarshal(configBytes, &sim.config)
|
|
|
|
}
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case datastore.ErrNotFound:
|
|
|
|
sim.config = config{}
|
|
|
|
default:
|
|
|
|
return xerrors.Errorf("failed to load config: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// saveConfig saves the current config to the datastore. This must be called whenever the config is
|
|
|
|
// changed.
|
|
|
|
func (sim *Simulation) saveConfig() error {
|
|
|
|
buf, err := json.Marshal(sim.config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-17 09:42:09 +00:00
|
|
|
return sim.Node.MetadataDS.Put(context.TODO(), sim.key("config"), buf)
|
2021-06-08 00:45:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 00:01:30 +00:00
|
|
|
var simulationPrefix = datastore.NewKey("/simulation")
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// key returns the the key in the form /simulation/<subkey>/<simulation-name>. For example,
|
|
|
|
// /simulation/head/default.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (sim *Simulation) key(subkey string) datastore.Key {
|
|
|
|
return simulationPrefix.ChildString(subkey).ChildString(sim.name)
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:42:59 +00:00
|
|
|
// loadNamedTipSet the tipset with the given name (for this simulation)
|
|
|
|
func (sim *Simulation) loadNamedTipSet(name string) (*types.TipSet, error) {
|
2021-12-17 09:42:09 +00:00
|
|
|
tskBytes, err := sim.Node.MetadataDS.Get(context.TODO(), sim.key(name))
|
2021-06-08 17:42:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load tipset %s/%s: %w", sim.name, name, err)
|
|
|
|
}
|
|
|
|
tsk, err := types.TipSetKeyFromBytes(tskBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to parse tipste %v (%s/%s): %w", tskBytes, sim.name, name, err)
|
|
|
|
}
|
2021-12-17 09:42:09 +00:00
|
|
|
ts, err := sim.Node.Chainstore.LoadTipSet(context.TODO(), tsk)
|
2021-06-08 17:42:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load tipset %s (%s/%s): %w", tsk, sim.name, name, err)
|
|
|
|
}
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storeNamedTipSet stores the tipset at name (relative to the simulation).
|
|
|
|
func (sim *Simulation) storeNamedTipSet(name string, ts *types.TipSet) error {
|
2021-12-17 09:42:09 +00:00
|
|
|
if err := sim.Node.MetadataDS.Put(context.TODO(), sim.key(name), ts.Key().Bytes()); err != nil {
|
2021-06-08 17:42:59 +00:00
|
|
|
return xerrors.Errorf("failed to store tipset (%s/%s): %w", sim.name, name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// GetHead returns the current simulation head.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (sim *Simulation) GetHead() *types.TipSet {
|
|
|
|
return sim.head
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:42:59 +00:00
|
|
|
// GetStart returns simulation's parent tipset.
|
|
|
|
func (sim *Simulation) GetStart() *types.TipSet {
|
|
|
|
return sim.start
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// GetNetworkVersion returns the current network version for the simulation.
|
2021-06-07 22:05:52 +00:00
|
|
|
func (sim *Simulation) GetNetworkVersion() network.Version {
|
2021-12-17 23:43:39 +00:00
|
|
|
return sim.StateManager.GetNetworkVersion(context.TODO(), sim.head.Height())
|
2021-06-07 22:05:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// SetHead updates the current head of the simulation and stores it in the metadata store. This is
|
|
|
|
// called for every Simulation.Step.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (sim *Simulation) SetHead(head *types.TipSet) error {
|
2021-06-08 17:42:59 +00:00
|
|
|
if err := sim.storeNamedTipSet("head", head); err != nil {
|
|
|
|
return err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
sim.head = head
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// Name returns the simulation's name.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (sim *Simulation) Name() string {
|
|
|
|
return sim.name
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// SetUpgradeHeight sets the height of the given network version change (and saves the config).
|
|
|
|
//
|
|
|
|
// This fails if the specified epoch has already passed or the new upgrade schedule is invalid.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (sim *Simulation) SetUpgradeHeight(nv network.Version, epoch abi.ChainEpoch) (_err error) {
|
|
|
|
if epoch <= sim.head.Height() {
|
|
|
|
return xerrors.Errorf("cannot set upgrade height in the past (%d <= %d)", epoch, sim.head.Height())
|
|
|
|
}
|
|
|
|
|
|
|
|
if sim.config.Upgrades == nil {
|
|
|
|
sim.config.Upgrades = make(map[network.Version]abi.ChainEpoch, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sim.config.Upgrades[nv] = epoch
|
|
|
|
defer func() {
|
|
|
|
if _err != nil {
|
|
|
|
// try to restore the old config on error.
|
|
|
|
_ = sim.loadConfig()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
newUpgradeSchedule, err := sim.config.upgradeSchedule()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-27 10:21:42 +00:00
|
|
|
sm, err := stmgr.NewStateManager(sim.Node.Chainstore, consensus.NewTipSetExecutor(filcns.RewardFunc), vm.Syscalls(mock.Verifier), newUpgradeSchedule, nil)
|
2021-05-19 00:01:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = sim.saveConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-08 18:22:11 +00:00
|
|
|
sim.StateManager = sm
|
2021-05-19 00:01:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// ListUpgrades returns any future network upgrades.
|
2021-06-07 21:54:20 +00:00
|
|
|
func (sim *Simulation) ListUpgrades() (stmgr.UpgradeSchedule, error) {
|
|
|
|
upgrades, err := sim.config.upgradeSchedule()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var pending stmgr.UpgradeSchedule
|
|
|
|
for _, upgrade := range upgrades {
|
|
|
|
if upgrade.Height < sim.head.Height() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pending = append(pending, upgrade)
|
|
|
|
}
|
|
|
|
return pending, nil
|
|
|
|
}
|
2021-06-08 18:58:19 +00:00
|
|
|
|
|
|
|
type AppliedMessage struct {
|
|
|
|
types.Message
|
|
|
|
types.MessageReceipt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk walks the simulation's chain from the current head back to the first tipset.
|
|
|
|
func (sim *Simulation) Walk(
|
|
|
|
ctx context.Context,
|
2021-06-12 17:08:36 +00:00
|
|
|
lookback int64,
|
2021-06-08 18:58:19 +00:00
|
|
|
cb func(sm *stmgr.StateManager,
|
|
|
|
ts *types.TipSet,
|
|
|
|
stCid cid.Cid,
|
|
|
|
messages []*AppliedMessage) error,
|
|
|
|
) error {
|
2021-06-21 18:41:06 +00:00
|
|
|
store := sim.Node.Chainstore.ActorStore(ctx)
|
2021-06-12 17:08:36 +00:00
|
|
|
minEpoch := sim.start.Height()
|
|
|
|
if lookback != 0 {
|
|
|
|
minEpoch = sim.head.Height() - abi.ChainEpoch(lookback)
|
2021-06-10 17:04:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 18:16:26 +00:00
|
|
|
// Given tha loading messages and receipts can be a little bit slow, we do this in parallel.
|
|
|
|
//
|
|
|
|
// 1. We spin up some number of workers.
|
|
|
|
// 2. We hand tipsets to workers in round-robin order.
|
|
|
|
// 3. We pull "resolved" tipsets in the same round-robin order.
|
|
|
|
// 4. We serially call the callback in reverse-chain order.
|
|
|
|
//
|
|
|
|
// We have a buffer of size 1 for both resolved tipsets and unresolved tipsets. This should
|
|
|
|
// ensure that we never block unecessarily.
|
|
|
|
|
|
|
|
type work struct {
|
|
|
|
ts *types.TipSet
|
|
|
|
stCid cid.Cid
|
|
|
|
recCid cid.Cid
|
|
|
|
}
|
|
|
|
type result struct {
|
|
|
|
ts *types.TipSet
|
|
|
|
stCid cid.Cid
|
|
|
|
messages []*AppliedMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is more disk bound than CPU bound, but eh...
|
|
|
|
workerCount := runtime.NumCPU() * 2
|
|
|
|
|
|
|
|
workQs := make([]chan *work, workerCount)
|
|
|
|
resultQs := make([]chan *result, workerCount)
|
|
|
|
|
|
|
|
for i := range workQs {
|
|
|
|
workQs[i] = make(chan *work, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range resultQs {
|
|
|
|
resultQs[i] = make(chan *result, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
grp, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
// Walk the chain and fire off work items.
|
|
|
|
grp.Go(func() error {
|
|
|
|
ts := sim.head
|
|
|
|
stCid, recCid, err := sim.StateManager.TipSetState(ctx, ts)
|
2021-06-08 18:58:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-10 18:16:26 +00:00
|
|
|
i := 0
|
2021-06-12 17:16:50 +00:00
|
|
|
for ts.Height() > minEpoch {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:16:26 +00:00
|
|
|
select {
|
|
|
|
case workQs[i] <- &work{ts, stCid, recCid}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2021-06-08 18:58:19 +00:00
|
|
|
|
2021-06-10 18:16:26 +00:00
|
|
|
stCid = ts.MinTicketBlock().ParentStateRoot
|
|
|
|
recCid = ts.MinTicketBlock().ParentMessageReceipts
|
2021-12-13 13:15:38 +00:00
|
|
|
ts, err = sim.Node.Chainstore.LoadTipSet(ctx, ts.Parents())
|
2021-06-10 18:16:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading parent: %w", err)
|
|
|
|
}
|
|
|
|
i = (i + 1) % workerCount
|
|
|
|
}
|
|
|
|
for _, q := range workQs {
|
|
|
|
close(q)
|
2021-06-08 18:58:19 +00:00
|
|
|
}
|
2021-06-10 18:16:26 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Spin up one worker per queue pair.
|
|
|
|
for i := 0; i < workerCount; i++ {
|
|
|
|
workQ := workQs[i]
|
|
|
|
resultQ := resultQs[i]
|
|
|
|
grp.Go(func() error {
|
2021-06-12 17:16:50 +00:00
|
|
|
for {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
var job *work
|
|
|
|
var ok bool
|
|
|
|
select {
|
|
|
|
case job, ok = <-workQ:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
msgs, err := sim.Node.Chainstore.MessagesForTipset(ctx, job.ts)
|
2021-06-10 18:16:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
recs, err := blockadt.AsArray(store, job.recCid)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("amt load: %w", err)
|
|
|
|
}
|
|
|
|
applied := make([]*AppliedMessage, len(msgs))
|
|
|
|
var rec types.MessageReceipt
|
|
|
|
err = recs.ForEach(&rec, func(i int64) error {
|
|
|
|
applied[i] = &AppliedMessage{
|
|
|
|
Message: *msgs[i].VMMessage(),
|
|
|
|
MessageReceipt: rec,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case resultQ <- &result{
|
|
|
|
ts: job.ts,
|
|
|
|
stCid: job.stCid,
|
|
|
|
messages: applied,
|
|
|
|
}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2021-06-08 18:58:19 +00:00
|
|
|
}
|
2021-06-10 18:16:26 +00:00
|
|
|
close(resultQ)
|
2021-06-08 18:58:19 +00:00
|
|
|
return nil
|
|
|
|
})
|
2021-06-10 18:16:26 +00:00
|
|
|
}
|
2021-06-08 18:58:19 +00:00
|
|
|
|
2021-06-10 18:16:26 +00:00
|
|
|
// Process results in the same order we enqueued them.
|
|
|
|
grp.Go(func() error {
|
|
|
|
qs := resultQs
|
|
|
|
for len(qs) > 0 {
|
|
|
|
newQs := qs[:0]
|
|
|
|
for _, q := range qs {
|
2021-06-12 19:02:36 +00:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2021-06-10 18:16:26 +00:00
|
|
|
select {
|
|
|
|
case r, ok := <-q:
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := cb(sim.StateManager, r.ts, r.stCid, r.messages)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
newQs = append(newQs, q)
|
|
|
|
}
|
|
|
|
qs = newQs
|
2021-06-08 18:58:19 +00:00
|
|
|
}
|
2021-06-10 18:16:26 +00:00
|
|
|
return nil
|
|
|
|
})
|
2021-06-08 18:58:19 +00:00
|
|
|
|
2021-06-10 18:16:26 +00:00
|
|
|
// Wait for everything to finish.
|
|
|
|
return grp.Wait()
|
2021-06-08 18:58:19 +00:00
|
|
|
}
|