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
177 lines (155 loc) · 5.47 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
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"context"
"encoding/binary"
"fmt"
"os"
"runtime"
"strconv"
"sync/atomic"
"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/execution/evm"
"github.com/hyperledger/burrow/execution/evm/sha3"
"github.com/hyperledger/burrow/genesis"
"github.com/hyperledger/burrow/keys/mock"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/lifecycle"
lConfig "github.com/hyperledger/burrow/logging/logconfig"
"github.com/hyperledger/burrow/permission"
)
const (
ChainName = "Integration_Test_Chain"
testDir = "./test_scratch/tm_test"
)
// Enable logger output during tests
// Starting point for assigning range of ports for tests
// Start at unprivileged port (hoping for the best)
const startingPort uint16 = 1024
// For each port claimant assign a bucket
const startingPortSeparation uint16 = 10
const startingPortBuckets = 1000
// Mutable port to assign to next claimant
var port = uint32(startingPort)
var node uint64 = 0
// We use this to wrap tests
func TestKernel(validatorAccount *acm.PrivateAccount, keysAccounts []*acm.PrivateAccount,
testConfig *config.BurrowConfig, loggingConfig *lConfig.LoggingConfig) *core.Kernel {
fmt.Println("Creating integration test Kernel...")
logger := logging.NewNoopLogger()
if loggingConfig != nil {
var err error
// Change config as needed
logger, err = lifecycle.NewLoggerFromLoggingConfig(loggingConfig)
if err != nil {
panic(err)
}
}
privValidator := tendermint.NewPrivValidatorMemory(validatorAccount, validatorAccount)
keyClient := mock.NewKeyClient(keysAccounts...)
kernel, err := core.NewKernel(context.Background(), keyClient, privValidator,
testConfig.GenesisDoc,
testConfig.Tendermint.TendermintConfig(),
testConfig.RPC,
testConfig.Keys,
nil,
[]execution.ExecutionOption{execution.VMOptions(evm.DebugOpcodes)},
testConfig.Tendermint.DefaultAuthorizedPeersProvider(),
logger)
if err != nil {
panic(err)
}
return kernel
}
func EnterTestDirectory() (cleanup func()) {
os.RemoveAll(testDir)
os.MkdirAll(testDir, 0777)
os.Chdir(testDir)
os.MkdirAll("config", 0777)
return func() { os.RemoveAll(testDir) }
}
func TestGenesisDoc(addressables []*acm.PrivateAccount) *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")
}
return genesis.MakeGenesisDocFromAccounts(ChainName, nil, genesisTime, accounts,
map[string]validator.Validator{
"genesis_validator": validator.FromAccount(accounts["user_0"], 1<<16),
})
}
// Deterministic account generation helper. Pass number of accounts to make
func MakePrivateAccounts(n int) []*acm.PrivateAccount {
accounts := make([]*acm.PrivateAccount, n)
for i := 0; i < n; i++ {
accounts[i] = acm.GeneratePrivateAccountFromSecret("mysecret" + strconv.Itoa(i))
}
return accounts
}
// Some helpers for setting Burrow's various ports in non-colliding ranges for tests
func ClaimPorts() uint16 {
_, file, _, _ := runtime.Caller(1)
startIndex := uint16(binary.LittleEndian.Uint16(sha3.Sha3([]byte(file)))) % startingPortBuckets
newPort := startingPort + startIndex*startingPortSeparation
// In case overflow
if newPort < startingPort {
newPort += startingPort
}
if !atomic.CompareAndSwapUint32(&port, uint32(startingPort), uint32(newPort)) {
panic("GetPort() called before ClaimPorts() or ClaimPorts() called twice")
}
return uint16(atomic.LoadUint32(&port))
}
func GetPort() uint16 {
return uint16(atomic.AddUint32(&port, 1))
}
// Gets an name based on an incrementing counter for running multiple nodes
func GetName() string {
nodeNumber := atomic.AddUint64(&node, 1)
return fmt.Sprintf("node_%03d", nodeNumber)
}
func GetLocalAddress() string {
return fmt.Sprintf("127.0.0.1:%v", GetPort())
}
func GetTCPLocalAddress() string {
return fmt.Sprintf("tcp://127.0.0.1:%v", GetPort())
}
func NewTestConfig(genesisDoc *genesis.GenesisDoc) *config.BurrowConfig {
name := GetName()
cnf := config.DefaultBurrowConfig()
cnf.GenesisDoc = genesisDoc
cnf.Tendermint.Moniker = name
cnf.Tendermint.TendermintRoot = fmt.Sprintf(".burrow_%s", name)
cnf.Tendermint.ListenAddress = GetTCPLocalAddress()
cnf.Tendermint.ExternalAddress = cnf.Tendermint.ListenAddress
cnf.RPC.GRPC.ListenAddress = GetLocalAddress()
cnf.RPC.Metrics.ListenAddress = GetTCPLocalAddress()
cnf.RPC.Info.ListenAddress = GetTCPLocalAddress()
cnf.Keys.RemoteAddress = ""
return cnf
}