Skip to content

Commit 2fcbdd8

Browse files
authored
[DX-2252] Allow to pass context to all components' constructors (#2216)
1 parent bf1e79f commit 2fcbdd8

File tree

26 files changed

+161
-80
lines changed

26 files changed

+161
-80
lines changed

framework/.changeset/v0.11.6.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Allow to pass context to all components' constructors

framework/components/blockchain/anvil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blockchain
22

33
import (
4+
"context"
45
"strings"
56

67
"github.com/smartcontractkit/chainlink-testing-framework/framework"
@@ -31,8 +32,7 @@ func defaultAnvil(in *Input) {
3132
}
3233
}
3334

34-
// newAnvil deploy foundry anvil node
35-
func newAnvil(in *Input) (*Output, error) {
35+
func newAnvil(ctx context.Context, in *Input) (*Output, error) {
3636
if in.Out != nil && in.Out.UseCache {
3737
return in.Out, nil
3838
}
@@ -50,5 +50,5 @@ func newAnvil(in *Input) (*Output, error) {
5050

5151
framework.L.Info().Any("Cmd", strings.Join(entryPoint, " ")).Msg("Creating anvil with command")
5252

53-
return createGenericEvmContainer(in, req, false)
53+
return createGenericEvmContainer(ctx, in, req, false)
5454
}

framework/components/blockchain/anvil_zksync.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blockchain
22

33
import (
4+
"context"
45
"os"
56
"path/filepath"
67
"strings"
@@ -41,7 +42,7 @@ RUN curl -L https://raw.githubusercontent.com/matter-labs/foundry-zksync/main/in
4142
// creating a dockerfile in a temporary directory with the necessary commands to install
4243
// foundry-zksync.
4344
// see: https://foundry-book.zksync.io/getting-started/installation#using-foundry-with-docker
44-
func newAnvilZksync(in *Input) (*Output, error) {
45+
func newAnvilZksync(ctx context.Context, in *Input) (*Output, error) {
4546
defaultAnvilZksync(in)
4647
req := baseRequest(in, WithoutWsEndpoint)
4748

@@ -77,7 +78,7 @@ func newAnvilZksync(in *Input) (*Output, error) {
7778

7879
framework.L.Info().Any("Cmd", strings.Join(req.Entrypoint, " ")).Msg("Creating anvil zkSync with command")
7980

80-
output, err := createGenericEvmContainer(in, req, false)
81+
output, err := createGenericEvmContainer(ctx, in, req, false)
8182
if err != nil {
8283
return nil, err
8384
}

framework/components/blockchain/aptos.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ func defaultAptos(in *Input) {
5050
}
5151
}
5252

53-
func newAptos(in *Input) (*Output, error) {
53+
func newAptos(ctx context.Context, in *Input) (*Output, error) {
5454
defaultAptos(in)
55-
ctx := context.Background()
5655
containerName := framework.DefaultTCName("blockchain-node")
5756

5857
absPath, err := filepath.Abs(in.ContractsDir)
@@ -129,12 +128,12 @@ func newAptos(in *Input) (*Output, error) {
129128
return nil, err
130129
}
131130
cmdStr := []string{"aptos", "init", "--network=local", "--assume-yes", fmt.Sprintf("--private-key=%s", DefaultAptosPrivateKey)}
132-
_, err = dc.ExecContainer(containerName, cmdStr)
131+
_, err = dc.ExecContainerWithContext(ctx, containerName, cmdStr)
133132
if err != nil {
134133
return nil, err
135134
}
136135
fundCmd := []string{"aptos", "account", "fund-with-faucet", "--account", DefaultAptosAccount, "--amount", "1000000000000"}
137-
_, err = dc.ExecContainer(containerName, fundCmd)
136+
_, err = dc.ExecContainerWithContext(ctx, containerName, fundCmd)
138137
if err != nil {
139138
return nil, err
140139
}

framework/components/blockchain/besu.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package blockchain
22

3+
import "context"
4+
35
const (
46
DefaultBesuPrivateKey1 = "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"
57
DefaultBesuPrivateKey2 = "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3"
@@ -24,7 +26,7 @@ func defaultBesu(in *Input) {
2426
}
2527
}
2628

27-
func newBesu(in *Input) (*Output, error) {
29+
func newBesu(ctx context.Context, in *Input) (*Output, error) {
2830
defaultBesu(in)
2931
req := baseRequest(in, WithWsEndpoint)
3032

@@ -48,5 +50,5 @@ func newBesu(in *Input) (*Output, error) {
4850
entryPoint := append(defaultCmd, in.DockerCmdParamsOverrides...)
4951
req.Cmd = entryPoint
5052

51-
return createGenericEvmContainer(in, req, true)
53+
return createGenericEvmContainer(ctx, in, req, true)
5254
}

framework/components/blockchain/blockchain.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blockchain
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/testcontainers/testcontainers-go"
@@ -93,29 +94,34 @@ type Node struct {
9394
InternalHTTPUrl string `toml:"internal_http_url"`
9495
}
9596

96-
// NewBlockchainNetwork this is an abstraction that can spin up various blockchain network simulators
9797
func NewBlockchainNetwork(in *Input) (*Output, error) {
98+
// pass context to input if needed in the future
99+
return NewWithContext(context.Background(), in)
100+
}
101+
102+
// NewBlockchainNetwork this is an abstraction that can spin up various blockchain network simulators
103+
func NewWithContext(ctx context.Context, in *Input) (*Output, error) {
98104
var out *Output
99105
var err error
100106
switch in.Type {
101107
case TypeAnvil:
102-
out, err = newAnvil(in)
108+
out, err = newAnvil(ctx, in)
103109
case TypeGeth:
104-
out, err = newGeth(in)
110+
out, err = newGeth(ctx, in)
105111
case TypeBesu:
106-
out, err = newBesu(in)
112+
out, err = newBesu(ctx, in)
107113
case TypeSolana:
108-
out, err = newSolana(in)
114+
out, err = newSolana(ctx, in)
109115
case TypeAptos:
110-
out, err = newAptos(in)
116+
out, err = newAptos(ctx, in)
111117
case TypeSui:
112-
out, err = newSui(in)
118+
out, err = newSui(ctx, in)
113119
case TypeTron:
114-
out, err = newTron(in)
120+
out, err = newTron(ctx, in)
115121
case TypeAnvilZKSync:
116-
out, err = newAnvilZksync(in)
122+
out, err = newAnvilZksync(ctx, in)
117123
case TypeTon:
118-
out, err = newTon(in)
124+
out, err = newTon(ctx, in)
119125
default:
120126
return nil, fmt.Errorf("blockchain type is not supported or empty, must be 'anvil' or 'geth'")
121127
}

framework/components/blockchain/containers.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ func baseRequest(in *Input, useWS ExposeWs) testcontainers.ContainerRequest {
6565
return req
6666
}
6767

68-
func createGenericEvmContainer(in *Input, req testcontainers.ContainerRequest, useWS bool) (*Output, error) {
69-
ctx := context.Background()
68+
func createGenericEvmContainer(ctx context.Context, in *Input, req testcontainers.ContainerRequest, useWS bool) (*Output, error) {
7069
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
7170
ContainerRequest: req,
7271
Started: true,
@@ -75,7 +74,7 @@ func createGenericEvmContainer(in *Input, req testcontainers.ContainerRequest, u
7574
return nil, err
7675
}
7776

78-
host, err := framework.GetHost(c)
77+
host, err := framework.GetHostWithContext(ctx, c)
7978
if err != nil {
8079
return nil, err
8180
}

framework/components/blockchain/geth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blockchain
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67

@@ -77,7 +78,7 @@ func defaultGeth(in *Input) {
7778
}
7879
}
7980

80-
func newGeth(in *Input) (*Output, error) {
81+
func newGeth(ctx context.Context, in *Input) (*Output, error) {
8182
defaultGeth(in)
8283
req := baseRequest(in, WithoutWsEndpoint)
8384
defaultCmd := []string{
@@ -184,5 +185,5 @@ func newGeth(in *Input) (*Output, error) {
184185
}
185186
req.Cmd = entryPoint
186187

187-
return createGenericEvmContainer(in, req, false)
188+
return createGenericEvmContainer(ctx, in, req, false)
188189
}

framework/components/blockchain/solana.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ func defaultSolana(in *Input) {
4343
}
4444
}
4545

46-
func newSolana(in *Input) (*Output, error) {
46+
func newSolana(ctx context.Context, in *Input) (*Output, error) {
4747
if in.Out != nil && in.Out.UseCache {
4848
return in.Out, nil
4949
}
5050
defaultSolana(in)
51-
ctx := context.Background()
5251

5352
containerName := framework.DefaultTCName("blockchain-node")
5453
// Solana do not allow to set ws port, it just uses --rpc-port=N and sets WS as N+1 automatically
@@ -140,7 +139,7 @@ func newSolana(in *Input) (*Output, error) {
140139
if err != nil {
141140
return nil, err
142141
}
143-
host, err := framework.GetHost(c)
142+
host, err := framework.GetHostWithContext(ctx, c)
144143
if err != nil {
145144
return nil, err
146145
}

framework/components/blockchain/sui.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ func fundAccount(url string, address string) error {
5454
}
5555

5656
// generateKeyData generates a wallet and returns all the data
57-
func generateKeyData(containerName string, keyCipherType string) (*SuiWalletInfo, error) {
57+
func generateKeyData(ctx context.Context, containerName string, keyCipherType string) (*SuiWalletInfo, error) {
5858
cmdStr := []string{"sui", "keytool", "generate", keyCipherType, "--json"}
5959
dc, err := framework.NewDockerClient()
6060
if err != nil {
6161
return nil, err
6262
}
63-
keyOut, err := dc.ExecContainer(containerName, cmdStr)
63+
keyOut, err := dc.ExecContainerWithContext(ctx, containerName, cmdStr)
6464
if err != nil {
6565
return nil, err
6666
}
@@ -90,9 +90,8 @@ func defaultSui(in *Input) {
9090
}
9191
}
9292

93-
func newSui(in *Input) (*Output, error) {
93+
func newSui(ctx context.Context, in *Input) (*Output, error) {
9494
defaultSui(in)
95-
ctx := context.Background()
9695
containerName := framework.DefaultTCName("blockchain-node")
9796

9897
absPath, err := filepath.Abs(in.ContractsDir)
@@ -167,7 +166,7 @@ func newSui(in *Input) (*Output, error) {
167166
if err != nil {
168167
return nil, err
169168
}
170-
suiAccount, err := generateKeyData(containerName, "ed25519")
169+
suiAccount, err := generateKeyData(ctx, containerName, "ed25519")
171170
if err != nil {
172171
return nil, err
173172
}

0 commit comments

Comments
 (0)