Skip to content

Commit

Permalink
Merge pull request #2024 from filecoin-project/feat/virtual-gas
Browse files Browse the repository at this point in the history
Add virtual gas
  • Loading branch information
Jakub Sztandera authored Jun 15, 2020
2 parents c98972a + a23a87a commit 18cf7b0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
11 changes: 7 additions & 4 deletions chain/types/execresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ type ExecutionTrace struct {
type GasTrace struct {
Name string

Location []Loc
TotalGas int64
ComputeGas int64
StorageGas int64
Location []Loc
TotalGas int64
ComputeGas int64
StorageGas int64
TotalVirtualGas int64
VirtualComputeGas int64
VirtualStorageGas int64

TimeTaken time.Duration
Extra interface{}
Expand Down
9 changes: 9 additions & 0 deletions chain/vm/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ type GasCharge struct {
Name string
ComputeGas int64
StorageGas int64

VirtualCompute int64
VirtualStorage int64
}

func (g GasCharge) Total() int64 {
return g.ComputeGas*GasComputeMulti + g.StorageGas*GasStorageMulti
}
func (g GasCharge) WithVirtual(compute, storage int64) GasCharge {
out := g
out.VirtualCompute = compute
out.VirtualStorage = storage
return out
}

func newGasCharge(name string, computeGas int64, storageGas int64) GasCharge {
return GasCharge{
Expand Down
16 changes: 10 additions & 6 deletions chain/vm/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,19 +524,23 @@ func (rt *Runtime) chargeGasInternal(gas GasCharge, skip int) aerrors.ActorError
}

gasTrace := types.GasTrace{
Name: gas.Name,
TotalGas: toUse,
ComputeGas: gas.ComputeGas,
StorageGas: gas.StorageGas,
Callers: callers[:cout],
Name: gas.Name,
TotalGas: toUse,
ComputeGas: gas.ComputeGas,
StorageGas: gas.StorageGas,
TotalVirtualGas: gas.VirtualCompute*GasComputeMulti + gas.VirtualStorage*GasStorageMulti,
VirtualComputeGas: gas.VirtualCompute,
VirtualStorageGas: gas.VirtualStorage,
Callers: callers[:cout],
}
rt.executionTrace.GasCharges = append(rt.executionTrace.GasCharges, &gasTrace)
rt.lastGasChargeTime = now
rt.lastGasCharge = &gasTrace

if rt.gasUsed+toUse > rt.gasAvailable {
rt.gasUsed = rt.gasAvailable
return aerrors.Newf(exitcode.SysErrOutOfGas, "not enough gas: used=%d, available=%d", rt.gasUsed, rt.gasAvailable)
return aerrors.Newf(exitcode.SysErrOutOfGas, "not enough gas: used=%d, available=%d",
rt.gasUsed, rt.gasAvailable)
}
rt.gasUsed += toUse
return nil
Expand Down
25 changes: 22 additions & 3 deletions cli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ var compStateTemplate = `
}
.slow-true-false { color: #660; }
.slow-true-true { color: #f80; }
.deemp { color: #444; }
table {
font-size: 12px;
border-collapse: collapse;
Expand Down Expand Up @@ -1060,8 +1061,20 @@ var compStateMsg = `
<summary>Gas Trace</summary>
<table>
<tr><th>Name</th><th>Total/Compute/Storage</th><th>Time Taken</th><th>Location</th></tr>
{{define "virt" -}}
{{- if . -}}
<span class="deemp">+({{.}})</span>
{{- end -}}
{{- end}}
{{define "gasC" -}}
<td>{{.TotalGas}}{{template "virt" .TotalVirtualGas }}/{{.ComputeGas}}{{template "virt" .VirtualComputeGas}}/{{.StorageGas}}{{template "virt" .VirtualStorageGas}}</td>
{{- end}}
{{range .GasCharges}}
<tr><td>{{.Name}}</td><td>{{.TotalGas}}/{{.ComputeGas}}/{{.StorageGas}}</td><td>{{.TimeTaken}}</td>
<tr><td>{{.Name}}</td>
{{template "gasC" .}}
<td>{{.TimeTaken}}</td>
<td>
{{ $fImp := FirstImportant .Location }}
{{ if $fImp }}
Expand Down Expand Up @@ -1098,7 +1111,10 @@ var compStateMsg = `
</td></tr>
{{end}}
{{with SumGas .GasCharges}}
<tr class="sum"><td><b>Sum</b></td><td>{{.TotalGas}}/{{.ComputeGas}}/{{.StorageGas}}</td><td>{{.TimeTaken}}</td><td></td></tr>
<tr class="sum"><td><b>Sum</b></td>
{{template "gasC" .}}
<td>{{.TimeTaken}}</td>
<td></td></tr>
{{end}}
</table>
</details>
Expand Down Expand Up @@ -1201,7 +1217,10 @@ func sumGas(changes []*types.GasTrace) types.GasTrace {
out.TotalGas += gc.TotalGas
out.ComputeGas += gc.ComputeGas
out.StorageGas += gc.StorageGas
out.TimeTaken += gc.TimeTaken

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

return out
Expand Down

0 comments on commit 18cf7b0

Please sign in to comment.