From 3b6edf85cac9a3d78539f17030d0c5bbb88c71a4 Mon Sep 17 00:00:00 2001 From: Rotem Hemo Date: Fri, 3 Jan 2020 14:32:05 -0500 Subject: [PATCH 1/2] -- Adding receiver function to transaction that returns the receiver of a transaction -- Fix indexer to show incoming received transactions --- data/transactions/transaction.go | 12 +++++++++ node/indexer/db.go | 2 +- node/indexer/indexer_test.go | 42 +++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/data/transactions/transaction.go b/data/transactions/transaction.go index 171686aca5..ed1b7b47b1 100644 --- a/data/transactions/transaction.go +++ b/data/transactions/transaction.go @@ -412,6 +412,18 @@ func (tx Transaction) TxAmount() basics.MicroAlgos { } } +// Receiver returns the address of the receiver. If the transaction has no receiver, it returns the empty address. +func (tx Transaction) GetReceiverAddress() basics.Address { + switch tx.Type { + case protocol.PaymentTx: + return tx.PaymentTxnFields.Receiver + case protocol.AssetTransferTx: + return tx.AssetTransferTxnFields.AssetReceiver + default: + return basics.Address{} + } +} + // EstimateEncodedSize returns the estimated encoded size of the transaction including the signature. // This function is to be used for calculating the fee func (tx Transaction) EstimateEncodedSize() int { diff --git a/node/indexer/db.go b/node/indexer/db.go index 10ee4daa41..86d58c8965 100644 --- a/node/indexer/db.go +++ b/node/indexer/db.go @@ -124,7 +124,7 @@ func (idb *DB) AddBlock(b bookkeeping.Block) error { } for _, txad := range payset { txn := txad.SignedTxn - _, err = stmt.Exec(txn.ID().String(), txn.Txn.Sender.String(), txn.Txn.Receiver.String(), b.Round(), b.TimeStamp) + _, err = stmt.Exec(txn.ID().String(), txn.Txn.Sender.String(), txn.Txn.GetReceiverAddress().String(), b.Round(), b.TimeStamp) if err != nil { return err } diff --git a/node/indexer/indexer_test.go b/node/indexer/indexer_test.go index 3a0e8b69ec..7a9a0395df 100644 --- a/node/indexer/indexer_test.go +++ b/node/indexer/indexer_test.go @@ -124,6 +124,31 @@ func (s *IndexSuite) TestIndexer_DuplicateRounds() { } } +func (s *IndexSuite) TestIndexer_Asset() { + query := "SELECT txid from transactions where (from_addr = $1 OR to_addr = $1)" + rows, err := s.idx.IDB.dbr.Handle.Query(query, s.addrs[0].String()) + require.NoError(s.T(), err) + defer rows.Close() + + txids := make(map[string]bool, 0) + var txid string + for rows.Next() { + err := rows.Scan(&txid) + require.NoError(s.T(), err) + txids[txid] = true + } + + // make sure all txns are in list + for _, txn := range s.txns { + if txn.Txn.Type == protocol.AssetTransferTx { + if txn.Txn.Sender == s.addrs[0] || txn.Txn.AssetReceiver == s.addrs[0] { + require.True(s.T(), txids[txn.ID().String()]) + } + } + } + +} + func TestExampleTestSuite(t *testing.T) { suite.Run(t, new(IndexSuite)) } @@ -201,17 +226,28 @@ func generateTestObjects(numTxs, numAccs int) ([]transactions.Transaction, []tra exp := iss + 10 txs[i] = transactions.Transaction{ - Type: protocol.PaymentTx, Header: transactions.Header{ Sender: addresses[s], Fee: basics.MicroAlgos{Raw: f}, FirstValid: basics.Round(iss), LastValid: basics.Round(exp), }, - PaymentTxnFields: transactions.PaymentTxnFields{ + } + + // Create half assets and half payment + if i%2 == 0 { + txs[i].Type = protocol.PaymentTx + txs[i].PaymentTxnFields = transactions.PaymentTxnFields{ Receiver: addresses[r], Amount: basics.MicroAlgos{Raw: uint64(a)}, - }, + } + } else { + txs[i].Type = protocol.AssetTransferTx + txs[i].AssetTransferTxnFields = transactions.AssetTransferTxnFields{ + AssetReceiver: addresses[r], + AssetAmount: uint64(a), + XferAsset: basics.AssetIndex(uint64(rand.Intn(20000))), + } } signed[i] = txs[i].Sign(secrets[s]) } From 3dbd786972fc7e5535c71bcc82fac299b51334ac Mon Sep 17 00:00:00 2001 From: Rotem Hemo Date: Fri, 3 Jan 2020 15:00:59 -0500 Subject: [PATCH 2/2] -- Fix function name --- data/transactions/transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/transactions/transaction.go b/data/transactions/transaction.go index ed1b7b47b1..418e6ee9fa 100644 --- a/data/transactions/transaction.go +++ b/data/transactions/transaction.go @@ -412,7 +412,7 @@ func (tx Transaction) TxAmount() basics.MicroAlgos { } } -// Receiver returns the address of the receiver. If the transaction has no receiver, it returns the empty address. +// GetReceiverAddress returns the address of the receiver. If the transaction has no receiver, it returns the empty address. func (tx Transaction) GetReceiverAddress() basics.Address { switch tx.Type { case protocol.PaymentTx: