forked from hyperledger-archives/burrow
/
routes.go
264 lines (238 loc) · 8.79 KB
/
routes.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
255
256
257
258
259
260
261
262
263
264
// 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 core
import (
"fmt"
acm "github.com/hyperledger/burrow/account"
"github.com/hyperledger/burrow/definitions"
ctypes "github.com/hyperledger/burrow/rpc/tendermint/core/types"
"github.com/hyperledger/burrow/txs"
rpc "github.com/tendermint/go-rpc/server"
rpctypes "github.com/tendermint/go-rpc/types"
)
// TODO: [ben] encapsulate Routes into a struct for a given TendermintPipe
// Magic! Should probably be configurable, but not shouldn't be so huge we
// end up DoSing ourselves.
const maxBlockLookback = 20
// TODO: eliminate redundancy between here and reading code from core/
type TendermintRoutes struct {
tendermintPipe definitions.TendermintPipe
}
func (tmRoutes *TendermintRoutes) GetRoutes() map[string]*rpc.RPCFunc {
var routes = map[string]*rpc.RPCFunc{
"subscribe": rpc.NewWSRPCFunc(tmRoutes.Subscribe, "eventId"),
"unsubscribe": rpc.NewWSRPCFunc(tmRoutes.Unsubscribe, "subscriptionId"),
"status": rpc.NewRPCFunc(tmRoutes.StatusResult, ""),
"net_info": rpc.NewRPCFunc(tmRoutes.NetInfoResult, ""),
"genesis": rpc.NewRPCFunc(tmRoutes.GenesisResult, ""),
"chain_id": rpc.NewRPCFunc(tmRoutes.ChainIdResult, ""),
"get_account": rpc.NewRPCFunc(tmRoutes.GetAccountResult, "address"),
"get_storage": rpc.NewRPCFunc(tmRoutes.GetStorageResult, "address,key"),
"call": rpc.NewRPCFunc(tmRoutes.CallResult, "fromAddress,toAddress,data"),
"call_code": rpc.NewRPCFunc(tmRoutes.CallCodeResult, "fromAddress,code,data"),
"dump_storage": rpc.NewRPCFunc(tmRoutes.DumpStorageResult, "address"),
"list_accounts": rpc.NewRPCFunc(tmRoutes.ListAccountsResult, ""),
"get_name": rpc.NewRPCFunc(tmRoutes.GetNameResult, "name"),
"list_names": rpc.NewRPCFunc(tmRoutes.ListNamesResult, ""),
"broadcast_tx": rpc.NewRPCFunc(tmRoutes.BroadcastTxResult, "tx"),
"blockchain": rpc.NewRPCFunc(tmRoutes.BlockchainInfo, "minHeight,maxHeight"),
"get_block": rpc.NewRPCFunc(tmRoutes.GetBlock, "height"),
"list_unconfirmed_txs": rpc.NewRPCFunc(tmRoutes.ListUnconfirmedTxs, ""),
"list_validators": rpc.NewRPCFunc(tmRoutes.ListValidators, ""),
"dump_consensus_state": rpc.NewRPCFunc(tmRoutes.DumpConsensusState, ""),
"unsafe/gen_priv_account": rpc.NewRPCFunc(tmRoutes.GenPrivAccountResult, ""),
"unsafe/sign_tx": rpc.NewRPCFunc(tmRoutes.SignTxResult, "tx,privAccounts"),
// TODO: [Silas] do we also carry forward "consensus_state" as in v0?
}
return routes
}
func (tmRoutes *TendermintRoutes) Subscribe(wsCtx rpctypes.WSRPCContext,
eventId string) (ctypes.BurrowResult, error) {
// NOTE: RPCResponses of subscribed events have id suffix "#event"
// TODO: we really ought to allow multiple subscriptions from the same client address
// to the same event. The code as it stands reflects the somewhat broken tendermint
// implementation. We can use GenerateSubId to randomize the subscriptions id
// and return it in the result. This would require clients to hang on to a
// subscription id if they wish to unsubscribe, but then again they can just
// drop their connection
result, err := tmRoutes.tendermintPipe.Subscribe(eventId,
func(result ctypes.BurrowResult) {
wsCtx.GetRemoteAddr()
// NOTE: EventSwitch callbacks must be nonblocking
wsCtx.TryWriteRPCResponse(
rpctypes.NewRPCResponse(wsCtx.Request.ID+"#event", &result, ""))
})
if err != nil {
return nil, err
} else {
return result, nil
}
}
func (tmRoutes *TendermintRoutes) Unsubscribe(wsCtx rpctypes.WSRPCContext,
subscriptionId string) (ctypes.BurrowResult, error) {
result, err := tmRoutes.tendermintPipe.Unsubscribe(subscriptionId)
if err != nil {
return nil, err
} else {
return result, nil
}
}
func (tmRoutes *TendermintRoutes) StatusResult() (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.Status(); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) NetInfoResult() (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.NetInfo(); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) GenesisResult() (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.Genesis(); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) ChainIdResult() (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.ChainId(); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) GetAccountResult(address []byte) (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.GetAccount(address); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) GetStorageResult(address, key []byte) (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.GetStorage(address, key); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) CallResult(fromAddress, toAddress,
data []byte) (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.Call(fromAddress, toAddress, data); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) CallCodeResult(fromAddress, code,
data []byte) (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.CallCode(fromAddress, code, data); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) DumpStorageResult(address []byte) (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.DumpStorage(address); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) ListAccountsResult() (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.ListAccounts(); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) GetNameResult(name string) (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.GetName(name); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) ListNamesResult() (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.ListNames(); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) GenPrivAccountResult() (ctypes.BurrowResult, error) {
//if r, err := tmRoutes.tendermintPipe.GenPrivAccount(); err != nil {
// return nil, err
//} else {
// return r, nil
//}
return nil, fmt.Errorf("Unimplemented as poor practice to generate private account over unencrypted RPC")
}
func (tmRoutes *TendermintRoutes) SignTxResult(tx txs.Tx,
privAccounts []*acm.PrivAccount) (ctypes.BurrowResult, error) {
// if r, err := tmRoutes.tendermintPipe.SignTx(tx, privAccounts); err != nil {
// return nil, err
// } else {
// return r, nil
// }
return nil, fmt.Errorf("Unimplemented as poor practice to pass private account over unencrypted RPC")
}
func (tmRoutes *TendermintRoutes) BroadcastTxResult(tx txs.Tx) (ctypes.BurrowResult, error) {
if r, err := tmRoutes.tendermintPipe.BroadcastTxSync(tx); err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) BlockchainInfo(minHeight,
maxHeight int) (ctypes.BurrowResult, error) {
r, err := tmRoutes.tendermintPipe.BlockchainInfo(minHeight, maxHeight,
maxBlockLookback)
if err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) ListUnconfirmedTxs() (ctypes.BurrowResult, error) {
// Get all Txs for now
r, err := tmRoutes.tendermintPipe.ListUnconfirmedTxs(-1)
if err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) GetBlock(height int) (ctypes.BurrowResult, error) {
r, err := tmRoutes.tendermintPipe.GetBlock(height)
if err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) ListValidators() (ctypes.BurrowResult, error) {
r, err := tmRoutes.tendermintPipe.ListValidators()
if err != nil {
return nil, err
} else {
return r, nil
}
}
func (tmRoutes *TendermintRoutes) DumpConsensusState() (ctypes.BurrowResult, error) {
return tmRoutes.tendermintPipe.DumpConsensusState()
}