This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
builder.go
103 lines (84 loc) · 2.57 KB
/
builder.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
package mock
import (
"context"
"testing"
addr "github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
"github.com/minio/blake2b-simd"
)
// Build for fluent initialization of a mock runtime.
type RuntimeBuilder struct {
rt *Runtime
}
// Initializes a new builder with a receiving actor address.
func NewBuilder(ctx context.Context, receiver addr.Address) *RuntimeBuilder {
m := &Runtime{
ctx: ctx,
epoch: 0,
networkVersion: network.VersionMax,
receiver: receiver,
caller: addr.Address{},
callerType: cid.Undef,
miner: addr.Address{},
idAddresses: make(map[addr.Address]addr.Address),
circulatingSupply: abi.NewTokenAmount(0),
state: cid.Undef,
store: make(map[cid.Cid][]byte),
hashfunc: blake2b.Sum256,
balance: abi.NewTokenAmount(0),
valueReceived: abi.NewTokenAmount(0),
actorCodeCIDs: make(map[addr.Address]cid.Cid),
newActorAddr: addr.Undef,
t: nil, // Initialized at Build()
expectValidateCallerAny: false,
expectValidateCallerAddr: nil,
expectValidateCallerType: nil,
expectCreateActor: nil,
expectSends: make([]*expectedMessage, 0),
expectVerifySigs: make([]*expectVerifySig, 0),
}
return &RuntimeBuilder{m}
}
// Builds a new runtime object with the configured values.
func (b *RuntimeBuilder) Build(t testing.TB) *Runtime {
cpy := *b.rt
// Deep copy the mutable values.
cpy.store = make(map[cid.Cid][]byte)
for k, v := range b.rt.store { //nolint:nomaprange
cpy.store[k] = v
}
cpy.t = t
return &cpy
}
func (b *RuntimeBuilder) WithEpoch(epoch abi.ChainEpoch) *RuntimeBuilder {
b.rt.epoch = epoch
return b
}
func (b *RuntimeBuilder) WithCaller(address addr.Address, code cid.Cid) *RuntimeBuilder {
b.rt.caller = address
b.rt.callerType = code
return b
}
func (b *RuntimeBuilder) WithMiner(miner addr.Address) *RuntimeBuilder {
b.rt.miner = miner
return b
}
func (b *RuntimeBuilder) WithBalance(balance, received abi.TokenAmount) *RuntimeBuilder {
b.rt.balance = balance
b.rt.valueReceived = received
return b
}
func (b *RuntimeBuilder) WithNetworkVersion(version network.Version) *RuntimeBuilder {
b.rt.networkVersion = version
return b
}
func (b *RuntimeBuilder) WithActorType(addr addr.Address, code cid.Cid) *RuntimeBuilder {
b.rt.actorCodeCIDs[addr] = code
return b
}
func (b *RuntimeBuilder) WithHasher(f func(data []byte) [32]byte) *RuntimeBuilder {
b.rt.hashfunc = f
return b
}