This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
integration.go
215 lines (186 loc) · 6.4 KB
/
integration.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package integration
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/validator"
"github.com/hyperledger/burrow/config"
"github.com/hyperledger/burrow/consensus/tendermint"
"github.com/hyperledger/burrow/core"
"github.com/hyperledger/burrow/execution"
"github.com/hyperledger/burrow/genesis"
"github.com/hyperledger/burrow/keys/mock"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/logconfig"
"github.com/hyperledger/burrow/permission"
"github.com/hyperledger/burrow/rpc"
"github.com/stretchr/testify/require"
)
const (
ChainName = "Integration_Test_Chain"
scratchDir = "test_scratch"
)
// Enable logger output during tests
var node uint64 = 0
func NoConsensus(conf *config.BurrowConfig) {
conf.Tendermint.Enabled = false
}
func CommitImmediately(conf *config.BurrowConfig) {
conf.Execution.TimeoutFactor = 0
}
func RunNode(t testing.TB, genesisDoc *genesis.GenesisDoc, privateAccounts []*acm.PrivateAccount,
options ...func(*config.BurrowConfig)) (kern *core.Kernel, shutdown func()) {
var err error
testConfig, cleanup := NewTestConfig(genesisDoc, options...)
// Uncomment for log output from tests
// testConfig.Logging = logconfig.New().Root(func(sink *logconfig.SinkConfig) *logconfig.SinkConfig {
// return sink.SetOutput(logconfig.StderrOutput())
// })
testConfig.Logging = logconfig.New().Root(func(sink *logconfig.SinkConfig) *logconfig.SinkConfig {
return sink.SetTransform(logconfig.FilterTransform(logconfig.IncludeWhenAllMatch,
"total_validator")).SetOutput(logconfig.StdoutOutput())
})
kern, err = TestKernel(privateAccounts[0], privateAccounts, testConfig)
require.NoError(t, err)
err = kern.Boot()
require.NoError(t, err)
// Sometimes better to not shutdown as logging errors on shutdown may obscure real issue
return kern, func() {
Shutdown(kern)
cleanup()
}
}
func NewTestConfig(genesisDoc *genesis.GenesisDoc,
options ...func(*config.BurrowConfig)) (conf *config.BurrowConfig, cleanup func()) {
nodeNumber := atomic.AddUint64(&node, 1)
name := fmt.Sprintf("node_%03d", nodeNumber)
conf = config.DefaultBurrowConfig()
testDir, cleanup := EnterTestDirectory()
conf.BurrowDir = path.Join(testDir, fmt.Sprintf(".burrow_%s", name))
conf.GenesisDoc = genesisDoc
conf.Tendermint.Moniker = name
// Make blocks for purposes of tests
conf.Tendermint.CreateEmptyBlocks = tendermint.AlwaysCreateEmptyBlocks
conf.Keys.RemoteAddress = ""
// Assign run of ports
const freeport = "0"
conf.Tendermint.ListenHost = rpc.LocalHost
conf.Tendermint.ListenPort = freeport
conf.RPC.GRPC.ListenHost = rpc.LocalHost
conf.RPC.GRPC.ListenPort = freeport
conf.RPC.Metrics.ListenHost = rpc.LocalHost
conf.RPC.Metrics.ListenPort = freeport
conf.RPC.Info.ListenHost = rpc.LocalHost
conf.RPC.Info.ListenPort = freeport
conf.RPC.Web3.ListenHost = rpc.LocalHost
conf.RPC.Web3.ListenPort = freeport
conf.Execution.TimeoutFactor = 0.5
conf.Execution.VMOptions = []execution.VMOption{}
for _, opt := range options {
if opt != nil {
opt(conf)
}
}
return conf, cleanup
}
// We use this to wrap tests
func TestKernel(validatorAccount *acm.PrivateAccount, keysAccounts []*acm.PrivateAccount,
testConfig *config.BurrowConfig) (*core.Kernel, error) {
fmt.Println("Creating integration test Kernel...")
kern, err := core.NewKernel(testConfig.BurrowDir)
if err != nil {
return nil, err
}
kern.SetLogger(logging.NewNoopLogger())
if testConfig.Logging != nil {
err := kern.LoadLoggerFromConfig(testConfig.Logging)
if err != nil {
return nil, err
}
}
kern.SetKeyClient(mock.NewKeyClient(keysAccounts...))
err = kern.LoadExecutionOptionsFromConfig(testConfig.Execution)
if err != nil {
return nil, err
}
err = kern.LoadState(testConfig.GenesisDoc)
if err != nil {
return nil, err
}
privVal := tendermint.NewPrivValidatorMemory(validatorAccount, validatorAccount)
err = kern.LoadTendermintFromConfig(testConfig, privVal)
if err != nil {
return nil, err
}
kern.AddProcesses(core.DefaultProcessLaunchers(kern, testConfig.RPC, testConfig.Keys)...)
return kern, nil
}
func EnterTestDirectory() (testDir string, cleanup func()) {
var err error
testDir, err = ioutil.TempDir("", scratchDir)
if err != nil {
panic(fmt.Errorf("could not make temp dir for integration tests: %v", err))
}
// If you need to inspectdirs
//testDir := scratchDir
os.RemoveAll(testDir)
os.MkdirAll(testDir, 0777)
os.Chdir(testDir)
os.MkdirAll("config", 0777)
return testDir, func() { os.RemoveAll(testDir) }
}
// TestGenesisDoc creates genesis from a set of accounts
// and validators from indices within that slice
func TestGenesisDoc(addressables []*acm.PrivateAccount, vals ...int) *genesis.GenesisDoc {
accounts := make(map[string]*acm.Account, len(addressables))
for i, pa := range addressables {
account := acm.FromAddressable(pa)
account.Balance += 1 << 32
account.Permissions = permission.AllAccountPermissions.Clone()
accounts[fmt.Sprintf("user_%v", i)] = account
}
genesisTime, err := time.Parse("02-01-2006", "27-10-2017")
if err != nil {
panic("could not parse test genesis time")
}
validators := make(map[string]*validator.Validator)
for _, i := range vals {
name := fmt.Sprintf("user_%d", i)
validators[name] = validator.FromAccount(accounts[name], 1<<16)
// Tendermint validators use a different addressing scheme for secp256k1
accounts[name].Address = validators[name].GetAddress()
}
return genesis.MakeGenesisDocFromAccounts(ChainName, nil, genesisTime, accounts, validators)
}
// Default deterministic account generation helper, pass number of accounts to make
func MakePrivateAccounts(sec string, n int) []*acm.PrivateAccount {
accounts := make([]*acm.PrivateAccount, n)
for i := 0; i < n; i++ {
accounts[i] = acm.GeneratePrivateAccountFromSecret(sec + strconv.Itoa(i))
}
return accounts
}
func MakeEthereumAccounts(sec string, n int) []*acm.PrivateAccount {
accounts := make([]*acm.PrivateAccount, n)
for i := 0; i < n; i++ {
accounts[i] = acm.GenerateEthereumAccountFromSecret(sec + strconv.Itoa(i))
}
return accounts
}
func Shutdown(kern *core.Kernel) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := kern.Shutdown(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error shutting down test kernel %v: %v", kern, err)
}
}