Skip to content

Instantly share code, notes, and snippets.

@aakselrod
Created March 25, 2015 17:31
Show Gist options
  • Save aakselrod/9bc80ca8734d61858d16 to your computer and use it in GitHub Desktop.
Save aakselrod/9bc80ca8734d61858d16 to your computer and use it in GitHub Desktop.
Gets the spent record from a btcd database for a transaction
package main
import (
"encoding/hex"
"fmt"
"github.com/btcsuite/goleveldb/leveldb"
"github.com/btcsuite/goleveldb/leveldb/opt"
)
func main() {
txHash := "fa6764111c4784d8ff56371f88046dbdca34be434d6e5136f759b15efbced153"
txKey, err := hex.DecodeString(txHash)
if err != nil {
fmt.Println("Error decoding hex hash:", err.Error())
return
}
// Hash is stored backwards - WEIRD!
for i := 0; i < 16; i++ {
txKey[i], txKey[31-i] = txKey[31-i], txKey[i]
}
// Want the "spent" records
txKey = append(txKey, byte('s'), byte('x'))
options := opt.Options{
BlockCacher: opt.DefaultBlockCacher,
Compression: opt.NoCompression,
OpenFilesCacher: opt.DefaultOpenFilesCacher,
}
ro := opt.ReadOptions{}
db, err := leveldb.OpenFile(
"/home/aakselrod/.btcd/data/testnet/blocks_leveldb",
&options,
)
if err != nil {
fmt.Println("Error opening DB:", err.Error())
return
}
defer db.Close()
buf, err := db.Get(txKey, &ro)
if err != nil {
fmt.Println("Error getting spent record for TX:", err.Error())
return
}
fmt.Println(hex.EncodeToString(buf))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment