forked from cosmos/ethermint-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
179 lines (141 loc) · 4.7 KB
/
helpers_test.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
package app
import (
"crypto/ecdsa"
"encoding/json"
"errors"
"math/big"
"os"
"path/filepath"
"strings"
"testing"
"github.com/tendermint/tmlibs/log"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
ethUtils "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rlp"
"github.com/tendermint/ethermint/cmd/utils"
"github.com/tendermint/ethermint/ethereum"
)
var (
receiverAddress = common.StringToAddress("0x1234123412341234123412341234123412341234")
)
// implements: tendermint.rpc.client.HTTPClient
type MockClient struct {
SentBroadcastTx chan struct{} // fires when we call broadcast_tx_sync
}
func NewMockClient() *MockClient { return &MockClient{make(chan struct{})} }
func (mc *MockClient) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
_ = result
switch method {
case "status":
result = &ctypes.ResultStatus{}
return result, nil
case "broadcast_tx_sync":
close(mc.SentBroadcastTx)
result = &ctypes.ResultBroadcastTx{}
return result, nil
}
return nil, errors.New("Shouldn't happen.")
}
func generateKeyPair(t *testing.T) (*ecdsa.PrivateKey, common.Address) {
privateKey, err := crypto.GenerateKey()
if err != nil {
t.Errorf("Error generating a private key: %v", err)
}
address := crypto.PubkeyToAddress(privateKey.PublicKey)
return privateKey, address
}
func createTx(t *testing.T, key *ecdsa.PrivateKey, nonce uint64,
to common.Address, amount, gasLimit, gasPrice *big.Int, data []byte) *ethTypes.Transaction {
signer := ethTypes.HomesteadSigner{}
transaction, err := ethTypes.SignTx(
ethTypes.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data),
signer,
key,
)
if err != nil {
t.Errorf("Error creating the transaction: %v", err)
}
return transaction
}
func createTxBytes(t *testing.T, key *ecdsa.PrivateKey, nonce uint64,
to common.Address, amount, gasLimit, gasPrice *big.Int, data []byte) []byte {
transaction := createTx(t, key, nonce, to, amount, gasLimit, gasPrice, data)
encodedTransaction, err := rlp.EncodeToBytes(transaction)
if err != nil {
t.Errorf("Error encoding the transaction: %v", err)
}
return encodedTransaction
}
// TODO: [adrian] Change node.Node to use ethereum.Node, which is our own node
// without the networking stack. This should be held off until we decide on
// the new design.
// mimics abciEthereumAction from cmd/ethermint/main.go
func makeTestApp(tempDatadir string, addresses []common.Address,
mockClient *MockClient) (*node.Node, *ethereum.Backend, *EthermintApplication, error) {
stack, err := makeTestSystemNode(tempDatadir, addresses, mockClient)
if err != nil {
return nil, nil, nil, err
}
ethUtils.StartNode(stack)
var backend *ethereum.Backend
if err = stack.Service(&backend); err != nil {
return nil, nil, nil, err
}
app, err := NewEthermintApplication(backend, nil, nil)
app.SetLogger(log.TestingLogger())
return stack, backend, app, err
}
// mimics MakeSystemNode from ethereum/node.go
func makeTestSystemNode(tempDatadir string, addresses []common.Address,
mockClient *MockClient) (*node.Node, error) {
// Configure the node's service container
nodeConf := utils.DefaultNodeConfig()
utils.SetEthermintNodeConfig(&nodeConf)
nodeConf.DataDir = tempDatadir
// Configure the Ethereum service
ethConf := eth.DefaultConfig
utils.SetEthermintEthConfig(ðConf)
genesis, err := makeTestGenesis(addresses)
if err != nil {
return nil, err
}
ethConf.Genesis = genesis
// Assemble and return the protocol stack
stack, err := node.New(&nodeConf)
if err != nil {
return nil, err
}
return stack, stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
return ethereum.NewBackend(ctx, ðConf, mockClient)
})
}
func makeTestGenesis(addresses []common.Address) (*core.Genesis, error) {
currentDir, err := os.Getwd()
if err != nil {
return nil, err
}
genesisPath := filepath.Join(strings.TrimSuffix(currentDir, "/app"), "setup/genesis.json")
file, err := os.Open(genesisPath)
if err != nil {
return nil, err
}
defer file.Close() // nolint: errcheck
genesis := new(core.Genesis)
if err := json.NewDecoder(file).Decode(genesis); err != nil {
ethUtils.Fatalf("invalid genesis file: %v", err)
}
balance, result := new(big.Int).SetString("10000000000000000000000000000000000", 10)
if !result {
return nil, errors.New("BigInt conversion error")
}
for _, addr := range addresses {
genesis.Alloc[addr] = core.GenesisAccount{Balance: balance}
}
return genesis, nil
}