Skip to content

Commit

Permalink
Merge pull request #27 from phoreproject/sort-transactions-by-index
Browse files Browse the repository at this point in the history
Sort transactions by index in block as well as height
  • Loading branch information
anchaj authored Jul 22, 2019
2 parents b3e476a + 9c410a6 commit a616d43
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions bitcoin/phored/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ type ReceivedTx struct {
tx wire.MsgTx
blockHeight int32
blockTime time.Time
blockIndex int32
}

// RetrieveTransactions fetches transactions from the rpc server and stores them into the database
Expand Down Expand Up @@ -1161,7 +1162,8 @@ func (w *RPCWallet) RetrieveTransactions() error {

transactions = append(transactions, w.receiveTransactions(scriptAddresses, false)...)
sort.SliceStable(transactions, func(i, j int) bool {
return transactions[i].blockHeight < transactions[j].blockHeight
return transactions[i].blockHeight < transactions[j].blockHeight ||
(transactions[i].blockHeight == transactions[j].blockHeight && transactions[i].blockIndex < transactions[j].blockIndex)
})

for _, tx := range transactions {
Expand Down Expand Up @@ -1240,7 +1242,26 @@ func (w *RPCWallet) receiveTransactions(addrs []btc.Address, lookAhead bool) []R
continue
}

transactions = append(transactions, ReceivedTx{transaction, int32(block.Height), time.Unix(block.Time, 0)})
// replace this by sending the index of the transaction in the block
index := int32(-1)

for i, transactionHex := range block.Tx {
if transactionHex == transaction.TxHash().String() {
index = int32(i)
}
}

if index == -1 {
log.Errorf("could not find transaction in block")
continue
}

transactions = append(transactions, ReceivedTx{
tx: transaction,
blockHeight: int32(block.Height),
blockTime: time.Unix(block.Time, 0),
blockIndex: index,
})
}
}
return transactions
Expand Down
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

var (
VERSION = "2.0.1"
VERSION = "2.0.2"
USERAGENT = "/Phore-Marketplace-go:" + VERSION + "/"
)

Expand Down

0 comments on commit a616d43

Please sign in to comment.