Skip to content

Commit

Permalink
Uniformly return height 0 for mempool RawTransaction results.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Aug 13, 2024
1 parent 0f1ed05 commit 56fe52a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Changed

- The `RawTransaction` values returned from a call to `GetMempoolStream`
now report a `Height` value of `0`, in order to be consistent with
the results of calls to `GetTransaction`. See the documentation of
`RawTransaction` in `walletrpc/service.proto` for more details on
the semantics of this field.

### Fixed

- Parsing of `getrawtransaction` results is now platform-independent.
Expand Down
12 changes: 6 additions & 6 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ func mempoolStub(method string, params []json.RawMessage) (json.RawMessage, erro
if txid != "mempooltxid-1" {
testT.Fatal("unexpected txid")
}
r, _ := json.Marshal("aabb")
r, _ := json.Marshal(map[string]string{"hex":"aabb"})
return r, nil
case 5:
// Simulate that still no new block has arrived ...
Expand Down Expand Up @@ -637,7 +637,7 @@ func mempoolStub(method string, params []json.RawMessage) (json.RawMessage, erro
if txid != "mempooltxid-2" {
testT.Fatal("unexpected txid")
}
r, _ := json.Marshal("ccdd")
r, _ := json.Marshal(map[string]string{"hex":"ccdd"})
return r, nil
case 8:
// A new block arrives, this will cause these two tx to be returned
Expand Down Expand Up @@ -669,7 +669,7 @@ func TestMempoolStream(t *testing.T) {
return nil
})
if err != nil {
t.Fatal("GetMempool failed")
t.Errorf("GetMempool failed: %v", err)
}

// This should return two transactions.
Expand All @@ -678,7 +678,7 @@ func TestMempoolStream(t *testing.T) {
return nil
})
if err != nil {
t.Fatal("GetMempool failed")
t.Errorf("GetMempool failed: %v", err)
}
if len(replies) != 2 {
t.Fatal("unexpected number of tx")
Expand All @@ -688,13 +688,13 @@ func TestMempoolStream(t *testing.T) {
if !bytes.Equal([]byte(replies[0].GetData()), []byte{0xaa, 0xbb}) {
t.Fatal("unexpected tx contents")
}
if replies[0].GetHeight() != 200 {
if replies[0].GetHeight() != 0 {
t.Fatal("unexpected tx height")
}
if !bytes.Equal([]byte(replies[1].GetData()), []byte{0xcc, 0xdd}) {
t.Fatal("unexpected tx contents")
}
if replies[1].GetHeight() != 200 {
if replies[1].GetHeight() != 0 {
t.Fatal("unexpected tx height")
}

Expand Down
25 changes: 8 additions & 17 deletions common/mempool.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"encoding/hex"
"encoding/json"
"sync"
"time"
Expand Down Expand Up @@ -105,35 +104,27 @@ func refreshMempoolTxns() error {
// We've already fetched this transaction
continue
}
g_txidSeen[txid(txidstr)] = struct{}{}

// We haven't fetched this transaction already.
g_txidSeen[txid(txidstr)] = struct{}{}
txidJSON, err := json.Marshal(txidstr)
if err != nil {
return err
}
// The "0" is because we only need the raw hex, which is returned as
// just a hex string, and not even a json string (with quotes).
params := []json.RawMessage{txidJSON, json.RawMessage("0")}

params := []json.RawMessage{txidJSON, json.RawMessage("1")}
result, rpcErr := RawRequest("getrawtransaction", params)
if rpcErr != nil {
// Not an error; mempool transactions can disappear
continue
}
// strip the quotes
var txStr string
err = json.Unmarshal(result, &txStr)
if err != nil {
return err
}
txBytes, err := hex.DecodeString(txStr)

rawtx, err := ParseRawTransaction(result)
if err != nil {
return err
}
newRtx := &walletrpc.RawTransaction{
Data: txBytes,
Height: uint64(g_lastBlockChainInfo.Blocks),
}
g_txList = append(g_txList, newRtx)

g_txList = append(g_txList, rawtx)
}
return nil
}

0 comments on commit 56fe52a

Please sign in to comment.