commit 4996d275087ededb25ed8c9c8745bb8cace685f8
parent caeef8c14a7b4f5c17f45469001c54c4927c4677
Author: Shivakumar <[email protected]>
Date: Wed, 5 Aug 2026 23:50:04 +0530
feat(blockchain): add GetBlockByTxID
Signed-off-by: Shivakumar <[email protected]>
Diffstat:
1 file changed, 17 insertions(+), 0 deletions(-)
diff --git a/internal/blockchain/blockchain.go b/internal/blockchain/blockchain.go
@@ -303,6 +303,23 @@ func (bc *Blockchain) GetBlockByHeight(height uint64) (*Block, error) {
return bc.GetBlock(hash)
}
+// GetBlockByTxID returns the block with the given transaction ID.
+func (bc *Blockchain) GetBlockByTxID(txID []byte) (*Block, error) {
+ if len(txID) != crypto.HashSize {
+ return nil, ErrInvalidTxID
+ }
+
+ bc.mu.RLock()
+ hash, err := bc.store.Get(storage.TxIndexKey(txID))
+ bc.mu.RUnlock() // Release lock before calling GetBlock to prevent potential deadlocks
+
+ if err != nil {
+ return nil, err
+ }
+
+ return bc.GetBlock(hash)
+}
+
// GetTransaction locates a transaction by its ID across the entire blockchain.
func (bc *Blockchain) GetTransaction(txID []byte) (*transaction.Transaction, error) {
if len(txID) != crypto.HashSize {