/
test_helper.go
131 lines (121 loc) · 4.95 KB
/
test_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package svc
import (
"context"
"crypto/ecdsa"
"fmt"
"ftm-gas-monetization/internal/config"
"ftm-gas-monetization/internal/logger"
"ftm-gas-monetization/internal/repository/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
client "github.com/ethereum/go-ethereum/ethclient"
"github.com/testcontainers/testcontainers-go/wait"
"strconv"
"time"
"github.com/testcontainers/testcontainers-go"
)
const (
TestChainId = 1337
TestChainGasLimit = 10000000000
TestChainGasPrice = 6721975
)
type testAccount struct {
Address common.Address
PrivateKey *ecdsa.PrivateKey
}
type TestChain struct {
RawRpc *client.Client
AdminAcc *testAccount
FunderAcc *testAccount
ProjectsManagerAcc *testAccount
ProjectOwnerAcc *testAccount
DataProviderAcc *testAccount
container testcontainers.Container
port string
}
func SetupTestChain(logger *logger.AppLogger) *TestChain {
// setup db container
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
container, port, err := createContainer(ctx, logger)
if err != nil {
logger.Fatal("failed to setup test", err)
}
c, err := client.Dial(fmt.Sprintf("http://localhost:%s", port))
if err != nil {
logger.Fatal("failed to connect to ganache", err)
return nil
}
return &TestChain{
container: container,
RawRpc: c,
AdminAcc: initializeTestAccount("bb39aa88008bc6260ff9ebc816178c47a01c44efe55810ea1f271c00f5878812"),
FunderAcc: initializeTestAccount("29c8b4ff78e41dafd561f5cd4a90103faf20a5b509a4b6281947b8fcdcfa8f71"),
ProjectsManagerAcc: initializeTestAccount("460503be96e3b97c2d6fb737bef83d89df42e4a36adef2e8fb4f0976b70d1b2a"),
ProjectOwnerAcc: initializeTestAccount("1516a467486cd4340e5f0e8193eea05c9106bb0dce26a03047580c25c9191f93"),
DataProviderAcc: initializeTestAccount("904d5dea0bdffb09d78a81c15f0b3b893f504679eb8cd1de585309cad58e6285"),
port: port,
}
}
// SetupTestRpc initializes rpc client
func (tch *TestChain) SetupTestRpc(contractAddress common.Address, logger *logger.AppLogger) *rpc.Rpc {
rpcCfg := &config.Rpc{
OperaRpcUrl: fmt.Sprintf("http://localhost:%s", tch.port),
}
gmCfg := &config.GasMonetization{
ContractAddress: contractAddress.String(),
DataProviderPK: "904d5dea0bdffb09d78a81c15f0b3b893f504679eb8cd1de585309cad58e6285",
StartFromBlock: 0,
}
return rpc.New(rpcCfg, gmCfg, logger)
}
func (tch *TestChain) TearDown() {
tch.RawRpc.Close()
_ = tch.container.Terminate(context.Background())
}
// createContainer creates a test container for postgres database
func createContainer(ctx context.Context, logger *logger.AppLogger) (testcontainers.Container, string, error) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "trufflesuite/ganache-cli:v6.12.2",
ExposedPorts: []string{"8545/tcp"},
Env: nil,
WaitingFor: wait.ForListeningPort("8545/tcp"),
Cmd: []string{
"--chainId", strconv.Itoa(TestChainId),
"--gasLimit", strconv.Itoa(TestChainGasLimit),
"--gasPrice", "6721975",
"--account", "0xbb39aa88008bc6260ff9ebc816178c47a01c44efe55810ea1f271c00f5878812,100000000000000000000",
"--account", "0x29c8b4ff78e41dafd561f5cd4a90103faf20a5b509a4b6281947b8fcdcfa8f71,100000000000000000000",
"--account", "0x460503be96e3b97c2d6fb737bef83d89df42e4a36adef2e8fb4f0976b70d1b2a,100000000000000000000",
"--account", "0x1516a467486cd4340e5f0e8193eea05c9106bb0dce26a03047580c25c9191f93,100000000000000000000",
"--account", "0x904d5dea0bdffb09d78a81c15f0b3b893f504679eb8cd1de585309cad58e6285,100000000000000000000",
"--account", "0x41cc3b5e94e73b9135ff13a62d885f6ddb996a29b70a35eb22cf665494d224c3,100000000000000000000",
"--account", "0x8aa6aebfb06fe75b9ccd82a9c8f0eaf5b0201c7bff1b67225be881e659149434,100000000000000000000",
"--account", "0x15364f287dfff78d5cb13c103bc47b27170f2b857c6a301fe644ddbe730ef94c,100000000000000000000",
"--account", "0x2af7b50b4cb680afb9d312e41038008133ab55339136e355ddc54c361afdb40b,100000000000000000000",
"--account", "0x118f2ba03f3d9be6e810fd129b009a29deb419cc3614ade7f251e4a42ad79378,100000000000000000000",
},
},
Started: true,
}
container, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
return container, "", fmt.Errorf("failed to start container: %v", err)
}
p, err := container.MappedPort(ctx, "8545/tcp")
if err != nil {
return container, "", fmt.Errorf("failed to get container external port: %v", err)
}
logger.Infof("ganache container ready and running at port: ", p.Port())
// wait for the chain to be ready
time.Sleep(time.Second)
return container, p.Port(), nil
}
// initializeTestAccount initializes a test account
func initializeTestAccount(privateKey string) *testAccount {
acc := &testAccount{}
acc.PrivateKey, _ = crypto.HexToECDSA(privateKey)
acc.Address = crypto.PubkeyToAddress(acc.PrivateKey.PublicKey)
return acc
}