|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/docker/docker/api/types/container" |
| 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 | +type Accounts struct { |
| 17 | + HDPath string `json:"hdPath"` |
| 18 | + Mnemonic string `json:"mnemonic"` |
| 19 | + PrivateKeys []string `json:"privateKeys"` |
| 20 | + More []string `json:"more"` |
| 21 | +} |
| 22 | + |
| 23 | +var TRONAccounts = Accounts{ |
| 24 | + HDPath: "m/44'/195'/0'/0/", |
| 25 | + Mnemonic: "resemble birth wool happy sun burger fatal trumpet globe purity health ritual", |
| 26 | + PrivateKeys: []string{ |
| 27 | + "932a39242805a1b1095638027f26af9664d1d5bf8ab3b7527ee75e7efb2946dd", |
| 28 | + "1c17c9c049d36cde7e5ea99df6c86e0474b04f0e258ab619a1e674f397a17152", |
| 29 | + "458130a239671674746582184711a6f8d633355df1a491b9f3b323576134c2e9", |
| 30 | + "2676fd1427968e07feaa9aff967d4ba7607c5497c499968c098d0517cd75cfbb", |
| 31 | + "d26b24a691ff2b03ee6ab65bf164def216f73574996b9ca6299c43a9a63767ac", |
| 32 | + "55df6adf3d081944dbe4688205d94f236fb4427ac44f3a286a96d47db0860667", |
| 33 | + "8a9a60ddd722a40753c2a38edd6b6fa38e806d681c9b08a520ba4912e62b6458", |
| 34 | + "75eb182fb623acf5e53d9885c4e8578f2530533a96c753481cc4277ecc6022de", |
| 35 | + "6c4b22b1d9d68ef7a8ecd151cd4ffdd4ecc2a7b3a3f8a9f9f9bbdbcef6671f10", |
| 36 | + "e578d66453cb41b6c923b9caa91c375a0545eeb171ccafc60b46fa834ce5c200", |
| 37 | + }, |
| 38 | + // should not be empty, otherwise TRE will panic |
| 39 | + More: []string{}, |
| 40 | +} |
| 41 | + |
| 42 | +const ( |
| 43 | + DefaultTronPort = "9090" |
| 44 | +) |
| 45 | + |
| 46 | +func defaultTron(in *Input) { |
| 47 | + if in.Image == "" { |
| 48 | + in.Image = "tronbox/tre" |
| 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 | + accountsData, err := json.Marshal(TRONAccounts) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + _, err = accounts.WriteString(string(accountsData)) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + req := testcontainers.ContainerRequest{ |
| 77 | + AlwaysPullImage: in.PullImage, |
| 78 | + Image: in.Image, |
| 79 | + Name: containerName, |
| 80 | + ExposedPorts: []string{bindPort}, |
| 81 | + Networks: []string{framework.DefaultNetworkName}, |
| 82 | + NetworkAliases: map[string][]string{ |
| 83 | + framework.DefaultNetworkName: {containerName}, |
| 84 | + }, |
| 85 | + Labels: framework.DefaultTCLabels(), |
| 86 | + HostConfigModifier: func(h *container.HostConfig) { |
| 87 | + h.PortBindings = framework.MapTheSamePort(bindPort) |
| 88 | + }, |
| 89 | + WaitingFor: wait.ForLog("Mnemonic").WithPollInterval(200 * time.Millisecond).WithStartupTimeout(1 * time.Minute), |
| 90 | + Files: []testcontainers.ContainerFile{ |
| 91 | + { |
| 92 | + HostFilePath: accounts.Name(), |
| 93 | + ContainerFilePath: "/config/accounts.json", |
| 94 | + FileMode: 0644, |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 100 | + ContainerRequest: req, |
| 101 | + Started: true, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + host, err := c.Host(ctx) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + return &Output{ |
| 113 | + UseCache: true, |
| 114 | + ChainID: in.ChainID, |
| 115 | + Family: "tron", |
| 116 | + ContainerName: containerName, |
| 117 | + Nodes: []*Node{ |
| 118 | + { |
| 119 | + HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, in.Port), |
| 120 | + DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port), |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, nil |
| 124 | +} |
0 commit comments