|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/docker/docker/api/types/container" |
| 7 | + "github.com/docker/go-connections/nat" |
| 8 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/testcontainers/testcontainers-go" |
| 13 | + "github.com/testcontainers/testcontainers-go/wait" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + AccountsFile = `{ |
| 18 | + "hdPath": "m/44'/195'/0'/0/", |
| 19 | + "mnemonic": "resemble birth wool happy sun burger fatal trumpet globe purity health ritual", |
| 20 | + "privateKeys": [ |
| 21 | + "cf36898af3c63e13537063ae165ec8262fafac188f09200647b4b76b6f212b90", |
| 22 | + "7d5110f81cc6b2c65a532066e81fe813edf781e24f4e0fa42d22a3003dae7a54", |
| 23 | + "ae0de6fb5450622bfc96ec0c25a8a5cb85256d1f9d6cbbe5fd9de073d22f0060", |
| 24 | + "e972c3c213f8ba8cfe9a75e5d0b48310f4e35715f70986edd1eade904dd03437", |
| 25 | + "23d81a4d6c85661b58922e68db09cca0ebe77c787beb0e12c8d29da111568855" |
| 26 | + ], |
| 27 | + "more": [ |
| 28 | + { |
| 29 | + "hdPath": "m/44'/195'/0'/0/", |
| 30 | + "mnemonic": "resemble birth wool happy sun burger fatal trumpet globe purity health ritual", |
| 31 | + "privateKeys": [ |
| 32 | + "cf36898af3c63e13537063ae165ec8262fafac188f09200647b4b76b6f212b90", |
| 33 | + "7d5110f81cc6b2c65a532066e81fe813edf781e24f4e0fa42d22a3003dae7a54", |
| 34 | + "ae0de6fb5450622bfc96ec0c25a8a5cb85256d1f9d6cbbe5fd9de073d22f0060", |
| 35 | + "e972c3c213f8ba8cfe9a75e5d0b48310f4e35715f70986edd1eade904dd03437", |
| 36 | + "23d81a4d6c85661b58922e68db09cca0ebe77c787beb0e12c8d29da111568855" |
| 37 | + ] |
| 38 | + } |
| 39 | + ] |
| 40 | + } |
| 41 | + ` |
| 42 | + DefaultTronPort = "9090" |
| 43 | + DefaultTronSolidityPort = "8091" |
| 44 | +) |
| 45 | + |
| 46 | +func defaultTron(in *Input) { |
| 47 | + if in.Image == "" { |
| 48 | + in.Image = "trontools/quickstart:2.1.1" |
| 49 | + } |
| 50 | + if in.Port == "" { |
| 51 | + in.Port = DefaultTronPort |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func newTron(in *Input) (*Output, error) { |
| 56 | + defaultTron(in) |
| 57 | + ctx := context.Background() |
| 58 | + |
| 59 | + containerName := framework.DefaultTCName("blockchain-node") |
| 60 | + bindPort := fmt.Sprintf("%s/tcp", in.Port) |
| 61 | + |
| 62 | + accounts, err := os.CreateTemp("", "accounts.json") |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + _, err = accounts.WriteString(AccountsFile) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + req := testcontainers.ContainerRequest{ |
| 72 | + AlwaysPullImage: in.PullImage, |
| 73 | + Image: in.Image, |
| 74 | + Name: containerName, |
| 75 | + ExposedPorts: []string{bindPort, "18190/tcp", "18191/tcp"}, |
| 76 | + Networks: []string{framework.DefaultNetworkName}, |
| 77 | + NetworkAliases: map[string][]string{ |
| 78 | + framework.DefaultNetworkName: {containerName}, |
| 79 | + }, |
| 80 | + Env: map[string]string{ |
| 81 | + "accounts": "10", |
| 82 | + }, |
| 83 | + Labels: framework.DefaultTCLabels(), |
| 84 | + HostConfigModifier: func(h *container.HostConfig) { |
| 85 | + h.PortBindings = framework.MapTheSamePort(bindPort, "18190/tcp", "19191/tcp") |
| 86 | + }, |
| 87 | + WaitingFor: wait.ForListeningPort(nat.Port(in.Port)).WithStartupTimeout(60 * time.Second).WithPollInterval(200 * time.Millisecond), |
| 88 | + Files: []testcontainers.ContainerFile{ |
| 89 | + { |
| 90 | + HostFilePath: accounts.Name(), |
| 91 | + ContainerFilePath: "/config/accounts.json", |
| 92 | + FileMode: 0644, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 98 | + ContainerRequest: req, |
| 99 | + Started: true, |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + host, err := c.Host(ctx) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + return &Output{ |
| 111 | + UseCache: true, |
| 112 | + ChainID: in.ChainID, |
| 113 | + Family: "tron", |
| 114 | + ContainerName: containerName, |
| 115 | + Nodes: []*Node{ |
| 116 | + { |
| 117 | + HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, in.Port), |
| 118 | + DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port), |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, nil |
| 122 | +} |
0 commit comments