simplify the case of missing outputs
parent
fd4073e544
commit
0b9341ef3c
|
@ -299,7 +299,7 @@ func (b *BitcoinRPC) GetAddress(txid string, vout uint32) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return tx.GetAddress(vout)
|
return tx.GetAddress(vout), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BitcoinRPC) call(req interface{}, res interface{}) error {
|
func (b *BitcoinRPC) call(req interface{}, res interface{}) error {
|
||||||
|
|
45
blockbook.go
45
blockbook.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
@ -11,10 +10,6 @@ type BlockParser interface {
|
||||||
ParseBlock(b []byte) (*Block, error)
|
ParseBlock(b []byte) (*Block, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
ErrNotFound = errors.New("not found")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Blocks interface {
|
type Blocks interface {
|
||||||
GetBestBlockHash() (string, error)
|
GetBestBlockHash() (string, error)
|
||||||
GetBlockHash(height uint32) (string, error)
|
GetBlockHash(height uint32) (string, error)
|
||||||
|
@ -24,8 +19,8 @@ type Blocks interface {
|
||||||
|
|
||||||
type Outpoints interface {
|
type Outpoints interface {
|
||||||
// GetAddress looks up a transaction output and returns its address.
|
// GetAddress looks up a transaction output and returns its address.
|
||||||
// ErrNotFound is returned if the output is not found. Address can be
|
// Address can be empty string in case it's not found or not
|
||||||
// empty string in case it's not intelligable.
|
// intelligable.
|
||||||
GetAddress(txid string, vout uint32) (string, error)
|
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
|
// Lookup output in in the outpoint index. In case it's not
|
||||||
// found, take a look in this block.
|
// found, take a look in this block.
|
||||||
a, err := outpoints.GetAddress(i.Txid, i.Vout)
|
a, err := outpoints.GetAddress(i.Txid, i.Vout)
|
||||||
if err == ErrNotFound {
|
|
||||||
a, err = b.GetAddress(i.Txid, i.Vout)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if a == "" {
|
||||||
|
a = b.GetAddress(i.Txid, i.Vout)
|
||||||
|
}
|
||||||
if a != "" {
|
if a != "" {
|
||||||
addrs[a] = struct{}{}
|
addrs[a] = struct{}{}
|
||||||
|
} else {
|
||||||
|
log.Printf("warn: output not found: %s:%d", i.Txid, i.Vout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return addrs, nil
|
return addrs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) GetAddress(txid string, vout uint32) (string, error) {
|
func (b *Block) GetAddress(txid string, vout uint32) string {
|
||||||
var t *Tx
|
|
||||||
for i, _ := range b.Txs {
|
for i, _ := range b.Txs {
|
||||||
if b.Txs[i].Txid == txid {
|
if b.Txs[i].Txid == txid {
|
||||||
t = &b.Txs[i]
|
return b.Txs[i].GetAddress(vout)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t == nil {
|
return "" // tx not found
|
||||||
// Transaction output was not found.
|
|
||||||
return "", ErrNotFound
|
|
||||||
}
|
|
||||||
return t.GetAddress(vout)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tx) GetAddress(vout uint32) (string, error) {
|
func (t *Tx) GetAddress(vout uint32) string {
|
||||||
if vout >= uint32(len(t.Vout)) {
|
if vout < uint32(len(t.Vout)) {
|
||||||
// The output doesn't exist.
|
return t.Vout[vout].GetAddress()
|
||||||
return "", ErrNotFound
|
|
||||||
}
|
}
|
||||||
return t.Vout[vout].GetAddress(), nil
|
return "" // output not found
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Vout) GetAddress() string {
|
func (o *Vout) GetAddress() string {
|
||||||
if len(o.ScriptPubKey.Addresses) != 1 {
|
if len(o.ScriptPubKey.Addresses) == 1 {
|
||||||
// The output address is not intelligible.
|
return o.ScriptPubKey.Addresses[0]
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
return o.ScriptPubKey.Addresses[0]
|
return "" // output address not intelligible
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -71,9 +71,6 @@ func (d *RocksDB) GetAddress(txid string, vout uint32) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if v.Size() == 0 {
|
|
||||||
return "", ErrNotFound
|
|
||||||
}
|
|
||||||
defer v.Free()
|
defer v.Free()
|
||||||
return unpackAddress(v.Data())
|
return unpackAddress(v.Data())
|
||||||
}
|
}
|
||||||
|
@ -268,9 +265,6 @@ func (d *RocksDB) GetLastBlockHash() (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer v.Free()
|
defer v.Free()
|
||||||
if v.Size() == 0 {
|
|
||||||
return "", ErrNotFound
|
|
||||||
}
|
|
||||||
return unpackBlockValue(v.Data())
|
return unpackBlockValue(v.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue