/
services.go
254 lines (226 loc) · 7.35 KB
/
services.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package commands
import (
"fmt"
"os"
"strings"
"gopkg.in/urfave/cli.v1"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
ethUtils "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
abcitypes "github.com/tendermint/tendermint/abci/types"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/node"
pv "github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/proxy"
"github.com/blockservice/echoin/api"
"github.com/blockservice/echoin/app"
emtUtils "github.com/blockservice/echoin/vm/cmd/utils"
"github.com/blockservice/echoin/vm/ethereum"
)
type Services struct {
backend *api.Backend
tmNode *node.Node
emNode *ethereum.Node
}
func startServices(rootDir string, storeApp *app.StoreApp) (*Services, error) {
// Setup the go-ethereum node and start it
emNode := emtUtils.MakeFullNode(context)
startNode(context, emNode)
// Fetch the registered service of this type
var backend *api.Backend
if err := emNode.Service(&backend); err != nil {
ethUtils.Fatalf("ethereum backend service not running: %v", err)
}
// In-proc RPC connection so ABCI.Query can be forwarded over the ethereum rpc
rpcClient, err := emNode.Attach()
if err != nil {
ethUtils.Fatalf("Failed to attach to the inproc geth: %v", err)
}
// Create the ABCI app
ethApp, err := app.NewEthermintApplication(backend, rpcClient, nil)
if err != nil {
log.Warn(err.Error())
os.Exit(1)
}
ethApp.SetLogger(emtUtils.EthermintLogger().With("module", "vm"))
// Create Basecoin app
basecoinApp, err := createBaseApp(rootDir, storeApp, ethApp, backend.Ethereum())
if err != nil {
log.Warn(err.Error())
os.Exit(1)
}
// Create & start tendermint node
tmNode, err := startTendermint(basecoinApp)
if err != nil {
log.Warn(err.Error())
os.Exit(1)
}
backend.SetTMNode(tmNode)
return &Services{backend, tmNode, emNode}, nil
}
// startNode copies the logic from go-ethereum
func startNode(ctx *cli.Context, stack *ethereum.Node) {
emtUtils.StartNode(stack)
// Unlock any account specifically requested
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
passwords := ethUtils.MakePasswordList(ctx)
unlocks := strings.Split(ctx.GlobalString(ethUtils.UnlockedAccountFlag.Name), ",")
for i, account := range unlocks {
if trimmed := strings.TrimSpace(account); trimmed != "" {
unlockAccount(ctx, ks, trimmed, i, passwords)
}
}
// Register wallet event handlers to open and auto-derive wallets
events := make(chan accounts.WalletEvent, 16)
stack.AccountManager().Subscribe(events)
go func() {
// Create an chain state reader for self-derivation
rpcClient, err := stack.Attach()
if err != nil {
ethUtils.Fatalf("Failed to attach to self: %v", err)
}
stateReader := ethclient.NewClient(rpcClient)
// Open and self derive any wallets already attached
for _, wallet := range stack.AccountManager().Wallets() {
if err := wallet.Open(""); err != nil {
log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err)
} else {
wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader)
}
}
// Listen for wallet event till termination
for event := range events {
if event.Kind == accounts.WalletArrived {
if err := event.Wallet.Open(""); err != nil {
log.Warn("New wallet appeared, failed to open", "url",
event.Wallet.URL(), "err", err)
} else {
status, _ := event.Wallet.Status()
log.Info("New wallet appeared", "url", event.Wallet.URL(),
"status", status)
event.Wallet.SelfDerive(accounts.DefaultBaseDerivationPath,
stateReader)
}
} else {
log.Info("Old wallet dropped", "url", event.Wallet.URL())
event.Wallet.Close()
}
}
}()
}
// tries unlocking the specified account a few times.
// nolint: unparam
func unlockAccount(ctx *cli.Context, ks *keystore.KeyStore, address string, i int,
passwords []string) (accounts.Account, string) {
account, err := ethUtils.MakeAddress(ks, address)
if err != nil {
ethUtils.Fatalf("Could not list accounts: %v", err)
}
for trials := 0; trials < 3; trials++ {
prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3)
password := getPassPhrase(prompt, false, i, passwords)
err = ks.Unlock(account, password)
if err == nil {
log.Info("Unlocked account", "address", account.Address.Hex())
return account, password
}
if err, ok := err.(*keystore.AmbiguousAddrError); ok {
log.Info("Unlocked account", "address", account.Address.Hex())
return ambiguousAddrRecovery(ks, err, password), password
}
if err != keystore.ErrDecrypt {
// No need to prompt again if the error is not decryption-related.
break
}
}
// All trials expended to unlock account, bail out
ethUtils.Fatalf("Failed to unlock account %s (%v)", address, err)
return accounts.Account{}, ""
}
// getPassPhrase retrieves the password associated with an account, either fetched
// from a list of preloaded passphrases, or requested interactively from the user.
// nolint: unparam
func getPassPhrase(prompt string, confirmation bool, i int, passwords []string) string {
// If a list of passwords was supplied, retrieve from them
if len(passwords) > 0 {
if i < len(passwords) {
return passwords[i]
}
return passwords[len(passwords)-1]
}
// Otherwise prompt the user for the password
if prompt != "" {
log.Info(prompt)
}
password, err := console.Stdin.PromptPassword("Passphrase: ")
if err != nil {
ethUtils.Fatalf("Failed to read passphrase: %v", err)
}
if confirmation {
confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ")
if err != nil {
ethUtils.Fatalf("Failed to read passphrase confirmation: %v", err)
}
if password != confirm {
ethUtils.Fatalf("Passphrases do not match")
}
}
return password
}
func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError,
auth string) accounts.Account {
fmt.Printf("Multiple key files exist for address %x:\n", err.Addr)
for _, a := range err.Matches {
fmt.Println(" ", a.URL)
}
fmt.Println("Testing your passphrase against all of them...")
var match *accounts.Account
for _, a := range err.Matches {
if err := ks.Unlock(a, auth); err == nil {
match = &a
break
}
}
if match == nil {
ethUtils.Fatalf("None of the listed files could be unlocked.")
}
fmt.Printf("Your passphrase unlocked %s\n", match.URL)
fmt.Println("In order to avoid this warning, remove the following duplicate key files:")
for _, a := range err.Matches {
if a != *match {
fmt.Println(" ", a.URL)
}
}
return *match
}
func startTendermint(basecoinApp abcitypes.Application) (*node.Node, error) {
cfg, err := tcmd.ParseConfig()
if err != nil {
return nil, err
}
var papp proxy.ClientCreator
if basecoinApp != nil {
papp = proxy.NewLocalClientCreator(basecoinApp)
} else {
papp = proxy.DefaultClientCreator(cfg.ProxyApp, cfg.ABCI, cfg.DBDir())
}
// Create & start tendermint node
n, err := node.NewNode(cfg,
pv.LoadOrGenFilePV(cfg.PrivValidatorFile()),
papp,
node.DefaultGenesisDocProviderFunc(cfg),
node.DefaultDBProvider,
node.DefaultMetricsProvider,
logger.With("module", "node"))
if err != nil {
return nil, err
}
err = n.Start()
if err != nil {
return nil, err
}
return n, nil
}