Skip to content

Commit

Permalink
Add UtxosByScriptHashCount
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Jan 24, 2025
1 parent 0062118 commit 4e66627
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions database/tbcd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type Database interface {
BlockInTxIndex(ctx context.Context, hash *chainhash.Hash) (bool, error)
ScriptHashByOutpoint(ctx context.Context, op Outpoint) (*ScriptHash, error)
UtxosByScriptHash(ctx context.Context, sh ScriptHash, start uint64, count uint64) ([]Utxo, error)
UtxosByScriptHashCount(ctx context.Context, sh ScriptHash) (uint64, error)
}

// XXX there exist various types in this file that need to be reevaluated.
Expand Down
21 changes: 21 additions & 0 deletions database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,27 @@ func (l *ldb) UtxosByScriptHash(ctx context.Context, sh tbcd.ScriptHash, start u
return utxos, nil
}

func (l *ldb) UtxosByScriptHashCount(ctx context.Context, sh tbcd.ScriptHash) (uint64, error) {
log.Tracef("UtxosByScriptHashCount")
defer log.Tracef("UtxosByScriptHashCount exit")

var prefix [33]byte
prefix[0] = 'h'
copy(prefix[1:], sh[:])
oDB := l.pool[level.OutputsDB]
it := oDB.NewIterator(util.BytesPrefix(prefix[:]), nil)
defer it.Release()
var x uint64
for it.Next() {
x++
}
if err := it.Error(); err != nil {
return 0, IteratorError(err)
}

return x, nil
}

func (l *ldb) BlockUtxoUpdate(ctx context.Context, direction int, utxos map[tbcd.Outpoint]tbcd.CacheOutput) error {
log.Tracef("BlockUtxoUpdate")
defer log.Tracef("BlockUtxoUpdate exit")
Expand Down
15 changes: 14 additions & 1 deletion service/tbc/tbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1825,12 +1825,25 @@ func (s *Server) UtxosByScriptHash(ctx context.Context, hash tbcd.ScriptHash, st
defer log.Tracef("UtxosByScriptHash exit")

if s.cfg.ExternalHeaderMode {
return nil, errors.New("cannot call UtxosByScriptHash on TBC running in External Header mode")
return nil, errors.New("cannot call UtxosByScriptHash on " +
"TBC running in External Header mode")
}

return s.db.UtxosByScriptHash(ctx, hash, start, count)
}

func (s *Server) UtxosByScriptHashCount(ctx context.Context, hash tbcd.ScriptHash) (uint64, error) {
log.Tracef("UtxosByScriptHashCount")
defer log.Tracef("UtxosByScriptHashCount exit")

if s.cfg.ExternalHeaderMode {
return 0, errors.New("cannot call UtxosByScriptHashCount on " +
"TBC running in External Header mode")
}

return s.db.UtxosByScriptHashCount(ctx, hash)
}

// ScriptHashAvailableToSpend returns a boolean which indicates whether
// a specific output (uniquely identified by TxId output index) is
// available for spending in the UTXO table.
Expand Down

0 comments on commit 4e66627

Please sign in to comment.