/
types.go
175 lines (152 loc) · 5.65 KB
/
types.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
package vmcontext
import (
"context"
"encoding/binary"
"fmt"
"time"
"github.com/dchest/blake2b"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/venus/pkg/state"
"github.com/filecoin-project/venus/venus-shared/actors/adt"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/account"
blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore"
"github.com/filecoin-project/venus/venus-shared/types"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/pkg/errors"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
"github.com/filecoin-project/venus/pkg/fork"
"github.com/filecoin-project/venus/pkg/state/tree"
"github.com/filecoin-project/venus/pkg/vm/dispatch"
"github.com/filecoin-project/venus/pkg/vm/gas"
)
type (
ExecCallBack func(cid.Cid, *types.Message, *Ret) error
CircSupplyCalculator func(context.Context, abi.ChainEpoch, tree.Tree) (abi.TokenAmount, error)
LookbackStateGetter func(context.Context, abi.ChainEpoch) (*state.View, error)
TipSetGetter func(context.Context, abi.ChainEpoch) (types.TipSetKey, error)
)
type ExecutionLane int
const (
// ExecutionLaneDefault signifies a default, non prioritized execution lane.
ExecutionLaneDefault ExecutionLane = iota
// ExecutionLanePriority signifies a prioritized execution lane with reserved resources.
ExecutionLanePriority
)
type VmOption struct { //nolint
CircSupplyCalculator CircSupplyCalculator
LookbackStateGetter LookbackStateGetter
NetworkVersion network.Version
Rnd HeadChainRandomness
BaseFee abi.TokenAmount
Fork fork.IFork
ActorCodeLoader *dispatch.CodeLoader
Epoch abi.ChainEpoch
Timestamp uint64
GasPriceSchedule *gas.PricesSchedule
PRoot cid.Cid
Bsstore blockstoreutil.Blockstore
SysCallsImpl SyscallsImpl
TipSetGetter TipSetGetter
Tracing bool
ActorDebugging bool
// ReturnEvents decodes and returns emitted events.
ReturnEvents bool
// ExecutionLane specifies the execution priority of the created vm
ExecutionLane ExecutionLane
}
type ILookBack interface {
StateView(ctx context.Context, ts *types.TipSet) (*state.View, error)
GetLookbackTipSetForRound(ctx context.Context, ts *types.TipSet, round abi.ChainEpoch, version network.Version) (*types.TipSet, cid.Cid, error)
}
func LookbackStateGetterForTipset(ctx context.Context, backer ILookBack, fork fork.IFork, ts *types.TipSet) LookbackStateGetter {
return func(ctx context.Context, round abi.ChainEpoch) (*state.View, error) {
ver := fork.GetNetworkVersion(ctx, round)
ts, _, err := backer.GetLookbackTipSetForRound(ctx, ts, round, ver)
if err != nil {
return nil, err
}
return backer.StateView(ctx, ts)
}
}
func TipSetGetterForTipset(tsGet func(context.Context, *types.TipSet, abi.ChainEpoch, bool) (*types.TipSet, error),
ts *types.TipSet,
) TipSetGetter {
return func(ctx context.Context, round abi.ChainEpoch) (types.TipSetKey, error) {
ts, err := tsGet(ctx, ts, round, true)
if err != nil {
return types.EmptyTSK, err
}
return ts.Key(), nil
}
}
// ChainRandomness define randomness method in filecoin
type HeadChainRandomness interface {
GetChainRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error)
GetBeaconRandomness(ctx context.Context, round abi.ChainEpoch) ([32]byte, error)
}
func DrawRandomnessFromDigest(digest [32]byte, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
h := blake2b.New256()
if err := binary.Write(h, binary.BigEndian, int64(pers)); err != nil {
return nil, fmt.Errorf("deriving randomness: %w", err)
}
_, err := h.Write(digest[:])
if err != nil {
return nil, fmt.Errorf("hashing VRFDigest: %w", err)
}
if err := binary.Write(h, binary.BigEndian, round); err != nil {
return nil, fmt.Errorf("deriving randomness: %w", err)
}
_, err = h.Write(entropy)
if err != nil {
return nil, fmt.Errorf("hashing entropy: %w", err)
}
return h.Sum(nil), nil
}
type Ret struct {
GasTracker *gas.GasTracker
OutPuts gas.GasOutputs
Receipt types.MessageReceipt
ActorErr error
Duration time.Duration
Events []types.Event
}
// Failure returns with a non-zero exit code.
func Failure(exitCode exitcode.ExitCode, gasAmount int64) types.MessageReceipt {
return types.MessageReceipt{
ExitCode: exitCode,
Return: []byte{},
GasUsed: gasAmount,
}
}
type Interface interface {
ApplyMessage(ctx context.Context, cmsg types.ChainMsg) (*Ret, error)
ApplyImplicitMessage(ctx context.Context, msg types.ChainMsg) (*Ret, error)
Flush(ctx context.Context) (cid.Cid, error)
}
func ResolveToDeterministicAddress(ctx context.Context, state tree.Tree, addr address.Address, cst cbor.IpldStore) (address.Address, error) {
if addr.Protocol() == address.BLS || addr.Protocol() == address.SECP256K1 || addr.Protocol() == address.Delegated {
return addr, nil
}
act, found, err := state.GetActor(ctx, addr)
if err != nil {
return address.Undef, errors.Wrapf(err, "failed to find actor: %s", addr)
}
if !found {
return address.Undef, fmt.Errorf("actor not found %s", addr)
}
if state.Version() >= tree.StateTreeVersion5 {
if act.Address != nil {
// If there _is_ an f4 address, return it as "key" address
return *act.Address, nil
}
}
aast, err := account.Load(adt.WrapStore(ctx, cst), act)
if err != nil {
return address.Undef, fmt.Errorf("failed to get account actor state for %s: %w", addr, err)
}
return aast.PubkeyAddress()
}