Skip to content

Commit

Permalink
fix: sort utxos by oldest to prevent mismatched messages (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Jul 10, 2024
1 parent 0ca3a1b commit 033b58a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
16 changes: 12 additions & 4 deletions chains/btc/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import (
"sort"
)

type Status struct {
Confirmed bool
BlockHeight uint64 `json:"block_height"`
BlockHash string `json:"block_hash"`
BlockTime uint64 `json:"block_time"`
}

type Utxo struct {
TxID string `json:"txid"`
Vout uint32 `json:"vout"`
Value uint64 `json:"value"`
TxID string `json:"txid"`
Vout uint32 `json:"vout"`
Value uint64 `json:"value"`
Status Status `json:"status"`
}

type Fee struct {
Expand Down Expand Up @@ -69,7 +77,7 @@ func (a *MempoolAPI) Utxos(address string) ([]Utxo, error) {
return nil, err
}
sort.Slice(utxos, func(i int, j int) bool {
return utxos[i].TxID < utxos[j].TxID
return utxos[i].Status.BlockTime < utxos[j].Status.BlockTime
})

return utxos, nil
Expand Down
16 changes: 14 additions & 2 deletions chains/btc/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,24 @@ func (s *MempoolTestSuite) Test_Utxo_SuccessfulFetch() {
{
TxID: "28154e2008912d27978225c096c22ffe2ea65e1d55bf440ee41c21f9489c7fe1",
Vout: 0,
Value: 11198,
Value: 11197,
Status: mempool.Status{
Confirmed: true,
BlockHeight: 2812826,
BlockHash: "000000000000001a01d4058773384f2c23aed5a7e5ede252f99e290fa58324a3",
BlockTime: 1715083122,
},
},
{
TxID: "28154e2008912d27978225c096c22ffe2ea65e1d55bf440ee41c21f9489c7fe9",
Vout: 0,
Value: 11197,
Value: 11198,
Status: mempool.Status{
Confirmed: true,
BlockHeight: 2812827,
BlockHash: "000000000000001a01d4058773384f2c23aed5a7e5ede252f99e290fa58324a5",
BlockTime: 1715083123,
},
},
})
}
Expand Down
4 changes: 2 additions & 2 deletions chains/btc/mempool/test-data/successful-utxo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
{"txid":"28154e2008912d27978225c096c22ffe2ea65e1d55bf440ee41c21f9489c7fe9","vout":0,"status":{"confirmed":true,"block_height":2812826,"block_hash":"000000000000001a01d4058773384f2c23aed5a7e5ede252f99e290fa58324a3","block_time":1715083122},"value":11197},
{"txid":"28154e2008912d27978225c096c22ffe2ea65e1d55bf440ee41c21f9489c7fe1","vout":0,"status":{"confirmed":true,"block_height":2812826,"block_hash":"000000000000001a01d4058773384f2c23aed5a7e5ede252f99e290fa58324a3","block_time":1715083122},"value":11198}
{"txid":"28154e2008912d27978225c096c22ffe2ea65e1d55bf440ee41c21f9489c7fe9","vout":0,"status":{"confirmed":true,"block_height":2812827,"block_hash":"000000000000001a01d4058773384f2c23aed5a7e5ede252f99e290fa58324a5","block_time":1715083123},"value":11198},
{"txid":"28154e2008912d27978225c096c22ffe2ea65e1d55bf440ee41c21f9489c7fe1","vout":0,"status":{"confirmed":true,"block_height":2812826,"block_hash":"000000000000001a01d4058773384f2c23aed5a7e5ede252f99e290fa58324a3","block_time":1715083122},"value":11197}
]

0 comments on commit 033b58a

Please sign in to comment.