From dcfc9a542d825f4b881cd2eea379fcfdc7838a9b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 13 Feb 2017 23:22:31 -0500 Subject: [PATCH] fetch commit separate from header --- cmd/commands/query.go | 10 +++++----- cmd/commands/utils.go | 17 +++++++++++++---- demo/start.sh | 44 ++++++++++++++++++++++++++++++++++++++----- plugins/ibc/ibc.go | 1 + 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/cmd/commands/query.go b/cmd/commands/query.go index 4f8102034e..33e616c64d 100644 --- a/cmd/commands/query.go +++ b/cmd/commands/query.go @@ -133,7 +133,7 @@ func cmdBlock(c *cli.Context) error { return errors.New(cmn.Fmt("Height must be an int, got %v: %v", heightString, err)) } - nextBlock, err := getBlock(c, height) + header, commit, err := getHeaderAndCommit(c, height) if err != nil { return err } @@ -143,12 +143,12 @@ func cmdBlock(c *cli.Context) error { JSON BlockJSON `json:"json"` }{ BlockHex{ - Header: wire.BinaryBytes(nextBlock.Header), - Commit: wire.BinaryBytes(nextBlock.LastCommit), + Header: wire.BinaryBytes(header), + Commit: wire.BinaryBytes(commit), }, BlockJSON{ - Header: nextBlock.Header, - Commit: nextBlock.LastCommit, + Header: header, + Commit: commit, }, }))) diff --git a/cmd/commands/utils.go b/cmd/commands/utils.go index 92a805734a..8ef18e3dfe 100644 --- a/cmd/commands/utils.go +++ b/cmd/commands/utils.go @@ -80,15 +80,24 @@ func getAcc(tmAddr string, address []byte) (*types.Account, error) { return acc, nil } -func getBlock(c *cli.Context, height int) (*tmtypes.Block, error) { +func getHeaderAndCommit(c *cli.Context, height int) (*tmtypes.Header, *tmtypes.Commit, error) { tmResult := new(ctypes.TMResult) tmAddr := c.String("node") clientURI := client.NewClientURI(tmAddr) _, err := clientURI.Call("block", map[string]interface{}{"height": height}, tmResult) if err != nil { - return nil, errors.New(cmn.Fmt("Error on broadcast tx: %v", err)) + return nil, nil, errors.New(cmn.Fmt("Error on broadcast tx: %v", err)) } - res := (*tmResult).(*ctypes.ResultBlock) - return res.Block, nil + resBlock := (*tmResult).(*ctypes.ResultBlock) + header := resBlock.Block.Header + + _, err = clientURI.Call("commit", map[string]interface{}{"height": height}, tmResult) + if err != nil { + return nil, nil, errors.New(cmn.Fmt("Error on broadcast tx: %v", err)) + } + resCommit := (*tmResult).(*ctypes.ResultCommit) + commit := resCommit.Commit + + return header, commit, nil } diff --git a/demo/start.sh b/demo/start.sh index ad5da68d1c..b404a33c01 100644 --- a/demo/start.sh +++ b/demo/start.sh @@ -9,6 +9,32 @@ function removeQuotes() { echo "$temp" } +function waitForNode() { + addr=$1 + set +e + curl -s $addr/status > /dev/null + ERR=$? + while [ "$ERR" != 0 ]; do + sleep 1 + curl -s $addr/status > /dev/null + ERR=$? + done + set -e + echo "... node $addr is up" +} + +function waitForBlock() { + addr=$1 + b1=`curl -s $addr/status | jq .result[1].latest_block_height` + b2=$b1 + while [ "$b2" != "$b1" ]; do + echo "Waiting for node $addr to commit a block ..." + sleep 1 + b2=`curl -s $addr/status | jq .result[1].latest_block_height` + done +} + + # grab the chain ids CHAIN_ID1=$(cat ./data/chain1/basecoin/genesis.json | jq .[1]) CHAIN_ID1=$(removeQuotes $CHAIN_ID1) @@ -35,7 +61,9 @@ basecoin start --address tcp://localhost:36658 --dir ./data/chain2/basecoin &> c echo "" echo "... waiting for chains to start" echo "" -sleep 10 + +waitForNode localhost:46657 +waitForNode localhost:36657 echo "... registering chain1 on chain2" echo "" @@ -54,14 +82,14 @@ echo "... querying for packet data" echo "" # query for the packet data and proof QUERY_RESULT=$(basecoin query ibc,egress,$CHAIN_ID1,$CHAIN_ID2,1) -HEIGHT=$(echo $QUERY_RESULT | jq .height) +LAST_HEIGHT=$(echo $QUERY_RESULT | jq .last_height) PACKET=$(echo $QUERY_RESULT | jq .value) PROOF=$(echo $QUERY_RESULT | jq .proof) PACKET=$(removeQuotes $PACKET) PROOF=$(removeQuotes $PROOF) echo "" echo "QUERY_RESULT: $QUERY_RESULT" -echo "HEIGHT: $HEIGHT" +echo "LAST_HEIGHT: $LAST_HEIGHT" echo "PACKET: $PACKET" echo "PROOF: $PROOF" @@ -71,6 +99,12 @@ echo "... waiting for some blocks to be mined" echo "" sleep 5 +waitForBlock localhost:46657 +waitForBlock localhost:36657 + +# we need the header at height H=LAST_HEIGHT + 1 +HEIGHT=$(($LAST_HEIGHT + 1)) + echo "" echo "... querying for block data" echo "" @@ -95,12 +129,12 @@ echo "" echo "... posting packet from chain1 on chain2" echo "" # post the packet from chain1 to chain2 -basecoin tx ibc --amount 10 $CHAIN_FLAGS2 packet post --from $CHAIN_ID1 --height $((HEIGHT + 1)) --packet 0x$PACKET --proof 0x$PROOF +basecoin tx ibc --amount 10 $CHAIN_FLAGS2 packet post --from $CHAIN_ID1 --height $HEIGHT --packet 0x$PACKET --proof 0x$PROOF echo "" echo "... checking if the packet is present on chain2" echo "" -# query for the packet on chain2 ! +# query for the packet on chain2 basecoin query --node tcp://localhost:36657 ibc,ingress,test_chain_2,test_chain_1,1 echo "" diff --git a/plugins/ibc/ibc.go b/plugins/ibc/ibc.go index 4d1e6a5ec2..d1bd8d53b3 100644 --- a/plugins/ibc/ibc.go +++ b/plugins/ibc/ibc.go @@ -1,6 +1,7 @@ package ibc import ( + "bytes" "errors" "net/url" "strings"