|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/testcontainers/testcontainers-go" |
| 8 | + "github.com/testcontainers/testcontainers-go/network" |
| 9 | + |
| 10 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain/canton" |
| 11 | +) |
| 12 | + |
| 13 | +func newCanton(ctx context.Context, in *Input) (*Output, error) { |
| 14 | + if in.NumberOfValidators >= 100 { |
| 15 | + return nil, fmt.Errorf("number of validators too high: %d, max is 99", in.NumberOfValidators) |
| 16 | + } |
| 17 | + |
| 18 | + // TODO - remove debug prints |
| 19 | + fmt.Println("Starting Canton blockchain node...") |
| 20 | + fmt.Println("Creating network...") |
| 21 | + dockerNetwork, err := network.New(ctx, network.WithAttachable()) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + fmt.Println("Network created:", dockerNetwork.Name) |
| 26 | + |
| 27 | + // Set up Postgres container |
| 28 | + postgresReq := canton.PostgresContainerRequest(in.NumberOfValidators, dockerNetwork.Name) |
| 29 | + fmt.Printf("Starting postgres container %s...\n", postgresReq.Name) |
| 30 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 31 | + ContainerRequest: postgresReq, |
| 32 | + Started: true, |
| 33 | + }) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + _ = c |
| 38 | + fmt.Println("Postgres container started") |
| 39 | + |
| 40 | + // Set up Canton container |
| 41 | + cantonReq := canton.CantonContainerRequest(dockerNetwork.Name, in.NumberOfValidators, in.Image) |
| 42 | + fmt.Printf("Starting canton container %s...\n", cantonReq.Name) |
| 43 | + cantonContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 44 | + ContainerRequest: cantonReq, |
| 45 | + Started: true, |
| 46 | + }) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + _ = cantonContainer |
| 51 | + fmt.Println("Canton container started") |
| 52 | + |
| 53 | + // Set up Splice container |
| 54 | + spliceReq := canton.SpliceContainerRequest(dockerNetwork.Name, in.NumberOfValidators, in.Image) |
| 55 | + fmt.Printf("Starting splice container %s...\n", spliceReq.Name) |
| 56 | + spliceContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 57 | + ContainerRequest: spliceReq, |
| 58 | + Started: true, |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + _ = spliceContainer |
| 64 | + fmt.Println("Splice container started") |
| 65 | + |
| 66 | + // Set up Nginx container |
| 67 | + nginxReq := canton.NginxContainerRequest(dockerNetwork.Name, in.NumberOfValidators, in.Port) |
| 68 | + fmt.Printf("Starting nginx container %s...\n", nginxReq.Name) |
| 69 | + nginxContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 70 | + ContainerRequest: nginxReq, |
| 71 | + Started: true, |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + fmt.Println("Nginx container started") |
| 77 | + |
| 78 | + host, err := nginxContainer.Host(ctx) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + return &Output{ |
| 84 | + UseCache: false, |
| 85 | + Type: in.Type, |
| 86 | + Family: FamilyCanton, |
| 87 | + ContainerName: nginxReq.Name, |
| 88 | + Nodes: []*Node{ |
| 89 | + { |
| 90 | + ExternalHTTPUrl: fmt.Sprintf("http://%s:%s", host, in.Port), |
| 91 | + InternalHTTPUrl: fmt.Sprintf("http://%s:%s", nginxReq.Name, in.Port), // TODO - should be docker-internal port instead? |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, nil |
| 95 | +} |
0 commit comments