Merge pull request #817 from cosmos/bucky/aminoify

fix tests
This commit is contained in:
Ethan Buchman 2018-04-09 15:37:52 +03:00 committed by GitHub
commit ccd92358db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 75 additions and 84 deletions

7
Gopkg.lock generated
View File

@ -305,6 +305,7 @@
version = "v0.7.0"
[[projects]]
branch = "bucky/aminoify"
name = "github.com/tendermint/tendermint"
packages = [
"blockchain",
@ -342,8 +343,7 @@
"types/priv_validator",
"version"
]
revision = "7afe74a963b5222bbc063545f4a9261d27b188de"
version = "0.19.0-rc2"
revision = "466c3ab1c79c1e9856aeea80c9e11c31219d11f0"
[[projects]]
name = "github.com/tendermint/tmlibs"
@ -448,6 +448,7 @@
"transport"
]
revision = "5b3c4e850e90a4cf6a20ebd46c8b32a0a3afcb9e"
source = "github.com/grpc/grpc-go"
version = "v1.7.5"
[[projects]]
@ -459,6 +460,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "ad719da6da7632d7fd405a696e66e36d5616142a499358189d88312dc1946d34"
inputs-digest = "d81bbe5d23b9bc1e1829c207a57857c40957c4c0dac6c70f5af04e0930cbd652"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -70,7 +70,8 @@
[[constraint]]
name = "github.com/tendermint/tendermint"
version = "0.19.0-rc2"
#version = "0.19.0-rc2"
branch = "bucky/aminoify"
[[override]]
name = "github.com/tendermint/tmlibs"

View File

@ -35,8 +35,6 @@ import (
bapp "github.com/cosmos/cosmos-sdk/examples/basecoin/app"
btypes "github.com/cosmos/cosmos-sdk/examples/basecoin/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
)
var (
@ -90,7 +88,7 @@ func TestKeys(t *testing.T) {
res, body = request(t, port, "GET", "/keys", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var m [2]keys.KeyOutput
err = json.Unmarshal([]byte(body), &m)
err = cdc.UnmarshalJSON([]byte(body), &m)
require.Nil(t, err)
assert.Equal(t, m[0].Name, name, "Did not serve keys name correctly")
@ -103,7 +101,7 @@ func TestKeys(t *testing.T) {
res, body = request(t, port, "GET", keyEndpoint, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var m2 keys.KeyOutput
err = json.Unmarshal([]byte(body), &m2)
err = cdc.UnmarshalJSON([]byte(body), &m2)
require.Nil(t, err)
assert.Equal(t, newName, m2.Name, "Did not serve keys name correctly")
@ -143,7 +141,7 @@ func TestNodeStatus(t *testing.T) {
require.Equal(t, http.StatusOK, res.StatusCode, body)
var nodeInfo p2p.NodeInfo
err := json.Unmarshal([]byte(body), &nodeInfo)
err := cdc.UnmarshalJSON([]byte(body), &nodeInfo)
require.Nil(t, err, "Couldn't parse node info")
assert.NotEqual(t, p2p.NodeInfo{}, nodeInfo, "res: %v", res)
@ -166,7 +164,7 @@ func TestBlock(t *testing.T) {
res, body := request(t, port, "GET", "/blocks/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := json.Unmarshal([]byte(body), &resultBlock)
err := cdc.UnmarshalJSON([]byte(body), &resultBlock)
require.Nil(t, err, "Couldn't parse block")
assert.NotEqual(t, ctypes.ResultBlock{}, resultBlock)
@ -194,7 +192,7 @@ func TestValidators(t *testing.T) {
res, body := request(t, port, "GET", "/validatorsets/latest", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err := json.Unmarshal([]byte(body), &resultVals)
err := cdc.UnmarshalJSON([]byte(body), &resultVals)
require.Nil(t, err, "Couldn't parse validatorset")
assert.NotEqual(t, ctypes.ResultValidators{}, resultVals)
@ -204,7 +202,7 @@ func TestValidators(t *testing.T) {
res, body = request(t, port, "GET", "/validatorsets/1", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err = json.Unmarshal([]byte(body), &resultVals)
err = cdc.UnmarshalJSON([]byte(body), &resultVals)
require.Nil(t, err, "Couldn't parse validatorset")
assert.NotEqual(t, ctypes.ResultValidators{}, resultVals)
@ -221,6 +219,9 @@ func TestCoinSend(t *testing.T) {
res, body := request(t, port, "GET", "/accounts/8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6", nil)
require.Equal(t, http.StatusNoContent, res.StatusCode, body)
acc := getAccount(t, sendAddr)
initialBalance := acc.GetCoins()
// create TX
receiveAddr, resultTx := doSend(t, port, seed)
waitForHeight(resultTx.Height + 1)
@ -230,24 +231,15 @@ func TestCoinSend(t *testing.T) {
assert.Equal(t, uint32(0), resultTx.DeliverTx.Code)
// query sender
res, body = request(t, port, "GET", "/accounts/"+sendAddr, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var m auth.BaseAccount
err := json.Unmarshal([]byte(body), &m)
require.Nil(t, err)
coins := m.Coins
acc = getAccount(t, sendAddr)
coins := acc.GetCoins()
mycoins := coins[0]
assert.Equal(t, coinDenom, mycoins.Denom)
assert.Equal(t, coinAmount-1, mycoins.Amount)
assert.Equal(t, initialBalance[0].Amount-1, mycoins.Amount)
// query receiver
res, body = request(t, port, "GET", "/accounts/"+receiveAddr, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err = json.Unmarshal([]byte(body), &m)
require.Nil(t, err)
coins = m.Coins
acc = getAccount(t, receiveAddr)
coins = acc.GetCoins()
mycoins = coins[0]
assert.Equal(t, coinDenom, mycoins.Denom)
assert.Equal(t, int64(1), mycoins.Amount)
@ -255,6 +247,9 @@ func TestCoinSend(t *testing.T) {
func TestIBCTransfer(t *testing.T) {
acc := getAccount(t, sendAddr)
initialBalance := acc.GetCoins()
// create TX
resultTx := doIBCTransfer(t, port, seed)
@ -265,16 +260,11 @@ func TestIBCTransfer(t *testing.T) {
assert.Equal(t, uint32(0), resultTx.DeliverTx.Code)
// query sender
res, body := request(t, port, "GET", "/accounts/"+sendAddr, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var m auth.BaseAccount
err := json.Unmarshal([]byte(body), &m)
require.Nil(t, err)
coins := m.Coins
acc = getAccount(t, sendAddr)
coins := acc.GetCoins()
mycoins := coins[0]
assert.Equal(t, coinDenom, mycoins.Denom)
assert.Equal(t, coinAmount-2, mycoins.Amount)
assert.Equal(t, initialBalance[0].Amount-1, mycoins.Amount)
// TODO: query ibc egress packet state
}
@ -350,6 +340,7 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) {
"staking": dbm.NewMemDB(),
}
app := bapp.NewBasecoinApp(logger, dbs)
cdc = bapp.MakeCodec() // XXX
genesisFile := config.GenesisFile()
genDoc, err := tmtypes.GenesisDocFromFile(genesisFile)
@ -371,10 +362,7 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) {
if err != nil {
return nil, nil, err
}
genDoc.AppState = stateBytes
cdc := wire.NewCodec()
wire.RegisterCrypto(cdc)
genDoc.AppStateJSON = stateBytes
// LCD listen address
port = fmt.Sprintf("%d", 17377) // XXX
@ -388,12 +376,12 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) {
if err != nil {
return nil, nil, err
}
lcd, err := startLCD(cdc, logger, listenAddr)
lcd, err := startLCD(logger, listenAddr)
if err != nil {
return nil, nil, err
}
waitForStart(cdc)
waitForStart()
return node, lcd, nil
}
@ -427,7 +415,7 @@ func startTM(cfg *tmcfg.Config, logger log.Logger, genDoc *tmtypes.GenesisDoc, p
}
// start the LCD. note this blocks!
func startLCD(cdc *wire.Codec, logger log.Logger, listenAddr string) (net.Listener, error) {
func startLCD(logger log.Logger, listenAddr string) (net.Listener, error) {
handler := createHandler(cdc)
return tmrpc.StartHTTPServer(listenAddr, handler, logger)
}
@ -449,6 +437,16 @@ func request(t *testing.T, port, method, path string, payload []byte) (*http.Res
return res, string(output)
}
func getAccount(t *testing.T, sendAddr string) sdk.Account {
// get the account to get the sequence
res, body := request(t, port, "GET", "/accounts/"+sendAddr, nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var acc sdk.Account
err := cdc.UnmarshalJSON([]byte(body), &acc)
require.Nil(t, err)
return acc
}
func doSend(t *testing.T, port, seed string) (receiveAddr string, resultTx ctypes.ResultBroadcastTxCommit) {
// create receive address
@ -457,20 +455,15 @@ func doSend(t *testing.T, port, seed string) (receiveAddr string, resultTx ctype
require.Nil(t, err)
receiveAddr = receiveInfo.PubKey.Address().String()
// get the account to get the sequence
res, body := request(t, port, "GET", "/accounts/"+sendAddr, nil)
// require.Equal(t, http.StatusOK, res.StatusCode, body)
acc := auth.BaseAccount{}
err = json.Unmarshal([]byte(body), &acc)
require.Nil(t, err)
sequence := acc.Sequence
acc := getAccount(t, sendAddr)
sequence := acc.GetSequence()
// send
jsonStr := []byte(fmt.Sprintf(`{ "name":"%s", "password":"%s", "sequence":%d, "amount":[{ "denom": "%s", "amount": 1 }] }`, name, password, sequence, coinDenom))
res, body = request(t, port, "POST", "/accounts/"+receiveAddr+"/send", jsonStr)
res, body := request(t, port, "POST", "/accounts/"+receiveAddr+"/send", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err = json.Unmarshal([]byte(body), &resultTx)
err = cdc.UnmarshalJSON([]byte(body), &resultTx)
require.Nil(t, err)
return receiveAddr, resultTx
@ -485,19 +478,15 @@ func doIBCTransfer(t *testing.T, port, seed string) (resultTx ctypes.ResultBroad
receiveAddr := receiveInfo.PubKey.Address().String()
// get the account to get the sequence
res, body := request(t, port, "GET", "/accounts/"+sendAddr, nil)
// require.Equal(t, http.StatusOK, res.StatusCode, body)
acc := auth.BaseAccount{}
err = json.Unmarshal([]byte(body), &acc)
require.Nil(t, err)
sequence := acc.Sequence
acc := getAccount(t, sendAddr)
sequence := acc.GetSequence()
// send
jsonStr := []byte(fmt.Sprintf(`{ "name":"%s", "password":"%s", "sequence":%d, "amount":[{ "denom": "%s", "amount": 1 }] }`, name, password, sequence, coinDenom))
res, body = request(t, port, "POST", "/ibc/testchain/"+receiveAddr+"/send", jsonStr)
res, body := request(t, port, "POST", "/ibc/testchain/"+receiveAddr+"/send", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
err = json.Unmarshal([]byte(body), &resultTx)
err = cdc.UnmarshalJSON([]byte(body), &resultTx)
require.Nil(t, err)
return resultTx
@ -519,7 +508,7 @@ func waitForHeight(height int64) {
}
res.Body.Close()
err = json.Unmarshal([]byte(body), &resultBlock)
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
if err != nil {
fmt.Println("RES", res)
fmt.Println("BODY", string(body))
@ -534,13 +523,11 @@ func waitForHeight(height int64) {
}
// wait for 2 blocks
func waitForStart(cdc *wire.Codec) {
func waitForStart() {
waitHeight := int64(2)
for {
time.Sleep(time.Second)
var resultBlock ctypes.ResultBlock
url := fmt.Sprintf("http://localhost:%v%v", port, "/blocks/latest")
res, err := http.Get(url)
if err != nil {
@ -559,6 +546,7 @@ func waitForStart(cdc *wire.Codec) {
}
res.Body.Close()
resultBlock := new(ctypes.ResultBlock)
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
if err != nil {
fmt.Println("RES", res)

12
client/lcd/wire.go Normal file
View File

@ -0,0 +1,12 @@
package lcd
import (
amino "github.com/tendermint/go-amino"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
var cdc = amino.NewCodec()
func init() {
ctypes.RegisterAmino(cdc)
}

View File

@ -1,7 +1,6 @@
package rpc
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
@ -49,7 +48,7 @@ func getBlock(height *int64) ([]byte, error) {
// TODO move maarshalling into cmd/rest functions
// output, err := tmwire.MarshalJSON(res)
output, err := json.MarshalIndent(res, "", " ")
output, err := cdc.MarshalJSON(res)
if err != nil {
return nil, err
}

View File

@ -1,7 +1,6 @@
package rpc
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
@ -61,7 +60,7 @@ func NodeInfoRequestHandler(w http.ResponseWriter, r *http.Request) {
}
nodeInfo := status.NodeInfo
output, err := json.MarshalIndent(nodeInfo, "", " ")
output, err := cdc.MarshalJSON(nodeInfo)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))

View File

@ -1,7 +1,6 @@
package rpc
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
@ -37,7 +36,7 @@ func GetValidators(height *int64) ([]byte, error) {
return nil, err
}
output, err := json.MarshalIndent(res, "", " ")
output, err := cdc.MarshalJSON(res)
if err != nil {
return nil, err
}

View File

@ -1,17 +1,12 @@
package rpc
import (
"github.com/cosmos/cosmos-sdk/wire"
amino "github.com/tendermint/go-amino"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
var cdc *wire.Codec
var cdc = amino.NewCodec()
func init() {
cdc = wire.NewCodec()
RegisterWire(cdc)
}
func RegisterWire(cdc *wire.Codec) {
ctypes.RegisterAmino(cdc)
}

View File

@ -11,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
)
// GetAccountCmd for the auth.BaseAccount type
@ -20,9 +19,9 @@ func GetAccountCmdDefault(storeName string, cdc *wire.Codec) *cobra.Command {
}
func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder {
return func(accBytes []byte) (sdk.Account, error) {
acct := new(auth.BaseAccount)
err := cdc.UnmarshalBinary(accBytes, &acct)
return func(accBytes []byte) (acct sdk.Account, err error) {
// acct := new(auth.BaseAccount)
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
if err != nil {
panic(err)
}

View File

@ -2,7 +2,6 @@ package rest
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
@ -56,7 +55,7 @@ func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.A
}
// print out whole account
output, err := json.MarshalIndent(account, "", " ")
output, err := cdc.MarshalJSON(account)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Could't marshall query result. Error: %s", err.Error())))

View File

@ -2,7 +2,6 @@ package rest
import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
@ -43,7 +42,7 @@ func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.Response
w.Write([]byte(err.Error()))
return
}
err = json.Unmarshal(body, &m)
err = cdc.UnmarshalJSON(body, &m)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
@ -86,7 +85,7 @@ func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.Response
return
}
output, err := json.MarshalIndent(res, "", " ")
output, err := cdc.MarshalJSON(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))