Skip to content

Commit

Permalink
Fallback to scanner to fetch confirmation info for external txs (#82)
Browse files Browse the repository at this point in the history
* Fallback to esplora to fetch confirmation info for external txs

* Fallback to electrum to get tx confirmation info
  • Loading branch information
altafan authored Feb 8, 2024
1 parent b21c38e commit ddbbdd3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
5 changes: 0 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ const (
// RootPathKey is the key to use a custom root path for the wallet,
// instead of the default m/84'/[1776|1]' (depending on network).
RootPathKey = "ROOT_PATH"
// EsploraUrlKey is the key for the esplora block esplorer consumed by the
// neutrino blockchain scanner.
EsploraUrlKey = "ESPLORA_URL"
// ElectrumUrlKey is the key for the electrum server endpoint consumed by the
// electrum blockchain scanner.
ElectrumUrlKey = "ELECTRUM_URL"
Expand Down Expand Up @@ -105,7 +102,6 @@ var (
defaultProfilerPort = 18001
defaultStatsInterval = 600 // 10 minutes
defaultUtxoExpiryDuration = 360 // 6 minutes (3 blocks)
defaultEsploraUrl = "https://blockstream.info/liquid/api"
defaultElectrumUrl = "ssl://blockstream.info:995"
defaultDustAmount = uint64(450)

Expand Down Expand Up @@ -147,7 +143,6 @@ func init() {
vip.SetDefault(ProfilerPortKey, defaultProfilerPort)
vip.SetDefault(StatsIntervalKey, defaultStatsInterval)
vip.SetDefault(UtxoExpiryDurationKey, defaultUtxoExpiryDuration)
vip.SetDefault(EsploraUrlKey, defaultEsploraUrl)
vip.SetDefault(DbUserKey, "root")
vip.SetDefault(DbPassKey, "secret")
vip.SetDefault(DbHostKey, "127.0.0.1")
Expand Down
1 change: 1 addition & 0 deletions internal/core/application/transaction_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewTransactionService(
format = fmt.Sprintf("transaction service: %s", format)
log.Debugf(format, a...)
}

svc := &TransactionService{
repoManager, bcScanner, net, utxoExpiryDuration, dustAmount, logFn,
}
Expand Down
40 changes: 37 additions & 3 deletions internal/infrastructure/blockchain-scanner/electrum/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type service struct {
utxoChannelByAccount map[string]chan []*domain.Utxo
txChannelByAccount map[string]chan *domain.Transaction
reportChannelByAccount map[string]chan accountReport
blocksByHeight map[uint64]blockInfo

log func(format string, a ...interface{})
warn func(err error, format string, a ...interface{})
Expand Down Expand Up @@ -80,6 +81,7 @@ func NewService(args ServiceArgs) (ports.BlockchainScanner, error) {
accountAddressesByScriptHash := make(
map[string]map[string]domain.AddressInfo,
)
blocksByHeight := make(map[uint64]blockInfo)

client, err := args.client()
if err != nil {
Expand All @@ -98,7 +100,7 @@ func NewService(args ServiceArgs) (ports.BlockchainScanner, error) {
svc := &service{
client, db, lock, args.Network, accountAddressesByScriptHash,
utxoChannelByAccount, txChannelByAccount, reportChannelByAccount,
logFn, warnFn,
blocksByHeight, logFn, warnFn,
}
svc.db.registerEventHandler(svc.dbEventHandler)

Expand Down Expand Up @@ -382,10 +384,42 @@ func (s *service) GetTransactions(txids []string) ([]domain.Transaction, error)
}
txs := make([]domain.Transaction, 0, len(res))
for _, tx := range res {
txid := tx.TxHash().String()
scriptHash := calcScriptHash(hex.EncodeToString(tx.Outputs[0].Script))
history, _ := s.client.getScriptHashesHistory([]string{scriptHash})
var height int64
for _, tx := range history[scriptHash] {
if tx.Txid == txid {
height = tx.Height
break
}
}
var blockhash string
var blockheight uint64
var blocktime int64
if height > 0 {
info, ok := s.blocksByHeight[uint64(height)]
if ok {
blockhash = info.hash().String()
blockheight = info.Height
blocktime = info.timestamp()
} else {
info, _ := s.client.getBlocksInfo([]uint32{uint32(height)})
if len(info) > 0 {
s.blocksByHeight[uint64(height)] = info[0]
blockhash = info[0].hash().String()
blockheight = info[0].Height
blocktime = info[0].timestamp()
}
}
}
txHex, _ := tx.ToHex()
txs = append(txs, domain.Transaction{
TxID: tx.TxHash().String(),
TxHex: txHex,
TxID: txid,
TxHex: txHex,
BlockHash: blockhash,
BlockHeight: blockheight,
BlockTime: blocktime,
})
}
return txs, nil
Expand Down

0 comments on commit ddbbdd3

Please sign in to comment.