Newer
Older
package erisdb
import (
"crypto/rand"
"encoding/hex"
"fmt"
ep "github.com/eris-ltd/eris-db/erisdb/pipe"
rpc "github.com/eris-ltd/eris-db/rpc"
)
const (
// string used in subscriber ids.
ID = "socketrpc"
SERVICE_NAME = "erisdb"
GET_ACCOUNT = SERVICE_NAME + ".getAccount"
GET_STORAGE = SERVICE_NAME + ".getStorage"
GET_STORAGE_AT = SERVICE_NAME + ".getStorageAt"
GEN_PRIV_ACCOUNT = SERVICE_NAME + ".genPrivAccount"
GEN_PRIV_ACCOUNT_FROM_KEY = SERVICE_NAME + ".genPrivAccountFromKey"
GET_BLOCKCHAIN_INFO = SERVICE_NAME + ".getBlockchainInfo" // Blockchain
GET_GENESIS_HASH = SERVICE_NAME + ".getGenesisHash"
GET_LATEST_BLOCK_HEIGHT = SERVICE_NAME + ".getLatestBlockHeight"
GET_LATEST_BLOCK = SERVICE_NAME + ".getLatestBlock"
GET_BLOCKS = SERVICE_NAME + ".getBlocks"
GET_BLOCK = SERVICE_NAME + ".getBlock"
GET_CONSENSUS_STATE = SERVICE_NAME + ".getConsensusState" // Consensus
GET_VALIDATORS = SERVICE_NAME + ".getValidators"
GET_NETWORK_INFO = SERVICE_NAME + ".getNetworkInfo" // Net
GET_CLIENT_VERSION = SERVICE_NAME + ".getClientVersion"
GET_MONIKER = SERVICE_NAME + ".getMoniker"
GET_CHAIN_ID = SERVICE_NAME + ".getChainId"
IS_LISTENING = SERVICE_NAME + ".isListening"
GET_LISTENERS = SERVICE_NAME + ".getListeners"
GET_PEERS = SERVICE_NAME + ".getPeers"
GET_PEER = SERVICE_NAME + ".getPeer"
CALL = SERVICE_NAME + ".call" // Tx
CALL_CODE = SERVICE_NAME + ".callCode"
BROADCAST_TX = SERVICE_NAME + ".broadcastTx"
GET_UNCONFIRMED_TXS = SERVICE_NAME + ".getUnconfirmedTxs"
SIGN_TX = SERVICE_NAME + ".signTx"
TRANSACT = SERVICE_NAME + ".transact"
EVENT_SUBSCRIBE = SERVICE_NAME + ".eventSubscribe" // Events
EVENT_UNSUBSCRIBE = SERVICE_NAME + ".eventUnsubscribe"
EVENT_POLL = SERVICE_NAME + ".eventPoll"
type ErisDbMethods struct {
codec rpc.Codec
pipe ep.Pipe
}
// Used to handle requests. interface{} param is a wildcard used for example with socket events.
type RequestHandlerFunc func(*rpc.RPCRequest, interface{}) (interface{}, int, error)
// Private. Create a method name -> method handler map.
func (this *ErisDbMethods) getMethods() map[string]RequestHandlerFunc {
dhMap := make(map[string]RequestHandlerFunc)
// Accounts
dhMap[GET_ACCOUNTS] = this.AccountList
dhMap[GET_ACCOUNT] = this.Account
dhMap[GET_STORAGE] = this.AccountStorage
dhMap[GET_STORAGE_AT] = this.AccountStorageAt
dhMap[GEN_PRIV_ACCOUNT] = this.GenPrivAccount
dhMap[GEN_PRIV_ACCOUNT_FROM_KEY] = this.GenPrivAccountFromKey
// Blockchain
dhMap[GET_BLOCKCHAIN_INFO] = this.BlockchainInfo
dhMap[GET_GENESIS_HASH] = this.GenesisHash
dhMap[GET_LATEST_BLOCK_HEIGHT] = this.LatestBlockHeight
dhMap[GET_LATEST_BLOCK] = this.LatestBlock
dhMap[GET_BLOCKS] = this.Blocks
dhMap[GET_BLOCK] = this.Block
// Consensus
dhMap[GET_CONSENSUS_STATE] = this.ConsensusState
dhMap[GET_VALIDATORS] = this.Validators
// Network
dhMap[GET_NETWORK_INFO] = this.NetworkInfo
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
dhMap[GET_MONIKER] = this.Moniker
dhMap[GET_CHAIN_ID] = this.ChainId
dhMap[IS_LISTENING] = this.Listening
dhMap[GET_LISTENERS] = this.Listeners
dhMap[GET_PEERS] = this.Peers
dhMap[GET_PEER] = this.Peer
// Txs
dhMap[CALL] = this.Call
dhMap[CALL_CODE] = this.CallCode
dhMap[BROADCAST_TX] = this.BroadcastTx
dhMap[GET_UNCONFIRMED_TXS] = this.UnconfirmedTxs
dhMap[SIGN_TX] = this.SignTx
dhMap[TRANSACT] = this.Transact
return dhMap
}
// TODO add some sanity checks on address params and such.
// Look into the reflection code in core, see what can be used.
// *************************************** Accounts ***************************************
func (this *ErisDbMethods) GenPrivAccount(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
pac, errC := this.pipe.Accounts().GenPrivAccount()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return pac, 0, nil
}
func (this *ErisDbMethods) GenPrivAccountFromKey(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &PrivKeyParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
privKey := param.PrivKey
pac, errC := this.pipe.Accounts().GenPrivAccountFromKey(privKey)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return pac, 0, nil
}
func (this *ErisDbMethods) Account(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &AddressParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
address := param.Address
// TODO is address check?
account, errC := this.pipe.Accounts().Account(address)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return account, 0, nil
}
func (this *ErisDbMethods) AccountList(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &AccountsParam{}
fmt.Printf("ACCOUNT LIST REQUEST: %v\n", request)
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
fmt.Printf("PARAMS: %v\n", param)
list, errC := this.pipe.Accounts().Accounts(param.Filters)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return list, 0, nil
}
func (this *ErisDbMethods) AccountStorage(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &AddressParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
address := param.Address
storage, errC := this.pipe.Accounts().Storage(address)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return storage, 0, nil
}
func (this *ErisDbMethods) AccountStorageAt(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &StorageAtParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
address := param.Address
key := param.Key
storageItem, errC := this.pipe.Accounts().StorageAt(address, key)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return storageItem, 0, nil
}
// *************************************** Blockchain ************************************
func (this *ErisDbMethods) BlockchainInfo(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
status, errC := this.pipe.Blockchain().Info()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return status, 0, nil
}
func (this *ErisDbMethods) ChainId(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
chainId, errC := this.pipe.Blockchain().ChainId()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return &ep.ChainId{chainId}, 0, nil
}
func (this *ErisDbMethods) GenesisHash(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
hash, errC := this.pipe.Blockchain().GenesisHash()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return &ep.GenesisHash{hash}, 0, nil
}
func (this *ErisDbMethods) LatestBlockHeight(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
height, errC := this.pipe.Blockchain().LatestBlockHeight()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return &ep.LatestBlockHeight{height}, 0, nil
}
func (this *ErisDbMethods) LatestBlock(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
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
block, errC := this.pipe.Blockchain().LatestBlock()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return block, 0, nil
}
func (this *ErisDbMethods) Blocks(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &BlocksParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
blocks, errC := this.pipe.Blockchain().Blocks(param.Filters)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return blocks, 0, nil
}
func (this *ErisDbMethods) Block(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &HeightParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
height := param.Height
block, errC := this.pipe.Blockchain().Block(height)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return block, 0, nil
}
// *************************************** Consensus ************************************
func (this *ErisDbMethods) ConsensusState(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
state, errC := this.pipe.Consensus().State()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return state, 0, nil
}
func (this *ErisDbMethods) Validators(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
validators, errC := this.pipe.Consensus().Validators()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return validators, 0, nil
}
// *************************************** Net ************************************
func (this *ErisDbMethods) NetworkInfo(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
info, errC := this.pipe.Net().Info()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return info, 0, nil
}
func (this *ErisDbMethods) ClientVersion(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
version, errC := this.pipe.Net().ClientVersion()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return &ep.ClientVersion{version}, 0, nil
}
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
func (this *ErisDbMethods) Moniker(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
moniker, errC := this.pipe.Net().Moniker()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return &ep.Moniker{moniker}, 0, nil
}
func (this *ErisDbMethods) Listening(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
listening, errC := this.pipe.Net().Listening()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return &ep.Listening{listening}, 0, nil
}
func (this *ErisDbMethods) Listeners(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
listeners, errC := this.pipe.Net().Listeners()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return &ep.Listeners{listeners}, 0, nil
}
func (this *ErisDbMethods) Peers(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
peers, errC := this.pipe.Net().Peers()
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return peers, 0, nil
}
func (this *ErisDbMethods) Peer(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &PeerParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
address := param.Address
peer, errC := this.pipe.Net().Peer(address)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return peer, 0, nil
}
// *************************************** Txs ************************************
func (this *ErisDbMethods) Call(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &CallParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
address := param.Address
data := param.Data
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return call, 0, nil
}
func (this *ErisDbMethods) CallCode(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &CallCodeParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
code := param.Code
data := param.Data
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return call, 0, nil
}
func (this *ErisDbMethods) BroadcastTx(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return receipt, 0, nil
}
func (this *ErisDbMethods) Transact(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &TransactParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
receipt, errC := this.pipe.Transactor().Transact(param.PrivKey, param.Address, param.Data, param.GasLimit, param.Fee)
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return receipt, 0, nil
}
func (this *ErisDbMethods) UnconfirmedTxs(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return txs, 0, nil
}
func (this *ErisDbMethods) SignTx(request *rpc.RPCRequest, requester interface{}) (interface{}, int, error) {
param := &SignTxParam{}
err := this.codec.DecodeBytes(param, request.Params)
if err != nil {
return nil, rpc.INVALID_PARAMS, err
}
tx := param.Tx
pAccs := param.PrivAccounts
if errC != nil {
return nil, rpc.INTERNAL_ERROR, errC
}
return txRet, 0, nil
}
// **************************************************************************************
func generateSubId() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
rStr := hex.EncodeToString(b)
return strings.ToUpper(rStr), nil