From 3205e8487d38b209f0899164e2ebe89dc5a2db69 Mon Sep 17 00:00:00 2001
From: Silas Davis <silas@erisindustries.com>
Date: Fri, 16 Sep 2016 16:08:18 +0200
Subject: [PATCH] Made Encode/Decode tx error-free and tested

---
 txs/tx.go      | 42 +++++++++++++++---------------------------
 txs/tx_test.go | 26 +++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 28 deletions(-)

diff --git a/txs/tx.go b/txs/tx.go
index 9957d570..e0069c5b 100644
--- a/txs/tx.go
+++ b/txs/tx.go
@@ -14,7 +14,7 @@ import (
 	"github.com/tendermint/go-wire"
 
 	"github.com/tendermint/go-crypto"
-	"github.com/tendermint/tendermint/types" // votes for dupeout ..
+	tendermint_types "github.com/tendermint/tendermint/types" // votes for dupeout ..
 )
 
 var (
@@ -341,8 +341,8 @@ func (tx *RebondTx) String() string {
 
 type DupeoutTx struct {
 	Address []byte     `json:"address"`
-	VoteA   types.Vote `json:"vote_a"`
-	VoteB   types.Vote `json:"vote_b"`
+	VoteA   tendermint_types.Vote `json:"vote_a"`
+	VoteB   tendermint_types.Vote `json:"vote_b"`
 }
 
 func (tx *DupeoutTx) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
@@ -378,12 +378,6 @@ func (tx *PermissionsTx) String() string {
 //-----------------------------------------------------------------------------
 
 func TxHash(chainID string, tx Tx) []byte {
-	signBytes := acm.SignBytes(chainID, tx)
-	return wire.BinaryRipemd160(signBytes)
-}
-
-// [Silas] Leaving this implementation here for when we transition
-func TxHashFuture(chainID string, tx Tx) []byte {
 	signBytes := acm.SignBytes(chainID, tx)
 	hasher := ripemd160.New()
 	hasher.Write(signBytes)
@@ -393,34 +387,28 @@ func TxHashFuture(chainID string, tx Tx) []byte {
 
 //-----------------------------------------------------------------------------
 
-func EncodeTx(tx Tx) []byte {
-	wrapTx := struct {
-		Tx Tx `json:"unwrap"`
-	}{tx}
-	return wire.BinaryBytes(wrapTx)
+func EncodeTx(tx Tx) ([]byte, error) {
+	var n int
+	var err error
+	buf := new(bytes.Buffer)
+	wire.WriteBinary(struct{ Tx }{tx}, buf, &n, &err)
+	if err != nil {
+		return nil, err
+	}
+	return buf.Bytes(), nil
 }
 
-//func EncodeTx(tx txs.Tx) []byte {
-//	buf := new(bytes.Buffer)
-//	var n int
-//	var err error
-//	wire.WriteBinary(struct{ types.Tx }{tx}, buf, &n, &err)
-//	if err != nil {
-//		return err
-//	}
-//}
-
 // panic on err
-func DecodeTx(txBytes []byte) Tx {
+func DecodeTx(txBytes []byte) (Tx, error) {
 	var n int
 	var err error
 	tx := new(Tx)
 	buf := bytes.NewBuffer(txBytes)
 	wire.ReadBinaryPtr(tx, buf, len(txBytes), &n, &err)
 	if err != nil {
-		panic(err)
+		return nil, err
 	}
-	return *tx
+	return *tx, nil
 }
 
 func GenerateReceipt(chainId string, tx Tx) Receipt {
diff --git a/txs/tx_test.go b/txs/tx_test.go
index 5a73d351..1fecefa5 100644
--- a/txs/tx_test.go
+++ b/txs/tx_test.go
@@ -6,9 +6,9 @@ import (
 	acm "github.com/eris-ltd/eris-db/account"
 	ptypes "github.com/eris-ltd/eris-db/permission/types"
 
+	"github.com/stretchr/testify/assert"
 	. "github.com/tendermint/go-common"
 	"github.com/tendermint/go-crypto"
-	//"github.com/tendermint/tendermint/types"
 )
 
 var chainID = "myChainID"
@@ -177,6 +177,30 @@ func TestPermissionsTxSignable(t *testing.T) {
 	}
 }
 
+func TestEncodeTxDecodeTx(t *testing.T) {
+	inputAddress := []byte{1, 2, 3, 4, 5}
+	outputAddress := []byte{5, 4, 3, 2, 1}
+	amount := int64(2)
+	sequence := 1
+	tx := &SendTx{
+		Inputs: []*TxInput{{
+			Address:  inputAddress,
+			Amount:   amount,
+			Sequence: sequence,
+		}},
+		Outputs: []*TxOutput{{
+			Address: outputAddress,
+			Amount:  amount,
+		}},
+	}
+	txBytes, err := EncodeTx(tx)
+	if err != nil {
+		t.Fatal(err)
+	}
+	txOut, err := DecodeTx(txBytes)
+	assert.Equal(t, tx, txOut)
+}
+
 /*
 func TestDupeoutTxSignable(t *testing.T) {
 	privAcc := acm.GenPrivAccount()
-- 
GitLab