-
Notifications
You must be signed in to change notification settings - Fork 136
/
fvm.go
240 lines (214 loc) · 5.85 KB
/
fvm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//go:build cgo && (amd64 || arm64 || riscv64)
// +build cgo
// +build amd64 arm64 riscv64
package ffi
// #cgo linux LDFLAGS: ${SRCDIR}/libfilcrypto.a -Wl,-unresolved-symbols=ignore-all
// #cgo darwin LDFLAGS: ${SRCDIR}/libfilcrypto.a -Wl,-undefined,dynamic_lookup
// #cgo pkg-config: ${SRCDIR}/filcrypto.pc
// #include "./filcrypto.h"
import "C"
import (
"context"
"fmt"
gobig "math/big"
"runtime"
"github.com/filecoin-project/filecoin-ffi/cgo"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
type FVM struct {
executor *cgo.FvmMachine
}
const (
applyExplicit = iota
applyImplicit
)
type FVMOpts struct {
FVMVersion uint64
Externs cgo.Externs
Epoch abi.ChainEpoch
Timestamp uint64
ChainID uint64
BaseFee abi.TokenAmount
BaseCircSupply abi.TokenAmount
NetworkVersion network.Version
StateBase cid.Cid
Tracing bool
Debug bool
ActorRedirect cid.Cid
}
// CreateFVM creates a new FVM instance.
func CreateFVM(opts *FVMOpts) (*FVM, error) {
baseFeeHi, baseFeeLo, err := splitBigInt(opts.BaseFee)
if err != nil {
return nil, xerrors.Errorf("invalid basefee: %w", err)
}
baseCircSupplyHi, baseCircSupplyLo, err := splitBigInt(opts.BaseCircSupply)
if err != nil {
return nil, xerrors.Errorf("invalid circ supply: %w", err)
}
exHandle := cgo.Register(context.TODO(), opts.Externs)
var executor *cgo.FvmMachine
if !opts.Debug {
executor, err = cgo.CreateFvmMachine(cgo.FvmRegisteredVersion(opts.FVMVersion),
uint64(opts.Epoch),
opts.Timestamp,
opts.ChainID,
baseFeeHi,
baseFeeLo,
baseCircSupplyHi,
baseCircSupplyLo,
uint64(opts.NetworkVersion),
cgo.AsSliceRefUint8(opts.StateBase.Bytes()),
opts.Tracing,
exHandle, exHandle,
)
} else {
executor, err = cgo.CreateFvmDebugMachine(cgo.FvmRegisteredVersion(opts.FVMVersion),
uint64(opts.Epoch),
opts.Timestamp,
opts.ChainID,
baseFeeHi,
baseFeeLo,
baseCircSupplyHi,
baseCircSupplyLo,
uint64(opts.NetworkVersion),
cgo.AsSliceRefUint8(opts.StateBase.Bytes()),
cgo.AsSliceRefUint8(opts.ActorRedirect.Bytes()),
true,
exHandle, exHandle,
)
}
if err != nil {
return nil, err
}
fvm := &FVM{
executor: executor,
}
runtime.SetFinalizer(fvm, func(f *FVM) {
// Just to be extra safe
if f.executor == nil {
return
}
executor := f.executor
f.executor = nil
executor.Destroy()
cgo.Unregister(exHandle)
})
return fvm, nil
}
func (f *FVM) ApplyMessage(msgBytes []byte, chainLen uint) (*ApplyRet, error) {
// NOTE: we need to call KeepAlive here (and below) because go doesn't guarantee that the
// receiver will live to the end of the function. If we don't do this, go _will_ garbage
// collect the FVM, causing us to run the finalizer while we're in the middle of using the
// FVM.
defer runtime.KeepAlive(f)
resp, err := cgo.FvmMachineExecuteMessage(
f.executor,
cgo.AsSliceRefUint8(msgBytes),
uint64(chainLen),
applyExplicit,
)
if err != nil {
return nil, err
}
return buildResponse(resp)
}
func (f *FVM) ApplyImplicitMessage(msgBytes []byte) (*ApplyRet, error) {
defer runtime.KeepAlive(f)
resp, err := cgo.FvmMachineExecuteMessage(
f.executor,
cgo.AsSliceRefUint8(msgBytes),
0, // this isn't an on-chain message, so it has no chain length.
applyImplicit,
)
if err != nil {
return nil, err
}
return buildResponse(resp)
}
func buildResponse(resp cgo.FvmMachineExecuteResponseGo) (*ApplyRet, error) {
var eventsRoot *cid.Cid
if len(resp.EventsRoot) > 0 {
if eventsRootCid, err := cid.Cast(resp.EventsRoot); err != nil {
return nil, fmt.Errorf("failed to cast events root CID: %w", err)
} else {
eventsRoot = &eventsRootCid
}
}
return &ApplyRet{
Return: resp.ReturnVal,
ExitCode: resp.ExitCode,
GasUsed: int64(resp.GasUsed),
MinerPenalty: reformBigInt(resp.PenaltyHi, resp.PenaltyLo),
MinerTip: reformBigInt(resp.MinerTipHi, resp.MinerTipLo),
BaseFeeBurn: reformBigInt(resp.BaseFeeBurnHi, resp.BaseFeeBurnLo),
OverEstimationBurn: reformBigInt(resp.OverEstimationBurnHi, resp.OverEstimationBurnLo),
Refund: reformBigInt(resp.RefundHi, resp.RefundLo),
GasRefund: int64(resp.GasRefund),
GasBurned: int64(resp.GasBurned),
ExecTraceBytes: resp.ExecTrace,
FailureInfo: resp.FailureInfo,
EventsRoot: eventsRoot,
EventsBytes: resp.Events,
}, nil
}
func (f *FVM) Flush() (cid.Cid, error) {
defer runtime.KeepAlive(f)
stateRoot, err := cgo.FvmMachineFlush(f.executor)
if err != nil {
return cid.Undef, err
}
return cid.Cast(stateRoot)
}
type ApplyRet struct {
Return []byte
ExitCode uint64
GasUsed int64
MinerPenalty abi.TokenAmount
MinerTip abi.TokenAmount
BaseFeeBurn abi.TokenAmount
OverEstimationBurn abi.TokenAmount
Refund abi.TokenAmount
GasRefund int64
GasBurned int64
ExecTraceBytes []byte
FailureInfo string
EventsRoot *cid.Cid
EventsBytes []byte
}
// NOTE: We only support 64bit platforms
// returns hi, lo
func splitBigInt(i big.Int) (hi uint64, lo uint64, err error) {
if i.Sign() < 0 {
return 0, 0, xerrors.Errorf("negative number: %s", i)
}
words := i.Bits()
switch len(words) {
case 2:
hi = uint64(words[1])
fallthrough
case 1:
lo = uint64(words[0])
case 0:
default:
return 0, 0, xerrors.Errorf("exceeds max bigint size: %s", i)
}
return hi, lo, nil
}
func reformBigInt(hi, lo uint64) big.Int {
var words []gobig.Word
if hi > 0 {
words = []gobig.Word{gobig.Word(lo), gobig.Word(hi)}
} else if lo > 0 {
words = []gobig.Word{gobig.Word(lo)}
} else {
return big.Zero()
}
int := new(gobig.Int)
int.SetBits(words)
return big.NewFromGo(int)
}