Newer
Older
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// 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 rpc
import (
"fmt"
acm "github.com/hyperledger/burrow/account"
"github.com/hyperledger/burrow/binary"
bcm "github.com/hyperledger/burrow/blockchain"
"github.com/hyperledger/burrow/consensus/tendermint/query"
"github.com/hyperledger/burrow/event"
"github.com/hyperledger/burrow/execution"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
logging_types "github.com/hyperledger/burrow/logging/types"
"github.com/hyperledger/burrow/txs"
tm_types "github.com/tendermint/tendermint/types"
)
// Magic! Should probably be configurable, but not shouldn't be so huge we
// end up DoSing ourselves.
const MaxBlockLookback = 100
// Base service that provides implementation for all underlying RPC methods
type Service interface {
// Transact
BroadcastTx(tx txs.Tx) (*ResultBroadcastTx, error)
// Events
Subscribe(eventId string, callback func(eventData event.AnyEventData)) (*ResultSubscribe, error)
Unsubscribe(subscriptionId string) (*ResultUnsubscribe, error)
// Status
Status() (*ResultStatus, error)
NetInfo() (*ResultNetInfo, error)
// Accounts
GetAccount(address acm.Address) (*ResultGetAccount, error)
ListAccounts() (*ResultListAccounts, error)
GetStorage(address acm.Address, key []byte) (*ResultGetStorage, error)
DumpStorage(address acm.Address) (*ResultDumpStorage, error)
// Simulated call
Call(fromAddress, toAddress acm.Address, data []byte) (*ResultCall, error)
CallCode(fromAddress acm.Address, code, data []byte) (*ResultCall, error)
// Blockchain
Genesis() (*ResultGenesis, error)
ChainId() (*ResultChainId, error)
BlockchainInfo(minHeight, maxHeight uint64) (*ResultBlockchainInfo, error)
GetBlock(height uint64) (*ResultGetBlock, error)
// Consensus
ListUnconfirmedTxs(maxTxs int) (*ResultListUnconfirmedTxs, error)
ListValidators() (*ResultListValidators, error)
DumpConsensusState() (*ResultDumpConsensusState, error)
Peers() (*ResultPeers, error)
// Names
GetName(name string) (*ResultGetName, error)
ListNames() (*ResultListNames, error)
// Private keys and signing
SignTx(tx txs.Tx, concretePrivateAccounts []*acm.ConcretePrivateAccount) (*ResultSignTx, error)
GeneratePrivateAccount() (*ResultGeneratePrivateAccount, error)
}
type service struct {
state acm.StateIterable
eventEmitter event.Emitter
nameReg execution.NameRegIterable
blockchain bcm.Blockchain
transactor execution.Transactor
nodeView query.NodeView
logger logging_types.InfoTraceLogger
}
var _ Service = &service{}
func NewService(state acm.StateIterable, eventEmitter event.Emitter, nameReg execution.NameRegIterable,
blockchain bcm.Blockchain, transactor execution.Transactor, nodeView query.NodeView,
logger logging_types.InfoTraceLogger) *service {
return &service{
state: state,
eventEmitter: eventEmitter,
nameReg: nameReg,
blockchain: blockchain,
transactor: transactor,
nodeView: nodeView,
logger: logger.With(structure.ComponentKey, "Service"),
}
}
// All methods in this file return (Result*, error) which is the return
// signature assumed by go-rpc
func (s *service) Subscribe(eventId string, callback func(event.AnyEventData)) (*ResultSubscribe, error) {
subscriptionId, err := event.GenerateSubId()
logging.InfoMsg(s.logger, "Subscribing to event",
"eventId", eventId, "subscriptionId", subscriptionId)
if err != nil {
return nil, err
}
err = s.eventEmitter.Subscribe(subscriptionId, eventId, callback)
if err != nil {
return nil, err
}
return &ResultSubscribe{
SubscriptionId: subscriptionId,
Event: eventId,
}, nil
}
func (s *service) Unsubscribe(subscriptionId string) (*ResultUnsubscribe, error) {
err := s.eventEmitter.Unsubscribe(subscriptionId)
if err != nil {
return nil, err
} else {
return &ResultUnsubscribe{SubscriptionId: subscriptionId}, nil
}
}
func (s *service) Status() (*ResultStatus, error) {
tip := s.blockchain.Tip()
latestHeight := tip.LastBlockHeight()
var (
latestBlockMeta *tm_types.BlockMeta
latestBlockHash []byte
latestBlockTime int64
)
if latestHeight != 0 {
latestBlockMeta = s.nodeView.BlockStore().LoadBlockMeta(int(latestHeight))
latestBlockHash = latestBlockMeta.Header.Hash()
latestBlockTime = latestBlockMeta.Header.Time.UnixNano()
}
return &ResultStatus{
NodeInfo: s.nodeView.NodeInfo(),
GenesisHash: s.blockchain.GenesisHash(),
PubKey: s.nodeView.PrivValidatorPublicKey(),
LatestBlockHash: latestBlockHash,
LatestBlockHeight: latestHeight,
LatestBlockTime: latestBlockTime}, nil
}
func (s *service) ChainId() (*ResultChainId, error) {
return &ResultChainId{
ChainName: s.blockchain.GenesisDoc().ChainName,
ChainId: s.blockchain.ChainID(),
GenesisHash: s.blockchain.GenesisHash(),
}, nil
}
func (s *service) Peers() (*ResultPeers, error) {
peers := make([]*Peer, s.nodeView.Peers().Size())
for i, peer := range s.nodeView.Peers().List() {
peers[i] = &Peer{
NodeInfo: peer.NodeInfo(),
IsOutbound: peer.IsOutbound(),
}
}
return &ResultPeers{
Peers: peers,
}, nil
}
func (s *service) NetInfo() (*ResultNetInfo, error) {
listening := s.nodeView.IsListening()
listeners := []string{}
for _, listener := range s.nodeView.Listeners() {
listeners = append(listeners, listener.String())
}
peers, err := s.Peers()
if err != nil {
return nil, err
}
return &ResultNetInfo{
Listening: listening,
Listeners: listeners,
Peers: peers.Peers,
}, nil
}
func (s *service) Genesis() (*ResultGenesis, error) {
return &ResultGenesis{
Genesis: s.blockchain.GenesisDoc(),
}, nil
}
// Accounts
func (s *service) GetAccount(address acm.Address) (*ResultGetAccount, error) {
acc, err := s.state.GetAccount(address)
if err != nil {
return nil, err
}
return &ResultGetAccount{Account: acm.AsConcreteAccount(acc)}, nil
}
func (s *service) ListAccounts() (*ResultListAccounts, error) {
accounts := make([]*acm.ConcreteAccount, 0)
s.state.IterateAccounts(func(account acm.Account) (stop bool) {
accounts = append(accounts, acm.AsConcreteAccount(account))
return
})
return &ResultListAccounts{
BlockHeight: s.blockchain.Tip().LastBlockHeight(),
Accounts: accounts,
}, nil
}
func (s *service) GetStorage(address acm.Address, key []byte) (*ResultGetStorage, error) {
account, err := s.state.GetAccount(address)
if err != nil {
return nil, err
}
if account == nil {
return nil, fmt.Errorf("UnknownAddress: %s", address)
}
value, err := s.state.GetStorage(address, binary.LeftPadWord256(key))
if err != nil {
return nil, err
}
if value == binary.Zero256 {
return &ResultGetStorage{Key: key, Value: nil}, nil
}
return &ResultGetStorage{Key: key, Value: value.UnpadLeft()}, nil
}
func (s *service) DumpStorage(address acm.Address) (*ResultDumpStorage, error) {
account, err := s.state.GetAccount(address)
if err != nil {
return nil, err
}
if account == nil {
return nil, fmt.Errorf("UnknownAddress: %X", address)
}
storageItems := []StorageItem{}
s.state.IterateStorage(address, func(key, value binary.Word256) (stop bool) {
storageItems = append(storageItems, StorageItem{Key: key.UnpadLeft(), Value: value.UnpadLeft()})
return
})
return &ResultDumpStorage{
StorageRoot: account.StorageRoot(),
StorageItems: storageItems,
}, nil
}
func (s *service) Call(fromAddress, toAddress acm.Address, data []byte) (*ResultCall, error) {
call, err := s.transactor.Call(fromAddress, toAddress, data)
if err != nil {
return nil, err
}
return &ResultCall{
Call: call,
}, nil
}
func (s *service) CallCode(fromAddress acm.Address, code, data []byte) (*ResultCall, error) {
call, err := s.transactor.CallCode(fromAddress, code, data)
if err != nil {
return nil, err
}
return &ResultCall{
Call: call,
}, nil
}
// Name registry
func (s *service) GetName(name string) (*ResultGetName, error) {
entry := s.nameReg.GetNameRegEntry(name)
if entry == nil {
return nil, fmt.Errorf("name %s not found", name)
}
return &ResultGetName{Entry: entry}, nil
}
func (s *service) ListNames() (*ResultListNames, error) {
var names []*execution.NameRegEntry
s.nameReg.IterateNameRegEntries(func(entry *execution.NameRegEntry) (stop bool) {
names = append(names, entry)
return false
})
return &ResultListNames{
BlockHeight: s.blockchain.Tip().LastBlockHeight(),
Names: names,
}, nil
}
func (s *service) BroadcastTx(tx txs.Tx) (*ResultBroadcastTx, error) {
receipt, err := s.transactor.BroadcastTx(tx)
if err != nil {
return nil, err
}
return &ResultBroadcastTx{
Receipt: receipt,
}, nil
}
func (s *service) ListUnconfirmedTxs(maxTxs int) (*ResultListUnconfirmedTxs, error) {
// Get all transactions for now
transactions, err := s.nodeView.MempoolTransactions(maxTxs)
if err != nil {
return nil, err
}
wrappedTxs := make([]txs.Wrapper, len(transactions))
for i, tx := range transactions {
wrappedTxs[i] = txs.Wrap(tx)
}
return &ResultListUnconfirmedTxs{
N: len(transactions),
Txs: wrappedTxs,
}, nil
}
// Returns the current blockchain height and metadata for a range of blocks
// between minHeight and maxHeight. Only returns maxBlockLookback block metadata
// from the top of the range of blocks.
// Passing 0 for maxHeight sets the upper height of the range to the current
// blockchain height.
func (s *service) BlockchainInfo(minHeight, maxHeight uint64) (*ResultBlockchainInfo, error) {
latestHeight := s.blockchain.Tip().LastBlockHeight()
if minHeight == 0 {
minHeight = 1
}
if maxHeight == 0 || latestHeight < maxHeight {
maxHeight = latestHeight
}
if maxHeight > minHeight && maxHeight-minHeight > MaxBlockLookback {
minHeight = maxHeight - MaxBlockLookback
}
blockMetas := []*tm_types.BlockMeta{}
for height := maxHeight; height >= minHeight; height-- {
blockMeta := s.nodeView.BlockStore().LoadBlockMeta(int(height))
blockMetas = append(blockMetas, blockMeta)
}
return &ResultBlockchainInfo{
LastHeight: latestHeight,
BlockMetas: blockMetas,
}, nil
}
func (s *service) GetBlock(height uint64) (*ResultGetBlock, error) {
return &ResultGetBlock{
Block: s.nodeView.BlockStore().LoadBlock(int(height)),
BlockMeta: s.nodeView.BlockStore().LoadBlockMeta(int(height)),
}, nil
}
func (s *service) ListValidators() (*ResultListValidators, error) {
// TODO: when we reintroduce support for bonding and unbonding update this
// to reflect the mutable bonding state
validators := s.blockchain.Validators()
concreteValidators := make([]*acm.ConcreteValidator, len(validators))
for i, validator := range validators {
concreteValidators[i] = acm.AsConcreteValidator(validator)
}
return &ResultListValidators{
BlockHeight: s.blockchain.Tip().LastBlockHeight(),
BondedValidators: concreteValidators,
UnbondingValidators: nil,
}, nil
}
func (s *service) DumpConsensusState() (*ResultDumpConsensusState, error) {
peerRoundState, err := s.nodeView.PeerRoundStates()
if err != nil {
return nil, err
}
return &ResultDumpConsensusState{
RoundState: s.nodeView.RoundState(),
PeerRoundStates: peerRoundState,
}, nil
}
// TODO: Either deprecate this or ensure it can only happen over secure transport
func (s *service) SignTx(tx txs.Tx, concretePrivateAccounts []*acm.ConcretePrivateAccount) (*ResultSignTx, error) {
privateAccounts := make([]acm.PrivateAccount, len(concretePrivateAccounts))
for i, cpa := range concretePrivateAccounts {
privateAccounts[i] = cpa.PrivateAccount()
}
tx, err := s.transactor.SignTx(tx, privateAccounts)
return &ResultSignTx{Tx: txs.Wrap(tx)}, err
}
func (s *service) GeneratePrivateAccount() (*ResultGeneratePrivateAccount, error) {
privateAccount, err := acm.GeneratePrivateAccount()
if err != nil {
return nil, err
}
return &ResultGeneratePrivateAccount{
PrivAccount: acm.AsConcretePrivateAccount(privateAccount),