99 "log"
1010 "os"
1111 "os/exec"
12+ "slices"
1213 "sort"
1314 "strings"
1415 "sync/atomic"
@@ -49,13 +50,14 @@ var (
4950)
5051
5152type 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
5961}
6062
6163type Account struct {
@@ -116,31 +118,36 @@ func NewDockerTestFramework(t *testing.T, opts ...options.Option[DockerTestFrame
116118 })
117119}
118120
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 {
120144 ch := make (chan error )
121145 stopCh := make (chan struct {})
122146 defer close (ch )
123147 defer close (stopCh )
124148
125- issueCandidacyPayloadMap := make (map [string ]bool )
126- if len (optIssueCandidacyPayloadMap ) > 0 {
127- issueCandidacyPayloadMap = optIssueCandidacyPayloadMap [0 ]
128- }
129-
130149 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 ()
144151
145152 // make sure that the channel is not already closed
146153 select {
@@ -222,12 +229,18 @@ func (d *DockerTestFramework) WaitUntilSync() error {
222229 return nil
223230}
224231
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+
226238 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 ,
231244 }
232245}
233246
@@ -284,23 +297,36 @@ func (d *DockerTestFramework) AccountsFromNodes(nodes ...*Node) []string {
284297 return accounts
285298}
286299
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+ }
289304
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+ }
291313
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
294317 }
295318
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+ }
299323 }
324+
325+ d .DockerComposeUp (true )
300326}
301327
302328func (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
304330 ctx := context .TODO ()
305331 receiverAddr , implicitPrivateKey := d .getAddress (iotago .AddressImplicitAccountCreation )
306332 implicitOutputID , implicitAccountOutput := d .RequestFaucetFunds (ctx , receiverAddr )
@@ -309,10 +335,10 @@ func (d *DockerTestFramework) CreateAccount(opts ...options.Option[builder.Accou
309335 accountAddress , ok := accountID .ToAddress ().(* iotago.AccountAddress )
310336 require .True (d .Testing , ok )
311337
312- // make sure implicit account is committed
338+ // make sure an implicit account is committed
313339 d .CheckAccountStatus (ctx , iotago .EmptyBlockID , implicitOutputID .TransactionID (), implicitOutputID , accountAddress )
314340
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
316342 accEd25519Addr , accPrivateKey := d .getAddress (iotago .AddressEd25519 )
317343 accBlockIssuerKey := iotago .Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey (accPrivateKey .Public ().(ed25519.PublicKey ))
318344 accountOutput := options .Apply (builder .NewAccountOutputBuilder (accEd25519Addr , implicitAccountOutput .BaseTokenAmount ()),
0 commit comments