Fixed MempoolSync issues: P2PK addresses, missing txs

This commit is contained in:
Jakub Matys 2018-10-04 12:49:28 +02:00
parent 5fc74eca31
commit 07493fc198

View File

@ -5,7 +5,6 @@ package rpc
import ( import (
"blockbook/bchain" "blockbook/bchain"
"encoding/json" "encoding/json"
"errors"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -13,6 +12,7 @@ import (
"time" "time"
"github.com/deckarep/golang-set" "github.com/deckarep/golang-set"
"github.com/juju/errors"
) )
var testMap = map[string]func(t *testing.T, th *TestHandler){ var testMap = map[string]func(t *testing.T, th *TestHandler){
@ -110,18 +110,19 @@ func loadTestData(coin string, parser bchain.BlockChainParser) (*TestData, error
} }
func setTxAddresses(parser bchain.BlockChainParser, tx *bchain.Tx) error { func setTxAddresses(parser bchain.BlockChainParser, tx *bchain.Tx) error {
// pack and unpack transaction in order to get addresses decoded - ugly but works for i := range tx.Vout {
var tmp *bchain.Tx ad, err := parser.GetAddrDescFromVout(&tx.Vout[i])
b, err := parser.PackTx(tx, 0, 0) if err != nil {
if err == nil { return err
tmp, _, err = parser.UnpackTx(b)
if err == nil {
for i := 0; i < len(tx.Vout); i++ {
tx.Vout[i].ScriptPubKey.Addresses = tmp.Vout[i].ScriptPubKey.Addresses
}
} }
addrs := []string{}
a, s, err := parser.GetAddressesFromAddrDesc(ad)
if err == nil && s {
addrs = append(addrs, a...)
}
tx.Vout[i].ScriptPubKey.Addresses = addrs
} }
return err return nil
} }
func testGetBlockHash(t *testing.T, h *TestHandler) { func testGetBlockHash(t *testing.T, h *TestHandler) {
@ -206,7 +207,7 @@ func testMempoolSync(t *testing.T, h *TestHandler) {
continue continue
} }
txid2addrs := getMempoolAddresses(t, h, txs) txid2addrs := getTxid2addrs(t, h, txs)
if len(txid2addrs) == 0 { if len(txid2addrs) == 0 {
t.Skip("Skipping test, no addresses in mempool") t.Skip("Skipping test, no addresses in mempool")
} }
@ -215,7 +216,7 @@ func testMempoolSync(t *testing.T, h *TestHandler) {
for _, a := range addrs { for _, a := range addrs {
got, err := h.Chain.GetMempoolTransactions(a) got, err := h.Chain.GetMempoolTransactions(a)
if err != nil { if err != nil {
t.Fatal(err) t.Fatalf("address %q: %s", a, err)
} }
if !containsString(got, txid) { if !containsString(got, txid) {
t.Errorf("ResyncMempool() - for address %s, transaction %s wasn't found in mempool", a, txid) t.Errorf("ResyncMempool() - for address %s, transaction %s wasn't found in mempool", a, txid)
@ -343,26 +344,21 @@ func getMempool(t *testing.T, h *TestHandler) []string {
return txs return txs
} }
func getMempoolAddresses(t *testing.T, h *TestHandler, txs []string) map[string][]string { func getTxid2addrs(t *testing.T, h *TestHandler, txs []string) map[string][]string {
txid2addrs := map[string][]string{} txid2addrs := map[string][]string{}
for i := 0; i < len(txs); i++ { for i := range txs {
tx, err := h.Chain.GetTransactionForMempool(txs[i]) tx, err := h.Chain.GetTransactionForMempool(txs[i])
if err != nil { if err != nil {
if isMissingTx(err) {
continue
}
t.Fatal(err) t.Fatal(err)
} }
setTxAddresses(h.Chain.GetChainParser(), tx)
addrs := []string{} addrs := []string{}
for _, vin := range tx.Vin { for j := range tx.Vout {
for _, a := range vin.Addresses { for _, a := range tx.Vout[j].ScriptPubKey.Addresses {
if isSearchableAddr(a) { addrs = append(addrs, a)
addrs = append(addrs, a)
}
}
}
for _, vout := range tx.Vout {
for _, a := range vout.ScriptPubKey.Addresses {
if isSearchableAddr(a) {
addrs = append(addrs, a)
}
} }
} }
if len(addrs) > 0 { if len(addrs) > 0 {
@ -372,8 +368,18 @@ func getMempoolAddresses(t *testing.T, h *TestHandler, txs []string) map[string]
return txid2addrs return txid2addrs
} }
func isSearchableAddr(addr string) bool { func isMissingTx(err error) bool {
return len(addr) > 3 && addr[:3] != "OP_" switch e1 := err.(type) {
case *errors.Err:
switch e2 := e1.Cause().(type) {
case *bchain.RPCError:
if e2.Code == -5 { // "No such mempool or blockchain transaction"
return true
}
}
}
return false
} }
func intersect(a, b []string) []string { func intersect(a, b []string) []string {