From 2673d1df9b485b020c8373991ee8c0f3c637bb7a Mon Sep 17 00:00:00 2001 From: Benjamin Bollen <ben@erisindustries.com> Date: Fri, 1 Jul 2016 17:56:14 +0200 Subject: [PATCH] Move /rpc/tendermint implementation for Account and mempool --- consensus/tendermint/tendermint.go | 6 +++ definitions/consensus.go | 5 +++ manager/eris-mint/pipe.go | 62 +++++++++++++++++++++++--- rpc/tendermint/core/accounts.go | 70 ------------------------------ rpc/tendermint/core/mempool.go | 18 ++++---- 5 files changed, 75 insertions(+), 86 deletions(-) delete mode 100644 rpc/tendermint/core/accounts.go diff --git a/consensus/tendermint/tendermint.go b/consensus/tendermint/tendermint.go index 9b803242..f48f7df7 100644 --- a/consensus/tendermint/tendermint.go +++ b/consensus/tendermint/tendermint.go @@ -30,6 +30,7 @@ import ( node "github.com/tendermint/tendermint/node" proxy "github.com/tendermint/tendermint/proxy" tendermint_types "github.com/tendermint/tendermint/types" + tmsp_types "github.com/tendermint/tmsp/types" log "github.com/eris-ltd/eris-logger" @@ -194,6 +195,11 @@ func (this *TendermintNode) PublicValidatorKey() crypto.PubKey { return copyPublicValidatorKey } +func (this *TendermintNode) BroadcastTransaction(transaction []byte, + callback func(*tmsp_types.Response)) error { + return this.tmintNode.MempoolReactor().BroadcastTx(transaction, callback) +} + //------------------------------------------------------------------------------ // Helper functions diff --git a/definitions/consensus.go b/definitions/consensus.go index 20735cb2..98e7ed86 100644 --- a/definitions/consensus.go +++ b/definitions/consensus.go @@ -20,6 +20,7 @@ import ( crypto "github.com/tendermint/go-crypto" p2p "github.com/tendermint/go-p2p" tendermint_types "github.com/tendermint/tendermint/types" + tmsp_types "github.com/tendermint/tmsp/types" rpc_tendermint_types "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" ) @@ -42,6 +43,10 @@ type ConsensusEngine interface { // Private Validator PublicValidatorKey() crypto.PubKey + // Memory pool + BroadcastTransaction(transaction []byte, + callback func(*tmsp_types.Response)) error + } // type Communicator interface { diff --git a/manager/eris-mint/pipe.go b/manager/eris-mint/pipe.go index 75832015..d1071584 100644 --- a/manager/eris-mint/pipe.go +++ b/manager/eris-mint/pipe.go @@ -21,6 +21,7 @@ import ( "fmt" db "github.com/tendermint/go-db" + tendermint_common "github.com/tendermint/go-common" tendermint_events "github.com/tendermint/go-events" tendermint_types "github.com/tendermint/tendermint/types" wire "github.com/tendermint/go-wire" @@ -270,21 +271,63 @@ func (pipe *ErisMintPipe) Genesis() (*rpc_tendermint_types.ResultGenesis, error) // Accounts func (pipe *ErisMintPipe) GetAccount(address []byte) (*rpc_tendermint_types.ResultGetAccount, error) { - return nil, fmt.Errorf("Unimplemented.") + cache := pipe.erisMint.GetCheckCache() + // cache := mempoolReactor.Mempool.GetCache() + account := cache.GetAccount(address) + if account == nil { + log.Warn("Nil Account") + return &rpc_tendermint_types.ResultGetAccount{nil}, nil + } + return &rpc_tendermint_types.ResultGetAccount{account}, nil } func (pipe *ErisMintPipe) ListAccounts() (*rpc_tendermint_types.ResultListAccounts, error) { - return nil, fmt.Errorf("Unimplemented.") + var blockHeight int + var accounts []*account.Account + state := pipe.erisMint.GetState() + blockHeight = state.LastBlockHeight + state.GetAccounts().Iterate(func(key []byte, value []byte) bool { + accounts = append(accounts, account.DecodeAccount(value)) + return false + }) + return &rpc_tendermint_types.ResultListAccounts{blockHeight, accounts}, nil } func (pipe *ErisMintPipe) GetStorage(address, key []byte) (*rpc_tendermint_types.ResultGetStorage, error) { - return nil, fmt.Errorf("Unimplemented.") + state := pipe.erisMint.GetState() + // state := consensusState.GetState() + account := state.GetAccount(address) + if account == nil { + return nil, fmt.Errorf("UnknownAddress: %X", address) + } + storageRoot := account.StorageRoot + storageTree := state.LoadStorage(storageRoot) + + _, value, exists := storageTree.Get( + tendermint_common.LeftPadWord256(key).Bytes()) + if !exists { // value == nil { + return &rpc_tendermint_types.ResultGetStorage{key, nil}, nil + } + return &rpc_tendermint_types.ResultGetStorage{key, value}, nil } func (pipe *ErisMintPipe) DumpStorage(address []byte) (*rpc_tendermint_types.ResultDumpStorage, error) { - return nil, fmt.Errorf("Unimplemented.") + state := pipe.erisMint.GetState() + account := state.GetAccount(address) + if account == nil { + return nil, fmt.Errorf("UnknownAddress: %X", address) + } + storageRoot := account.StorageRoot + storageTree := state.LoadStorage(storageRoot) + storageItems := []rpc_tendermint_types.StorageItem{} + storageTree.Iterate(func(key []byte, value []byte) bool { + storageItems = append(storageItems, rpc_tendermint_types.StorageItem{key, + value}) + return false + }) + return &rpc_tendermint_types.ResultDumpStorage{storageRoot, storageItems}, nil } // Call @@ -317,9 +360,14 @@ func (pipe *ErisMintPipe) ListNames() (*rpc_tendermint_types.ResultListNames, er } // Memory pool -func (pipe *ErisMintPipe) BroadcastTxAsync(transaction transaction.Tx) (*rpc_tendermint_types.ResultBroadcastTx, - error) { - return nil, fmt.Errorf("Unimplemented.") +// NOTE: transaction must be signed +func (pipe *ErisMintPipe) BroadcastTxAsync(tx transaction.Tx) ( + *rpc_tendermint_types.ResultBroadcastTx, error) { + err := pipe.consensusEngine.BroadcastTransaction(transaction.EncodeTx(tx), nil) + if err != nil { + return nil, fmt.Errorf("Error broadcasting transaction: %v", err) + } + return &rpc_tendermint_types.ResultBroadcastTx{}, nil } func (pipe *ErisMintPipe) BroadcastTxSync(transaction transaction.Tx) (*rpc_tendermint_types.ResultBroadcastTx, diff --git a/rpc/tendermint/core/accounts.go b/rpc/tendermint/core/accounts.go deleted file mode 100644 index 86d99f8a..00000000 --- a/rpc/tendermint/core/accounts.go +++ /dev/null @@ -1,70 +0,0 @@ -package core - -import ( - "fmt" - - acm "github.com/eris-ltd/eris-db/account" - ctypes "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" - . "github.com/tendermint/go-common" -) - -func GenPrivAccount() (*ctypes.ResultGenPrivAccount, error) { - return &ctypes.ResultGenPrivAccount{acm.GenPrivAccount()}, nil -} - -// If the account is not known, returns nil, nil. -func GetAccount(address []byte) (*ctypes.ResultGetAccount, error) { - cache := erisMint.GetCheckCache() - // cache := mempoolReactor.Mempool.GetCache() - account := cache.GetAccount(address) - if account == nil { - log.Warn("Nil Account") - return &ctypes.ResultGetAccount{nil}, nil - } - return &ctypes.ResultGetAccount{account}, nil -} - -func GetStorage(address, key []byte) (*ctypes.ResultGetStorage, error) { - state := erisMint.GetState() - // state := consensusState.GetState() - account := state.GetAccount(address) - if account == nil { - return nil, fmt.Errorf("UnknownAddress: %X", address) - } - storageRoot := account.StorageRoot - storageTree := state.LoadStorage(storageRoot) - - _, value, exists := storageTree.Get(LeftPadWord256(key).Bytes()) - if !exists { // value == nil { - return &ctypes.ResultGetStorage{key, nil}, nil - } - return &ctypes.ResultGetStorage{key, value}, nil -} - -func ListAccounts() (*ctypes.ResultListAccounts, error) { - var blockHeight int - var accounts []*acm.Account - state := erisMint.GetState() - blockHeight = state.LastBlockHeight - state.GetAccounts().Iterate(func(key []byte, value []byte) bool { - accounts = append(accounts, acm.DecodeAccount(value)) - return false - }) - return &ctypes.ResultListAccounts{blockHeight, accounts}, nil -} - -func DumpStorage(address []byte) (*ctypes.ResultDumpStorage, error) { - state := erisMint.GetState() - account := state.GetAccount(address) - if account == nil { - return nil, fmt.Errorf("UnknownAddress: %X", address) - } - storageRoot := account.StorageRoot - storageTree := state.LoadStorage(storageRoot) - storageItems := []ctypes.StorageItem{} - storageTree.Iterate(func(key []byte, value []byte) bool { - storageItems = append(storageItems, ctypes.StorageItem{key, value}) - return false - }) - return &ctypes.ResultDumpStorage{storageRoot, storageItems}, nil -} diff --git a/rpc/tendermint/core/mempool.go b/rpc/tendermint/core/mempool.go index 78afb712..5e596670 100644 --- a/rpc/tendermint/core/mempool.go +++ b/rpc/tendermint/core/mempool.go @@ -5,20 +5,20 @@ import ( ctypes "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" txs "github.com/eris-ltd/eris-db/txs" - "github.com/tendermint/tendermint/types" + // "github.com/tendermint/tendermint/types" tmsp "github.com/tendermint/tmsp/types" ) //----------------------------------------------------------------------------- -// NOTE: tx must be signed -func BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { - err := mempoolReactor.BroadcastTx(tx, nil) - if err != nil { - return nil, fmt.Errorf("Error broadcasting transaction: %v", err) - } - return &ctypes.ResultBroadcastTx{}, nil -} +// // NOTE: tx must be signed +// func BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) { +// err := mempoolReactor.BroadcastTx(tx, nil) +// if err != nil { +// return nil, fmt.Errorf("Error broadcasting transaction: %v", err) +// } +// return &ctypes.ResultBroadcastTx{}, nil +// } // Note: tx must be signed func BroadcastTxSync(tx txs.Tx) (*ctypes.ResultBroadcastTx, error) { -- GitLab