forked from cerc-io/laconicd-deprecated
Query and bug fixes (#110)
* Fix queries and bugs * Fix issues from rebasing * Fix rpc query address
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/ethermint/x/evm/types"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -43,7 +42,7 @@ func GetCmdGetBlockNumber(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
return nil
|
||||
}
|
||||
|
||||
var out *hexutil.Big
|
||||
var out types.QueryResBlockNumber
|
||||
cdc.MustUnmarshalJSON(res, &out)
|
||||
return cliCtx.PrintOutput(out)
|
||||
},
|
||||
@@ -68,7 +67,7 @@ func GetCmdGetStorageAt(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
fmt.Printf("could not resolve: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
var out hexutil.Bytes
|
||||
var out types.QueryResStorage
|
||||
cdc.MustUnmarshalJSON(res, &out)
|
||||
return cliCtx.PrintOutput(out)
|
||||
},
|
||||
@@ -92,9 +91,9 @@ func GetCmdGetCode(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
fmt.Printf("could not resolve: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
var out []byte
|
||||
var out types.QueryResCode
|
||||
cdc.MustUnmarshalJSON(res, &out)
|
||||
return cliCtx.PrintOutput(hexutil.Bytes(out))
|
||||
return cliCtx.PrintOutput(out)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -116,7 +115,7 @@ func GetCmdGetNonce(queryRoute string, cdc *codec.Codec) *cobra.Command {
|
||||
fmt.Printf("could not resolve: %s\n", err)
|
||||
return nil
|
||||
}
|
||||
var out hexutil.Uint64
|
||||
var out types.QueryResNonce
|
||||
cdc.MustUnmarshalJSON(res, &out)
|
||||
return cliCtx.PrintOutput(out)
|
||||
},
|
||||
|
||||
+19
-17
@@ -3,7 +3,9 @@ package evm
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/ethermint/utils"
|
||||
"github.com/cosmos/ethermint/version"
|
||||
"github.com/cosmos/ethermint/x/evm/types"
|
||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
@@ -44,8 +46,7 @@ func NewQuerier(keeper Keeper) sdk.Querier {
|
||||
func queryProtocolVersion(keeper Keeper) ([]byte, sdk.Error) {
|
||||
vers := version.ProtocolVersion
|
||||
|
||||
bigRes := hexutil.Uint(vers)
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, bigRes)
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, hexutil.Uint(vers))
|
||||
if err != nil {
|
||||
panic("could not marshal result to JSON")
|
||||
}
|
||||
@@ -54,11 +55,13 @@ func queryProtocolVersion(keeper Keeper) ([]byte, sdk.Error) {
|
||||
}
|
||||
|
||||
func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
|
||||
addr := ethcmn.BytesToAddress([]byte(path[1]))
|
||||
addr := ethcmn.HexToAddress(path[1])
|
||||
balance := keeper.GetBalance(ctx, addr)
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, balance)
|
||||
|
||||
bRes := types.QueryResBalance{Balance: utils.MarshalBigInt(balance)}
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, bRes)
|
||||
if err != nil {
|
||||
panic("could not marshal result to JSON: ")
|
||||
panic("could not marshal result to JSON: " + err.Error())
|
||||
}
|
||||
|
||||
return res, nil
|
||||
@@ -66,10 +69,8 @@ func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Er
|
||||
|
||||
func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
|
||||
num := ctx.BlockHeight()
|
||||
hexUint := hexutil.Uint64(num)
|
||||
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, hexUint)
|
||||
|
||||
bnRes := types.QueryResBlockNumber{Number: num}
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, bnRes)
|
||||
if err != nil {
|
||||
panic("could not marshal result to JSON: " + err.Error())
|
||||
}
|
||||
@@ -78,11 +79,11 @@ func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
|
||||
}
|
||||
|
||||
func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
|
||||
addr := ethcmn.BytesToAddress([]byte(path[1]))
|
||||
key := ethcmn.BytesToHash([]byte(path[2]))
|
||||
addr := ethcmn.HexToAddress(path[1])
|
||||
key := ethcmn.HexToHash(path[2])
|
||||
val := keeper.GetState(ctx, addr, key)
|
||||
bRes := hexutil.Bytes(val.Bytes())
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, &bRes)
|
||||
bRes := types.QueryResStorage{Value: val.Bytes()}
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, bRes)
|
||||
if err != nil {
|
||||
panic("could not marshal result to JSON: " + err.Error())
|
||||
}
|
||||
@@ -90,9 +91,10 @@ func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Er
|
||||
}
|
||||
|
||||
func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
|
||||
addr := ethcmn.BytesToAddress([]byte(path[1]))
|
||||
addr := ethcmn.HexToAddress(path[1])
|
||||
code := keeper.GetCode(ctx, addr)
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, code)
|
||||
cRes := types.QueryResCode{Code: code}
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, cRes)
|
||||
if err != nil {
|
||||
panic("could not marshal result to JSON: " + err.Error())
|
||||
}
|
||||
@@ -101,9 +103,9 @@ func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error
|
||||
}
|
||||
|
||||
func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
|
||||
addr := ethcmn.BytesToAddress([]byte(path[1]))
|
||||
addr := ethcmn.HexToAddress(path[1])
|
||||
nonce := keeper.GetNonce(ctx, addr)
|
||||
nRes := hexutil.Uint64(nonce)
|
||||
nRes := types.QueryResNonce{Nonce: nonce}
|
||||
res, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
|
||||
if err != nil {
|
||||
panic("could not marshal result to JSON: " + err.Error())
|
||||
|
||||
+12
-27
@@ -1,9 +1,8 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/ethermint/utils"
|
||||
|
||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
@@ -19,7 +18,7 @@ func RegisterAmino(cdc *codec.Codec) {
|
||||
cdc.RegisterConcrete(EncodableTxData{}, "ethermint/EncodedMessage", nil)
|
||||
}
|
||||
|
||||
// TxData implements the Ethereum transaction data structure. It is used
|
||||
// EncodableTxData implements the Ethereum transaction data structure. It is used
|
||||
// solely as intended in Ethereum abiding by the protocol.
|
||||
type EncodableTxData struct {
|
||||
AccountNonce uint64 `json:"nonce"`
|
||||
@@ -47,33 +46,19 @@ func unmarshalAmino(td *EncodableTxData, text string) (err error) {
|
||||
return cdc.UnmarshalBinaryBare([]byte(text), td)
|
||||
}
|
||||
|
||||
func marshalBigInt(i *big.Int) string {
|
||||
bz, err := i.MarshalText()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(bz)
|
||||
}
|
||||
|
||||
func unmarshalBigInt(s string) (*big.Int, error) {
|
||||
ret := new(big.Int)
|
||||
err := ret.UnmarshalText([]byte(s))
|
||||
return ret, err
|
||||
}
|
||||
|
||||
// MarshalAmino defines custom encoding scheme for TxData
|
||||
func (td TxData) MarshalAmino() (string, error) {
|
||||
e := EncodableTxData{
|
||||
AccountNonce: td.AccountNonce,
|
||||
Price: marshalBigInt(td.Price),
|
||||
Price: utils.MarshalBigInt(td.Price),
|
||||
GasLimit: td.GasLimit,
|
||||
Recipient: td.Recipient,
|
||||
Amount: marshalBigInt(td.Amount),
|
||||
Amount: utils.MarshalBigInt(td.Amount),
|
||||
Payload: td.Payload,
|
||||
|
||||
V: marshalBigInt(td.V),
|
||||
R: marshalBigInt(td.R),
|
||||
S: marshalBigInt(td.S),
|
||||
V: utils.MarshalBigInt(td.V),
|
||||
R: utils.MarshalBigInt(td.R),
|
||||
S: utils.MarshalBigInt(td.S),
|
||||
|
||||
Hash: td.Hash,
|
||||
}
|
||||
@@ -95,7 +80,7 @@ func (td *TxData) UnmarshalAmino(text string) (err error) {
|
||||
td.Payload = e.Payload
|
||||
td.Hash = e.Hash
|
||||
|
||||
price, err := unmarshalBigInt(e.Price)
|
||||
price, err := utils.UnmarshalBigInt(e.Price)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -105,7 +90,7 @@ func (td *TxData) UnmarshalAmino(text string) (err error) {
|
||||
td.Price = price
|
||||
}
|
||||
|
||||
amt, err := unmarshalBigInt(e.Amount)
|
||||
amt, err := utils.UnmarshalBigInt(e.Amount)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -115,7 +100,7 @@ func (td *TxData) UnmarshalAmino(text string) (err error) {
|
||||
td.Amount = amt
|
||||
}
|
||||
|
||||
v, err := unmarshalBigInt(e.V)
|
||||
v, err := utils.UnmarshalBigInt(e.V)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -125,7 +110,7 @@ func (td *TxData) UnmarshalAmino(text string) (err error) {
|
||||
td.V = v
|
||||
}
|
||||
|
||||
r, err := unmarshalBigInt(e.R)
|
||||
r, err := utils.UnmarshalBigInt(e.R)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -135,7 +120,7 @@ func (td *TxData) UnmarshalAmino(text string) (err error) {
|
||||
td.R = r
|
||||
}
|
||||
|
||||
s, err := unmarshalBigInt(e.S)
|
||||
s, err := utils.UnmarshalBigInt(e.S)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/ethermint/crypto"
|
||||
"github.com/cosmos/ethermint/utils"
|
||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -140,8 +141,8 @@ func TestMsgEthereumTxAmino(t *testing.T) {
|
||||
|
||||
func TestMarshalAndUnmarshalInt(t *testing.T) {
|
||||
i := big.NewInt(3)
|
||||
m := marshalBigInt(i)
|
||||
i2, err := unmarshalBigInt(m)
|
||||
m := utils.MarshalBigInt(i)
|
||||
i2, err := utils.UnmarshalBigInt(m)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, i, i2)
|
||||
@@ -152,15 +153,15 @@ func TestMarshalAndUnmarshalData(t *testing.T) {
|
||||
hash := ethcmn.BigToHash(big.NewInt(2))
|
||||
e := EncodableTxData{
|
||||
AccountNonce: 2,
|
||||
Price: marshalBigInt(big.NewInt(3)),
|
||||
Price: utils.MarshalBigInt(big.NewInt(3)),
|
||||
GasLimit: 1,
|
||||
Recipient: &addr,
|
||||
Amount: marshalBigInt(big.NewInt(4)),
|
||||
Amount: utils.MarshalBigInt(big.NewInt(4)),
|
||||
Payload: []byte("test"),
|
||||
|
||||
V: marshalBigInt(big.NewInt(5)),
|
||||
R: marshalBigInt(big.NewInt(6)),
|
||||
S: marshalBigInt(big.NewInt(7)),
|
||||
V: utils.MarshalBigInt(big.NewInt(5)),
|
||||
R: utils.MarshalBigInt(big.NewInt(6)),
|
||||
S: utils.MarshalBigInt(big.NewInt(7)),
|
||||
|
||||
Hash: &hash,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package types
|
||||
|
||||
// QueryResProtocolVersion is response type for protocol version query
|
||||
type QueryResProtocolVersion struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (q QueryResProtocolVersion) String() string {
|
||||
return q.Version
|
||||
}
|
||||
|
||||
// QueryResBalance is response type for balance query
|
||||
type QueryResBalance struct {
|
||||
Balance string `json:"balance"`
|
||||
}
|
||||
|
||||
func (q QueryResBalance) String() string {
|
||||
return q.Balance
|
||||
}
|
||||
|
||||
// QueryResBlockNumber is response type for block number query
|
||||
type QueryResBlockNumber struct {
|
||||
Number int64 `json:"blockNumber"`
|
||||
}
|
||||
|
||||
func (q QueryResBlockNumber) String() string {
|
||||
return string(q.Number)
|
||||
}
|
||||
|
||||
// QueryResStorage is response type for storage query
|
||||
type QueryResStorage struct {
|
||||
Value []byte `json:"value"`
|
||||
}
|
||||
|
||||
func (q QueryResStorage) String() string {
|
||||
return string(q.Value)
|
||||
}
|
||||
|
||||
// QueryResCode is response type for code query
|
||||
type QueryResCode struct {
|
||||
Code []byte
|
||||
}
|
||||
|
||||
func (q QueryResCode) String() string {
|
||||
return string(q.Code)
|
||||
}
|
||||
|
||||
// QueryResNonce is response type for Nonce query
|
||||
type QueryResNonce struct {
|
||||
Nonce uint64 `json:"nonce"`
|
||||
}
|
||||
|
||||
func (q QueryResNonce) String() string {
|
||||
return string(q.Nonce)
|
||||
}
|
||||
Reference in New Issue
Block a user