From 1dd6f1994988cc761f1a96bc13e9dd726ebf30cf Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 16:57:02 -0500 Subject: [PATCH 01/41] [test] refs #279 - Add `TestPendingTxnGetTimestamp` --- src/coin/skycoin/models/coin_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 8dcac029..93b6c7f5 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -3,6 +3,7 @@ package skycoin import ( "encoding/hex" "testing" + "time" "github.com/stretchr/testify/require" @@ -325,3 +326,11 @@ func TestSkycoinCreatedTxnFee(t *testing.T) { _, err = cTxn.ComputeFee("NOCOINATALL") testutil.RequireError(t, err, "Invalid ticker") } + +func TestPendingTxnGetTimestamp(t *testing.T) { + cur := time.Now() + sTxn := new(SkycoinPendingTransaction) + sTxn.Transaction.Received = cur + + require.Equal(t, core.Timestamp(cur.Unix()), sTxn.GetTimestamp()) +} From 30ddee17f4e5e85de7f6e2519ccd52500e379ba5 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 17:08:53 -0500 Subject: [PATCH 02/41] [test] refs #279 - Add `TestPendingTxnGetInputs` --- src/coin/skycoin/models/coin_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 93b6c7f5..4d54426c 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -2,6 +2,7 @@ package skycoin import ( "encoding/hex" + "fmt" "testing" "time" @@ -334,3 +335,21 @@ func TestPendingTxnGetTimestamp(t *testing.T) { require.Equal(t, core.Timestamp(cur.Unix()), sTxn.GetTimestamp()) } + +func TestPendingTxnGetInputs(t *testing.T) { + hashes := make([]string, 0) + for i := 0; i < 10; i++ { + hashes = append(hashes, fmt.Sprintf("hash%d", i)) + } + sTxn := new(SkycoinPendingTransaction) + inputs := make([]readable.TransactionInput, 0) + for _, hash := range hashes { + inputs = append(inputs, readable.TransactionInput{Hash: hash}) + } + sTxn.Transaction.Transaction.In = inputs + inHashes := make([]string, 0) + for _, input := range sTxn.GetInputs() { + inHashes = append(inHashes, input.GetId()) + } + requirethat.ElementsMatch(t, hashes, inHashes) +} From 59c146e05d4e70c335d1505aa508e3c94329c3b7 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 17:13:02 -0500 Subject: [PATCH 03/41] [test] refs #279 - Add `TestPendingTxnGetOutputs` --- src/coin/skycoin/models/coin_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 4d54426c..bdc6369c 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -353,3 +353,21 @@ func TestPendingTxnGetInputs(t *testing.T) { } requirethat.ElementsMatch(t, hashes, inHashes) } + +func TestPendingTxnGetOutputs(t *testing.T) { + hashes := make([]string, 0) + for i := 0; i < 10; i++ { + hashes = append(hashes, fmt.Sprintf("hash%d", i)) + } + sTxn := new(SkycoinPendingTransaction) + outputs := make([]readable.TransactionOutput, 0) + for _, hash := range hashes { + outputs = append(outputs, readable.TransactionOutput{Hash: hash}) + } + sTxn.Transaction.Transaction.Out = outputs + outHashes := make([]string, 0) + for _, output := range sTxn.GetOutputs() { + outHashes = append(outHashes, output.GetId()) + } + requirethat.ElementsMatch(t, hashes, outHashes) +} From b3ff4d26864e027053fcddcfddc000121e0cbaee Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 17:20:00 -0500 Subject: [PATCH 04/41] [test] refs #279 - Add `TestPendingTxnGetId` --- src/coin/skycoin/models/coin_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index bdc6369c..0ac890d0 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -371,3 +371,17 @@ func TestPendingTxnGetOutputs(t *testing.T) { } requirethat.ElementsMatch(t, hashes, outHashes) } + +func TestPendingTxnGetId(t *testing.T) { + hashes := make([]string, 0) + for i := 0; i < 10; i++ { + hashes = append(hashes, fmt.Sprintf("hash%d", i)) + } + for _, hash := range hashes { + t.Run(hash, func(t *testing.T) { + sTxn := new(SkycoinPendingTransaction) + sTxn.Transaction.Transaction.Hash = hash + require.Equal(t, hash, sTxn.GetId()) + }) + } +} From 0cc68dc8962a18736e2f373240b90b429ad1255f Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 17:37:04 -0500 Subject: [PATCH 05/41] [test] refs #279 - Add `TestPendingTxnComputeFee` --- src/coin/skycoin/models/coin_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 0ac890d0..361b122b 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -13,6 +13,7 @@ import ( "github.com/SkycoinProject/skycoin/src/readable" "github.com/SkycoinProject/skycoin/src/testutil" "github.com/fibercrypto/fibercryptowallet/src/core" + "github.com/fibercrypto/fibercryptowallet/src/errors" "github.com/fibercrypto/fibercryptowallet/src/util/requirethat" ) @@ -385,3 +386,28 @@ func TestPendingTxnGetId(t *testing.T) { }) } } + +func TestPendingTxnComputeFee(t *testing.T) { + tests := []struct { + ticker string + fee uint64 + amount uint64 + wantedError error + }{ + {ticker: Sky, wantedError: nil}, + {ticker: CoinHour, fee: 20, amount: 20, wantedError: nil}, + {ticker: CoinHour, fee: 42, amount: 42, wantedError: nil}, + {ticker: CalculatedHour, wantedError: errors.ErrNotImplemented}, + {ticker: "INVALIDTICKER", wantedError: errors.ErrInvalidAltcoinTicker}, + } + + for _, tt := range tests { + t.Run(tt.ticker, func(t *testing.T) { + sTxn := new(SkycoinPendingTransaction) + sTxn.Transaction.Transaction.Fee = tt.fee + amount, err := sTxn.ComputeFee(tt.ticker) + require.Equal(t, tt.amount, amount) + require.Equal(t, tt.wantedError, err) + }) + } +} From 6f4b426adee948e9e70e90efd63862eab259a54f Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 19:19:58 -0500 Subject: [PATCH 06/41] [test] refs #279 - Adding 4 new test `Test_newCreatedTransactionOutput` `Test_newCreatedTransactionInput` `Test_newCreatedTransaction` `Test_blockTxnToCreatedTxn` --- src/coin/skycoin/models/coin_test.go | 320 +++++++++++++++++++++++++++ 1 file changed, 320 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 361b122b..13602e30 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -411,3 +411,323 @@ func TestPendingTxnComputeFee(t *testing.T) { }) } } + +func Test_newCreatedTransactionOutput(t *testing.T) { + tests := []struct { + uxID string + address string + coins string + hours string + }{ + {uxID: "uxId1", address: "addr1", coins: "coins1", hours: "hours1"}, + {uxID: "uxId2", address: "addr2", coins: "coins2", hours: "hours2"}, + {uxID: "uxId3", address: "addr3", coins: "coins3", hours: "hours3"}, + {uxID: "uxId4", address: "addr4", coins: "coins4", hours: "hours4"}, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("case%d", i), func(t *testing.T) { + txn := newCreatedTransactionOutput(tt.uxID, tt.address, tt.coins, tt.hours) + require.Equal(t, tt.uxID, txn.UxID) + require.Equal(t, tt.address, txn.Address) + require.Equal(t, tt.coins, txn.Coins) + require.Equal(t, tt.hours, txn.Hours) + }) + } +} + +func Test_newCreatedTransactionInput(t *testing.T) { + tests := []struct { + uxID string + address string + coins string + hours string + calculatedHours string + time uint64 + block uint64 + txID string + }{ + { + uxID: "uxId1", + address: "addr1", + coins: "coins1", + hours: "hours1", + calculatedHours: "cH1", + txID: "id1", + time: 1, + block: 1, + }, + { + uxID: "uxId2", + address: "addr2", + coins: "coins2", + hours: "hours2", + calculatedHours: "cH2", + txID: "id2", + time: 2, + block: 2, + }, + { + uxID: "uxId3", + address: "addr3", + coins: "coins3", + hours: "hours3", + calculatedHours: "cH3", + txID: "id3", + time: 3, + block: 3, + }, + { + uxID: "uxId4", + address: "addr4", + coins: "coins4", + hours: "hours4", + calculatedHours: "cH4", + txID: "id4", + time: 4, + block: 4, + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("case%d", i), func(t *testing.T) { + txn := newCreatedTransactionInput( + tt.uxID, tt.address, tt.coins, tt.hours, tt.calculatedHours, tt.time, tt.block, tt.txID, + ) + require.Equal(t, tt.uxID, txn.UxID) + require.Equal(t, tt.address, txn.Address) + require.Equal(t, tt.coins, txn.Coins) + require.Equal(t, tt.hours, txn.Hours) + require.Equal(t, tt.calculatedHours, txn.CalculatedHours) + require.Equal(t, tt.time, txn.Time) + require.Equal(t, tt.block, txn.Block) + require.Equal(t, tt.txID, txn.TxID) + }) + } +} + +func Test_newCreatedTransaction(t *testing.T) { + tests := []struct { + length uint32 + txnType uint8 + txID string + innerHash string + fee string + ins []api.CreatedTransactionInput + outs []api.CreatedTransactionOutput + sigs []string + }{ + { + length: 1, + txnType: 1, + txID: "txID1", + innerHash: "hash1", + fee: "fee1", + ins: []api.CreatedTransactionInput{ + api.CreatedTransactionInput{UxID: "UxID1"}, + }, + outs: []api.CreatedTransactionOutput{ + api.CreatedTransactionOutput{UxID: "UxID1"}, + }, + sigs: []string{"first1", "second1"}, + }, + { + length: 2, + txnType: 2, + txID: "txID2", + innerHash: "hash2", + fee: "fee2", + ins: []api.CreatedTransactionInput{ + api.CreatedTransactionInput{UxID: "UxID2"}, + }, + outs: []api.CreatedTransactionOutput{ + api.CreatedTransactionOutput{UxID: "UxID2"}, + }, + sigs: []string{"first2", "second2"}, + }, + { + length: 3, + txnType: 3, + txID: "txID3", + innerHash: "hash3", + fee: "fee3", + ins: []api.CreatedTransactionInput{ + api.CreatedTransactionInput{UxID: "UxID3"}, + }, + outs: []api.CreatedTransactionOutput{ + api.CreatedTransactionOutput{UxID: "UxID3"}, + }, + sigs: []string{"first3", "second3"}, + }, + { + length: 4, + txnType: 4, + txID: "txID4", + innerHash: "hash4", + fee: "fee4", + ins: []api.CreatedTransactionInput{ + api.CreatedTransactionInput{UxID: "UxID4"}, + }, + outs: []api.CreatedTransactionOutput{ + api.CreatedTransactionOutput{UxID: "UxID4"}, + }, + sigs: []string{"first4", "second4"}, + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("case%d", i), func(t *testing.T) { + txn := newCreatedTransaction( + tt.length, + tt.txnType, + tt.txID, + tt.innerHash, + tt.fee, + tt.ins, + tt.outs, + tt.sigs, + ) + require.Equal(t, tt.length, txn.Length) + require.Equal(t, tt.txnType, txn.Type) + require.Equal(t, tt.txID, txn.TxID) + require.Equal(t, tt.innerHash, txn.InnerHash) + require.Equal(t, tt.fee, txn.Fee) + requirethat.ElementsMatch(t, tt.ins, txn.In) + requirethat.ElementsMatch(t, tt.outs, txn.Out) + requirethat.ElementsMatch(t, tt.sigs, txn.Sigs) + }) + } +} + +func Test_blockTxnToCreatedTxn(t *testing.T) { + tests := []struct { + Address string + Coins string + Hours uint64 + CalculatedHours uint64 + Time uint64 + Block uint64 + Hash string + Length uint32 + Type uint8 + InnerHash string + Fee uint64 + sigs []string + }{ + { + Address: "addr1", + Coins: "coins1", + Hours: 1, + CalculatedHours: 1, + Time: 1, + Hash: "hash1", + Length: 1, + Type: 1, + InnerHash: "inner1", + Fee: 1, + sigs: []string{"first1", "second1"}, + }, + { + Address: "addr2", + Coins: "coins2", + Hours: 2, + CalculatedHours: 2, + Time: 2, + Hash: "hash2", + Length: 2, + Type: 2, + InnerHash: "inner2", + Fee: 2, + sigs: []string{"first2", "second2"}, + }, + { + Address: "addr3", + Coins: "coins3", + Hours: 3, + CalculatedHours: 3, + Time: 3, + Hash: "hash3", + Length: 3, + Type: 3, + InnerHash: "inner3", + Fee: 3, + sigs: []string{"first3", "second3"}, + }, + { + Address: "addr4", + Coins: "coins4", + Hours: 4, + CalculatedHours: 4, + Time: 4, + Hash: "hash4", + Length: 4, + Type: 4, + InnerHash: "inner4", + Fee: 4, + sigs: []string{"first4", "second4"}, + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("case%d", i), func(*testing.T) { + block := readable.BlockTransactionVerbose{ + Length: tt.Length, + Type: tt.Type, + Hash: tt.Hash, + InnerHash: tt.InnerHash, + Fee: tt.Fee, + In: []readable.TransactionInput{ + readable.TransactionInput{ + Hash: tt.Hash, + Address: tt.Address, + Coins: tt.Coins, + Hours: tt.Hours, + CalculatedHours: tt.CalculatedHours, + }, + }, + Out: []readable.TransactionOutput{ + readable.TransactionOutput{ + Hash: tt.Hash, + Address: tt.Address, + Coins: tt.Coins, + Hours: tt.Hours, + }, + }, + Sigs: tt.sigs, + } + txn, err := blockTxnToCreatedTxn(block, tt.Time) + require.NoError(t, err) + require.Equal(t, tt.Length, txn.Length) + require.Equal(t, tt.Type, txn.Type) + require.Equal(t, tt.Hash, txn.TxID) + require.Equal(t, tt.InnerHash, txn.InnerHash) + require.Equal(t, fmt.Sprint(tt.Fee), txn.Fee) + require.Equal(t, 1, len(txn.In)) + require.Equal(t, 1, len(txn.Out)) + requirethat.ElementsMatch(t, + []api.CreatedTransactionInput{ + newCreatedTransactionInput( + tt.Hash, + tt.Address, + tt.Coins, + fmt.Sprint(tt.Hours), + fmt.Sprint(tt.CalculatedHours), + tt.Time, + tt.Block, + tt.Hash, + ), + }, + txn.In, + ) + requirethat.ElementsMatch(t, + []api.CreatedTransactionOutput{ + newCreatedTransactionOutput( + tt.Hash, tt.Address, tt.Coins, fmt.Sprint(tt.Hours), + ), + }, + txn.Out, + ) + requirethat.ElementsMatch(t, tt.sigs, txn.Sigs) + }) + } +} From 90f05ac264ec4d0ef5dfde2307773c7cefe44353 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 19:56:06 -0500 Subject: [PATCH 07/41] [pending][test] refs #279 - Change `SkycoinPendingTransaction` structure Changes: - Field `SkycoinPendingTransaction:Transaction` changes its type to `*readable.UnconfirmedTransactionVerbose` - Code and tests change accordingly --- src/coin/skycoin/models/account.go | 4 +-- src/coin/skycoin/models/coin.go | 2 +- src/coin/skycoin/models/coin_test.go | 40 +++++++++++++++------------- src/coin/skycoin/models/network.go | 2 +- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/coin/skycoin/models/account.go b/src/coin/skycoin/models/account.go index ec278569..a97a57ea 100644 --- a/src/coin/skycoin/models/account.go +++ b/src/coin/skycoin/models/account.go @@ -187,7 +187,7 @@ func (wlt *RemoteWallet) ListPendingTransactions() (core.TransactionIterator, er } txns := make([]core.Transaction, 0) for _, ut := range response.Transactions { - txns = append(txns, &SkycoinPendingTransaction{Transaction: ut}) + txns = append(txns, &SkycoinPendingTransaction{Transaction: &ut}) } return NewSkycoinTransactionIterator(txns), nil } @@ -308,7 +308,7 @@ func (wlt *LocalWallet) ListPendingTransactions() (core.TransactionIterator, err } txns := make([]core.Transaction, 0) for _, ut := range response.Transactions { - txns = append(txns, &SkycoinPendingTransaction{Transaction: ut}) + txns = append(txns, &SkycoinPendingTransaction{Transaction: &ut}) } return NewSkycoinTransactionIterator(txns), nil } diff --git a/src/coin/skycoin/models/coin.go b/src/coin/skycoin/models/coin.go index d16824da..cb54d1b1 100644 --- a/src/coin/skycoin/models/coin.go +++ b/src/coin/skycoin/models/coin.go @@ -25,7 +25,7 @@ var logCoin = logging.MustGetLogger("Skycoin coin") Implements Transaction interface */ type SkycoinPendingTransaction struct { - Transaction readable.UnconfirmedTransactionVerbose + Transaction *readable.UnconfirmedTransactionVerbose } func (txn *SkycoinPendingTransaction) SupportedAssets() []string { diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 13602e30..73acb84e 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -258,20 +258,6 @@ func TestPendingTxnStatus(t *testing.T) { require.Equal(t, core.TXN_STATUS_PENDING, pendTxn.GetStatus()) } -func TestPendingTxnFee(t *testing.T) { - pendTxn := new(SkycoinPendingTransaction) - - fee, err := pendTxn.ComputeFee(Sky) - require.NoError(t, err) - require.Equal(t, uint64(0), fee) - - _, err = pendTxn.ComputeFee(CalculatedHour) - testutil.RequireError(t, err, "Feature not implemented") - - _, err = pendTxn.ComputeFee("NOCOINATALL") - testutil.RequireError(t, err, "Invalid ticker") -} - func TestUninjectedTxnTimestamp(t *testing.T) { coreTxn := new(SkycoinUninjectedTransaction) require.Equal(t, core.Timestamp(0), coreTxn.GetTimestamp()) @@ -332,7 +318,7 @@ func TestSkycoinCreatedTxnFee(t *testing.T) { func TestPendingTxnGetTimestamp(t *testing.T) { cur := time.Now() sTxn := new(SkycoinPendingTransaction) - sTxn.Transaction.Received = cur + sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{Received: cur} require.Equal(t, core.Timestamp(cur.Unix()), sTxn.GetTimestamp()) } @@ -347,7 +333,11 @@ func TestPendingTxnGetInputs(t *testing.T) { for _, hash := range hashes { inputs = append(inputs, readable.TransactionInput{Hash: hash}) } - sTxn.Transaction.Transaction.In = inputs + sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + In: inputs, + }, + } inHashes := make([]string, 0) for _, input := range sTxn.GetInputs() { inHashes = append(inHashes, input.GetId()) @@ -365,7 +355,11 @@ func TestPendingTxnGetOutputs(t *testing.T) { for _, hash := range hashes { outputs = append(outputs, readable.TransactionOutput{Hash: hash}) } - sTxn.Transaction.Transaction.Out = outputs + sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + Out: outputs, + }, + } outHashes := make([]string, 0) for _, output := range sTxn.GetOutputs() { outHashes = append(outHashes, output.GetId()) @@ -381,7 +375,11 @@ func TestPendingTxnGetId(t *testing.T) { for _, hash := range hashes { t.Run(hash, func(t *testing.T) { sTxn := new(SkycoinPendingTransaction) - sTxn.Transaction.Transaction.Hash = hash + sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + Hash: hash, + }, + } require.Equal(t, hash, sTxn.GetId()) }) } @@ -404,7 +402,11 @@ func TestPendingTxnComputeFee(t *testing.T) { for _, tt := range tests { t.Run(tt.ticker, func(t *testing.T) { sTxn := new(SkycoinPendingTransaction) - sTxn.Transaction.Transaction.Fee = tt.fee + sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + Fee: tt.fee, + }, + } amount, err := sTxn.ComputeFee(tt.ticker) require.Equal(t, tt.amount, amount) require.Equal(t, tt.wantedError, err) diff --git a/src/coin/skycoin/models/network.go b/src/coin/skycoin/models/network.go index 78a2e788..8829bd77 100644 --- a/src/coin/skycoin/models/network.go +++ b/src/coin/skycoin/models/network.go @@ -133,7 +133,7 @@ func (spex *SkycoinPEX) GetTxnPool() (core.TransactionIterator, error) { } skycoinTxns := make([]core.Transaction, 0) for _, txn := range txns { - skycoinTxns = append(skycoinTxns, &SkycoinPendingTransaction{Transaction: txn}) + skycoinTxns = append(skycoinTxns, &SkycoinPendingTransaction{Transaction: &txn}) } return NewSkycoinTransactionIterator(skycoinTxns), nil } From b4b841a768a3fa4b1466839fcca42b4392fddbb8 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 20:17:38 -0500 Subject: [PATCH 08/41] [test] refs #279 - Add `TestPendingTxnToCreatedTransaction` --- src/coin/skycoin/models/coin_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 73acb84e..7fd6d72f 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -733,3 +733,26 @@ func Test_blockTxnToCreatedTxn(t *testing.T) { }) } } + +func TestPendingTxnToCreatedTransaction(t *testing.T) { + for i := 0; i < 5; i++ { + t.Run(fmt.Sprintf("case%d", i), func(t *testing.T) { + txn := &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + Hash: fmt.Sprintf("hash%d", i), + }, + Announced: time.Now(), + }, + } + expected, err := blockTxnToCreatedTxn( + txn.Transaction.Transaction, + uint64(txn.Transaction.Announced.UnixNano()), + ) + require.NoError(t, err) + created, err1 := txn.ToCreatedTransaction() + require.NoError(t, err1) + require.Equal(t, expected, created) + }) + } +} From e95ee2c737a3dae9b7ce3a0caf8b3cef388dac00 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 22:41:57 -0500 Subject: [PATCH 09/41] [test] refs #279 - Add `Test_serializeCreatedTransaction` --- src/coin/mocks/ReadableTxn.go | 36 ++++++++++++++++++++++++++++ src/coin/skycoin/models/coin_test.go | 28 ++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/coin/mocks/ReadableTxn.go diff --git a/src/coin/mocks/ReadableTxn.go b/src/coin/mocks/ReadableTxn.go new file mode 100644 index 00000000..fbe33f1f --- /dev/null +++ b/src/coin/mocks/ReadableTxn.go @@ -0,0 +1,36 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. + +package mocks + +import ( + api "github.com/SkycoinProject/skycoin/src/api" + mock "github.com/stretchr/testify/mock" +) + +// ReadableTxn is an autogenerated mock type for the ReadableTxn type +type ReadableTxn struct { + mock.Mock +} + +// ToCreatedTransaction provides a mock function with given fields: +func (_m *ReadableTxn) ToCreatedTransaction() (*api.CreatedTransaction, error) { + ret := _m.Called() + + var r0 *api.CreatedTransaction + if rf, ok := ret.Get(0).(func() *api.CreatedTransaction); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*api.CreatedTransaction) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 7fd6d72f..cd5c76cb 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -2,6 +2,7 @@ package skycoin import ( "encoding/hex" + goerrors "errors" "fmt" "testing" "time" @@ -12,6 +13,7 @@ import ( "github.com/SkycoinProject/skycoin/src/cipher" "github.com/SkycoinProject/skycoin/src/readable" "github.com/SkycoinProject/skycoin/src/testutil" + "github.com/fibercrypto/fibercryptowallet/src/coin/mocks" "github.com/fibercrypto/fibercryptowallet/src/core" "github.com/fibercrypto/fibercryptowallet/src/errors" "github.com/fibercrypto/fibercryptowallet/src/util/requirethat" @@ -756,3 +758,29 @@ func TestPendingTxnToCreatedTransaction(t *testing.T) { }) } } + +func Test_serializeCreatedTransaction(t *testing.T) { + mockTxn := new(mocks.ReadableTxn) + id := "0000000000000000000000000000000000000000000000000000000000000000" + created := &api.CreatedTransaction{ + TxID: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", + InnerHash: "No-match", + } + mockTxn.On("ToCreatedTransaction").Return(nil, goerrors.New("failure")).Once() + mockTxn.On("ToCreatedTransaction").Return(created, nil) + + _, err := serializeCreatedTransaction(mockTxn) + require.Error(t, err) + + _, err = serializeCreatedTransaction(mockTxn) + require.Error(t, err) + + created.InnerHash = id + ser, err := serializeCreatedTransaction(mockTxn) + require.NoError(t, err) + txn, err := created.ToTransaction() + require.NoError(t, err) + expected, err := txn.Serialize() + require.NoError(t, err) + require.Equal(t, expected, ser) +} From 52a962f2b5293549181f392cbcd7ad5113cd7cc3 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Thu, 23 Jan 2020 22:42:13 -0500 Subject: [PATCH 10/41] [test] refs #279 - Add `TestSkycoinPendingTransactionEncodeSkycoinTransaction` --- src/coin/skycoin/models/coin_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index cd5c76cb..1667be5f 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -784,3 +784,21 @@ func Test_serializeCreatedTransaction(t *testing.T) { require.NoError(t, err) require.Equal(t, expected, ser) } + +func TestSkycoinPendingTransactionEncodeSkycoinTransaction(t *testing.T) { + date, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") + sTxn := &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + InnerHash: "0000000000000000000000000000000000000000000000000000000000000000", + Hash: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", + }, + Announced: date, + }, + } + ser, err := sTxn.EncodeSkycoinTransaction() + require.NoError(t, err) + exp, err := serializeCreatedTransaction(sTxn) + require.NoError(t, err) + require.Equal(t, exp, ser) +} From c5d77699075c57b83bbda6de74312ad407fb3574 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Fri, 24 Jan 2020 12:43:34 -0500 Subject: [PATCH 11/41] [test] refs #279 - Remove unnecessary nolint in tests --- src/coin/skycoin/models/account_test.go | 18 +++++++++--------- src/coin/skycoin/models/blockchain_test.go | 4 ++-- src/coin/skycoin/models/coin_test.go | 3 ++- src/coin/skycoin/models/wallet_test.go | 21 ++++++++++++++------- src/util/main_test.go | 4 ++-- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/coin/skycoin/models/account_test.go b/src/coin/skycoin/models/account_test.go index 0156a145..40305d2d 100644 --- a/src/coin/skycoin/models/account_test.go +++ b/src/coin/skycoin/models/account_test.go @@ -58,10 +58,10 @@ func TestWalletListPendingTransactions(t *testing.T) { wlt := wallets.Value() account := wlt.GetCryptoAccount() global_mock.On(method, wlt.GetId()).Return(response, errors.New("failure")).Once() - txns, err := account.ListPendingTransactions() // nolint gosec + _, err := account.ListPendingTransactions() require.Error(t, err) global_mock.On(method, wlt.GetId()).Return(response, nil).Once() - txns, err = account.ListPendingTransactions() + txns, err := account.ListPendingTransactions() require.NoError(t, err) for txns.Next() { iter := NewSkycoinTransactionOutputIterator(txns.Value().GetOutputs()) @@ -95,10 +95,10 @@ func TestSkycoinAddressGetBalance(t *testing.T) { response.Confirmed = readable.Balance{Coins: uint64(42000000), Hours: uint64(200)} skyAddrs := addr.GetCryptoAccount() global_mock.On("Balance", []string{addr.String()}).Return(response, errors.New("failure")).Once() - val, err := skyAddrs.GetBalance(Sky) // nolint gosec + _, err = skyAddrs.GetBalance(Sky) require.Error(t, err) global_mock.On("Balance", []string{addr.String()}).Return(response, nil) - val, err = skyAddrs.GetBalance(Sky) + val, err := skyAddrs.GetBalance(Sky) require.NoError(t, err) require.Equal(t, val, uint64(42000000)) val, err = skyAddrs.GetBalance(CoinHour) @@ -216,22 +216,22 @@ func TestLocalWalletGetBalance(t *testing.T) { // wallet not found wlt := &LocalWallet{WalletDir: "./testdata", Id: "no_wallet.wlt"} - val, err := wlt.GetBalance(Sky) // nolint gosec + _, err := wlt.GetBalance(Sky) require.Error(t, err) // api interaction error wlt = &LocalWallet{WalletDir: "./testdata", Id: "test.wlt"} - val, err = wlt.GetBalance(Sky) // nolint gosec + _, err = wlt.GetBalance(Sky) require.Error(t, err) // invalid HeadOutputs wlt = &LocalWallet{WalletDir: "./testdata", Id: "test.wlt"} - val, err = wlt.GetBalance(Sky) // nolint gosec + _, err = wlt.GetBalance(Sky) require.Error(t, err) response.HeadOutputs = response.HeadOutputs[:len(response.HeadOutputs)-1] // all well - val, err = wlt.GetBalance(Sky) + val, err := wlt.GetBalance(Sky) require.NoError(t, err) require.Equal(t, uint64(84000000), val) val, err = wlt.GetBalance(CoinHour) @@ -239,7 +239,7 @@ func TestLocalWalletGetBalance(t *testing.T) { require.Equal(t, uint64(84), val) //invalid ticker - _, err = wlt.GetBalance("INVALID_TICKER") // nolint gosec + _, err = wlt.GetBalance("INVALID_TICKER") require.Error(t, err) } diff --git a/src/coin/skycoin/models/blockchain_test.go b/src/coin/skycoin/models/blockchain_test.go index 8f2a949b..60c9fb3d 100644 --- a/src/coin/skycoin/models/blockchain_test.go +++ b/src/coin/skycoin/models/blockchain_test.go @@ -154,10 +154,10 @@ func TestSkycoinBlockchainStatusGetLastBlock(t *testing.T) { status := &SkycoinBlockchain{CacheTime: 20} // api interaction error - block, err := status.GetLastBlock() // nolint gosec + _, err := status.GetLastBlock() require.Error(t, err) - block, err = status.GetLastBlock() + block, err := status.GetLastBlock() require.NoError(t, err) val, err2 := block.GetVersion() require.NoError(t, err2) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 1667be5f..9e0ec643 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -786,7 +786,8 @@ func Test_serializeCreatedTransaction(t *testing.T) { } func TestSkycoinPendingTransactionEncodeSkycoinTransaction(t *testing.T) { - date, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") + date, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") + require.NoError(t, err) sTxn := &SkycoinPendingTransaction{ Transaction: &readable.UnconfirmedTransactionVerbose{ Transaction: readable.BlockTransactionVerbose{ diff --git a/src/coin/skycoin/models/wallet_test.go b/src/coin/skycoin/models/wallet_test.go index db096eca..0910b94c 100644 --- a/src/coin/skycoin/models/wallet_test.go +++ b/src/coin/skycoin/models/wallet_test.go @@ -1704,9 +1704,11 @@ func TestSkycoinLocalWalletEncrypt(t *testing.T) { clean := err == nil tt.srv.Encrypt(tt.name, tt.pwd) - encrypted, _ := tt.srv.IsEncrypted(tt.name) // nolint gosec + encrypted, err := tt.srv.IsEncrypted(tt.name) + require.NoError(t, err) if clean { - _ = wallet.Save(wlt, tt.srv.walletDir) // nolint gosec + err = wallet.Save(wlt, tt.srv.walletDir) + require.NoError(t, err) } require.Equal(t, tt.valid, encrypted) }) @@ -1739,9 +1741,11 @@ func TestSkycoinLocalWalletDecrypt(t *testing.T) { clean := err == nil tt.srv.Decrypt(tt.name, tt.pwd) - encrypted, _ := tt.srv.IsEncrypted(tt.name) // nolint gosec + encrypted, err := tt.srv.IsEncrypted(tt.name) + require.NoError(t, err) if clean { - _ = wallet.Save(wlt, tt.srv.walletDir) // nolint gosec + err = wallet.Save(wlt, tt.srv.walletDir) + require.NoError(t, err) } require.Equal(t, tt.valid, encrypted) }) @@ -1786,7 +1790,8 @@ func TestWalletsReadyForTxn(t *testing.T) { mock.AnythingOfType("*mocks.Transaction"), ).Return( func(w core.Wallet, txn core.Transaction) bool { - ok, _ := checkTxnSupported(mockWlt, w, txn) // nolint gosec + ok, err := checkTxnSupported(mockWlt, w, txn) + require.NoError(t, err) return ok }, nil, @@ -1948,8 +1953,10 @@ func TestSeedServiceGenerateMnemonic(t *testing.T) { func TestSeedServiceVerifyMnemonic(t *testing.T) { srv := new(SeedService) - mnc128, _ := srv.GenerateMnemonic(128) // nolint gosec - mnc256, _ := srv.GenerateMnemonic(256) // nolint gosec + mnc128, err := srv.GenerateMnemonic(128) + require.NoError(t, err) + mnc256, err := srv.GenerateMnemonic(256) + require.NoError(t, err) tests := []struct { name string mnemonic string diff --git a/src/util/main_test.go b/src/util/main_test.go index f6425f9d..7e03ab57 100644 --- a/src/util/main_test.go +++ b/src/util/main_test.go @@ -35,7 +35,7 @@ func TestPubKeyFromBytes(t *testing.T) { require.Nil(t, err) require.Equal(t, pubKey, mockPubKey) - _, err = PubKeyFromBytes(`CUSTOMTICKER`, bytes) // nolint gosec + _, err = PubKeyFromBytes(`CUSTOMTICKER`, bytes) require.NotNil(t, err) } @@ -65,6 +65,6 @@ func TestSecKeyFromBytes(t *testing.T) { require.Nil(t, err) require.Equal(t, secKey, mockSecKey) - _, err = SecKeyFromBytes(`CUSTOMTICKER`, bytes) // nolint gosec + _, err = SecKeyFromBytes(`CUSTOMTICKER`, bytes) require.NotNil(t, err) } From 1b9ece075e7475df1b58e59e110f52e658bf04c2 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Fri, 24 Jan 2020 12:54:19 -0500 Subject: [PATCH 12/41] [test] refs #279 - Fix error catching in `TestSkycoinLocalWalletDecrypt` and `TestSkycoinLocalWalletEncrypt` --- src/coin/skycoin/models/wallet_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/coin/skycoin/models/wallet_test.go b/src/coin/skycoin/models/wallet_test.go index 0910b94c..a59a93d5 100644 --- a/src/coin/skycoin/models/wallet_test.go +++ b/src/coin/skycoin/models/wallet_test.go @@ -1705,10 +1705,12 @@ func TestSkycoinLocalWalletEncrypt(t *testing.T) { tt.srv.Encrypt(tt.name, tt.pwd) encrypted, err := tt.srv.IsEncrypted(tt.name) - require.NoError(t, err) if clean { + require.NoError(t, err) err = wallet.Save(wlt, tt.srv.walletDir) require.NoError(t, err) + } else { + require.Error(t, err) } require.Equal(t, tt.valid, encrypted) }) @@ -1742,10 +1744,12 @@ func TestSkycoinLocalWalletDecrypt(t *testing.T) { tt.srv.Decrypt(tt.name, tt.pwd) encrypted, err := tt.srv.IsEncrypted(tt.name) - require.NoError(t, err) if clean { + require.NoError(t, err) err = wallet.Save(wlt, tt.srv.walletDir) require.NoError(t, err) + } else { + require.Error(t, err) } require.Equal(t, tt.valid, encrypted) }) From 6d9c2bf175da3947a9da67bf2b6f69b5b53c545c Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Fri, 24 Jan 2020 14:27:25 -0500 Subject: [PATCH 13/41] [coin] - Fix error in `verifyReadableTransaction` When `ReadableTxn:ToCreatedTransaction` is called, the conversion is valid only if it does not return an error , then the needed condition is`err == nil` --- src/coin/skycoin/models/coin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coin/skycoin/models/coin.go b/src/coin/skycoin/models/coin.go index cb54d1b1..5de9d906 100644 --- a/src/coin/skycoin/models/coin.go +++ b/src/coin/skycoin/models/coin.go @@ -174,7 +174,7 @@ func (txn *SkycoinPendingTransaction) EncodeSkycoinTransaction() ([]byte, error) func verifyReadableTransaction(rTxn skytypes.ReadableTxn, checkSigned bool) error { var createdTxn *api.CreatedTransaction - if cTxn, err := rTxn.ToCreatedTransaction(); err != nil { + if cTxn, err := rTxn.ToCreatedTransaction(); err == nil { createdTxn = cTxn } else { return err From 0afd01928b19be7b50023f6cab3da107044adc80 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Fri, 24 Jan 2020 14:51:20 -0500 Subject: [PATCH 14/41] [test] refs #279 - Add `Test_verifyReadableTransaction` --- src/coin/skycoin/models/coin_test.go | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 9e0ec643..fb0704bc 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -803,3 +803,33 @@ func TestSkycoinPendingTransactionEncodeSkycoinTransaction(t *testing.T) { require.NoError(t, err) require.Equal(t, exp, ser) } + +func Test_verifyReadableTransaction(t *testing.T) { + mockTxn := new(mocks.ReadableTxn) + id := "0000000000000000000000000000000000000000000000000000000000000000" + created := &api.CreatedTransaction{ + TxID: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", + InnerHash: "No-match", + } + mockTxn.On("ToCreatedTransaction").Return(nil, goerrors.New("failure")).Once() + mockTxn.On("ToCreatedTransaction").Return(created, nil) + + // ReadableTxn error + err := verifyReadableTransaction(mockTxn, false) + require.Error(t, err) + + // transaction hash error + err = verifyReadableTransaction(mockTxn, false) + require.Error(t, err) + + // Verify fail + created.InnerHash = id + err = verifyReadableTransaction(mockTxn, false) + require.Error(t, err) + + // VerifyUnsigned fail + err = verifyReadableTransaction(mockTxn, true) + require.Error(t, err) + + //TODO: add a case that not raise an error +} From 8d2f4443779fb44b3cff1445cd1dfbe4250282a5 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Fri, 24 Jan 2020 15:08:55 -0500 Subject: [PATCH 15/41] [test] refs #279 - Add `TestPendingTxnVerifySignature` --- src/coin/skycoin/models/coin_test.go | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index fb0704bc..84ff4583 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -833,3 +833,38 @@ func Test_verifyReadableTransaction(t *testing.T) { //TODO: add a case that not raise an error } + +func TestPendingTxnVerifySignature(t *testing.T) { + tests := []struct { + name string + sTxn *SkycoinPendingTransaction + }{ + { + name: "empty transaction", + sTxn: &SkycoinPendingTransaction{Transaction: new(readable.UnconfirmedTransactionVerbose)}, + }, + { + name: "cero outputs", + sTxn: &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + In: []readable.TransactionInput{readable.TransactionInput{}}, + }, + }, + }, + }, + //TODO: add valid tests + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if !tt.sTxn.Transaction.IsValid { + require.Error(t, tt.sTxn.VerifySigned()) + require.Error(t, tt.sTxn.VerifyUnsigned()) + tt.sTxn.Transaction.IsValid = true + } + require.Equal(t, verifyReadableTransaction(tt.sTxn, false), tt.sTxn.VerifyUnsigned()) + require.Equal(t, verifyReadableTransaction(tt.sTxn, true), tt.sTxn.VerifySigned()) + }) + } +} From 210e1bf305c8ea400b514390df3d7e07c4c80a10 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Fri, 24 Jan 2020 21:18:36 -0500 Subject: [PATCH 16/41] [test] refs #279 - Add `Test_checkFullySigned` --- src/coin/skycoin/models/coin_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 84ff4583..4acc2f00 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -868,3 +868,27 @@ func TestPendingTxnVerifySignature(t *testing.T) { }) } } + +func Test_checkFullySigned(t *testing.T) { + mockTxn := new(mocks.ReadableTxn) + id := "0000000000000000000000000000000000000000000000000000000000000000" + created := &api.CreatedTransaction{ + TxID: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", + InnerHash: "No-match", + } + mockTxn.On("ToCreatedTransaction").Return(nil, goerrors.New("failure")).Once() + mockTxn.On("ToCreatedTransaction").Return(created, nil) + + _, err := checkFullySigned(mockTxn) + require.Error(t, err) + + _, err = checkFullySigned(mockTxn) + require.Error(t, err) + + // false + created.InnerHash = id + val, err := checkFullySigned(mockTxn) + require.False(t, val) + + //TODO: add valid test +} From 0741746abb2acfdb607bf945f50359ad8331cacb Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 14:24:41 -0500 Subject: [PATCH 17/41] [test] refs #279 - Add `TestPendingTxnIsFullySigned` --- src/coin/skycoin/models/coin_test.go | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 4acc2f00..8d4f3e30 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -888,7 +888,40 @@ func Test_checkFullySigned(t *testing.T) { // false created.InnerHash = id val, err := checkFullySigned(mockTxn) + require.NoError(t, err) require.False(t, val) //TODO: add valid test } + +func TestPendingTxnIsFullySigned(t *testing.T) { + tests := []struct { + name string + sTxn *SkycoinPendingTransaction + }{ + { + name: "empty transaction", + sTxn: &SkycoinPendingTransaction{Transaction: new(readable.UnconfirmedTransactionVerbose)}, + }, + { + name: "cero outputs", + sTxn: &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + In: []readable.TransactionInput{readable.TransactionInput{}}, + }, + }, + }, + }, + //TODO: add valid tests + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fully, err := checkFullySigned(tt.sTxn) + fully1, err1 := tt.sTxn.IsFullySigned() + require.Equal(t, err, err1) + require.Equal(t, fully, fully1) + }) + } +} From 2a6ccdc7a316360d785e44dc1e77df8110c08c35 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 17:07:03 -0500 Subject: [PATCH 18/41] [test] refs #279 - Update `TestSkycoinUninjectedTransactionGetInputs` cover all the method --- src/coin/skycoin/models/coin_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 8d4f3e30..6e773115 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -172,6 +172,7 @@ func TestSkycoinUninjectedTransactionGetInputs(t *testing.T) { addr := makeAddress() + global_mock.On("UxOut", h.String()).Return(nil, goerrors.New("failure")).Once() global_mock.On("UxOut", h.String()).Return( &readable.SpentOutput{ OwnerAddress: addr.String(), @@ -184,6 +185,9 @@ func TestSkycoinUninjectedTransactionGetInputs(t *testing.T) { ) tiList := ut.GetInputs() + require.Nil(t, tiList) + + tiList = ut.GetInputs() ti := tiList[0] require.Equal(t, 1, len(tiList)) sky, err := ti.GetCoins(Sky) From 8e3f78cc2af4bd3f7bc8d442329112fec308a080 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 17:07:19 -0500 Subject: [PATCH 19/41] [test] refs #279 - Add `TestSkycoinUninjectedTransactionGetOutputs` --- src/coin/skycoin/models/coin_test.go | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 6e773115..e1ca3143 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -11,6 +11,7 @@ import ( "github.com/SkycoinProject/skycoin/src/api" "github.com/SkycoinProject/skycoin/src/cipher" + "github.com/SkycoinProject/skycoin/src/coin" "github.com/SkycoinProject/skycoin/src/readable" "github.com/SkycoinProject/skycoin/src/testutil" "github.com/fibercrypto/fibercryptowallet/src/coin/mocks" @@ -929,3 +930,74 @@ func TestPendingTxnIsFullySigned(t *testing.T) { }) } } + +func TestSkycoinUninjectedTransactionGetOutputs(t *testing.T) { + addr := makeAddress() + addr1 := makeAddress() + tests := []struct { + name string + ujTxn *SkycoinUninjectedTransaction + addrs []string + wantNil bool + }{ + { + name: "empty transaction", + ujTxn: &SkycoinUninjectedTransaction{ + outputs: []core.TransactionOutput{}, + }, + addrs: make([]string, 0), + }, + { + name: "nil outputs", + ujTxn: &SkycoinUninjectedTransaction{ + txn: &coin.Transaction{}, + }, + addrs: make([]string, 0), + }, + { + name: "incorrect output", + ujTxn: &SkycoinUninjectedTransaction{ + txn: &coin.Transaction{ + Out: []coin.TransactionOutput{ + coin.TransactionOutput{ + Coins: 9223372036854775808, + }, + }, + }, + }, + wantNil: true, + }, + { + name: "some outputs", + ujTxn: &SkycoinUninjectedTransaction{ + txn: &coin.Transaction{ + Out: []coin.TransactionOutput{ + coin.TransactionOutput{ + Address: addr, + }, + coin.TransactionOutput{ + Address: addr1, + }, + }, + }, + }, + addrs: []string{addr.String(), addr1.String()}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + outputs := tt.ujTxn.GetOutputs() + if tt.wantNil { + require.Nil(t, outputs) + } else { + require.Equal(t, len(tt.addrs), len(outputs)) + hashes := make([]string, len(tt.addrs)) + for i, out := range outputs { + hashes[i] = out.GetAddress().String() + } + requirethat.ElementsMatch(t, tt.addrs, hashes) + } + }) + } +} From 9ffcc1250ac769d8f297a680284521946d89079d Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 17:09:20 -0500 Subject: [PATCH 20/41] [test] refs #279 - Add `TestSkycoinUninjectedTransactionGetId` --- src/coin/skycoin/models/coin_test.go | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index e1ca3143..3ef28fa7 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1001,3 +1001,39 @@ func TestSkycoinUninjectedTransactionGetOutputs(t *testing.T) { }) } } + +func TestSkycoinUninjectedTransactionGetId(t *testing.T) { + tests := []struct { + ujTxn *SkycoinUninjectedTransaction + want string + }{ + { + ujTxn: &SkycoinUninjectedTransaction{ + txn: new(coin.Transaction), + }, + want: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", + }, + { + ujTxn: &SkycoinUninjectedTransaction{ + txn: &coin.Transaction{ + Length: 5, + }, + }, + want: "03e228f59704bc30de09f76fe9db0981ca77b6421aaa997e227d39bcc317174e", + }, + { + ujTxn: &SkycoinUninjectedTransaction{ + txn: &coin.Transaction{ + Type: 2, + }, + }, + want: "bb5e828965130b51e627725f6fea3247124da6799d28ccac81c247fd78b34621", + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("ID%d", i), func(t *testing.T) { + require.Equal(t, tt.want, tt.ujTxn.GetId()) + }) + } +} From 9f544632905a9ad0a62a898c3a5516677337e6d3 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 17:27:13 -0500 Subject: [PATCH 21/41] [test] refs #279 - Add `TestTransactionsGetTimestamp` Delete other `GetTimestamp` tests of diferents structures and merge them in one generic method that test all `core.Transaction:GetTimestamp` implementations --- src/coin/skycoin/models/coin_test.go | 48 +++++++++++++++++++++------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 3ef28fa7..12a66af9 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -265,9 +265,43 @@ func TestPendingTxnStatus(t *testing.T) { require.Equal(t, core.TXN_STATUS_PENDING, pendTxn.GetStatus()) } -func TestUninjectedTxnTimestamp(t *testing.T) { - coreTxn := new(SkycoinUninjectedTransaction) - require.Equal(t, core.Timestamp(0), coreTxn.GetTimestamp()) +func TestTransactionsGetTimestamp(t *testing.T) { + cur := time.Now() + tests := []struct { + name string + txn core.Transaction + want uint64 + }{ + { + name: "SkycoinUninjectTransaction", + txn: new(SkycoinUninjectedTransaction), + want: 0, + }, + { + name: "SkycoinTransaction", + txn: &SkycoinTransaction{ + skyTxn: readable.TransactionVerbose{ + Timestamp: 42, + }, + }, + want: 42, + }, + { + name: "SkycoinPendingTransaction", + txn: &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Received: cur, + }, + }, + want: uint64(cur.Unix()), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, core.Timestamp(tt.want), tt.txn.GetTimestamp()) + }) + } } func TestUninjectedTxnStatus(t *testing.T) { @@ -322,14 +356,6 @@ func TestSkycoinCreatedTxnFee(t *testing.T) { testutil.RequireError(t, err, "Invalid ticker") } -func TestPendingTxnGetTimestamp(t *testing.T) { - cur := time.Now() - sTxn := new(SkycoinPendingTransaction) - sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{Received: cur} - - require.Equal(t, core.Timestamp(cur.Unix()), sTxn.GetTimestamp()) -} - func TestPendingTxnGetInputs(t *testing.T) { hashes := make([]string, 0) for i := 0; i < 10; i++ { From df2369975be00ec92a37362323a4d34395210b20 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 17:47:52 -0500 Subject: [PATCH 22/41] [test] refs #279 - Add `TestTransactionGetStatus` Delete other `GetStatus` tests of diferents structures and merge them in one generic method that test all `core.Transaction:GetStatus` implementations --- src/coin/skycoin/models/coin_test.go | 65 ++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 12a66af9..4059977f 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -20,7 +20,7 @@ import ( "github.com/fibercrypto/fibercryptowallet/src/util/requirethat" ) -func TestSkycoinTransactionGetStatus(t *testing.T) { +func TestTransactionGetStatus(t *testing.T) { global_mock.On("Transaction", "hash1").Return( &readable.TransactionWithStatus{ Status: readable.TransactionStatus{ @@ -28,7 +28,7 @@ func TestSkycoinTransactionGetStatus(t *testing.T) { }, }, nil, - ) + ).Once() global_mock.On("Transaction", "hash2").Return( &readable.TransactionWithStatus{ Status: readable.TransactionStatus{ @@ -36,15 +36,52 @@ func TestSkycoinTransactionGetStatus(t *testing.T) { }, }, nil, - ) + ).Once() - thx1 := &SkycoinTransaction{skyTxn: readable.TransactionVerbose{}} - thx1.skyTxn.Hash = "hash1" - thx2 := &SkycoinTransaction{skyTxn: readable.TransactionVerbose{}} - thx2.skyTxn.Hash = "hash2" + tests := []struct { + name string + txn core.Transaction + want core.TransactionStatus + }{ + { + name: "SkycoinTransaction-Confirmed", + txn: &SkycoinTransaction{ + skyTxn: readable.TransactionVerbose{ + BlockTransactionVerbose: readable.BlockTransactionVerbose{ + Hash: "hash1", + }, + }, + }, + want: core.TXN_STATUS_CONFIRMED, + }, + { + name: "SkycoinTransaction-Unconfirmed", + txn: &SkycoinTransaction{ + skyTxn: readable.TransactionVerbose{ + BlockTransactionVerbose: readable.BlockTransactionVerbose{ + Hash: "hash2", + }, + }, + }, + want: core.TXN_STATUS_PENDING, + }, + { + name: "SkycoinPendingTransaction", + txn: new(SkycoinPendingTransaction), + want: core.TXN_STATUS_PENDING, + }, + { + name: "SkycoinUnjectedTransaction", + txn: new(SkycoinUninjectedTransaction), + want: core.TXN_STATUS_CREATED, + }, + } - require.Equal(t, thx1.GetStatus(), core.TXN_STATUS_CONFIRMED) - require.Equal(t, thx2.GetStatus(), core.TXN_STATUS_PENDING) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, tt.txn.GetStatus()) + }) + } } func TestSkycoinTransactionGetInputs(t *testing.T) { @@ -260,11 +297,6 @@ func TestSupportedAssets(t *testing.T) { requirethat.ElementsMatch(t, []string{Sky, CoinHour, CalculatedHour}, assets) } -func TestPendingTxnStatus(t *testing.T) { - pendTxn := new(SkycoinPendingTransaction) - require.Equal(t, core.TXN_STATUS_PENDING, pendTxn.GetStatus()) -} - func TestTransactionsGetTimestamp(t *testing.T) { cur := time.Now() tests := []struct { @@ -304,11 +336,6 @@ func TestTransactionsGetTimestamp(t *testing.T) { } } -func TestUninjectedTxnStatus(t *testing.T) { - coreTxn := new(SkycoinUninjectedTransaction) - require.Equal(t, core.TXN_STATUS_CREATED, coreTxn.GetStatus()) -} - func TestUninjectedTxnFee(t *testing.T) { coreTxn := new(SkycoinUninjectedTransaction) From f8c9d42f0aee24486388b07b9b0df525e6dd7730 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 17:53:56 -0500 Subject: [PATCH 23/41] [test] refs #279 - Update `TestTransactionGetStatus` --- src/coin/skycoin/models/coin_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 4059977f..5258fe16 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -37,6 +37,7 @@ func TestTransactionGetStatus(t *testing.T) { }, nil, ).Once() + global_mock.On("Transaction", "hash3").Return(nil, goerrors.New("failure")).Once() tests := []struct { name string @@ -65,6 +66,17 @@ func TestTransactionGetStatus(t *testing.T) { }, want: core.TXN_STATUS_PENDING, }, + { + name: "SkycoinTransaction-ApiError", + txn: &SkycoinTransaction{ + skyTxn: readable.TransactionVerbose{ + BlockTransactionVerbose: readable.BlockTransactionVerbose{ + Hash: "hash3", + }, + }, + }, + want: core.TXN_STATUS_CREATED, + }, { name: "SkycoinPendingTransaction", txn: new(SkycoinPendingTransaction), From ab0c686983992c00ab50a227251595af8c9befba Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 18:33:53 -0500 Subject: [PATCH 24/41] [test] refs #279 - Update `TestSkycoinTransactionGetInputs` cover all the method --- src/coin/skycoin/models/coin_test.go | 95 +++++++++++++++++++--------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 5258fe16..faef3a16 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -97,40 +97,77 @@ func TestTransactionGetStatus(t *testing.T) { } func TestSkycoinTransactionGetInputs(t *testing.T) { - //set correct return value + skyAmount := uint64(20000000) + chAmount := uint64(20) + response := &readable.TransactionWithStatusVerbose{ - Transaction: readable.TransactionVerbose{}, + Transaction: readable.TransactionVerbose{ + BlockTransactionVerbose: readable.BlockTransactionVerbose{ + In: []readable.TransactionInput{ + readable.TransactionInput{ + Hash: "I1", + Coins: "20", + Hours: chAmount, + }, + readable.TransactionInput{ + Hash: "I2", + Coins: "20", + Hours: uint64(20), + }, + }, + }, + }, } - response.Transaction.In = []readable.TransactionInput{ - readable.TransactionInput{ - Hash: "I1", - Coins: "20", - Hours: uint64(20), - CalculatedHours: uint64(20), + global_mock.On("TransactionVerbose", "hash1").Return(nil, goerrors.New("failure")).Once() + global_mock.On("TransactionVerbose", "hash1").Return(response, nil).Once() + + st := new(SkycoinTransaction) + st.skyTxn.Hash = "hash1" + + tests := []struct { + name string + txn core.Transaction + ids []string + wantNil bool + }{ + { + name: "SkycoinTransaction-ApiError", + txn: st, + wantNil: true, }, - readable.TransactionInput{ - Hash: "I2", - Coins: "20", - Hours: uint64(20), - CalculatedHours: uint64(20), + { + name: "SkycoinTransaction", + txn: st, + ids: []string{"I1", "I2"}, + }, + { + name: "SkycoinTransaction-InputsSaved", + txn: st, + ids: []string{"I1", "I2"}, }, } - global_mock.On("TransactionVerbose", "hash1").Return(response, nil) - - thx1 := &SkycoinTransaction{skyTxn: readable.TransactionVerbose{}} - thx1.skyTxn.Hash = "hash1" - - inputs := thx1.GetInputs() - require.Equal(t, inputs[0].GetId(), "I1") - require.Equal(t, inputs[1].GetId(), "I2") - it := NewSkycoinTransactioninputIterator(inputs) - for it.Next() { - sky, err := it.Value().GetCoins(Sky) - require.NoError(t, err) - require.Equal(t, sky, uint64(20000000)) - hours, err1 := it.Value().GetCoins(CoinHour) - require.NoError(t, err1) - require.Equal(t, hours, uint64(20)) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + inputs := tt.txn.GetInputs() + if tt.wantNil { + require.Nil(t, inputs) + } else { + ids := make([]string, 0) + it := NewSkycoinTransactioninputIterator(inputs) + for it.Next() { + input := it.Value() + ids = append(ids, input.GetId()) + sky, err := input.GetCoins(Sky) + require.NoError(t, err) + require.Equal(t, skyAmount, sky) + hours, err1 := it.Value().GetCoins(CoinHour) + require.NoError(t, err1) + require.Equal(t, chAmount, hours) + } + requirethat.ElementsMatch(t, tt.ids, ids) + } + }) } } From 543a6fa538122496e6406372d5de9d4074051fd7 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 20:13:58 -0500 Subject: [PATCH 25/41] [test] refs #279 - Add 2 test Tests: - `TestSkycoinTransactionGetOutputs` - `TestTransactionComputeFee` Changes: - Delete other `ComputeFee` tests of diferents structures and merge them in one generic method that test all `core.Transaction:ComputeFee` implementations --- src/coin/skycoin/models/coin_test.go | 265 +++++++++++++++++++-------- 1 file changed, 187 insertions(+), 78 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index faef3a16..62496fe1 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -4,6 +4,7 @@ import ( "encoding/hex" goerrors "errors" "fmt" + "strconv" "testing" "time" @@ -13,7 +14,6 @@ import ( "github.com/SkycoinProject/skycoin/src/cipher" "github.com/SkycoinProject/skycoin/src/coin" "github.com/SkycoinProject/skycoin/src/readable" - "github.com/SkycoinProject/skycoin/src/testutil" "github.com/fibercrypto/fibercryptowallet/src/coin/mocks" "github.com/fibercrypto/fibercryptowallet/src/core" "github.com/fibercrypto/fibercryptowallet/src/errors" @@ -161,7 +161,7 @@ func TestSkycoinTransactionGetInputs(t *testing.T) { sky, err := input.GetCoins(Sky) require.NoError(t, err) require.Equal(t, skyAmount, sky) - hours, err1 := it.Value().GetCoins(CoinHour) + hours, err1 := input.GetCoins(CoinHour) require.NoError(t, err1) require.Equal(t, chAmount, hours) } @@ -385,53 +385,6 @@ func TestTransactionsGetTimestamp(t *testing.T) { } } -func TestUninjectedTxnFee(t *testing.T) { - coreTxn := new(SkycoinUninjectedTransaction) - - fee, err := coreTxn.ComputeFee(Sky) - require.NoError(t, err) - require.Equal(t, uint64(0), fee) - - _, err = coreTxn.ComputeFee(CalculatedHour) - testutil.RequireError(t, err, "Feature not implemented") - - coreTxn.fee = 64 - fee, err = coreTxn.ComputeFee(CoinHour) - require.NoError(t, err) - require.Equal(t, uint64(64), fee) - - _, err = coreTxn.ComputeFee("NOCOINATALL") - testutil.RequireError(t, err, "Invalid ticker") -} - -func TestSkycoinTxnFee(t *testing.T) { - skyTxn := new(SkycoinTransaction) - - fee, err := skyTxn.ComputeFee(Sky) - require.NoError(t, err) - require.Equal(t, uint64(0), fee) - - _, err = skyTxn.ComputeFee(CalculatedHour) - testutil.RequireError(t, err, "Feature not implemented") - - _, err = skyTxn.ComputeFee("NOCOINATALL") - testutil.RequireError(t, err, "Invalid ticker") -} - -func TestSkycoinCreatedTxnFee(t *testing.T) { - cTxn := new(SkycoinCreatedTransaction) - - fee, err := cTxn.ComputeFee(Sky) - require.NoError(t, err) - require.Equal(t, uint64(0), fee) - - _, err = cTxn.ComputeFee(CalculatedHour) - testutil.RequireError(t, err, "Feature not implemented") - - _, err = cTxn.ComputeFee("NOCOINATALL") - testutil.RequireError(t, err, "Invalid ticker") -} - func TestPendingTxnGetInputs(t *testing.T) { hashes := make([]string, 0) for i := 0; i < 10; i++ { @@ -494,35 +447,6 @@ func TestPendingTxnGetId(t *testing.T) { } } -func TestPendingTxnComputeFee(t *testing.T) { - tests := []struct { - ticker string - fee uint64 - amount uint64 - wantedError error - }{ - {ticker: Sky, wantedError: nil}, - {ticker: CoinHour, fee: 20, amount: 20, wantedError: nil}, - {ticker: CoinHour, fee: 42, amount: 42, wantedError: nil}, - {ticker: CalculatedHour, wantedError: errors.ErrNotImplemented}, - {ticker: "INVALIDTICKER", wantedError: errors.ErrInvalidAltcoinTicker}, - } - - for _, tt := range tests { - t.Run(tt.ticker, func(t *testing.T) { - sTxn := new(SkycoinPendingTransaction) - sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{ - Transaction: readable.BlockTransactionVerbose{ - Fee: tt.fee, - }, - } - amount, err := sTxn.ComputeFee(tt.ticker) - require.Equal(t, tt.amount, amount) - require.Equal(t, tt.wantedError, err) - }) - } -} - func Test_newCreatedTransactionOutput(t *testing.T) { tests := []struct { uxID string @@ -1139,3 +1063,188 @@ func TestSkycoinUninjectedTransactionGetId(t *testing.T) { }) } } + +func TestSkycoinTransactionGetOutputs(t *testing.T) { + skyAmount := uint64(20000000) + chAmount := uint64(20) + + st := new(SkycoinTransaction) + outs := []readable.TransactionOutput{ + readable.TransactionOutput{ + Hash: "O1", + Coins: "20", + Hours: chAmount, + }, + readable.TransactionOutput{ + Hash: "O2", + Coins: "20", + Hours: chAmount, + }, + } + st.skyTxn.Out = outs + + tests := []struct { + name string + txn core.Transaction + ids []string + wantNil bool + }{ + { + name: "SkycoinTransaction1", + txn: st, + ids: []string{"O1", "O2"}, + }, + { + name: "SkycoinTransaction1-OutputsSaved", + txn: st, + ids: []string{"O1", "O2"}, + }, + { + name: "SkycoinTransaction1-OutputsSaved", + txn: &SkycoinTransaction{ + skyTxn: readable.TransactionVerbose{ + BlockTransactionVerbose: readable.BlockTransactionVerbose{ + Out: outs[:1], + }, + }, + }, + ids: []string{"O1"}, + }, + { + name: "SkycoinTransaction1-NoOutputs", + txn: new(SkycoinTransaction), + ids: []string{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + outputs := tt.txn.GetOutputs() + if tt.wantNil { + require.Nil(t, outputs) + } else { + ids := make([]string, 0) + it := NewSkycoinTransactionOutputIterator(outputs) + for it.Next() { + output := it.Value() + ids = append(ids, output.GetId()) + sky, err := output.GetCoins(Sky) + require.NoError(t, err) + require.Equal(t, skyAmount, sky) + hours, err1 := output.GetCoins(CoinHour) + require.NoError(t, err1) + require.Equal(t, chAmount, hours) + } + requirethat.ElementsMatch(t, tt.ids, ids) + } + }) + } +} + +func TestTransactionComputeFee(t *testing.T) { + expectedError := func(ticker string) error { + if ticker == CalculatedHour { + return errors.ErrNotImplemented + } + if ticker == Sky || ticker == CoinHour { + return nil + } + return errors.ErrInvalidAltcoinTicker + } + expectedAmount := func(ticker string, amount uint64) uint64 { + if ticker == CoinHour { + return amount + } + return 0 + } + pendingTxnWithFee := func(ticker string, fee string) (core.Transaction, uint64, bool, error) { + val, err := strconv.ParseUint(fee, 10, 64) + if err != nil { + return nil, 0, false, nil + } + return &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + Fee: val, + }, + }, + }, expectedAmount(ticker, val), true, expectedError(ticker) + } + uninjectedTxnWithFee := func(ticker string, fee string) (core.Transaction, uint64, bool, error) { + val, err := strconv.ParseUint(fee, 10, 64) + if err != nil { + return nil, 0, false, nil + } + return &SkycoinUninjectedTransaction{fee: val}, expectedAmount(ticker, val), true, expectedError(ticker) + } + skycoinTxnWithFee := func(ticker string, fee string) (core.Transaction, uint64, bool, error) { + val, err := strconv.ParseUint(fee, 10, 64) + if err != nil { + return nil, 0, false, nil + } + return &SkycoinTransaction{ + skyTxn: readable.TransactionVerbose{ + BlockTransactionVerbose: readable.BlockTransactionVerbose{ + Fee: val, + }, + }, + }, expectedAmount(ticker, val), true, expectedError(ticker) + } + createdTxnWithFee := func(ticker string, fee string) (core.Transaction, uint64, bool, error) { + val, err := strconv.ParseInt(fee, 10, 64) + var expError error + if err != nil && ticker == CoinHour { + expError = err + } else { + expError = expectedError(ticker) + } + return &SkycoinCreatedTransaction{ + skyTxn: api.CreatedTransaction{ + Fee: fee, + }, + }, expectedAmount(ticker, uint64(val)), true, expError + } + + tests := []struct { + name string + generator func(string, string) (core.Transaction, uint64, bool, error) + }{ + { + name: "SkycoinPendingTransaction", + generator: pendingTxnWithFee, + }, + { + name: "SkycoinUninjectedTransaction", + generator: uninjectedTxnWithFee, + }, + { + name: "SkycoinTransaction", + generator: skycoinTxnWithFee, + }, + { + name: "SkycoinCreatedTransaction", + generator: createdTxnWithFee, + }, + } + tickers := []string{Sky, CoinHour, CalculatedHour, "INVALIDTICKER"} + amounts := []string{"1", "2", "42", "100", "42,42", "1,1"} + + for _, tt := range tests { + for _, ticker := range tickers { + for _, amount := range amounts { + t.Run(tt.name+"-"+ticker, func(t *testing.T) { + thx, expected, valid, err := tt.generator(ticker, amount) + if valid { + val, err1 := thx.ComputeFee(ticker) + if err != nil { + require.Equal(t, err, err1) + } else { + require.NoError(t, err1) + require.Equal(t, expected, val) + } + } + }) + } + } + } +} From 30953e426d15a0d177c7484fdbab7109dbfaead4 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 20:37:24 -0500 Subject: [PATCH 26/41] [test] refs #279 - Add `TestTransactionIsFullySigned` Changes: - `TestTransactionIsFullySigned` changed to the new mentioned test with the purpose of be generic --- src/coin/skycoin/models/coin_test.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 62496fe1..840dc61d 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -15,6 +15,7 @@ import ( "github.com/SkycoinProject/skycoin/src/coin" "github.com/SkycoinProject/skycoin/src/readable" "github.com/fibercrypto/fibercryptowallet/src/coin/mocks" + "github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/skytypes" "github.com/fibercrypto/fibercryptowallet/src/core" "github.com/fibercrypto/fibercryptowallet/src/errors" "github.com/fibercrypto/fibercryptowallet/src/util/requirethat" @@ -925,18 +926,22 @@ func Test_checkFullySigned(t *testing.T) { //TODO: add valid test } -func TestPendingTxnIsFullySigned(t *testing.T) { +func TestTransactionIsFullySigned(t *testing.T) { + type skyTxn interface { + skytypes.ReadableTxn + core.Transaction + } tests := []struct { name string - sTxn *SkycoinPendingTransaction + txn skyTxn }{ { - name: "empty transaction", - sTxn: &SkycoinPendingTransaction{Transaction: new(readable.UnconfirmedTransactionVerbose)}, + name: "empty PendingTxn", + txn: &SkycoinPendingTransaction{Transaction: new(readable.UnconfirmedTransactionVerbose)}, }, { - name: "cero outputs", - sTxn: &SkycoinPendingTransaction{ + name: "cero outputs in PendingTxn", + txn: &SkycoinPendingTransaction{ Transaction: &readable.UnconfirmedTransactionVerbose{ Transaction: readable.BlockTransactionVerbose{ In: []readable.TransactionInput{readable.TransactionInput{}}, @@ -944,13 +949,17 @@ func TestPendingTxnIsFullySigned(t *testing.T) { }, }, }, + { + name: "empty Txn", + txn: new(SkycoinTransaction), + }, //TODO: add valid tests } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - fully, err := checkFullySigned(tt.sTxn) - fully1, err1 := tt.sTxn.IsFullySigned() + fully, err := checkFullySigned(tt.txn) + fully1, err1 := tt.txn.IsFullySigned() require.Equal(t, err, err1) require.Equal(t, fully, fully1) }) From b8a68176244e04d6ba47601bdcdad09a4369112f Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 21:00:55 -0500 Subject: [PATCH 27/41] [test] refs #279 - Add 2 test `TestTransactionVerifyUnsigned` and `TestTransactionVerifySigned` --- src/coin/skycoin/models/coin_test.go | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 840dc61d..20747f4c 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1257,3 +1257,53 @@ func TestTransactionComputeFee(t *testing.T) { } } } + +func TestTransactionVerifyUnsigned(t *testing.T) { + type skyTxn interface { + skytypes.ReadableTxn + core.Transaction + } + tests := []struct { + name string + txn skyTxn + }{ + { + name: "empty Txn", + txn: new(SkycoinTransaction), + }, + //TODO: add better tests + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := verifyReadableTransaction(tt.txn, false) + err1 := tt.txn.VerifyUnsigned() + require.Equal(t, err, err1) + }) + } +} + +func TestTransactionVerifySigned(t *testing.T) { + type skyTxn interface { + skytypes.ReadableTxn + core.Transaction + } + tests := []struct { + name string + txn skyTxn + }{ + { + name: "empty Txn", + txn: new(SkycoinTransaction), + }, + //TODO: add better tests + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := verifyReadableTransaction(tt.txn, true) + err1 := tt.txn.VerifySigned() + require.Equal(t, err, err1) + }) + } +} From eaad386a004fd56c8acd63d3a09cca278397a518 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 21:44:31 -0500 Subject: [PATCH 28/41] [test] refs #279 - Add `Test_getSkycoinTransactionInputsFromTxnHash` --- src/coin/skycoin/models/coin_test.go | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 20747f4c..0f98bd4a 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1307,3 +1307,50 @@ func TestTransactionVerifySigned(t *testing.T) { }) } } + +func Test_getSkycoinTransactionInputsFromTxnHash(t *testing.T) { + CleanGlobalMock() + + inputs := []readable.TransactionInput{ + readable.TransactionInput{ + Hash: "hash1", + }, + readable.TransactionInput{ + Hash: "hash2", + }, + readable.TransactionInput{ + Hash: "hash3", + }, + readable.TransactionInput{ + Hash: "hash4", + }, + } + + for len(inputs) > 0 { + global_mock.On("TransactionVerbose", "hash").Return( + &readable.TransactionWithStatusVerbose{ + Transaction: readable.TransactionVerbose{ + BlockTransactionVerbose: readable.BlockTransactionVerbose{ + In: inputs, + }, + }, + }, nil, + ).Once() + t.Run("InputsFromTxnHash", func(t *testing.T) { + txnInputs, err := getSkycoinTransactionInputsFromTxnHash("hash") + require.NoError(t, err) + rawInputs := make([]readable.TransactionInput, len(txnInputs)) + for i, in := range txnInputs { + skyIn, valid := in.(*SkycoinTransactionInput) + require.True(t, valid) + require.Nil(t, skyIn.spentOutput) + rawInputs[i] = skyIn.skyIn + } + requirethat.ElementsMatch(t, inputs, rawInputs) + }) + inputs = inputs[:len(inputs)-1] + } + global_mock.On("TransactionVerbose", "hash").Return(nil, goerrors.New("failure")) + _, err := getSkycoinTransactionInputsFromTxnHash("hash") + require.Error(t, err) +} From 271e507ad4d44e4685678c4ae561bc7cf2d2687a Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 22:02:15 -0500 Subject: [PATCH 29/41] [test] refs #279 - Update `TestSkycoinTransactionInputGetSpentOutput` --- src/coin/skycoin/models/coin_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 0f98bd4a..63a6e947 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -173,6 +173,13 @@ func TestSkycoinTransactionGetInputs(t *testing.T) { } func TestSkycoinTransactionInputGetSpentOutput(t *testing.T) { + CleanGlobalMock() + + input := &SkycoinTransactionInput{skyIn: readable.TransactionInput{Hash: "in1"}} + global_mock.On("UxOut", "in1").Return(nil, goerrors.New("failure")).Once() + output := input.GetSpentOutput() + require.Nil(t, output) + global_mock.On("UxOut", "in1").Return( &readable.SpentOutput{ OwnerAddress: "2JJ8pgq8EDAnrzf9xxBJapE2qkYLefW4uF8", @@ -181,10 +188,9 @@ func TestSkycoinTransactionInputGetSpentOutput(t *testing.T) { Uxid: "out1", }, nil, - ) + ).Once() - input := &SkycoinTransactionInput{skyIn: readable.TransactionInput{Hash: "in1"}} - output := input.GetSpentOutput() + output = input.GetSpentOutput() t.Logf("%#v", output) require.Equal(t, output.GetId(), "out1") From 132ea8792b65344e081b6bfaa359ac3a65ef83d9 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 23:02:27 -0500 Subject: [PATCH 30/41] [test] refs #279 - Add `TestSkycoinTransactionInputGetCoins` --- src/coin/skycoin/models/coin_test.go | 80 ++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 63a6e947..52c6a7ae 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1360,3 +1360,83 @@ func Test_getSkycoinTransactionInputsFromTxnHash(t *testing.T) { _, err := getSkycoinTransactionInputsFromTxnHash("hash") require.Error(t, err) } + +func TestSkycoinTransactionInputGetCoins(t *testing.T) { + invalidTicker := "INVALIDTICKER" + tests := []struct { + name string + input core.TransactionInput + ticker string + want uint64 + err bool + }{ + { + name: "SkycoinTransactionInput", + ticker: invalidTicker, + input: new(SkycoinTransactionInput), + err: true, + }, + { + name: "SkycoinTransactionInput", + ticker: Sky, + input: &SkycoinTransactionInput{ + skyIn: readable.TransactionInput{ + Coins: "20", + }, + }, + want: 20000000, + }, + { + name: "SkycoinTransactionInput", + ticker: Sky, + input: &SkycoinTransactionInput{ + skyIn: readable.TransactionInput{ + Coins: "20.1", + }, + }, + want: 20100000, + }, + { + name: "SkycoinTransactionInput", + ticker: Sky, + input: &SkycoinTransactionInput{ + skyIn: readable.TransactionInput{ + Coins: "20,1a", + }, + }, + err: true, + }, + { + name: "SkycoinTransactionInput", + ticker: CoinHour, + input: &SkycoinTransactionInput{ + skyIn: readable.TransactionInput{ + Hours: 42, + }, + }, + want: 42, + }, + { + name: "SkycoinTransactionInput", + ticker: CalculatedHour, + input: &SkycoinTransactionInput{ + skyIn: readable.TransactionInput{ + CalculatedHours: 42, + }, + }, + want: 42, + }, + } + + for _, tt := range tests { + t.Run(tt.name+"-"+tt.ticker, func(t *testing.T) { + amount, err := tt.input.GetCoins(tt.ticker) + if tt.err { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, tt.want, amount) + } + }) + } +} From 13234858f18ee39d2f8026e5c8ac7c9db00d2b31 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 23:06:11 -0500 Subject: [PATCH 31/41] [test] refs #279 - Add `TestSkycoinTransactionOutputGetAddress` --- src/coin/skycoin/models/coin_test.go | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 52c6a7ae..f7985329 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1440,3 +1440,42 @@ func TestSkycoinTransactionInputGetCoins(t *testing.T) { }) } } + +func TestSkycoinTransactionOutputGetAddress(t *testing.T) { + strAddr := makeAddress().String() + + tests := []struct { + name string + output core.TransactionOutput + err bool + want string + isNil bool + }{ + { + name: "SkycoinTransactionOutput-empty", + output: new(SkycoinTransactionOutput), + isNil: true, + }, + { + name: "SkycoinTransactionOutput-empty", + output: &SkycoinTransactionOutput{ + skyOut: readable.TransactionOutput{ + Address: strAddr, + }, + }, + want: strAddr, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + addr := tt.output.GetAddress() + if tt.isNil { + require.Nil(t, addr) + } else { + require.NotNil(t, addr) + require.Equal(t, tt.want, addr.String()) + } + }) + } +} From c325bd54027f2e9fdda22f86a2f532891178cb8e Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 23:18:28 -0500 Subject: [PATCH 32/41] [test] refs #279 - Change `TestSkycoinTransactionInputGetCoins` to `TestGetCoins` Make the test more generic. Every object with `GetCoins` method could be tested using it --- src/coin/skycoin/models/coin_test.go | 63 +++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index f7985329..cc8dceee 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1361,11 +1361,15 @@ func Test_getSkycoinTransactionInputsFromTxnHash(t *testing.T) { require.Error(t, err) } -func TestSkycoinTransactionInputGetCoins(t *testing.T) { +func TestGetCoins(t *testing.T) { + type CoinPackage interface { + GetCoins(string) (uint64, error) + } + invalidTicker := "INVALIDTICKER" tests := []struct { name string - input core.TransactionInput + input CoinPackage ticker string want uint64 err bool @@ -1426,6 +1430,61 @@ func TestSkycoinTransactionInputGetCoins(t *testing.T) { }, want: 42, }, + // TransactionOutput + { + name: "SkycoinTransactionOutput", + ticker: invalidTicker, + input: new(SkycoinTransactionOutput), + err: true, + }, + { + name: "SkycoinTransactionOutput", + ticker: Sky, + input: &SkycoinTransactionOutput{ + skyOut: readable.TransactionOutput{ + Coins: "20", + }, + }, + want: 20000000, + }, + { + name: "SkycoinTransactionOutput", + ticker: Sky, + input: &SkycoinTransactionOutput{ + skyOut: readable.TransactionOutput{ + Coins: "20.1", + }, + }, + want: 20100000, + }, + { + name: "SkycoinTransactionOutput", + ticker: Sky, + input: &SkycoinTransactionOutput{ + skyOut: readable.TransactionOutput{ + Coins: "20,1a", + }, + }, + err: true, + }, + { + name: "SkycoinTransactionOutput", + ticker: CoinHour, + input: &SkycoinTransactionOutput{ + skyOut: readable.TransactionOutput{ + Hours: 42, + }, + }, + want: 42, + }, + { + name: "SkycoinTransactionOutput", + ticker: CalculatedHour, + input: &SkycoinTransactionOutput{ + calculatedHours: 42, + }, + want: 42, + }, } for _, tt := range tests { From ab7ee722e056ff0b6fed5e0a0caf5cfcbf9d9a27 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 23:38:41 -0500 Subject: [PATCH 33/41] [test] refs #279 - Update `TestSkycoinTransactionOutputIsSpent` --- src/coin/skycoin/models/coin_test.go | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index cc8dceee..185db3c7 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -204,24 +204,17 @@ func TestSkycoinTransactionInputGetSpentOutput(t *testing.T) { } func TestSkycoinTransactionOutputIsSpent(t *testing.T) { - global_mock.On("UxOut", "out1").Return( - &readable.SpentOutput{ - SpentTxnID: "0000000000000000000000000000000000000000000000000000000000000000", - }, - nil, - ) - global_mock.On("UxOut", "out2").Return( - &readable.SpentOutput{ - SpentTxnID: "0", - }, - nil, - ) + badID := "0000000000000000000000000000000000000000000000000000000000000000" + global_mock.On("UxOut", "out").Return(nil, goerrors.New("failure")).Once() + global_mock.On("UxOut", "out").Return(&readable.SpentOutput{SpentTxnID: badID}, nil).Once() + global_mock.On("UxOut", "out").Return(&readable.SpentOutput{SpentTxnID: "42"}, nil).Once() - output1 := &SkycoinTransactionOutput{skyOut: readable.TransactionOutput{Hash: "out1"}} - output2 := &SkycoinTransactionOutput{skyOut: readable.TransactionOutput{Hash: "out2"}} + output := &SkycoinTransactionOutput{skyOut: readable.TransactionOutput{Hash: "out"}} - require.Equal(t, output1.IsSpent(), false) - require.Equal(t, output2.IsSpent(), true) + require.Equal(t, output.IsSpent(), false) + require.Equal(t, output.IsSpent(), false) + require.Equal(t, output.IsSpent(), true) + require.Equal(t, output.IsSpent(), true) } func TestUninjectedTransactionSignedUnsigned(t *testing.T) { From d078ba02c0b31aec5f298eaf7684b72500a9a42d Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sat, 25 Jan 2020 23:39:54 -0500 Subject: [PATCH 34/41] [test] refs #279 - Add `TestGet` Remove other `GetId` test, make a new one generic for all object that have a method `GetId` --- src/coin/skycoin/models/coin_test.go | 71 ++++++++++++++++++---------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 185db3c7..3980f387 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -429,24 +429,6 @@ func TestPendingTxnGetOutputs(t *testing.T) { requirethat.ElementsMatch(t, hashes, outHashes) } -func TestPendingTxnGetId(t *testing.T) { - hashes := make([]string, 0) - for i := 0; i < 10; i++ { - hashes = append(hashes, fmt.Sprintf("hash%d", i)) - } - for _, hash := range hashes { - t.Run(hash, func(t *testing.T) { - sTxn := new(SkycoinPendingTransaction) - sTxn.Transaction = &readable.UnconfirmedTransactionVerbose{ - Transaction: readable.BlockTransactionVerbose{ - Hash: hash, - }, - } - require.Equal(t, hash, sTxn.GetId()) - }) - } -} - func Test_newCreatedTransactionOutput(t *testing.T) { tests := []struct { uxID string @@ -1036,19 +1018,22 @@ func TestSkycoinUninjectedTransactionGetOutputs(t *testing.T) { } } -func TestSkycoinUninjectedTransactionGetId(t *testing.T) { +func TestGetId(t *testing.T) { + type ObjectWithID interface { + GetId() string + } tests := []struct { - ujTxn *SkycoinUninjectedTransaction - want string + obj ObjectWithID + want string }{ { - ujTxn: &SkycoinUninjectedTransaction{ + obj: &SkycoinUninjectedTransaction{ txn: new(coin.Transaction), }, want: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", }, { - ujTxn: &SkycoinUninjectedTransaction{ + obj: &SkycoinUninjectedTransaction{ txn: &coin.Transaction{ Length: 5, }, @@ -1056,18 +1041,54 @@ func TestSkycoinUninjectedTransactionGetId(t *testing.T) { want: "03e228f59704bc30de09f76fe9db0981ca77b6421aaa997e227d39bcc317174e", }, { - ujTxn: &SkycoinUninjectedTransaction{ + obj: &SkycoinUninjectedTransaction{ txn: &coin.Transaction{ Type: 2, }, }, want: "bb5e828965130b51e627725f6fea3247124da6799d28ccac81c247fd78b34621", }, + { + obj: &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + Hash: "hash1", + }, + }, + }, + want: "hash1", + }, + { + obj: &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + Hash: "hash2", + }, + }, + }, + want: "hash2", + }, + { + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + UxID: "uxid1", + }, + }, + want: "uxid1", + }, + { + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + UxID: "uxid2", + }, + }, + want: "uxid2", + }, } for i, tt := range tests { t.Run(fmt.Sprintf("ID%d", i), func(t *testing.T) { - require.Equal(t, tt.want, tt.ujTxn.GetId()) + require.Equal(t, tt.want, tt.obj.GetId()) }) } } From 433b56967a9de54eb5562202c780c66fe126ca1b Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sun, 26 Jan 2020 00:35:56 -0500 Subject: [PATCH 35/41] [test] refs #279 - Update `TestGetCoins` --- src/coin/skycoin/models/coin_test.go | 181 ++++++++++++++++++++++++--- 1 file changed, 166 insertions(+), 15 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 3980f387..baabc922 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1376,28 +1376,179 @@ func Test_getSkycoinTransactionInputsFromTxnHash(t *testing.T) { } func TestGetCoins(t *testing.T) { - type CoinPackage interface { + type ObjectWithCoins interface { GetCoins(string) (uint64, error) } invalidTicker := "INVALIDTICKER" tests := []struct { name string - input CoinPackage + obj ObjectWithCoins ticker string want uint64 err bool }{ + // SkycoinCreatedTransactionOutput + { + name: "SkycoinCreatedTransactionOutput", + ticker: invalidTicker, + obj: new(SkycoinCreatedTransactionOutput), + err: true, + }, + { + name: "SkycoinCreatedTransactionOutput", + ticker: Sky, + obj: &SkycoinCreatedTransactionOutput{ + skyOut: api.CreatedTransactionOutput{ + Coins: "20", + }, + }, + want: 20000000, + }, + { + name: "SkycoinCreatedTransactionOutput", + ticker: Sky, + obj: &SkycoinCreatedTransactionOutput{ + skyOut: api.CreatedTransactionOutput{ + Coins: "20.1", + }, + }, + want: 20100000, + }, + { + name: "SkycoinCreatedTransactionOutput", + ticker: Sky, + obj: &SkycoinCreatedTransactionOutput{ + skyOut: api.CreatedTransactionOutput{ + Coins: "20,1a", + }, + }, + err: true, + }, + { + name: "SkycoinCreatedTransactionOutput", + ticker: CoinHour, + obj: &SkycoinCreatedTransactionOutput{ + skyOut: api.CreatedTransactionOutput{ + Hours: "42", + }, + }, + want: 42, + }, + { + name: "SkycoinCreatedTransactionOutput", + ticker: CoinHour, + obj: &SkycoinCreatedTransactionOutput{ + skyOut: api.CreatedTransactionOutput{ + Hours: "42.1", + }, + }, + err: true, + }, + { + name: "SkycoinCreatedTransactionOutput", + ticker: CalculatedHour, + obj: &SkycoinCreatedTransactionOutput{ + calculatedHours: 42, + }, + want: 42, + }, + { + name: "SkycoinCreatedTransactionOutput", + ticker: CalculatedHour, + obj: &SkycoinCreatedTransactionOutput{ + calculatedHours: 50, + }, + want: 50, + }, + // SkycoinCreatedTransactionInput + { + name: "SkycoinCreatedTransactionInput", + ticker: invalidTicker, + obj: new(SkycoinCreatedTransactionInput), + err: true, + }, + { + name: "SkycoinCreatedTransactionInput", + ticker: Sky, + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + Coins: "20", + }, + }, + want: 20000000, + }, + { + name: "SkycoinCreatedTransactionInput", + ticker: Sky, + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + Coins: "20.1", + }, + }, + want: 20100000, + }, + { + name: "SkycoinCreatedTransactionInput", + ticker: Sky, + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + Coins: "20,1a", + }, + }, + err: true, + }, + { + name: "SkycoinCreatedTransactionInput", + ticker: CoinHour, + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + Hours: "42", + }, + }, + want: 42, + }, + { + name: "SkycoinCreatedTransactionInput", + ticker: CoinHour, + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + Hours: "42.1", + }, + }, + err: true, + }, + { + name: "SkycoinCreatedTransactionInput", + ticker: CalculatedHour, + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + CalculatedHours: "42", + }, + }, + want: 42, + }, + { + name: "SkycoinCreatedTransactionInput", + ticker: CalculatedHour, + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + CalculatedHours: "42.1", + }, + }, + err: true, + }, + // SkycoinTransactionInput { name: "SkycoinTransactionInput", ticker: invalidTicker, - input: new(SkycoinTransactionInput), + obj: new(SkycoinTransactionInput), err: true, }, { name: "SkycoinTransactionInput", ticker: Sky, - input: &SkycoinTransactionInput{ + obj: &SkycoinTransactionInput{ skyIn: readable.TransactionInput{ Coins: "20", }, @@ -1407,7 +1558,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionInput", ticker: Sky, - input: &SkycoinTransactionInput{ + obj: &SkycoinTransactionInput{ skyIn: readable.TransactionInput{ Coins: "20.1", }, @@ -1417,7 +1568,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionInput", ticker: Sky, - input: &SkycoinTransactionInput{ + obj: &SkycoinTransactionInput{ skyIn: readable.TransactionInput{ Coins: "20,1a", }, @@ -1427,7 +1578,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionInput", ticker: CoinHour, - input: &SkycoinTransactionInput{ + obj: &SkycoinTransactionInput{ skyIn: readable.TransactionInput{ Hours: 42, }, @@ -1437,7 +1588,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionInput", ticker: CalculatedHour, - input: &SkycoinTransactionInput{ + obj: &SkycoinTransactionInput{ skyIn: readable.TransactionInput{ CalculatedHours: 42, }, @@ -1448,13 +1599,13 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionOutput", ticker: invalidTicker, - input: new(SkycoinTransactionOutput), + obj: new(SkycoinTransactionOutput), err: true, }, { name: "SkycoinTransactionOutput", ticker: Sky, - input: &SkycoinTransactionOutput{ + obj: &SkycoinTransactionOutput{ skyOut: readable.TransactionOutput{ Coins: "20", }, @@ -1464,7 +1615,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionOutput", ticker: Sky, - input: &SkycoinTransactionOutput{ + obj: &SkycoinTransactionOutput{ skyOut: readable.TransactionOutput{ Coins: "20.1", }, @@ -1474,7 +1625,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionOutput", ticker: Sky, - input: &SkycoinTransactionOutput{ + obj: &SkycoinTransactionOutput{ skyOut: readable.TransactionOutput{ Coins: "20,1a", }, @@ -1484,7 +1635,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionOutput", ticker: CoinHour, - input: &SkycoinTransactionOutput{ + obj: &SkycoinTransactionOutput{ skyOut: readable.TransactionOutput{ Hours: 42, }, @@ -1494,7 +1645,7 @@ func TestGetCoins(t *testing.T) { { name: "SkycoinTransactionOutput", ticker: CalculatedHour, - input: &SkycoinTransactionOutput{ + obj: &SkycoinTransactionOutput{ calculatedHours: 42, }, want: 42, @@ -1503,7 +1654,7 @@ func TestGetCoins(t *testing.T) { for _, tt := range tests { t.Run(tt.name+"-"+tt.ticker, func(t *testing.T) { - amount, err := tt.input.GetCoins(tt.ticker) + amount, err := tt.obj.GetCoins(tt.ticker) if tt.err { require.Error(t, err) } else { From 2a1328cbc2fc514d1657853e1919311476b84a2d Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sun, 26 Jan 2020 00:36:29 -0500 Subject: [PATCH 36/41] [test] refs #279 - Add `TestSkycoinCreatedTransactionInputGetSpentOutput` --- src/coin/skycoin/models/coin_test.go | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index baabc922..bc3e88ed 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1703,3 +1703,78 @@ func TestSkycoinTransactionOutputGetAddress(t *testing.T) { }) } } + +func TestSkycoinCreatedTransactionInputGetSpentOutput(t *testing.T) { + tests := []struct { + addr string + coins string + hours string + uxid string + ccHours string + }{ + {addr: makeAddress().String(), coins: "1", hours: "1", uxid: "uxid1", ccHours: "1"}, + {addr: makeAddress().String(), coins: "2", hours: "2", uxid: "uxid2", ccHours: "2"}, + {addr: makeAddress().String(), coins: "3", hours: "3", uxid: "uxid3", ccHours: "3"}, + {addr: makeAddress().String(), coins: "4", hours: "4", uxid: "uxid4", ccHours: "4"}, + {addr: makeAddress().String(), coins: "5", hours: "5", uxid: "uxid5", ccHours: "5"}, + {addr: makeAddress().String(), coins: "6", hours: "6", uxid: "uxid6", ccHours: "6"}, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("GetSepent%d", i), func(t *testing.T) { + createdIn := &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + Address: tt.addr, + Coins: tt.coins, + Hours: tt.hours, + UxID: tt.uxid, + CalculatedHours: tt.ccHours, + }, + } + output := createdIn.GetSpentOutput() + createdOut, valid := output.(*SkycoinCreatedTransactionOutput) + require.True(t, valid) + for _, asset := range createdIn.SupportedAssets() { + cur, err := createdIn.GetCoins(asset) + require.NoError(t, err) + val, err := createdOut.GetCoins(asset) + require.NoError(t, err) + require.Equal(t, cur, val) + } + require.Equal(t, createdIn.GetId(), createdOut.GetId()) + require.Equal(t, tt.addr, createdOut.GetAddress().String()) + }) + } +} + +func Test_newCreatedTransactionOutputs(t *testing.T) { + outputs := []api.CreatedTransactionOutput{ + api.CreatedTransactionOutput{ + Address: "addr1", + }, + api.CreatedTransactionOutput{ + Address: "addr2", + }, + api.CreatedTransactionOutput{ + Address: "addr3", + }, + api.CreatedTransactionOutput{ + Address: "addr4", + }, + api.CreatedTransactionOutput{ + Address: "addr5", + }, + } + + for len(outputs) > 0 { + outs := newCreatedTransactionOutputs(outputs) + rawOutputs := make([]api.CreatedTransactionOutput, len(outputs)) + for i, out := range outs { + createdOut, valid := out.(*SkycoinCreatedTransactionOutput) + require.True(t, valid) + rawOutputs[i] = createdOut.skyOut + } + requirethat.ElementsMatch(t, outputs, rawOutputs) + outputs = outputs[:len(outputs)-1] + } +} From 52f4f5557f627c6796a4378d8ec10e44ecf4447c Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sun, 26 Jan 2020 00:39:28 -0500 Subject: [PATCH 37/41] [test] refs #279 - Update `GetId` --- src/coin/skycoin/models/coin_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index bc3e88ed..bc12760d 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1026,6 +1026,22 @@ func TestGetId(t *testing.T) { obj ObjectWithID want string }{ + { + obj: &SkycoinCreatedTransactionOutput{ + skyOut: api.CreatedTransactionOutput{ + UxID: "uxid", + }, + }, + want: "uxid", + }, + { + obj: &SkycoinCreatedTransactionInput{ + skyIn: api.CreatedTransactionInput{ + UxID: "uxid", + }, + }, + want: "uxid", + }, { obj: &SkycoinUninjectedTransaction{ txn: new(coin.Transaction), From 772067ced7919a56812d0190489595987e389d82 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sun, 26 Jan 2020 00:44:21 -0500 Subject: [PATCH 38/41] [test] refs #279 - Add generic `TestGetAddress` --- src/coin/skycoin/models/coin_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index bc12760d..0e16f7d1 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1681,7 +1681,7 @@ func TestGetCoins(t *testing.T) { } } -func TestSkycoinTransactionOutputGetAddress(t *testing.T) { +func TestGetAddress(t *testing.T) { strAddr := makeAddress().String() tests := []struct { @@ -1691,13 +1691,27 @@ func TestSkycoinTransactionOutputGetAddress(t *testing.T) { want string isNil bool }{ + { + name: "SkycoinCreatedTransactionOutput-empty", + output: new(SkycoinCreatedTransactionOutput), + isNil: true, + }, + { + name: "SkycoinCreatedTransactionOutput-valid", + output: &SkycoinCreatedTransactionOutput{ + skyOut: api.CreatedTransactionOutput{ + Address: strAddr, + }, + }, + want: strAddr, + }, { name: "SkycoinTransactionOutput-empty", output: new(SkycoinTransactionOutput), isNil: true, }, { - name: "SkycoinTransactionOutput-empty", + name: "SkycoinTransactionOutput-valid", output: &SkycoinTransactionOutput{ skyOut: readable.TransactionOutput{ Address: strAddr, From 7982622bb3c37895f5fd4c8e7fb066c88c597794 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sun, 26 Jan 2020 00:51:43 -0500 Subject: [PATCH 39/41] [test] refs #279 - Add generic `TestIsSpent` --- src/coin/skycoin/models/coin_test.go | 60 ++++++++++++---------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 0e16f7d1..2be1c5fb 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -203,18 +203,33 @@ func TestSkycoinTransactionInputGetSpentOutput(t *testing.T) { require.Equal(t, hours, uint64(20)) } -func TestSkycoinTransactionOutputIsSpent(t *testing.T) { +func TestIsSpent(t *testing.T) { badID := "0000000000000000000000000000000000000000000000000000000000000000" - global_mock.On("UxOut", "out").Return(nil, goerrors.New("failure")).Once() - global_mock.On("UxOut", "out").Return(&readable.SpentOutput{SpentTxnID: badID}, nil).Once() - global_mock.On("UxOut", "out").Return(&readable.SpentOutput{SpentTxnID: "42"}, nil).Once() - - output := &SkycoinTransactionOutput{skyOut: readable.TransactionOutput{Hash: "out"}} - - require.Equal(t, output.IsSpent(), false) - require.Equal(t, output.IsSpent(), false) - require.Equal(t, output.IsSpent(), true) - require.Equal(t, output.IsSpent(), true) + tests := []struct { + name string + output core.TransactionOutput + }{ + { + name: "SkycoinTransactionOutput", + output: &SkycoinTransactionOutput{skyOut: readable.TransactionOutput{Hash: "out"}}, + }, + { + name: "SkycoinCreatedTransactionOutput", + output: &SkycoinCreatedTransactionOutput{skyOut: api.CreatedTransactionOutput{UxID: "out"}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + global_mock.On("UxOut", "out").Return(nil, goerrors.New("failure")).Once() + global_mock.On("UxOut", "out").Return(&readable.SpentOutput{SpentTxnID: badID}, nil).Once() + global_mock.On("UxOut", "out").Return(&readable.SpentOutput{SpentTxnID: "42"}, nil).Once() + + require.Equal(t, tt.output.IsSpent(), false) + require.Equal(t, tt.output.IsSpent(), false) + require.Equal(t, tt.output.IsSpent(), true) + require.Equal(t, tt.output.IsSpent(), true) + }) + } } func TestUninjectedTransactionSignedUnsigned(t *testing.T) { @@ -289,29 +304,6 @@ func TestSkycoinUninjectedTransactionGetInputs(t *testing.T) { require.Equal(t, uint64(0), val) } -func TestSkycoinCreatedTransactionOutputIsSpent(t *testing.T) { - global_mock.On("UxOut", "out1").Return( - &readable.SpentOutput{ - SpentTxnID: "0000000000000000000000000000000000000000000000000000000000000000", - }, - nil, - ) - global_mock.On("UxOut", "out2").Return( - &readable.SpentOutput{ - SpentTxnID: "0", - }, - nil, - ) - - output1 := &SkycoinCreatedTransactionOutput{skyOut: api.CreatedTransactionOutput{UxID: "out1"}} - output2 := &SkycoinCreatedTransactionOutput{skyOut: api.CreatedTransactionOutput{UxID: "out2"}} - - require.Equal(t, output1.IsSpent(), false) - require.Equal(t, output2.IsSpent(), true) - require.Equal(t, output2.IsSpent(), true) - -} - func TestSupportedAssets(t *testing.T) { pendTxn := new(SkycoinPendingTransaction) assets := pendTxn.SupportedAssets() From f6a15527fa27aeacc8ab1fa78959cec3f68fd0b9 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sun, 26 Jan 2020 01:21:24 -0500 Subject: [PATCH 40/41] [test] refs #279 - Integrate `SkycoinCreatedTransaction` into tests --- src/coin/skycoin/models/coin_test.go | 70 ++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index 2be1c5fb..c828d49e 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -45,6 +45,18 @@ func TestTransactionGetStatus(t *testing.T) { txn core.Transaction want core.TransactionStatus }{ + { + name: "SkycoinCreatedTransaction-Created", + txn: new(SkycoinCreatedTransaction), + want: core.TXN_STATUS_CREATED, + }, + { + name: "SkycoinCreatedTransaction-Created", + txn: &SkycoinCreatedTransaction{ + skyTxn: api.CreatedTransaction{Length: 10}, + }, + want: core.TXN_STATUS_CREATED, + }, { name: "SkycoinTransaction-Confirmed", txn: &SkycoinTransaction{ @@ -131,6 +143,25 @@ func TestSkycoinTransactionGetInputs(t *testing.T) { ids []string wantNil bool }{ + { + name: "SkycoinCreatedTransaction", + txn: &SkycoinCreatedTransaction{ + skyTxn: api.CreatedTransaction{ + In: []api.CreatedTransactionInput{ + api.CreatedTransactionInput{ + UxID: "out1", Coins: "20", Hours: "20", + }, + api.CreatedTransactionInput{ + UxID: "out2", Coins: "20", Hours: "20", + }, + api.CreatedTransactionInput{ + UxID: "out3", Coins: "20", Hours: "20", + }, + }, + }, + }, + ids: []string{"out1", "out2", "out3"}, + }, { name: "SkycoinTransaction-ApiError", txn: st, @@ -350,6 +381,13 @@ func TestTransactionsGetTimestamp(t *testing.T) { txn: new(SkycoinUninjectedTransaction), want: 0, }, + { + name: "SkycoinCreatedTransaction-Created", + txn: &SkycoinCreatedTransaction{ + skyTxn: api.CreatedTransaction{Length: 10}, + }, + want: 0, + }, { name: "SkycoinTransaction", txn: &SkycoinTransaction{ @@ -368,6 +406,11 @@ func TestTransactionsGetTimestamp(t *testing.T) { }, want: uint64(cur.Unix()), }, + { + name: "SkycoinCreatedTransaction", + txn: new(SkycoinCreatedTransaction), + want: 0, + }, } for _, tt := range tests { @@ -1126,6 +1169,25 @@ func TestSkycoinTransactionGetOutputs(t *testing.T) { ids []string wantNil bool }{ + { + name: "SkycoinCreatedTransaction", + txn: &SkycoinCreatedTransaction{ + skyTxn: api.CreatedTransaction{ + Out: []api.CreatedTransactionOutput{ + api.CreatedTransactionOutput{ + UxID: "out1", Coins: "20", Hours: "20", + }, + api.CreatedTransactionOutput{ + UxID: "out2", Coins: "20", Hours: "20", + }, + api.CreatedTransactionOutput{ + UxID: "out3", Coins: "20", Hours: "20", + }, + }, + }, + }, + ids: []string{"out1", "out2", "out3"}, + }, { name: "SkycoinTransaction1", txn: st, @@ -1299,6 +1361,10 @@ func TestTransactionVerifyUnsigned(t *testing.T) { name: "empty Txn", txn: new(SkycoinTransaction), }, + { + name: "empty Txn", + txn: new(SkycoinCreatedTransaction), + }, //TODO: add better tests } @@ -1324,6 +1390,10 @@ func TestTransactionVerifySigned(t *testing.T) { name: "empty Txn", txn: new(SkycoinTransaction), }, + { + name: "empty Txn", + txn: new(SkycoinCreatedTransaction), + }, //TODO: add better tests } From 49f37171487b4a3e5dc5dd54403f77dda4b8b6a8 Mon Sep 17 00:00:00 2001 From: Lazaro Raul Date: Sun, 26 Jan 2020 01:34:29 -0500 Subject: [PATCH 41/41] [test] refs #279 - Add generic `TestEncodeSkycoinTransaction` --- src/coin/skycoin/models/coin_test.go | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/coin/skycoin/models/coin_test.go b/src/coin/skycoin/models/coin_test.go index c828d49e..18e46257 100644 --- a/src/coin/skycoin/models/coin_test.go +++ b/src/coin/skycoin/models/coin_test.go @@ -1870,3 +1870,50 @@ func Test_newCreatedTransactionOutputs(t *testing.T) { outputs = outputs[:len(outputs)-1] } } + +func TestEncodeSkycoinTransaction(t *testing.T) { + type SkycoinReadableTxn interface { + skytypes.SkycoinTxn + skytypes.ReadableTxn + } + + date, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") + require.NoError(t, err) + + tests := []struct { + name string + txn SkycoinReadableTxn + }{ + { + name: "SkycoinPendingTransaction", + txn: &SkycoinPendingTransaction{ + Transaction: &readable.UnconfirmedTransactionVerbose{ + Transaction: readable.BlockTransactionVerbose{ + InnerHash: "0000000000000000000000000000000000000000000000000000000000000000", + Hash: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", + }, + Announced: date, + }, + }, + }, + { + name: "SkycoinCreatedTransaction", + txn: &SkycoinCreatedTransaction{ + skyTxn: api.CreatedTransaction{ + InnerHash: "0000000000000000000000000000000000000000000000000000000000000000", + TxID: "78877fa898f0b4c45c9c33ae941e40617ad7c8657a307db62bc5691f92f4f60e", + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ser, err := tt.txn.EncodeSkycoinTransaction() + require.NoError(t, err) + exp, err := serializeCreatedTransaction(tt.txn) + require.NoError(t, err) + require.Equal(t, exp, ser) + }) + } +}