mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-27 10:24:36 +00:00
RPC refactor to separate core from core_client and the rest of RPC.
Other random changes.
This commit is contained in:
+14
-13
@@ -4,18 +4,19 @@ import (
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/account"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
)
|
||||
|
||||
func GenPrivAccount() (*ResponseGenPrivAccount, error) {
|
||||
return &ResponseGenPrivAccount{account.GenPrivAccount()}, nil
|
||||
func GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
|
||||
return &ctypes.ResponseGenPrivAccount{account.GenPrivAccount()}, nil
|
||||
}
|
||||
|
||||
func GetAccount(address []byte) (*ResponseGetAccount, error) {
|
||||
func GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
|
||||
cache := mempoolReactor.Mempool.GetCache()
|
||||
return &ResponseGetAccount{cache.GetAccount(address)}, nil
|
||||
return &ctypes.ResponseGetAccount{cache.GetAccount(address)}, nil
|
||||
}
|
||||
|
||||
func GetStorage(address, slot []byte) (*ResponseGetStorage, error) {
|
||||
func GetStorage(address, slot []byte) (*ctypes.ResponseGetStorage, error) {
|
||||
state := consensusState.GetState()
|
||||
account := state.GetAccount(address)
|
||||
if account == nil {
|
||||
@@ -26,12 +27,12 @@ func GetStorage(address, slot []byte) (*ResponseGetStorage, error) {
|
||||
|
||||
_, value := storage.Get(RightPadWord256(slot).Bytes())
|
||||
if value == nil {
|
||||
return &ResponseGetStorage{slot, nil}, nil
|
||||
return &ctypes.ResponseGetStorage{slot, nil}, nil
|
||||
}
|
||||
return &ResponseGetStorage{slot, value.([]byte)}, nil
|
||||
return &ctypes.ResponseGetStorage{slot, value.([]byte)}, nil
|
||||
}
|
||||
|
||||
func ListAccounts() (*ResponseListAccounts, error) {
|
||||
func ListAccounts() (*ctypes.ResponseListAccounts, error) {
|
||||
var blockHeight uint
|
||||
var accounts []*account.Account
|
||||
state := consensusState.GetState()
|
||||
@@ -40,10 +41,10 @@ func ListAccounts() (*ResponseListAccounts, error) {
|
||||
accounts = append(accounts, value.(*account.Account))
|
||||
return false
|
||||
})
|
||||
return &ResponseListAccounts{blockHeight, accounts}, nil
|
||||
return &ctypes.ResponseListAccounts{blockHeight, accounts}, nil
|
||||
}
|
||||
|
||||
func DumpStorage(addr []byte) (*ResponseDumpStorage, error) {
|
||||
func DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, error) {
|
||||
state := consensusState.GetState()
|
||||
account := state.GetAccount(addr)
|
||||
if account == nil {
|
||||
@@ -51,11 +52,11 @@ func DumpStorage(addr []byte) (*ResponseDumpStorage, error) {
|
||||
}
|
||||
storageRoot := account.StorageRoot
|
||||
storage := state.LoadStorage(storageRoot)
|
||||
storageItems := []StorageItem{}
|
||||
storageItems := []ctypes.StorageItem{}
|
||||
storage.Iterate(func(key interface{}, value interface{}) bool {
|
||||
storageItems = append(storageItems, StorageItem{
|
||||
storageItems = append(storageItems, ctypes.StorageItem{
|
||||
key.([]byte), value.([]byte)})
|
||||
return false
|
||||
})
|
||||
return &ResponseDumpStorage{storageRoot, storageItems}, nil
|
||||
return &ctypes.ResponseDumpStorage{storageRoot, storageItems}, nil
|
||||
}
|
||||
|
||||
+5
-4
@@ -3,12 +3,13 @@ package core
|
||||
import (
|
||||
"fmt"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func BlockchainInfo(minHeight, maxHeight uint) (*ResponseBlockchainInfo, error) {
|
||||
func BlockchainInfo(minHeight, maxHeight uint) (*ctypes.ResponseBlockchainInfo, error) {
|
||||
if maxHeight == 0 {
|
||||
maxHeight = blockStore.Height()
|
||||
} else {
|
||||
@@ -25,12 +26,12 @@ func BlockchainInfo(minHeight, maxHeight uint) (*ResponseBlockchainInfo, error)
|
||||
blockMetas = append(blockMetas, blockMeta)
|
||||
}
|
||||
|
||||
return &ResponseBlockchainInfo{blockStore.Height(), blockMetas}, nil
|
||||
return &ctypes.ResponseBlockchainInfo{blockStore.Height(), blockMetas}, nil
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func GetBlock(height uint) (*ResponseGetBlock, error) {
|
||||
func GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
|
||||
if height == 0 {
|
||||
return nil, fmt.Errorf("height must be greater than 0")
|
||||
}
|
||||
@@ -40,5 +41,5 @@ func GetBlock(height uint) (*ResponseGetBlock, error) {
|
||||
|
||||
blockMeta := blockStore.LoadBlockMeta(height)
|
||||
block := blockStore.LoadBlock(height)
|
||||
return &ResponseGetBlock{blockMeta, block}, nil
|
||||
return &ctypes.ResponseGetBlock{blockMeta, block}, nil
|
||||
}
|
||||
|
||||
+3
-8
@@ -3,21 +3,16 @@ package core
|
||||
import (
|
||||
"fmt"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
type Receipt struct {
|
||||
TxHash []byte
|
||||
CreatesContract uint8
|
||||
ContractAddr []byte
|
||||
}
|
||||
|
||||
// pass pointer?
|
||||
// Note: tx must be signed
|
||||
func BroadcastTx(tx types.Tx) (*ResponseBroadcastTx, error) {
|
||||
func BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error) {
|
||||
err := mempoolReactor.BroadcastTx(tx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
|
||||
@@ -33,7 +28,7 @@ func BroadcastTx(tx types.Tx) (*ResponseBroadcastTx, error) {
|
||||
contractAddr = state.NewContractAddress(callTx.Input.Address, uint64(callTx.Input.Sequence))
|
||||
}
|
||||
}
|
||||
return &ResponseBroadcastTx{Receipt{txHash, createsContract, contractAddr}}, nil
|
||||
return &ctypes.ResponseBroadcastTx{ctypes.Receipt{txHash, createsContract, contractAddr}}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
+5
-4
@@ -3,13 +3,14 @@ package core
|
||||
import (
|
||||
"github.com/tendermint/tendermint/config"
|
||||
dbm "github.com/tendermint/tendermint/db"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
sm "github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func Status() (*ResponseStatus, error) {
|
||||
func Status() (*ctypes.ResponseStatus, error) {
|
||||
db := dbm.NewMemDB()
|
||||
genesisState := sm.MakeGenesisStateFromFile(db, config.App().GetString("GenesisFile"))
|
||||
genesisHash := genesisState.Hash()
|
||||
@@ -25,12 +26,12 @@ func Status() (*ResponseStatus, error) {
|
||||
latestBlockTime = latestBlockMeta.Header.Time.UnixNano()
|
||||
}
|
||||
|
||||
return &ResponseStatus{genesisHash, config.App().GetString("Network"), latestBlockHash, latestHeight, latestBlockTime}, nil
|
||||
return &ctypes.ResponseStatus{genesisHash, config.App().GetString("Network"), latestBlockHash, latestHeight, latestBlockTime}, nil
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func NetInfo() (*ResponseNetInfo, error) {
|
||||
func NetInfo() (*ctypes.ResponseNetInfo, error) {
|
||||
listening := p2pSwitch.IsListening()
|
||||
network := config.App().GetString("Network")
|
||||
listeners := []string{}
|
||||
@@ -41,7 +42,7 @@ func NetInfo() (*ResponseNetInfo, error) {
|
||||
for _, peer := range p2pSwitch.Peers().List() {
|
||||
peers = append(peers, peer.String())
|
||||
}
|
||||
return &ResponseNetInfo{
|
||||
return &ctypes.ResponseNetInfo{
|
||||
Network: network,
|
||||
Listening: listening,
|
||||
Listeners: listeners,
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
)
|
||||
|
||||
/*
|
||||
TODO: support Call && GetStorage.
|
||||
*/
|
||||
|
||||
var Routes = map[string]*rpc.RPCFunc{
|
||||
"status": rpc.NewRPCFunc(Status, []string{}),
|
||||
"net_info": rpc.NewRPCFunc(NetInfo, []string{}),
|
||||
"blockchain": rpc.NewRPCFunc(BlockchainInfo, []string{"min_height", "max_height"}),
|
||||
"get_block": rpc.NewRPCFunc(GetBlock, []string{"height"}),
|
||||
"get_account": rpc.NewRPCFunc(GetAccount, []string{"address"}),
|
||||
"get_storage": rpc.NewRPCFunc(GetStorage, []string{"address", "storage"}),
|
||||
"call": rpc.NewRPCFunc(Call, []string{"address", "data"}),
|
||||
"list_validators": rpc.NewRPCFunc(ListValidators, []string{}),
|
||||
"dump_storage": rpc.NewRPCFunc(DumpStorage, []string{"address"}),
|
||||
"broadcast_tx": rpc.NewRPCFunc(BroadcastTx, []string{"tx"}),
|
||||
"list_accounts": rpc.NewRPCFunc(ListAccounts, []string{}),
|
||||
"unsafe/gen_priv_account": rpc.NewRPCFunc(GenPrivAccount, []string{}),
|
||||
"unsafe/sign_tx": rpc.NewRPCFunc(SignTx, []string{"tx", "privAccounts"}),
|
||||
}
|
||||
+5
-4
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/account"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
"github.com/tendermint/tendermint/vm"
|
||||
@@ -24,7 +25,7 @@ func toVMAccount(acc *account.Account) *vm.Account {
|
||||
|
||||
// Run a contract's code on an isolated and unpersisted state
|
||||
// Cannot be used to create new contracts
|
||||
func Call(address, data []byte) (*ResponseCall, error) {
|
||||
func Call(address, data []byte) (*ctypes.ResponseCall, error) {
|
||||
|
||||
st := consensusState.GetState() // performs a copy
|
||||
cache := mempoolReactor.Mempool.GetCache()
|
||||
@@ -48,12 +49,12 @@ func Call(address, data []byte) (*ResponseCall, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ResponseCall{Return: ret}, nil
|
||||
return &ctypes.ResponseCall{Return: ret}, nil
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ResponseSignTx, error) {
|
||||
func SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ctypes.ResponseSignTx, error) {
|
||||
// more checks?
|
||||
|
||||
for i, privAccount := range privAccounts {
|
||||
@@ -85,5 +86,5 @@ func SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ResponseSignTx,
|
||||
rebondTx := tx.(*types.RebondTx)
|
||||
rebondTx.Signature = privAccounts[0].Sign(rebondTx).(account.SignatureEd25519)
|
||||
}
|
||||
return &ResponseSignTx{tx}, nil
|
||||
return &ctypes.ResponseSignTx{tx}, nil
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package core
|
||||
package core_types
|
||||
|
||||
import (
|
||||
"github.com/tendermint/tendermint/account"
|
||||
@@ -55,6 +55,12 @@ type ResponseBroadcastTx struct {
|
||||
Receipt Receipt
|
||||
}
|
||||
|
||||
type Receipt struct {
|
||||
TxHash []byte
|
||||
CreatesContract uint8
|
||||
ContractAddr []byte
|
||||
}
|
||||
|
||||
type ResponseStatus struct {
|
||||
GenesisHash []byte
|
||||
Network string
|
||||
@@ -1,12 +1,13 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
sm "github.com/tendermint/tendermint/state"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func ListValidators() (*ResponseListValidators, error) {
|
||||
func ListValidators() (*ctypes.ResponseListValidators, error) {
|
||||
var blockHeight uint
|
||||
var bondedValidators []*sm.Validator
|
||||
var unbondingValidators []*sm.Validator
|
||||
@@ -22,5 +23,5 @@ func ListValidators() (*ResponseListValidators, error) {
|
||||
return false
|
||||
})
|
||||
|
||||
return &ResponseListValidators{blockHeight, bondedValidators, unbondingValidators}, nil
|
||||
return &ctypes.ResponseListValidators{blockHeight, bondedValidators, unbondingValidators}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user