Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aminoification #814

Merged
merged 11 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
104 changes: 46 additions & 58 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -230,31 +231,25 @@ 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)
}

func TestIBCTransfer(t *testing.T) {

acc := getAccount(t, sendAddr)
initialBalance := acc.GetCoins()

// create TX
resultTx := doIBCTransfer(t, port, seed)

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions client/lcd/wire.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rpc

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rpc

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -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()))
Expand Down
3 changes: 1 addition & 2 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rpc

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -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
}
Expand Down
Loading