add fromAddress argument to Call and CallCode rpc methods

This commit is contained in:
Ethan Buchman 2015-07-22 18:43:20 -04:00
parent c13b67253c
commit 290b74d8f1
5 changed files with 27 additions and 26 deletions

View File

@ -4,6 +4,7 @@ import (
rpc "github.com/tendermint/tendermint/rpc/server" rpc "github.com/tendermint/tendermint/rpc/server"
) )
// TODO: eliminate redundancy between here and reading code from core/
var Routes = map[string]*rpc.RPCFunc{ var Routes = map[string]*rpc.RPCFunc{
"status": rpc.NewRPCFunc(Status, []string{}), "status": rpc.NewRPCFunc(Status, []string{}),
"net_info": rpc.NewRPCFunc(NetInfo, []string{}), "net_info": rpc.NewRPCFunc(NetInfo, []string{}),
@ -12,8 +13,8 @@ var Routes = map[string]*rpc.RPCFunc{
"get_block": rpc.NewRPCFunc(GetBlock, []string{"height"}), "get_block": rpc.NewRPCFunc(GetBlock, []string{"height"}),
"get_account": rpc.NewRPCFunc(GetAccount, []string{"address"}), "get_account": rpc.NewRPCFunc(GetAccount, []string{"address"}),
"get_storage": rpc.NewRPCFunc(GetStorage, []string{"address", "key"}), "get_storage": rpc.NewRPCFunc(GetStorage, []string{"address", "key"}),
"call": rpc.NewRPCFunc(Call, []string{"address", "data"}), "call": rpc.NewRPCFunc(Call, []string{"fromAddress", "toAddress", "data"}),
"call_code": rpc.NewRPCFunc(CallCode, []string{"code", "data"}), "call_code": rpc.NewRPCFunc(CallCode, []string{"fromAddress", "code", "data"}),
"list_validators": rpc.NewRPCFunc(ListValidators, []string{}), "list_validators": rpc.NewRPCFunc(ListValidators, []string{}),
"dump_consensus_state": rpc.NewRPCFunc(DumpConsensusState, []string{}), "dump_consensus_state": rpc.NewRPCFunc(DumpConsensusState, []string{}),
"dump_storage": rpc.NewRPCFunc(DumpStorage, []string{"address"}), "dump_storage": rpc.NewRPCFunc(DumpStorage, []string{"address"}),

View File

@ -25,15 +25,15 @@ func toVMAccount(acc *acm.Account) *vm.Account {
// Run a contract's code on an isolated and unpersisted state // Run a contract's code on an isolated and unpersisted state
// Cannot be used to create new contracts // Cannot be used to create new contracts
func Call(address, data []byte) (*ctypes.ResponseCall, error) { func Call(fromAddress, toAddress, data []byte) (*ctypes.ResponseCall, error) {
st := consensusState.GetState() // performs a copy st := consensusState.GetState() // performs a copy
cache := state.NewBlockCache(st) cache := state.NewBlockCache(st)
outAcc := cache.GetAccount(address) outAcc := cache.GetAccount(toAddress)
if outAcc == nil { if outAcc == nil {
return nil, fmt.Errorf("Account %x does not exist", address) return nil, fmt.Errorf("Account %x does not exist", toAddress)
} }
callee := toVMAccount(outAcc) callee := toVMAccount(outAcc)
caller := &vm.Account{Address: Zero256} caller := &vm.Account{Address: LeftPadWord256(fromAddress)}
txCache := state.NewTxCache(cache) txCache := state.NewTxCache(cache)
params := vm.Params{ params := vm.Params{
BlockHeight: int64(st.LastBlockHeight), BlockHeight: int64(st.LastBlockHeight),
@ -53,12 +53,12 @@ func Call(address, data []byte) (*ctypes.ResponseCall, error) {
// Run the given code on an isolated and unpersisted state // Run the given code on an isolated and unpersisted state
// Cannot be used to create new contracts // Cannot be used to create new contracts
func CallCode(code, data []byte) (*ctypes.ResponseCall, error) { func CallCode(fromAddress, code, data []byte) (*ctypes.ResponseCall, error) {
st := consensusState.GetState() // performs a copy st := consensusState.GetState() // performs a copy
cache := mempoolReactor.Mempool.GetCache() cache := mempoolReactor.Mempool.GetCache()
callee := &vm.Account{Address: Zero256} callee := &vm.Account{Address: LeftPadWord256(fromAddress)}
caller := &vm.Account{Address: Zero256} caller := &vm.Account{Address: LeftPadWord256(fromAddress)}
txCache := state.NewTxCache(cache) txCache := state.NewTxCache(cache)
params := vm.Params{ params := vm.Params{
BlockHeight: int64(st.LastBlockHeight), BlockHeight: int64(st.LastBlockHeight),

View File

@ -17,8 +17,8 @@ import (
type Client interface { type Client interface {
BlockchainInfo(minHeight int, maxHeight int) (*ctypes.ResponseBlockchainInfo, error) BlockchainInfo(minHeight int, maxHeight int) (*ctypes.ResponseBlockchainInfo, error)
BroadcastTx(tx types.Tx) (*ctypes.Receipt, error) BroadcastTx(tx types.Tx) (*ctypes.Receipt, error)
Call(address []byte, data []byte) (*ctypes.ResponseCall, error) Call(fromAddress []byte, toAddress []byte, data []byte) (*ctypes.ResponseCall, error)
CallCode(code []byte, data []byte) (*ctypes.ResponseCall, error) CallCode(fromAddress []byte, code []byte, data []byte) (*ctypes.ResponseCall, error)
DumpConsensusState() (*ctypes.ResponseDumpConsensusState, error) DumpConsensusState() (*ctypes.ResponseDumpConsensusState, error)
DumpStorage(address []byte) (*ctypes.ResponseDumpStorage, error) DumpStorage(address []byte) (*ctypes.ResponseDumpStorage, error)
GenPrivAccount() (*acm.PrivAccount, error) GenPrivAccount() (*acm.PrivAccount, error)
@ -96,8 +96,8 @@ func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*ctypes.Receipt, error) {
return response.Result, nil return response.Result, nil
} }
func (c *ClientHTTP) Call(address []byte, data []byte) (*ctypes.ResponseCall, error) { func (c *ClientHTTP) Call(fromAddress []byte, toAddress []byte, data []byte) (*ctypes.ResponseCall, error) {
values, err := argsToURLValues([]string{"address", "data"}, address, data) values, err := argsToURLValues([]string{"fromAddress", "toAddress", "data"}, fromAddress, toAddress, data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -126,8 +126,8 @@ func (c *ClientHTTP) Call(address []byte, data []byte) (*ctypes.ResponseCall, er
return response.Result, nil return response.Result, nil
} }
func (c *ClientHTTP) CallCode(code []byte, data []byte) (*ctypes.ResponseCall, error) { func (c *ClientHTTP) CallCode(fromAddress []byte, code []byte, data []byte) (*ctypes.ResponseCall, error) {
values, err := argsToURLValues([]string{"code", "data"}, code, data) values, err := argsToURLValues([]string{"fromAddress", "code", "data"}, fromAddress, code, data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -660,11 +660,11 @@ func (c *ClientJSON) BroadcastTx(tx types.Tx) (*ctypes.Receipt, error) {
return response.Result, nil return response.Result, nil
} }
func (c *ClientJSON) Call(address []byte, data []byte) (*ctypes.ResponseCall, error) { func (c *ClientJSON) Call(fromAddress []byte, toAddress []byte, data []byte) (*ctypes.ResponseCall, error) {
request := rpctypes.RPCRequest{ request := rpctypes.RPCRequest{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: reverseFuncMap["Call"], Method: reverseFuncMap["Call"],
Params: []interface{}{address, data}, Params: []interface{}{fromAddress, toAddress, data},
Id: 0, Id: 0,
} }
body, err := c.RequestResponse(request) body, err := c.RequestResponse(request)
@ -687,11 +687,11 @@ func (c *ClientJSON) Call(address []byte, data []byte) (*ctypes.ResponseCall, er
return response.Result, nil return response.Result, nil
} }
func (c *ClientJSON) CallCode(code []byte, data []byte) (*ctypes.ResponseCall, error) { func (c *ClientJSON) CallCode(fromAddress []byte, code []byte, data []byte) (*ctypes.ResponseCall, error) {
request := rpctypes.RPCRequest{ request := rpctypes.RPCRequest{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: reverseFuncMap["CallCode"], Method: reverseFuncMap["CallCode"],
Params: []interface{}{code, data}, Params: []interface{}{fromAddress, code, data},
Id: 0, Id: 0,
} }
body, err := c.RequestResponse(request) body, err := c.RequestResponse(request)

View File

@ -182,8 +182,8 @@ func getStorage(t *testing.T, typ string, addr, key []byte) []byte {
return resp.Value return resp.Value
} }
func callCode(t *testing.T, client cclient.Client, code, data, expected []byte) { func callCode(t *testing.T, client cclient.Client, fromAddress, code, data, expected []byte) {
resp, err := client.CallCode(code, data) resp, err := client.CallCode(fromAddress, code, data)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -194,8 +194,8 @@ func callCode(t *testing.T, client cclient.Client, code, data, expected []byte)
} }
} }
func callContract(t *testing.T, client cclient.Client, address, data, expected []byte) { func callContract(t *testing.T, client cclient.Client, fromAddress, toAddress, data, expected []byte) {
resp, err := client.Call(address, data) resp, err := client.Call(fromAddress, toAddress, data)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -139,13 +139,13 @@ func testCallCode(t *testing.T, typ string) {
code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3} code := []byte{0x60, 0x5, 0x60, 0x6, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3}
data := []byte{} data := []byte{}
expected := []byte{0xb} expected := []byte{0xb}
callCode(t, client, code, data, expected) callCode(t, client, user[0].PubKey.Address(), code, data, expected)
// pass two ints as calldata, add, and return the result // pass two ints as calldata, add, and return the result
code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3} code = []byte{0x60, 0x0, 0x35, 0x60, 0x20, 0x35, 0x1, 0x60, 0x0, 0x52, 0x60, 0x20, 0x60, 0x0, 0xf3}
data = append(LeftPadWord256([]byte{0x5}).Bytes(), LeftPadWord256([]byte{0x6}).Bytes()...) data = append(LeftPadWord256([]byte{0x5}).Bytes(), LeftPadWord256([]byte{0x6}).Bytes()...)
expected = []byte{0xb} expected = []byte{0xb}
callCode(t, client, code, data, expected) callCode(t, client, user[0].PubKey.Address(), code, data, expected)
} }
func testCall(t *testing.T, typ string) { func testCall(t *testing.T, typ string) {
@ -183,7 +183,7 @@ func testCall(t *testing.T, typ string) {
// run a call through the contract // run a call through the contract
data := []byte{} data := []byte{}
expected := []byte{0xb} expected := []byte{0xb}
callContract(t, client, contractAddr, data, expected) callContract(t, client, user[0].PubKey.Address(), contractAddr, data, expected)
} }
func testNameReg(t *testing.T, typ string) { func testNameReg(t *testing.T, typ string) {