7
7
"crypto/ed25519"
8
8
"fmt"
9
9
"log"
10
+ "os"
10
11
"os/exec"
11
12
"sort"
12
13
"strings"
@@ -48,13 +49,14 @@ var (
48
49
)
49
50
50
51
type Node struct {
51
- Name string
52
- ContainerName string
53
- ClientURL string
54
- Client * nodeclient.Client
55
- AccountAddressBech32 string
56
- ContainerConfigs string
57
- PrivateKey string
52
+ Name string
53
+ ContainerName string
54
+ ClientURL string
55
+ Client * nodeclient.Client
56
+ AccountAddressBech32 string
57
+ ContainerConfigs string
58
+ PrivateKey string
59
+ IssueCandidacyPayload bool
58
60
}
59
61
60
62
type Account struct {
@@ -115,21 +117,36 @@ func NewDockerTestFramework(t *testing.T, opts ...options.Option[DockerTestFrame
115
117
})
116
118
}
117
119
120
+ func (d * DockerTestFramework ) DockerComposeUp (detach ... bool ) error {
121
+ cmd := exec .Command ("docker" , "compose" , "up" )
122
+
123
+ if len (detach ) > 0 && detach [0 ] {
124
+ cmd = exec .Command ("docker" , "compose" , "up" , "-d" )
125
+ }
126
+
127
+ cmd .Env = os .Environ ()
128
+ for _ , node := range d .nodes {
129
+ cmd .Env = append (cmd .Env , fmt .Sprintf ("ISSUE_CANDIDACY_PAYLOAD_%s=%t" , node .Name , node .IssueCandidacyPayload ))
130
+ }
131
+
132
+ var out strings.Builder
133
+ cmd .Stderr = & out
134
+ err := cmd .Run ()
135
+ if err != nil {
136
+ fmt .Println ("Docker compose up failed with error:" , err , ":" , out .String ())
137
+ }
138
+
139
+ return err
140
+ }
141
+
118
142
func (d * DockerTestFramework ) Run () error {
119
143
ch := make (chan error )
120
144
stopCh := make (chan struct {})
121
145
defer close (ch )
122
146
defer close (stopCh )
123
147
124
148
go func () {
125
- cmd := exec .Command ("docker" , "compose" , "up" )
126
- var out strings.Builder
127
- cmd .Stderr = & out
128
- err := cmd .Run ()
129
-
130
- if err != nil {
131
- fmt .Println ("Docker compose up failed with error:" , err , ":" , out .String ())
132
- }
149
+ err := d .DockerComposeUp ()
133
150
134
151
// make sure that the channel is not already closed
135
152
select {
@@ -211,12 +228,18 @@ func (d *DockerTestFramework) WaitUntilSync() error {
211
228
return nil
212
229
}
213
230
214
- func (d * DockerTestFramework ) AddValidatorNode (name string , containerName string , clientURL string , accAddrBech32 string ) {
231
+ func (d * DockerTestFramework ) AddValidatorNode (name string , containerName string , clientURL string , accAddrBech32 string , optIssueCandidacyPayload ... bool ) {
232
+ issueCandidacyPayload := true
233
+ if len (optIssueCandidacyPayload ) > 0 {
234
+ issueCandidacyPayload = optIssueCandidacyPayload [0 ]
235
+ }
236
+
215
237
d .nodes [name ] = & Node {
216
- Name : name ,
217
- ContainerName : containerName ,
218
- ClientURL : clientURL ,
219
- AccountAddressBech32 : accAddrBech32 ,
238
+ Name : name ,
239
+ ContainerName : containerName ,
240
+ ClientURL : clientURL ,
241
+ AccountAddressBech32 : accAddrBech32 ,
242
+ IssueCandidacyPayload : issueCandidacyPayload ,
220
243
}
221
244
}
222
245
@@ -273,52 +296,32 @@ func (d *DockerTestFramework) AccountsFromNodes(nodes ...*Node) []string {
273
296
return accounts
274
297
}
275
298
276
- func (d * DockerTestFramework ) StopIssueCandidacyPayload (nodes ... * Node ) {
277
- // build a new image from the current one so we could set IssueCandidacyPayload to false,
278
- // the committed image will not remember the container configs, so it's fine to commit the first validator of the nodes
279
- newImageName := "no-candidacy-payload-image"
280
- err := exec .Command ("docker" , "commit" , nodes [0 ].ContainerName , newImageName ).Run ()
281
- require .NoError (d .Testing , err )
299
+ func (d * DockerTestFramework ) StartIssueCandidacyPayload (nodes ... * Node ) {
300
+ if len (nodes ) == 0 {
301
+ return
302
+ }
282
303
283
304
for _ , node := range nodes {
284
- if node .AccountAddressBech32 == "" {
285
- continue
286
- }
305
+ node .IssueCandidacyPayload = true
306
+ }
287
307
288
- // stop the inx-validator that issues candidacy payload
289
- err = d .StopContainer (node .ContainerName )
290
- require .NoError (d .Testing , err )
308
+ d .DockerComposeUp (true )
309
+ }
291
310
292
- // start a new inx-validator that does not issue candidacy payload
293
- newContainerName := fmt .Sprintf ("%s-1" , node .ContainerName )
294
- cmd := fmt .Sprintf ("docker run --network docker-network_iota-core --env %s --name %s %s %s --validator.issueCandidacyPayload=false &" , node .PrivateKey , newContainerName , newImageName , node .ContainerConfigs )
295
- err = exec .Command ("bash" , "-c" , cmd ).Run ()
296
- require .NoError (d .Testing , err )
311
+ func (d * DockerTestFramework ) StopIssueCandidacyPayload (nodes ... * Node ) {
312
+ if len (nodes ) == 0 {
313
+ return
297
314
}
298
- }
299
315
300
- func (d * DockerTestFramework ) StartIssueCandidacyPayload (nodes ... * Node ) {
301
316
for _ , node := range nodes {
302
- if node .AccountAddressBech32 == "" {
303
- continue
304
- }
305
-
306
- // stop and remove the inx-validator that does not issue candidacy payload
307
- newContainerName := fmt .Sprintf ("%s-1" , node .ContainerName )
308
- err := d .StopContainer (newContainerName )
309
- require .NoError (d .Testing , err )
310
-
311
- err = exec .Command ("docker" , "rm" , newContainerName ).Run ()
312
- require .NoError (d .Testing , err )
313
-
314
- // start the inx-validator that issues candidacy payload
315
- err = d .RestartContainer (node .ContainerName )
316
- require .NoError (d .Testing , err )
317
+ node .IssueCandidacyPayload = false
317
318
}
319
+
320
+ d .DockerComposeUp (true )
318
321
}
319
322
320
323
func (d * DockerTestFramework ) CreateAccount (opts ... options.Option [builder.AccountOutputBuilder ]) * Account {
321
- // create implicit account by requesting faucet funds
324
+ // create an implicit account by requesting faucet funds
322
325
ctx := context .TODO ()
323
326
receiverAddr , implicitPrivateKey := d .getAddress (iotago .AddressImplicitAccountCreation )
324
327
implicitOutputID , implicitAccountOutput := d .RequestFaucetFunds (ctx , receiverAddr )
@@ -327,10 +330,10 @@ func (d *DockerTestFramework) CreateAccount(opts ...options.Option[builder.Accou
327
330
accountAddress , ok := accountID .ToAddress ().(* iotago.AccountAddress )
328
331
require .True (d .Testing , ok )
329
332
330
- // make sure implicit account is committed
333
+ // make sure an implicit account is committed
331
334
d .CheckAccountStatus (ctx , iotago .EmptyBlockID , implicitOutputID .TransactionID (), implicitOutputID , accountAddress )
332
335
333
- // transition to full account with new Ed25519 address and staking feature
336
+ // transition to a full account with new Ed25519 address and staking feature
334
337
accEd25519Addr , accPrivateKey := d .getAddress (iotago .AddressEd25519 )
335
338
accBlockIssuerKey := iotago .Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey (accPrivateKey .Public ().(ed25519.PublicKey ))
336
339
accountOutput := options .Apply (builder .NewAccountOutputBuilder (accEd25519Addr , implicitAccountOutput .BaseTokenAmount ()),
0 commit comments