9
9
"log"
10
10
"os"
11
11
"os/exec"
12
+ "slices"
12
13
"sort"
13
14
"strings"
14
15
"sync/atomic"
@@ -49,13 +50,14 @@ var (
49
50
)
50
51
51
52
type Node struct {
52
- Name string
53
- ContainerName string
54
- ClientURL string
55
- Client * nodeclient.Client
56
- AccountAddressBech32 string
57
- ContainerConfigs string
58
- PrivateKey string
53
+ Name string
54
+ ContainerName string
55
+ ClientURL string
56
+ Client * nodeclient.Client
57
+ AccountAddressBech32 string
58
+ ContainerConfigs string
59
+ PrivateKey string
60
+ IssueCandidacyPayload bool
59
61
}
60
62
61
63
type Account struct {
@@ -116,31 +118,36 @@ func NewDockerTestFramework(t *testing.T, opts ...options.Option[DockerTestFrame
116
118
})
117
119
}
118
120
119
- func (d * DockerTestFramework ) Run (optIssueCandidacyPayloadMap ... map [string ]bool ) error {
121
+ func (d * DockerTestFramework ) DockerComposeUp (detach ... bool ) error {
122
+ cmd := exec .Command ("docker" , "compose" , "up" )
123
+
124
+ if len (detach ) > 0 && detach [0 ] {
125
+ cmd = exec .Command ("docker" , "compose" , "up" , "-d" )
126
+ }
127
+
128
+ cmd .Env = os .Environ ()
129
+ for _ , node := range d .nodes {
130
+ cmd .Env = append (cmd .Env , fmt .Sprintf ("ISSUE_CANDIDACY_PAYLOAD_%s=%t" , node .Name , node .IssueCandidacyPayload ))
131
+ }
132
+
133
+ var out strings.Builder
134
+ cmd .Stderr = & out
135
+ err := cmd .Run ()
136
+ if err != nil {
137
+ fmt .Println ("Docker compose up failed with error:" , err , ":" , out .String ())
138
+ }
139
+
140
+ return err
141
+ }
142
+
143
+ func (d * DockerTestFramework ) Run () error {
120
144
ch := make (chan error )
121
145
stopCh := make (chan struct {})
122
146
defer close (ch )
123
147
defer close (stopCh )
124
148
125
- issueCandidacyPayloadMap := make (map [string ]bool )
126
- if len (optIssueCandidacyPayloadMap ) > 0 {
127
- issueCandidacyPayloadMap = optIssueCandidacyPayloadMap [0 ]
128
- }
129
-
130
149
go func () {
131
- cmd := exec .Command ("docker" , "compose" , "up" )
132
- cmd .Env = os .Environ ()
133
- for nodeName , issueCandidacyPayload := range issueCandidacyPayloadMap {
134
- cmd .Env = append (cmd .Env , fmt .Sprintf ("ISSUE_CANDIDACY_PAYLOAD_%s=%t" , nodeName , issueCandidacyPayload ))
135
- }
136
-
137
- var out strings.Builder
138
- cmd .Stderr = & out
139
- err := cmd .Run ()
140
-
141
- if err != nil {
142
- fmt .Println ("Docker compose up failed with error:" , err , ":" , out .String ())
143
- }
150
+ err := d .DockerComposeUp ()
144
151
145
152
// make sure that the channel is not already closed
146
153
select {
@@ -222,12 +229,18 @@ func (d *DockerTestFramework) WaitUntilSync() error {
222
229
return nil
223
230
}
224
231
225
- func (d * DockerTestFramework ) AddValidatorNode (name string , containerName string , clientURL string , accAddrBech32 string ) {
232
+ func (d * DockerTestFramework ) AddValidatorNode (name string , containerName string , clientURL string , accAddrBech32 string , optIssueCandidacyPayload ... bool ) {
233
+ issueCandidacyPayload := true
234
+ if len (optIssueCandidacyPayload ) > 0 {
235
+ issueCandidacyPayload = optIssueCandidacyPayload [0 ]
236
+ }
237
+
226
238
d .nodes [name ] = & Node {
227
- Name : name ,
228
- ContainerName : containerName ,
229
- ClientURL : clientURL ,
230
- AccountAddressBech32 : accAddrBech32 ,
239
+ Name : name ,
240
+ ContainerName : containerName ,
241
+ ClientURL : clientURL ,
242
+ AccountAddressBech32 : accAddrBech32 ,
243
+ IssueCandidacyPayload : issueCandidacyPayload ,
231
244
}
232
245
}
233
246
@@ -284,23 +297,36 @@ func (d *DockerTestFramework) AccountsFromNodes(nodes ...*Node) []string {
284
297
return accounts
285
298
}
286
299
287
- func (d * DockerTestFramework ) SetIssueCandidacyPayload (issueCandidacyPayloadMap map [string ]bool ) {
288
- cmd := exec .Command ("docker" , "compose" , "up" , "-d" )
300
+ func (d * DockerTestFramework ) StartIssueCandidacyPayload (nodeNames ... string ) {
301
+ if len (nodeNames ) == 0 {
302
+ return
303
+ }
289
304
290
- cmd .Env = os .Environ ()
305
+ for _ , node := range d .nodes {
306
+ if slices .Contains (nodeNames , node .Name ) {
307
+ node .IssueCandidacyPayload = true
308
+ }
309
+ }
310
+
311
+ d .DockerComposeUp (true )
312
+ }
291
313
292
- for nodeName , issueCandidacyPayload := range issueCandidacyPayloadMap {
293
- cmd .Env = append (cmd .Env , fmt .Sprintf ("ISSUE_CANDIDACY_PAYLOAD_%s=%t" , nodeName , issueCandidacyPayload ))
314
+ func (d * DockerTestFramework ) StopIssueCandidacyPayload (nodeNames ... string ) {
315
+ if len (nodeNames ) == 0 {
316
+ return
294
317
}
295
318
296
- err := cmd .Run ()
297
- if err != nil {
298
- fmt .Println ("Docker compose up failed with error:" , err )
319
+ for _ , node := range d .nodes {
320
+ if slices .Contains (nodeNames , node .Name ) {
321
+ node .IssueCandidacyPayload = false
322
+ }
299
323
}
324
+
325
+ d .DockerComposeUp (true )
300
326
}
301
327
302
328
func (d * DockerTestFramework ) CreateAccount (opts ... options.Option [builder.AccountOutputBuilder ]) * Account {
303
- // create implicit account by requesting faucet funds
329
+ // create an implicit account by requesting faucet funds
304
330
ctx := context .TODO ()
305
331
receiverAddr , implicitPrivateKey := d .getAddress (iotago .AddressImplicitAccountCreation )
306
332
implicitOutputID , implicitAccountOutput := d .RequestFaucetFunds (ctx , receiverAddr )
@@ -309,10 +335,10 @@ func (d *DockerTestFramework) CreateAccount(opts ...options.Option[builder.Accou
309
335
accountAddress , ok := accountID .ToAddress ().(* iotago.AccountAddress )
310
336
require .True (d .Testing , ok )
311
337
312
- // make sure implicit account is committed
338
+ // make sure an implicit account is committed
313
339
d .CheckAccountStatus (ctx , iotago .EmptyBlockID , implicitOutputID .TransactionID (), implicitOutputID , accountAddress )
314
340
315
- // transition to full account with new Ed25519 address and staking feature
341
+ // transition to a full account with new Ed25519 address and staking feature
316
342
accEd25519Addr , accPrivateKey := d .getAddress (iotago .AddressEd25519 )
317
343
accBlockIssuerKey := iotago .Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey (accPrivateKey .Public ().(ed25519.PublicKey ))
318
344
accountOutput := options .Apply (builder .NewAccountOutputBuilder (accEd25519Addr , implicitAccountOutput .BaseTokenAmount ()),
0 commit comments