From 0b9341ef3cb0a25eaee5891b0deebaaf292b631c Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Wed, 13 Sep 2017 00:53:53 +0200 Subject: [PATCH] simplify the case of missing outputs --- bitcoinrpc.go | 2 +- blockbook.go | 45 +++++++++++++++++---------------------------- rocksdb.go | 6 ------ 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/bitcoinrpc.go b/bitcoinrpc.go index 489d7457..46ab0cf2 100644 --- a/bitcoinrpc.go +++ b/bitcoinrpc.go @@ -299,7 +299,7 @@ func (b *BitcoinRPC) GetAddress(txid string, vout uint32) (string, error) { if err != nil { return "", err } - return tx.GetAddress(vout) + return tx.GetAddress(vout), nil } func (b *BitcoinRPC) call(req interface{}, res interface{}) error { diff --git a/blockbook.go b/blockbook.go index bd302451..7b741985 100644 --- a/blockbook.go +++ b/blockbook.go @@ -1,7 +1,6 @@ package main import ( - "errors" "flag" "log" "time" @@ -11,10 +10,6 @@ type BlockParser interface { ParseBlock(b []byte) (*Block, error) } -var ( - ErrNotFound = errors.New("not found") -) - type Blocks interface { GetBestBlockHash() (string, error) GetBlockHash(height uint32) (string, error) @@ -24,8 +19,8 @@ type Blocks interface { type Outpoints interface { // GetAddress looks up a transaction output and returns its address. - // ErrNotFound is returned if the output is not found. Address can be - // empty string in case it's not intelligable. + // Address can be empty string in case it's not found or not + // intelligable. GetAddress(txid string, vout uint32) (string, error) } @@ -77,49 +72,43 @@ func (b *Block) GetTxAddresses(outpoints Outpoints, tx *Tx) (map[string]struct{} // Lookup output in in the outpoint index. In case it's not // found, take a look in this block. a, err := outpoints.GetAddress(i.Txid, i.Vout) - if err == ErrNotFound { - a, err = b.GetAddress(i.Txid, i.Vout) - } if err != nil { return nil, err } + if a == "" { + a = b.GetAddress(i.Txid, i.Vout) + } if a != "" { addrs[a] = struct{}{} + } else { + log.Printf("warn: output not found: %s:%d", i.Txid, i.Vout) } } return addrs, nil } -func (b *Block) GetAddress(txid string, vout uint32) (string, error) { - var t *Tx +func (b *Block) GetAddress(txid string, vout uint32) string { for i, _ := range b.Txs { if b.Txs[i].Txid == txid { - t = &b.Txs[i] - break + return b.Txs[i].GetAddress(vout) } } - if t == nil { - // Transaction output was not found. - return "", ErrNotFound - } - return t.GetAddress(vout) + return "" // tx not found } -func (t *Tx) GetAddress(vout uint32) (string, error) { - if vout >= uint32(len(t.Vout)) { - // The output doesn't exist. - return "", ErrNotFound +func (t *Tx) GetAddress(vout uint32) string { + if vout < uint32(len(t.Vout)) { + return t.Vout[vout].GetAddress() } - return t.Vout[vout].GetAddress(), nil + return "" // output not found } func (o *Vout) GetAddress() string { - if len(o.ScriptPubKey.Addresses) != 1 { - // The output address is not intelligible. - return "" + if len(o.ScriptPubKey.Addresses) == 1 { + return o.ScriptPubKey.Addresses[0] } - return o.ScriptPubKey.Addresses[0] + return "" // output address not intelligible } var ( diff --git a/rocksdb.go b/rocksdb.go index 4dcb7eec..6669a21c 100644 --- a/rocksdb.go +++ b/rocksdb.go @@ -71,9 +71,6 @@ func (d *RocksDB) GetAddress(txid string, vout uint32) (string, error) { if err != nil { return "", err } - if v.Size() == 0 { - return "", ErrNotFound - } defer v.Free() return unpackAddress(v.Data()) } @@ -268,9 +265,6 @@ func (d *RocksDB) GetLastBlockHash() (string, error) { return "", err } defer v.Free() - if v.Size() == 0 { - return "", ErrNotFound - } return unpackBlockValue(v.Data()) }