Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-vball committed Nov 16, 2022
1 parent 62fedfb commit 21afb3f
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 33 deletions.
15 changes: 7 additions & 8 deletions chain/stmgr/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.

msg.Nonce = fromActor.Nonce

// TODO: maybe just use the invoker directly?
ret, err := vmi.ApplyImplicitMessage(ctx, msg)
ret, err := vmi.ApplyMessage(ctx, msg)
if err != nil && ret == nil {
return nil, xerrors.Errorf("apply message failed: %w", err)
}
Expand All @@ -155,19 +154,21 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.
}

func (sm *StateManager) CallAtStateAndVersion(ctx context.Context, msg *types.Message, ts *types.TipSet, stateCid cid.Cid, v network.Version) (*api.InvocResult, error) {
r := rand.NewStateRand(sm.cs, ts.Cids(), sm.beacon, sm.GetNetworkVersion)
nvGetter := func(context.Context, abi.ChainEpoch) network.Version {
return v
}

buffStore := blockstore.NewTieredBstore(sm.cs.StateBlockstore(), blockstore.NewMemorySync())
vmopt := &vm.VMOpts{
StateBase: stateCid,
Epoch: ts.Height() + 1,
Rand: r,
Rand: rand.NewStateRand(sm.cs, ts.Cids(), sm.beacon, nvGetter),
Bstore: buffStore,
Actors: sm.tsExec.NewActorRegistry(),
Syscalls: sm.Syscalls,
CircSupplyCalc: sm.GetVMCirculatingSupply,
NetworkVersion: v,
BaseFee: ts.Blocks()[0].ParentBaseFee,
BaseFee: types.NewInt(0),
LookbackState: LookbackStateGetterForTipset(sm, ts),
Tracing: true,
}
Expand Down Expand Up @@ -281,8 +282,6 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri
return nil, fmt.Errorf("failed to handle fork: %w", err)
}

r := rand.NewStateRand(sm.cs, ts.Cids(), sm.beacon, sm.GetNetworkVersion)

if span.IsRecordingEvents() {
span.AddAttributes(
trace.Int64Attribute("gas_limit", msg.GasLimit),
Expand All @@ -295,7 +294,7 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri
vmopt := &vm.VMOpts{
StateBase: stateCid,
Epoch: vmHeight,
Rand: r,
Rand: rand.NewStateRand(sm.cs, ts.Cids(), sm.beacon, sm.GetNetworkVersion),
Bstore: buffStore,
Actors: sm.tsExec.NewActorRegistry(),
Syscalls: sm.Syscalls,
Expand Down
19 changes: 19 additions & 0 deletions chain/types/execresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ type Loc struct {
Function string
}

func (et ExecutionTrace) SumGas() GasTrace {
return SumGas(et.GasCharges)
}

func SumGas(charges []*GasTrace) GasTrace {
var out GasTrace
for _, gc := range charges {
out.TotalGas += gc.TotalGas
out.ComputeGas += gc.ComputeGas
out.StorageGas += gc.StorageGas

out.TotalVirtualGas += gc.TotalVirtualGas
out.VirtualComputeGas += gc.VirtualComputeGas
out.VirtualStorageGas += gc.VirtualStorageGas
}

return out
}

func (l Loc) Show() bool {
ignorePrefix := []string{
"reflect.",
Expand Down
19 changes: 2 additions & 17 deletions cli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ var compStateMsg = `
{{end}}
</td></tr>
{{end}}
{{with SumGas .GasCharges}}
{{with sumGas .GasCharges}}
<tr class="sum"><td><b>Sum</b></td>
{{template "gasC" .}}
<td>{{if PrintTiming}}{{.TimeTaken}}{{end}}</td>
Expand Down Expand Up @@ -1355,7 +1355,7 @@ func ComputeStateHTMLTempl(w io.Writer, ts *types.TipSet, o *api.ComputeStateOut
"IsSlow": isSlow,
"IsVerySlow": isVerySlow,
"IntExit": func(i exitcode.ExitCode) int64 { return int64(i) },
"SumGas": SumGas,
"sumGas": types.SumGas,
"CodeStr": codeStr,
"Call": call,
"PrintTiming": func() bool { return printTiming },
Expand Down Expand Up @@ -1423,21 +1423,6 @@ func isVerySlow(t time.Duration) bool {
return t > 50*time.Millisecond
}

func SumGas(changes []*types.GasTrace) types.GasTrace {
var out types.GasTrace
for _, gc := range changes {
out.TotalGas += gc.TotalGas
out.ComputeGas += gc.ComputeGas
out.StorageGas += gc.StorageGas

out.TotalVirtualGas += gc.TotalVirtualGas
out.VirtualComputeGas += gc.VirtualComputeGas
out.VirtualStorageGas += gc.VirtualStorageGas
}

return out
}

func JsonParams(code cid.Cid, method abi.MethodNum, params []byte) (string, error) {
p, err := stmgr.GetParamType(filcns.NewActorRegistry(), code, method) // todo use api for correct actor registry
if err != nil {
Expand Down
118 changes: 113 additions & 5 deletions cmd/lotus-shed/gas-estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"text/tabwriter"

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

Expand All @@ -29,15 +30,15 @@ import (
)

// USAGE: Sync a node, then call migrate-nv17 on some old state. Pass in the cid of the migrated state root,
// the epoch you migrated at, the network version you migrated to, and a message hash. You will be able to replay any
// the epoch you migrated at, the network version you migrated to, and a message CID. You will be able to replay any
// message from between the migration epoch, and where your node originally synced to. Note: You may run into issues
// with nonces, or state that changed between the epoch you migrated at, and when the message was originally processed.
// with state that changed between the epoch you migrated at, and when the message was originally processed.
// This can be avoided by replaying messages from close to the migration epoch, or circumvented by using a custom
// FVM bundle.
var gasTraceCmd = &cli.Command{
Name: "trace-gas",
Description: "replay a message on the specified stateRoot and network version to get an execution trace",
ArgsUsage: "[migratedStateRootCid migrationEpoch networkVersion messageHash]",
ArgsUsage: "[migratedStateRootCid migrationEpoch networkVersion messageCid]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Expand Down Expand Up @@ -124,7 +125,7 @@ var gasTraceCmd = &cli.Command{
}

// Set to block limit so message will not run out of gas
msg.GasLimit = 10_000_000_000
msg.GasLimit = build.BlockGasLimit

err = cs.Load(ctx)
if err != nil {
Expand All @@ -148,12 +149,119 @@ var gasTraceCmd = &cli.Command{
},
}

var replayOfflineCmd = &cli.Command{
Name: "replay-offline",
Description: "replay a message on the specified stateRoot and network version to get an execution trace",
ArgsUsage: "[messageCid]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
},
&cli.Int64Flag{
Name: "lookback-limit",
Value: 1000,
},
},
Action: func(cctx *cli.Context) error {
ctx := context.TODO()

err := logging.SetLogLevel("*", "FATAL")
if err != nil {
return err
}

if cctx.NArg() != 1 {
return lcli.IncorrectNumArgs(cctx)
}

messageCid, err := cid.Decode(cctx.Args().Get(0))
if err != nil {
return fmt.Errorf("failed to parse input: %w", err)
}

lookbackLimit := cctx.Int("lookback-limit")

fsrepo, err := repo.NewFS(cctx.String("repo"))
if err != nil {
return err
}

lkrepo, err := fsrepo.Lock(repo.FullNode)
if err != nil {
return err
}

defer lkrepo.Close() //nolint:errcheck

bs, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}

defer func() {
if c, ok := bs.(io.Closer); ok {
if err := c.Close(); err != nil {
log.Warnf("failed to close blockstore: %s", err)
}
}
}()

mds, err := lkrepo.Datastore(context.Background(), "/metadata")
if err != nil {
return err
}

dcs := build.DrandConfigSchedule()
shd := beacon.Schedule{}
for _, dc := range dcs {
bc, err := drand.NewDrandBeacon(1598306400, build.BlockDelaySecs, nil, dc.Config) // 1598306400 is mainnet genesis time
if err != nil {
return xerrors.Errorf("creating drand beacon: %w", err)
}
shd = append(shd, beacon.BeaconPoint{Start: dc.Start, Beacon: bc})
}
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
defer cs.Close() //nolint:errcheck

sm, err := stmgr.NewStateManager(cs, filcns.NewTipSetExecutor(), vm.Syscalls(ffiwrapper.ProofVerifier), filcns.DefaultUpgradeSchedule(), shd)
if err != nil {
return err
}

msg, err := cs.GetMessage(ctx, messageCid)
if err != nil {
return err
}

err = cs.Load(ctx)
if err != nil {
return err
}

ts, _, _, err := sm.SearchForMessage(ctx, cs.GetHeaviestTipSet(), messageCid, abi.ChainEpoch(lookbackLimit), true)
if err != nil {
return err
}

tw := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', tabwriter.AlignRight)
res, err := sm.Call(ctx, msg, ts)
if err != nil {
return err
}
fmt.Println("Total gas used: ", res.MsgRct.GasUsed)
printInternalExecutions(0, []types.ExecutionTrace{res.ExecutionTrace}, tw)

return tw.Flush()
},
}

func printInternalExecutions(depth int, trace []types.ExecutionTrace, tw *tabwriter.Writer) {
if depth == 0 {
_, _ = fmt.Fprintf(tw, "Depth\tFrom\tTo\tMethod\tTotalGas\tComputeGas\tStorageGas\t\tExitCode\n")
}
for _, im := range trace {
sumGas := lcli.SumGas(im.GasCharges)
sumGas := im.SumGas()
_, _ = fmt.Fprintf(tw, "%d\t%s\t%s\t%d\t%d\t%d\t%d\t\t%d\n", depth, truncateString(im.Msg.From.String(), 10), truncateString(im.Msg.To.String(), 10), im.Msg.Method, sumGas.TotalGas, sumGas.ComputeGas, sumGas.StorageGas, im.MsgRct.ExitCode)
printInternalExecutions(depth+1, im.Subcalls, tw)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/lotus-shed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func main() {
fip36PollCmd,
invariantsCmd,
gasTraceCmd,
replayOfflineCmd,
}

app := &cli.App{
Expand Down
6 changes: 3 additions & 3 deletions cmd/lotus-shed/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ var migrationsCmd = &cli.Command{
return err
}

cache := nv15.NewMemMigrationCache()

blk, err := cs.GetBlock(ctx, blkCid)
if err != nil {
return err
Expand All @@ -137,13 +135,15 @@ var migrationsCmd = &cli.Command{
fmt.Println("completed round actual (without cache), took ", uncachedMigrationTime)

if !cctx.IsSet("skip-pre-migration") {
startTime = time.Now()
cache := nv15.NewMemMigrationCache()

ts1, err := cs.GetTipsetByHeight(ctx, blk.Height-240, migrationTs, false)
if err != nil {
return err
}

startTime = time.Now()

err = filcns.PreUpgradeActorsV9(ctx, sm, cache, ts1.ParentState(), ts1.Height()-1, ts1)
if err != nil {
return err
Expand Down

0 comments on commit 21afb3f

Please sign in to comment.