2021-01-29 16:51:08 -05:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/muun/libwallet"
|
|
|
|
"github.com/muun/recovery/electrum"
|
|
|
|
)
|
|
|
|
|
|
|
|
// scanTask encapsulates a parallelizable Scanner unit of work.
|
|
|
|
type scanTask struct {
|
|
|
|
servers *ServerProvider
|
|
|
|
client *electrum.Client
|
|
|
|
addresses []libwallet.MuunAddress
|
|
|
|
timeout time.Duration
|
|
|
|
exit chan struct{}
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:28:04 -04:00
|
|
|
// scanTaskResult contains a summary of the execution of a task.
|
|
|
|
type scanTaskResult struct {
|
|
|
|
Task *scanTask
|
|
|
|
Utxos []*Utxo
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:51:08 -05:00
|
|
|
// Execute obtains the Utxo set for the Task address, implementing a retry strategy.
|
2021-03-17 14:28:04 -04:00
|
|
|
func (t *scanTask) Execute() *scanTaskResult {
|
|
|
|
results := make(chan *scanTaskResult)
|
2021-01-29 16:51:08 -05:00
|
|
|
timeout := time.After(t.timeout)
|
|
|
|
|
|
|
|
// Keep the last error around, in case we reach the timeout and want to know the reason:
|
|
|
|
var lastError error
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Attempt to run the task:
|
2021-03-17 14:28:04 -04:00
|
|
|
go t.tryExecuteAsync(results)
|
2021-01-29 16:51:08 -05:00
|
|
|
|
|
|
|
// Wait until a result is sent, the timeout is reached or the task canceled, capturing errors
|
|
|
|
// errors along the way:
|
|
|
|
select {
|
|
|
|
case <-t.exit:
|
2021-03-17 14:28:04 -04:00
|
|
|
return t.exitResult() // stop retrying when we get the done signal
|
2021-01-29 16:51:08 -05:00
|
|
|
|
|
|
|
case result := <-results:
|
2021-03-17 14:28:04 -04:00
|
|
|
if result.Err == nil {
|
|
|
|
return result // we're done! nice work everyone.
|
|
|
|
}
|
2021-01-29 16:51:08 -05:00
|
|
|
|
2021-03-17 14:28:04 -04:00
|
|
|
lastError = result.Err // keep retrying when an attempt fails
|
2021-01-29 16:51:08 -05:00
|
|
|
|
|
|
|
case <-timeout:
|
2021-03-17 14:28:04 -04:00
|
|
|
return t.errorResult(fmt.Errorf("Task timed out. Last error: %w", lastError)) // stop on timeout
|
2021-01-29 16:51:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:28:04 -04:00
|
|
|
func (t *scanTask) tryExecuteAsync(results chan *scanTaskResult) {
|
2021-01-29 16:51:08 -05:00
|
|
|
// Errors will almost certainly arise from Electrum server failures, which are extremely
|
|
|
|
// common. Unreachable IPs, dropped connections, sudden EOFs, etc. We'll run this task, assuming
|
|
|
|
// the servers are at fault when something fails, disconnecting and cycling them as we retry.
|
2021-03-17 14:28:04 -04:00
|
|
|
result := t.tryExecute()
|
2021-01-29 16:51:08 -05:00
|
|
|
|
2021-03-17 14:28:04 -04:00
|
|
|
if result.Err != nil {
|
2021-01-29 16:51:08 -05:00
|
|
|
t.client.Disconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
results <- result
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:28:04 -04:00
|
|
|
func (t *scanTask) tryExecute() *scanTaskResult {
|
2021-01-29 16:51:08 -05:00
|
|
|
// If our client is not connected, make an attempt to connect to a server:
|
|
|
|
if !t.client.IsConnected() {
|
|
|
|
err := t.client.Connect(t.servers.NextServer())
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-17 14:28:04 -04:00
|
|
|
return t.errorResult(err)
|
2021-01-29 16:51:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the output scripts for all given addresses:
|
|
|
|
outputScripts, err := getOutputScripts(t.addresses)
|
|
|
|
if err != nil {
|
2021-03-17 14:28:04 -04:00
|
|
|
return t.errorResult(err)
|
2021-01-29 16:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the index hashes that Electrum requires to list outputs:
|
|
|
|
indexHashes, err := getIndexHashes(outputScripts)
|
|
|
|
if err != nil {
|
2021-03-17 14:28:04 -04:00
|
|
|
return t.errorResult(err)
|
2021-01-29 16:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call Electrum to get the unspent output list, grouped by index for each address:
|
|
|
|
var unspentRefGroups [][]electrum.UnspentRef
|
|
|
|
|
|
|
|
if t.client.SupportsBatching() {
|
|
|
|
unspentRefGroups, err = t.listUnspentWithBatching(indexHashes)
|
|
|
|
} else {
|
|
|
|
unspentRefGroups, err = t.listUnspentWithoutBatching(indexHashes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-17 14:28:04 -04:00
|
|
|
return t.errorResult(err)
|
2021-01-29 16:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the results into a list of `Utxos`:
|
2021-03-17 14:28:04 -04:00
|
|
|
var utxos []*Utxo
|
2021-01-29 16:51:08 -05:00
|
|
|
|
|
|
|
for i, unspentRefGroup := range unspentRefGroups {
|
|
|
|
for _, unspentRef := range unspentRefGroup {
|
2021-03-17 14:28:04 -04:00
|
|
|
newUtxo := &Utxo{
|
2021-01-29 16:51:08 -05:00
|
|
|
TxID: unspentRef.TxHash,
|
|
|
|
OutputIndex: unspentRef.TxPos,
|
|
|
|
Amount: unspentRef.Value,
|
|
|
|
Script: outputScripts[i],
|
|
|
|
Address: t.addresses[i],
|
|
|
|
}
|
|
|
|
|
|
|
|
utxos = append(utxos, newUtxo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:28:04 -04:00
|
|
|
return t.successResult(utxos)
|
2021-01-29 16:51:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *scanTask) listUnspentWithBatching(indexHashes []string) ([][]electrum.UnspentRef, error) {
|
|
|
|
unspentRefGroups, err := t.client.ListUnspentBatch(indexHashes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Listing with batching failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return unspentRefGroups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *scanTask) listUnspentWithoutBatching(indexHashes []string) ([][]electrum.UnspentRef, error) {
|
|
|
|
var unspentRefGroups [][]electrum.UnspentRef
|
|
|
|
|
|
|
|
for _, indexHash := range indexHashes {
|
|
|
|
newGroup, err := t.client.ListUnspent(indexHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Listing without batching failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unspentRefGroups = append(unspentRefGroups, newGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return unspentRefGroups, nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:28:04 -04:00
|
|
|
func (t *scanTask) errorResult(err error) *scanTaskResult {
|
|
|
|
return &scanTaskResult{Task: t, Err: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *scanTask) successResult(utxos []*Utxo) *scanTaskResult {
|
|
|
|
return &scanTaskResult{Task: t, Utxos: utxos}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *scanTask) exitResult() *scanTaskResult {
|
|
|
|
return &scanTaskResult{Task: t}
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:51:08 -05:00
|
|
|
// getIndexHashes calculates all the Electrum index hashes for a list of output scripts.
|
|
|
|
func getIndexHashes(outputScripts [][]byte) ([]string, error) {
|
|
|
|
indexHashes := make([]string, len(outputScripts))
|
|
|
|
|
|
|
|
for i, outputScript := range outputScripts {
|
|
|
|
indexHashes[i] = electrum.GetIndexHash(outputScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexHashes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOutputScripts creates all the scripts that send to an list of Bitcoin address.
|
|
|
|
func getOutputScripts(addresses []libwallet.MuunAddress) ([][]byte, error) {
|
|
|
|
outputScripts := make([][]byte, len(addresses))
|
|
|
|
|
|
|
|
for i, address := range addresses {
|
|
|
|
rawAddress := address.Address()
|
|
|
|
|
|
|
|
decodedAddress, err := btcutil.DecodeAddress(rawAddress, &chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to decode address %s: %w", rawAddress, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outputScript, err := txscript.PayToAddrScript(decodedAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to craft script for %s: %w", rawAddress, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outputScripts[i] = outputScript
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputScripts, nil
|
|
|
|
}
|