This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipc_test.go
204 lines (180 loc) · 7.31 KB
/
ipc_test.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
package itests
import (
"bytes"
"context"
"testing"
"github.com/consensus-shipyard/go-ipc-types/gateway"
"github.com/consensus-shipyard/go-ipc-types/sdk"
"github.com/consensus-shipyard/go-ipc-types/subnetactor"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/gen/genesis"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/itests/kit"
)
// TestIPCAccessors lightly tests all the basic IPC accessors
// to double-check that the basic serialization between Go
// and Rust works. Do not treat this as a proper end-to-end test
// but just as a sanity-check that the basic integration works.
func TestIPCAccessors(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)
defer func() {
t.Logf("[*] defer: cancelling %s context", t.Name())
cancel()
err := g.Wait()
require.NoError(t, err)
t.Logf("[*] defer: system %s stopped", t.Name())
}()
nodes, validators, ens := kit.EnsembleWithMirValidators(t, 1)
ens.BeginMirMining(ctx, g, validators...)
// check gateway state
api := nodes[0]
_, err := api.IPCReadGatewayState(ctx, genesis.DefaultIPCGatewayAddr, types.EmptyTSK)
require.NoError(t, err)
// add subnet actor
src, err := api.WalletDefaultAddress(ctx)
require.NoError(t, err)
networkName, err := api.StateNetworkName(ctx)
require.NoError(t, err)
parent, err := sdk.NewSubnetIDFromString(string(networkName))
require.NoError(t, err)
params := subnetactor.ConstructParams{
Parent: parent,
Name: "test",
IPCGatewayAddr: genesis.DefaultIPCGatewayAddrID,
BottomUpCheckPeriod: genesis.DefaultCheckpointPeriod,
TopDownCheckPeriod: genesis.DefaultCheckpointPeriod,
MinValidators: 1,
MinValidatorStake: abi.TokenAmount(types.MustParseFIL("1FIL")),
Consensus: subnetactor.Mir,
}
actorAddr, err := api.IPCAddSubnetActor(ctx, src, params)
require.NoError(t, err)
sn, err := sdk.NewSubnetIDFromString("/root/" + actorAddr.String())
require.NoError(t, err)
JoinSubnet(t, ctx, api, src, actorAddr)
e, err := api.IPCGetGenesisEpochForSubnet(ctx, genesis.DefaultIPCGatewayAddr, sn)
require.NoError(t, err)
// require that the genesis epoch is greater than zero
require.True(t, e > 0)
// get subnet actor state
_, err = api.IPCReadSubnetActorState(ctx, sn, types.EmptyTSK)
require.NoError(t, err)
checkEpoch := abi.ChainEpoch(genesis.DefaultCheckpointPeriod)
c, err := abi.CidBuilder.Sum([]byte("genesis"))
require.NoError(t, err)
SubmitCheckpoint(t, ctx, api, sn, src, checkEpoch, c)
// get list of child subnets and see there are none
l, err := api.IPCListChildSubnets(ctx, genesis.DefaultIPCGatewayAddr)
require.NoError(t, err)
require.Equal(t, len(l), 1)
// get checkpoint for epoch
ch, err := api.IPCGetCheckpoint(ctx, sn, checkEpoch)
require.NoError(t, err)
// see that the serialized version is the same
b, err := api.IPCGetCheckpointSerialized(ctx, sn, checkEpoch)
require.NoError(t, err)
ser := gateway.BottomUpCheckpoint{}
err = ser.UnmarshalCBOR(bytes.NewReader(b))
require.NoError(t, err)
require.Equal(t, *ch, ser)
// get empty checkpoint template
ch, err = api.IPCGetCheckpointTemplate(ctx, genesis.DefaultIPCGatewayAddr, 0)
require.NoError(t, err)
// see that the serialized version is the same
b, err = api.IPCGetCheckpointTemplateSerialized(ctx, genesis.DefaultIPCGatewayAddr, 0)
require.NoError(t, err)
ser = gateway.BottomUpCheckpoint{}
err = ser.UnmarshalCBOR(bytes.NewReader(b))
// to make the comparison we need to set the previous checkpoint to
// cid.Undef, the serialized function uses a dummyCid to allow the serialization,
// as cid.Undef can't be cbor serialized.
ser.Data.PrevCheck = cid.Undef
require.NoError(t, err)
require.Equal(t, *ch, ser)
// get previous checkpoint for child
_, err = api.IPCGetPrevCheckpointForChild(ctx, genesis.DefaultIPCGatewayAddr, sn)
require.NoError(t, err)
// get list of checkpoints
chs, err := api.IPCListCheckpoints(ctx, sn, 0, 2*genesis.DefaultCheckpointPeriod)
require.NoError(t, err)
require.Equal(t, len(chs), 1)
// get votes
hasVoted, err := api.IPCHasVotedBottomUpCheckpoint(ctx, sn, genesis.DefaultCheckpointPeriod, src)
require.NoError(t, err)
require.False(t, hasVoted)
hasVoted, err = api.IPCHasVotedTopDownCheckpoint(ctx, genesis.DefaultIPCGatewayAddr, genesis.DefaultCheckpointPeriod, src)
require.NoError(t, err)
require.False(t, hasVoted)
// fund subnet with a number of top-down messages
for i := 0; i < 5; i++ {
FundSubnet(t, ctx, api, sn, src)
}
// get the topdown messages
msgs, err := api.IPCGetTopDownMsgs(ctx, genesis.DefaultIPCGatewayAddr, sn, types.EmptyTSK, 0)
require.NoError(t, err)
require.Equal(t, len(msgs), 5)
msgs, err = api.IPCGetTopDownMsgs(ctx, genesis.DefaultIPCGatewayAddr, sn, types.EmptyTSK, 1)
require.NoError(t, err)
require.Equal(t, len(msgs), 4)
// check its serialization form
bmsgs, err := api.IPCGetTopDownMsgsSerialized(ctx, genesis.DefaultIPCGatewayAddr, sn, types.EmptyTSK, 1)
require.NoError(t, err)
require.Equal(t, len(msgs), 4)
serMsg := gateway.CrossMsg{}
err = serMsg.UnmarshalCBOR(bytes.NewReader(bmsgs[0]))
require.NoError(t, err)
require.Equal(t, *msgs[0], serMsg)
}
func JoinSubnet(t *testing.T, ctx context.Context, node *kit.TestFullNode, from, snActor address.Address) {
params, err := actors.SerializeParams(&subnetactor.JoinParams{ValidatorNetAddr: "test"})
require.NoError(t, err)
smsg, aerr := node.MpoolPushMessage(ctx, &types.Message{
To: snActor,
From: from,
Value: abi.TokenAmount(types.MustParseFIL("10")),
Method: builtin.MustGenerateFRCMethodNum("Join"),
Params: params,
}, nil)
require.NoError(t, aerr)
_, aerr = node.StateWaitMsg(ctx, smsg.Cid(), build.MessageConfidence, api.LookbackNoLimit, true)
require.NoError(t, aerr)
}
func FundSubnet(t *testing.T, ctx context.Context, node *kit.TestFullNode, sn sdk.SubnetID, from address.Address) {
params, err := actors.SerializeParams(&sn)
require.NoError(t, err)
smsg, aerr := node.MpoolPushMessage(ctx, &types.Message{
To: genesis.DefaultIPCGatewayAddr,
From: from,
Value: abi.TokenAmount(types.MustParseFIL("10")),
Method: builtin.MustGenerateFRCMethodNum("Fund"),
Params: params,
}, nil)
require.NoError(t, aerr)
_, aerr = node.StateWaitMsg(ctx, smsg.Cid(), build.MessageConfidence, api.LookbackNoLimit, true)
require.NoError(t, aerr)
}
func SubmitCheckpoint(t *testing.T, ctx context.Context, node *kit.TestFullNode, sn sdk.SubnetID, from address.Address, epoch abi.ChainEpoch, prev cid.Cid) {
ch := gateway.NewBottomUpCheckpoint(sn, epoch)
ch.Data.PrevCheck = prev
params, err := actors.SerializeParams(ch)
require.NoError(t, err)
smsg, aerr := node.MpoolPushMessage(ctx, &types.Message{
To: sn.Actor,
From: from,
Value: abi.NewTokenAmount(0),
Method: builtin.MustGenerateFRCMethodNum("SubmitCheckpoint"),
Params: params,
}, nil)
require.NoError(t, aerr)
_, aerr = node.StateWaitMsg(ctx, smsg.Cid(), build.MessageConfidence, api.LookbackNoLimit, true)
require.NoError(t, aerr)
}