feat(lotus-sim): store start tipset

This commit is contained in:
Steven Allen 2021-06-08 10:42:59 -07:00
parent a57c509e1e
commit 0faacbe154
2 changed files with 44 additions and 10 deletions

View File

@ -78,17 +78,15 @@ func (nd *Node) LoadSim(ctx context.Context, name string) (*Simulation, error) {
Node: nd,
name: name,
}
tskBytes, err := nd.MetadataDS.Get(sim.key("head"))
var err error
sim.head, err = sim.loadNamedTipSet("head")
if err != nil {
return nil, xerrors.Errorf("failed to load simulation %s: %w", name, err)
return nil, err
}
tsk, err := types.TipSetKeyFromBytes(tskBytes)
sim.start, err = sim.loadNamedTipSet("start")
if err != nil {
return nil, xerrors.Errorf("failed to parse simulation %s's tipset %v: %w", name, tskBytes, err)
}
sim.head, err = nd.Chainstore.LoadTipSet(tsk)
if err != nil {
return nil, xerrors.Errorf("failed to load simulation tipset %s: %w", tsk, err)
return nil, err
}
err = sim.loadConfig()
@ -126,6 +124,10 @@ func (nd *Node) CreateSim(ctx context.Context, name string, head *types.TipSet)
return nil, xerrors.Errorf("simulation named %s already exists", name)
}
if err := sim.storeNamedTipSet("start", head); err != nil {
return nil, xerrors.Errorf("failed to set simulation start: %w", err)
}
if err := sim.SetHead(head); err != nil {
return nil, err
}
@ -169,6 +171,7 @@ func (nd *Node) DeleteSim(ctx context.Context, name string) error {
// TODO: make this a bit more generic?
keys := []datastore.Key{
simulationPrefix.ChildString("head").ChildString(name),
simulationPrefix.ChildString("start").ChildString(name),
simulationPrefix.ChildString("config").ChildString(name),
}
var err error

View File

@ -82,6 +82,7 @@ type Simulation struct {
name string
config config
start *types.TipSet
sm *stmgr.StateManager
// head
@ -159,6 +160,31 @@ func (sim *Simulation) key(subkey string) datastore.Key {
return simulationPrefix.ChildString(subkey).ChildString(sim.name)
}
// loadNamedTipSet the tipset with the given name (for this simulation)
func (sim *Simulation) loadNamedTipSet(name string) (*types.TipSet, error) {
tskBytes, err := sim.MetadataDS.Get(sim.key(name))
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)
}
ts, err := sim.Chainstore.LoadTipSet(tsk)
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 {
if err := sim.MetadataDS.Put(sim.key(name), ts.Key().Bytes()); err != nil {
return xerrors.Errorf("failed to store tipset (%s/%s): %w", sim.name, name, err)
}
return nil
}
// Load loads the simulation state. This will happen automatically on first use, but it can be
// useful to preload for timing reasons.
func (sim *Simulation) Load(ctx context.Context) error {
@ -171,6 +197,11 @@ func (sim *Simulation) GetHead() *types.TipSet {
return sim.head
}
// GetStart returns simulation's parent tipset.
func (sim *Simulation) GetStart() *types.TipSet {
return sim.start
}
// GetNetworkVersion returns the current network version for the simulation.
func (sim *Simulation) GetNetworkVersion() network.Version {
return sim.sm.GetNtwkVersion(context.TODO(), sim.head.Height())
@ -179,8 +210,8 @@ func (sim *Simulation) GetNetworkVersion() network.Version {
// SetHead updates the current head of the simulation and stores it in the metadata store. This is
// called for every Simulation.Step.
func (sim *Simulation) SetHead(head *types.TipSet) error {
if err := sim.MetadataDS.Put(sim.key("head"), head.Key().Bytes()); err != nil {
return xerrors.Errorf("failed to store simulation head: %w", err)
if err := sim.storeNamedTipSet("head", head); err != nil {
return err
}
sim.st = nil // we'll compute this on-demand.
sim.head = head