rework index type
This commit is contained in:
parent
abaedd4f39
commit
7907bfeac7
@ -293,15 +293,6 @@ func (b *BitcoinRPC) GetTransaction(txid string) (*Tx, error) {
|
|||||||
return &res.Result, nil
|
return &res.Result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAddress returns address of transaction output.
|
|
||||||
func (b *BitcoinRPC) GetAddress(txid string, vout uint32) (string, error) {
|
|
||||||
tx, err := b.GetTransaction(txid)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return tx.GetAddress(vout), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BitcoinRPC) call(req interface{}, res interface{}) error {
|
func (b *BitcoinRPC) call(req interface{}, res interface{}) error {
|
||||||
httpData, err := json.Marshal(req)
|
httpData, err := json.Marshal(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
299
blockbook.go
299
blockbook.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/profile"
|
"github.com/pkg/profile"
|
||||||
@ -12,105 +13,19 @@ type BlockParser interface {
|
|||||||
ParseBlock(b []byte) (*Block, error)
|
ParseBlock(b []byte) (*Block, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Blocks interface {
|
type Blockchain interface {
|
||||||
GetBestBlockHash() (string, error)
|
GetBestBlockHash() (string, error)
|
||||||
GetBlockHash(height uint32) (string, error)
|
GetBlockHash(height uint32) (string, error)
|
||||||
GetBlockHeader(hash string) (*BlockHeader, error)
|
GetBlockHeader(hash string) (*BlockHeader, error)
|
||||||
GetBlock(hash string) (*Block, error)
|
GetBlock(hash string) (*Block, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Outpoints interface {
|
type Index interface {
|
||||||
// GetAddress looks up a transaction output and returns its address.
|
GetBestBlockHash() (string, error)
|
||||||
// Address can be empty string in case it's not found or not
|
GetBlockHash(height uint32) (string, error)
|
||||||
// intelligable.
|
GetTransactions(address string, lower uint32, higher uint32, fn func(txid string) error) error
|
||||||
GetAddress(txid string, vout uint32) (string, error)
|
ConnectBlock(block *Block) error
|
||||||
}
|
DisconnectBlock(block *Block) error
|
||||||
|
|
||||||
type Addresses interface {
|
|
||||||
GetTransactions(address string, lower uint32, higher uint32, fn func(txids []string) error) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type Indexer interface {
|
|
||||||
ConnectBlock(block *Block, txids map[string][]string) error
|
|
||||||
DisconnectBlock(block *Block, txids map[string][]string) error
|
|
||||||
GetLastBlockHash() (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) GetAllAddresses(outpoints Outpoints) (map[string][]string, error) {
|
|
||||||
addrs := make(map[string][]string, 0) // Address to a list of txids.
|
|
||||||
|
|
||||||
for i, _ := range b.Txs {
|
|
||||||
tx := &b.Txs[i]
|
|
||||||
ta, err := b.GetTxAddresses(outpoints, tx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for a, _ := range ta {
|
|
||||||
addrs[a] = append(addrs[a], tx.Txid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return addrs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) GetTxAddresses(outpoints Outpoints, tx *Tx) (map[string]struct{}, error) {
|
|
||||||
addrs := make(map[string]struct{}) // Only unique values.
|
|
||||||
|
|
||||||
// Process outputs.
|
|
||||||
for _, o := range tx.Vout {
|
|
||||||
a := o.GetAddress()
|
|
||||||
if a != "" {
|
|
||||||
addrs[a] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process inputs. For each input, we need to take a look to the
|
|
||||||
// outpoint index.
|
|
||||||
for _, i := range tx.Vin {
|
|
||||||
if i.Coinbase != "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lookup output in in the outpoint index. In case it's not
|
|
||||||
// found, take a look in this block.
|
|
||||||
a, err := outpoints.GetAddress(i.Txid, i.Vout)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if a == "" {
|
|
||||||
a = b.GetAddress(i.Txid, i.Vout)
|
|
||||||
}
|
|
||||||
if a != "" {
|
|
||||||
addrs[a] = struct{}{}
|
|
||||||
} else {
|
|
||||||
log.Printf("warn: output not found: %s:%d", i.Txid, i.Vout)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return addrs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) GetAddress(txid string, vout uint32) string {
|
|
||||||
for i, _ := range b.Txs {
|
|
||||||
if b.Txs[i].Txid == txid {
|
|
||||||
return b.Txs[i].GetAddress(vout)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "" // tx not found
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Tx) GetAddress(vout uint32) string {
|
|
||||||
if vout < uint32(len(t.Vout)) {
|
|
||||||
return t.Vout[vout].GetAddress()
|
|
||||||
}
|
|
||||||
return "" // output not found
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *Vout) GetAddress() string {
|
|
||||||
if len(o.ScriptPubKey.Addresses) == 1 {
|
|
||||||
return o.ScriptPubKey.Addresses[0]
|
|
||||||
}
|
|
||||||
return "" // output address not intelligible
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -121,8 +36,9 @@ var (
|
|||||||
|
|
||||||
dbPath = flag.String("path", "./data", "path to address index directory")
|
dbPath = flag.String("path", "./data", "path to address index directory")
|
||||||
|
|
||||||
blockHeight = flag.Int("blockheight", -1, "height of the starting block")
|
blockHeight = flag.Int("blockheight", -1, "height of the starting block")
|
||||||
blockUntil = flag.Int("blockuntil", -1, "height of the final block")
|
blockUntil = flag.Int("blockuntil", -1, "height of the final block")
|
||||||
|
|
||||||
queryAddress = flag.String("address", "", "query contents of this address")
|
queryAddress = flag.String("address", "", "query contents of this address")
|
||||||
|
|
||||||
resync = flag.Bool("resync", false, "resync until tip")
|
resync = flag.Bool("resync", false, "resync until tip")
|
||||||
@ -133,10 +49,6 @@ var (
|
|||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *prof {
|
|
||||||
defer profile.Start().Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
if *repair {
|
if *repair {
|
||||||
if err := RepairRocksDB(*dbPath); err != nil {
|
if err := RepairRocksDB(*dbPath); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -144,8 +56,15 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
timeout := time.Duration(*rpcTimeout) * time.Second
|
if *prof {
|
||||||
rpc := NewBitcoinRPC(*rpcURL, *rpcUser, *rpcPass, timeout)
|
defer profile.Start().Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
rpc := NewBitcoinRPC(
|
||||||
|
*rpcURL,
|
||||||
|
*rpcUser,
|
||||||
|
*rpcPass,
|
||||||
|
time.Duration(*rpcTimeout)*time.Second)
|
||||||
|
|
||||||
db, err := NewRocksDB(*dbPath)
|
db, err := NewRocksDB(*dbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -154,7 +73,7 @@ func main() {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
if *resync {
|
if *resync {
|
||||||
if err := resyncIndex(rpc, db, db); err != nil {
|
if err := resyncIndex(rpc, db); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,54 +91,48 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err = connectBlockRange(rpc, db, db, height, until); err != nil {
|
if err = connectBlocksParallel(rpc, db, height, until); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printResult(txids []string) error {
|
func printResult(txid string) error {
|
||||||
for i, txid := range txids {
|
log.Printf("%s", txid)
|
||||||
log.Printf("%d: %s", i, txid)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resyncIndex(
|
func resyncIndex(chain Blockchain, index Index) error {
|
||||||
blocks Blocks,
|
remote, err := chain.GetBestBlockHash()
|
||||||
outpoints Outpoints,
|
|
||||||
index Indexer,
|
|
||||||
) error {
|
|
||||||
best, err := blocks.GetBestBlockHash()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
last, err := index.GetLastBlockHash()
|
local, err := index.GetBestBlockHash()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
last = ""
|
local = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the local block is missing, we're indexing from the genesis block.
|
// If the local block is missing, we're indexing from the genesis block.
|
||||||
if last == "" {
|
if local == "" {
|
||||||
log.Printf("resync: genesis")
|
log.Printf("resync: genesis")
|
||||||
|
|
||||||
hash, err := blocks.GetBlockHash(0)
|
hash, err := chain.GetBlockHash(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return connectBlock(blocks, outpoints, index, hash)
|
return connectBlock(chain, index, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the locally indexed block is the same as the best block on the
|
// If the locally indexed block is the same as the best block on the
|
||||||
// network, we're done.
|
// network, we're done.
|
||||||
if last == best {
|
if local == remote {
|
||||||
log.Printf("resync: synced on %s", last)
|
log.Printf("resync: synced on %s", local)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is local tip on the best chain?
|
// Is local tip on the best chain?
|
||||||
header, err := blocks.GetBlockHeader(last)
|
header, err := chain.GetBlockHeader(local)
|
||||||
forked := false
|
forked := false
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if e, ok := err.(*RPCError); ok && e.Message == "Block not found" {
|
if e, ok := err.(*RPCError); ok && e.Message == "Block not found" {
|
||||||
@ -236,114 +149,142 @@ func resyncIndex(
|
|||||||
if forked {
|
if forked {
|
||||||
log.Printf("resync: local is forked")
|
log.Printf("resync: local is forked")
|
||||||
// TODO: resync after disconnecting
|
// TODO: resync after disconnecting
|
||||||
return disconnectBlock(blocks, outpoints, index, header.Hash)
|
return disconnectBlock(chain, index, header.Hash)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("resync: local is behind")
|
log.Printf("resync: local is behind")
|
||||||
return connectBlock(blocks, outpoints, index, header.Next)
|
return connectBlock(chain, index, header.Next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func connectBlock(
|
func connectBlock(
|
||||||
blocks Blocks,
|
chain Blockchain,
|
||||||
outpoints Outpoints,
|
index Index,
|
||||||
index Indexer,
|
|
||||||
hash string,
|
hash string,
|
||||||
) error {
|
) error {
|
||||||
bch := make(chan blockResult, 8)
|
bch := make(chan blockResult, 8)
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
go getBlockChain(hash, blocks, bch, done)
|
go getBlockChain(hash, chain, bch, done)
|
||||||
|
|
||||||
for res := range bch {
|
for res := range bch {
|
||||||
err := res.err
|
if res.err != nil {
|
||||||
block := res.block
|
return res.err
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
addrs, err := block.GetAllAddresses(outpoints)
|
err := index.ConnectBlock(res.block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := index.ConnectBlock(block, addrs); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func disconnectBlock(
|
func disconnectBlock(
|
||||||
blocks Blocks,
|
chain Blockchain,
|
||||||
outpoints Outpoints,
|
index Index,
|
||||||
index Indexer,
|
|
||||||
hash string,
|
hash string,
|
||||||
) error {
|
) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func connectBlockRange(
|
func connectBlocksParallel(
|
||||||
blocks Blocks,
|
chain Blockchain,
|
||||||
outpoints Outpoints,
|
index Index,
|
||||||
index Indexer,
|
|
||||||
lower uint32,
|
lower uint32,
|
||||||
higher uint32,
|
higher uint32,
|
||||||
) error {
|
) error {
|
||||||
bch := make(chan blockResult, 3)
|
const chunkSize = 100
|
||||||
|
const numWorkers = 8
|
||||||
|
|
||||||
go getBlockRange(lower, higher, blocks, bch)
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for res := range bch {
|
work := func(i int) {
|
||||||
if res.err != nil {
|
defer wg.Done()
|
||||||
return res.err
|
|
||||||
|
offset := uint32(chunkSize * i)
|
||||||
|
stride := uint32(chunkSize * numWorkers)
|
||||||
|
|
||||||
|
for low := offset; low <= higher; low += stride {
|
||||||
|
high := low + chunkSize - 1
|
||||||
|
if high > higher {
|
||||||
|
high = higher
|
||||||
|
}
|
||||||
|
err := connectBlockChunk(chain, index, low, high)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err) // TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
addrs, err := res.block.GetAllAddresses(outpoints)
|
}
|
||||||
|
for i := 0; i < numWorkers; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go work(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectBlockChunk(
|
||||||
|
chain Blockchain,
|
||||||
|
index Index,
|
||||||
|
lower uint32,
|
||||||
|
higher uint32,
|
||||||
|
) error {
|
||||||
|
connected, err := isBlockConnected(chain, index, higher)
|
||||||
|
if err != nil || connected {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
height := lower
|
||||||
|
hash, err := chain.GetBlockHash(lower)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for height <= higher {
|
||||||
|
block, err := chain.GetBlock(hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := index.ConnectBlock(res.block, addrs); err != nil {
|
hash = block.Next
|
||||||
|
height = block.Height + 1
|
||||||
|
err = index.ConnectBlock(block)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isBlockConnected(
|
||||||
|
chain Blockchain,
|
||||||
|
index Index,
|
||||||
|
height uint32,
|
||||||
|
) (bool, error) {
|
||||||
|
local, err := index.GetBlockHash(height)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
remote, err := chain.GetBlockHash(height)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if local != remote {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
type blockResult struct {
|
type blockResult struct {
|
||||||
block *Block
|
block *Block
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBlockRange(
|
|
||||||
lower uint32,
|
|
||||||
higher uint32,
|
|
||||||
blocks Blocks,
|
|
||||||
results chan<- blockResult,
|
|
||||||
) {
|
|
||||||
defer close(results)
|
|
||||||
|
|
||||||
height := lower
|
|
||||||
hash, err := blocks.GetBlockHash(height)
|
|
||||||
if err != nil {
|
|
||||||
results <- blockResult{err: err}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for height <= higher {
|
|
||||||
block, err := blocks.GetBlock(hash)
|
|
||||||
if err != nil {
|
|
||||||
results <- blockResult{err: err}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hash = block.Next
|
|
||||||
height = block.Height + 1
|
|
||||||
results <- blockResult{block: block}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getBlockChain(
|
func getBlockChain(
|
||||||
hash string,
|
hash string,
|
||||||
blocks Blocks,
|
chain Blockchain,
|
||||||
out chan blockResult,
|
out chan blockResult,
|
||||||
done chan struct{},
|
done chan struct{},
|
||||||
) {
|
) {
|
||||||
@ -355,7 +296,7 @@ func getBlockChain(
|
|||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
block, err := blocks.GetBlock(hash)
|
block, err := chain.GetBlock(hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
out <- blockResult{err: err}
|
out <- blockResult{err: err}
|
||||||
return
|
return
|
||||||
|
|||||||
384
rocksdb.go
384
rocksdb.go
@ -66,29 +66,14 @@ func (d *RocksDB) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RocksDB) GetAddress(txid string, vout uint32) (string, error) {
|
func (d *RocksDB) GetTransactions(address string, lower uint32, higher uint32, fn func(txid string) error) (err error) {
|
||||||
// log.Printf("rocksdb: outpoint get %s:%d", txid, vout)
|
|
||||||
|
|
||||||
k, err := packOutpointKey(txid, vout)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
v, err := d.db.Get(d.ro, k)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer v.Free()
|
|
||||||
return unpackAddress(v.Data())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *RocksDB) GetTransactions(address string, lower uint32, higher uint32, fn func(txids []string) error) (err error) {
|
|
||||||
log.Printf("rocksdb: address get %d:%d %s", lower, higher, address)
|
log.Printf("rocksdb: address get %d:%d %s", lower, higher, address)
|
||||||
|
|
||||||
kstart, err := packAddressKey(lower, address)
|
kstart, err := packOutputKey(address, lower)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
kstop, err := packAddressKey(higher, address)
|
kstop, err := packOutputKey(address, higher)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -97,211 +82,237 @@ func (d *RocksDB) GetTransactions(address string, lower uint32, higher uint32, f
|
|||||||
defer it.Close()
|
defer it.Close()
|
||||||
|
|
||||||
for it.Seek(kstart); it.Valid(); it.Next() {
|
for it.Seek(kstart); it.Valid(); it.Next() {
|
||||||
k := it.Key()
|
key := it.Key()
|
||||||
v := it.Value()
|
val := it.Value()
|
||||||
if bytes.Compare(k.Data(), kstop) > 0 {
|
if bytes.Compare(key.Data(), kstop) > 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
txids, err := unpackAddressVal(v.Data())
|
outpoints, err := unpackOutputValue(val.Data())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := fn(txids); err != nil {
|
for _, o := range outpoints {
|
||||||
return err
|
if err := fn(o.txid); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RocksDB) ConnectBlock(block *Block, txids map[string][]string) error {
|
const (
|
||||||
return d.writeBlock(block, txids, false /* delete */)
|
opInsert = 0
|
||||||
|
opDelete = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
func (d *RocksDB) ConnectBlock(block *Block) error {
|
||||||
|
return d.writeBlock(block, opInsert)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RocksDB) DisconnectBlock(block *Block, txids map[string][]string) error {
|
func (d *RocksDB) DisconnectBlock(block *Block) error {
|
||||||
return d.writeBlock(block, txids, true /* delete */)
|
return d.writeBlock(block, opDelete)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RocksDB) writeBlock(
|
func (d *RocksDB) writeBlock(block *Block, op int) error {
|
||||||
block *Block,
|
|
||||||
txids map[string][]string,
|
|
||||||
delete bool,
|
|
||||||
) error {
|
|
||||||
wb := gorocksdb.NewWriteBatch()
|
wb := gorocksdb.NewWriteBatch()
|
||||||
defer wb.Destroy()
|
defer wb.Destroy()
|
||||||
|
|
||||||
if err := d.writeHeight(wb, block, delete); err != nil {
|
if err := d.writeHeight(wb, block, op); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := d.writeOutpoints(wb, block, delete); err != nil {
|
if err := d.writeOutputs(wb, block, op); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := d.writeAddresses(wb, block, txids, delete); err != nil {
|
if err := d.writeInputs(wb, block, op); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return d.db.Write(d.wo, wb)
|
return d.db.Write(d.wo, wb)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Address Index
|
// Output Index
|
||||||
|
|
||||||
func (d *RocksDB) writeAddresses(
|
type outpoint struct {
|
||||||
|
txid string
|
||||||
|
vout uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *RocksDB) writeOutputs(
|
||||||
wb *gorocksdb.WriteBatch,
|
wb *gorocksdb.WriteBatch,
|
||||||
block *Block,
|
block *Block,
|
||||||
txids map[string][]string,
|
op int,
|
||||||
delete bool,
|
|
||||||
) error {
|
) error {
|
||||||
if delete {
|
switch op {
|
||||||
log.Printf("rocksdb: address delete %d in %d %s", len(txids), block.Height, block.Hash)
|
case opInsert:
|
||||||
} else {
|
log.Printf("rocksdb: outputs insert %d %s", block.Height, block.Hash)
|
||||||
log.Printf("rocksdb: address put %d in %d %s", len(txids), block.Height, block.Hash)
|
case opDelete:
|
||||||
|
log.Printf("rocksdb: outputs delete %d %s", block.Height, block.Hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
for addr, txids := range txids {
|
records := make(map[string][]outpoint)
|
||||||
k, err := packAddressKey(block.Height, addr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
v, err := packAddressVal(txids)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if delete {
|
|
||||||
wb.Delete(k)
|
|
||||||
} else {
|
|
||||||
wb.Put(k, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func packAddressKey(height uint32, address string) (b []byte, err error) {
|
|
||||||
b, err = packAddress(address)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h := packUint(height)
|
|
||||||
b = append(b, h...)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func packAddressVal(txids []string) (b []byte, err error) {
|
|
||||||
for _, txid := range txids {
|
|
||||||
t, err := packTxid(txid)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
b = append(b, t...)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const txidLen = 32
|
|
||||||
|
|
||||||
func unpackAddressVal(b []byte) (txids []string, err error) {
|
|
||||||
for i := 0; i < len(b); i += txidLen {
|
|
||||||
t, err := unpackTxid(b[i : i+txidLen])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
txids = append(txids, t)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Outpoint index
|
|
||||||
|
|
||||||
func (d *RocksDB) writeOutpoints(
|
|
||||||
wb *gorocksdb.WriteBatch,
|
|
||||||
block *Block,
|
|
||||||
delete bool,
|
|
||||||
) error {
|
|
||||||
if delete {
|
|
||||||
log.Printf("rocksdb: outpoints delete %d in %d %s", len(block.Txs), block.Height, block.Hash)
|
|
||||||
} else {
|
|
||||||
log.Printf("rocksdb: outpoints put %d in %d %s", len(block.Txs), block.Height, block.Hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tx := range block.Txs {
|
for _, tx := range block.Txs {
|
||||||
for _, vout := range tx.Vout {
|
for _, output := range tx.Vout {
|
||||||
k, err := packOutpointKey(tx.Txid, vout.N)
|
address := output.GetAddress()
|
||||||
if err != nil {
|
if address != "" {
|
||||||
return err
|
records[address] = append(records[address], outpoint{
|
||||||
}
|
txid: tx.Txid,
|
||||||
v, err := packAddress(vout.GetAddress())
|
vout: output.N,
|
||||||
if err != nil {
|
})
|
||||||
return err
|
|
||||||
}
|
|
||||||
if delete {
|
|
||||||
wb.Delete(k)
|
|
||||||
} else {
|
} else {
|
||||||
if len(v) > 0 {
|
log.Printf("rocksdb: skipping %s:%d", tx.Txid, output.N)
|
||||||
wb.Put(k, v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for address, outpoints := range records {
|
||||||
|
key, err := packOutputKey(address, block.Height)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
val, err := packOutputValue(outpoints)
|
||||||
|
|
||||||
|
switch op {
|
||||||
|
case opInsert:
|
||||||
|
wb.Put(key, val)
|
||||||
|
case opDelete:
|
||||||
|
wb.Delete(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func packOutpointKey(txid string, vout uint32) (b []byte, err error) {
|
func packOutputKey(address string, height uint32) ([]byte, error) {
|
||||||
t, err := packTxid(txid)
|
baddress, err := packAddress(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
v := packVarint(vout)
|
bheight := packUint(height)
|
||||||
b = append(b, t...)
|
buf := make([]byte, 0, len(baddress)+len(bheight))
|
||||||
b = append(b, v...)
|
buf = append(buf, baddress...)
|
||||||
return
|
buf = append(buf, bheight...)
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func packOutputValue(outpoints []outpoint) ([]byte, error) {
|
||||||
|
buf := make([]byte, 0)
|
||||||
|
for _, o := range outpoints {
|
||||||
|
btxid, err := packTxid(o.txid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
bvout := packVaruint(o.vout)
|
||||||
|
buf = append(buf, btxid...)
|
||||||
|
buf = append(buf, bvout...)
|
||||||
|
}
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func unpackOutputValue(buf []byte) ([]outpoint, error) {
|
||||||
|
outpoints := make([]outpoint, 0)
|
||||||
|
for i := 0; i < len(buf); {
|
||||||
|
txid, err := unpackTxid(buf[i : i+txIdUnpackedLen])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
i += txIdUnpackedLen
|
||||||
|
vout, voutLen := unpackVaruint(buf[i:])
|
||||||
|
i += voutLen
|
||||||
|
outpoints = append(outpoints, outpoint{
|
||||||
|
txid: txid,
|
||||||
|
vout: vout,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return outpoints, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input index
|
||||||
|
|
||||||
|
func (d *RocksDB) writeInputs(
|
||||||
|
wb *gorocksdb.WriteBatch,
|
||||||
|
block *Block,
|
||||||
|
op int,
|
||||||
|
) error {
|
||||||
|
switch op {
|
||||||
|
case opInsert:
|
||||||
|
log.Printf("rocksdb: inputs insert %d %s", block.Height, block.Hash)
|
||||||
|
case opDelete:
|
||||||
|
log.Printf("rocksdb: inputs delete %d %s", block.Height, block.Hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tx := range block.Txs {
|
||||||
|
for i, input := range tx.Vin {
|
||||||
|
key, err := packOutpoint(input.Txid, input.Vout)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
val, err := packOutpoint(tx.Txid, uint32(i))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch op {
|
||||||
|
case opInsert:
|
||||||
|
wb.Put(key, val)
|
||||||
|
case opDelete:
|
||||||
|
wb.Delete(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func packOutpoint(txid string, vout uint32) ([]byte, error) {
|
||||||
|
btxid, err := packTxid(txid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
bvout := packVaruint(vout)
|
||||||
|
buf := make([]byte, 0, len(btxid)+len(bvout))
|
||||||
|
buf = append(buf, btxid...)
|
||||||
|
buf = append(buf, bvout...)
|
||||||
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block index
|
// Block index
|
||||||
|
|
||||||
const (
|
func (d *RocksDB) GetBestBlockHash() (string, error) {
|
||||||
lastBlockHash = 0x00
|
return "", nil // TODO
|
||||||
)
|
}
|
||||||
|
|
||||||
var (
|
func (d *RocksDB) GetBlockHash(height uint32) (string, error) {
|
||||||
lastBlockHashKey = []byte{lastBlockHash}
|
key := packUint(height)
|
||||||
)
|
val, err := d.db.Get(d.ro, key)
|
||||||
|
|
||||||
func (d *RocksDB) GetLastBlockHash() (string, error) {
|
|
||||||
v, err := d.db.Get(d.ro, lastBlockHashKey)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer v.Free()
|
defer val.Free()
|
||||||
return unpackBlockValue(v.Data())
|
return unpackBlockValue(val.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *RocksDB) writeHeight(
|
func (d *RocksDB) writeHeight(
|
||||||
wb *gorocksdb.WriteBatch,
|
wb *gorocksdb.WriteBatch,
|
||||||
block *Block,
|
block *Block,
|
||||||
delete bool,
|
op int,
|
||||||
) error {
|
) error {
|
||||||
if delete {
|
switch op {
|
||||||
log.Printf("rocksdb: height delete %d %s", block.Height, block.Hash)
|
case opInsert:
|
||||||
} else {
|
|
||||||
log.Printf("rocksdb: height put %d %s", block.Height, block.Hash)
|
log.Printf("rocksdb: height put %d %s", block.Height, block.Hash)
|
||||||
|
case opDelete:
|
||||||
|
log.Printf("rocksdb: height delete %d %s", block.Height, block.Hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
bk := packUint(block.Height)
|
key := packUint(block.Height)
|
||||||
|
|
||||||
if delete {
|
switch op {
|
||||||
bv, err := packBlockValue(block.Prev)
|
case opInsert:
|
||||||
|
val, err := packBlockValue(block.Hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
wb.Delete(bk)
|
wb.Put(key, val)
|
||||||
wb.Put(lastBlockHashKey, bv)
|
case opDelete:
|
||||||
|
wb.Delete(key)
|
||||||
} else {
|
|
||||||
bv, err := packBlockValue(block.Hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
wb.Put(bk, bv)
|
|
||||||
wb.Put(lastBlockHashKey, bv)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -309,57 +320,54 @@ func (d *RocksDB) writeHeight(
|
|||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
|
|
||||||
|
const txIdUnpackedLen = 32
|
||||||
|
|
||||||
|
var ErrInvalidAddress = errors.New("invalid address")
|
||||||
|
|
||||||
func packUint(i uint32) []byte {
|
func packUint(i uint32) []byte {
|
||||||
b := make([]byte, 4)
|
buf := make([]byte, 4)
|
||||||
binary.BigEndian.PutUint32(b, i)
|
binary.BigEndian.PutUint32(buf, i)
|
||||||
return b
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func packVarint(i uint32) []byte {
|
func packVaruint(i uint32) []byte {
|
||||||
b := make([]byte, vlq.MaxLen32)
|
buf := make([]byte, vlq.MaxLen32)
|
||||||
n := vlq.PutUint(b, uint64(i))
|
ofs := vlq.PutUint(buf, uint64(i))
|
||||||
return b[:n]
|
return buf[:ofs]
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
func unpackVaruint(buf []byte) (uint32, int) {
|
||||||
ErrInvalidAddress = errors.New("invalid address")
|
i, ofs := vlq.Uint(buf)
|
||||||
)
|
return uint32(i), ofs
|
||||||
|
}
|
||||||
|
|
||||||
func packAddress(s string) ([]byte, error) {
|
func packAddress(address string) ([]byte, error) {
|
||||||
var b []byte
|
buf := base58.Decode(address)
|
||||||
if len(s) == 0 {
|
if len(buf) <= 4 {
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
b = base58.Decode(s)
|
|
||||||
if len(b) <= 4 {
|
|
||||||
return nil, ErrInvalidAddress
|
return nil, ErrInvalidAddress
|
||||||
}
|
}
|
||||||
b = b[:len(b)-4] // Slice off the checksum
|
return buf[:len(buf)-4], nil // Slice off the checksum
|
||||||
return b, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func unpackAddress(b []byte) (string, error) {
|
func unpackAddress(buf []byte) (string, error) {
|
||||||
if len(b) == 0 {
|
if len(buf) < 2 {
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
if len(b) == 1 {
|
|
||||||
return "", ErrInvalidAddress
|
return "", ErrInvalidAddress
|
||||||
}
|
}
|
||||||
return base58.CheckEncode(b[1:], b[0]), nil
|
return base58.CheckEncode(buf[1:], buf[0]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func packTxid(s string) ([]byte, error) {
|
func packTxid(txid string) ([]byte, error) {
|
||||||
return hex.DecodeString(s)
|
return hex.DecodeString(txid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unpackTxid(b []byte) (string, error) {
|
func unpackTxid(buf []byte) (string, error) {
|
||||||
return hex.EncodeToString(b), nil
|
return hex.EncodeToString(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func packBlockValue(hash string) ([]byte, error) {
|
func packBlockValue(hash string) ([]byte, error) {
|
||||||
return hex.DecodeString(hash)
|
return hex.DecodeString(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unpackBlockValue(b []byte) (string, error) {
|
func unpackBlockValue(buf []byte) (string, error) {
|
||||||
return hex.EncodeToString(b), nil
|
return hex.EncodeToString(buf), nil
|
||||||
}
|
}
|
||||||
|
|||||||
7
types.go
7
types.go
@ -26,6 +26,13 @@ type Vout struct {
|
|||||||
ScriptPubKey ScriptPubKey `json:"scriptPubKey"`
|
ScriptPubKey ScriptPubKey `json:"scriptPubKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (vout *Vout) GetAddress() string {
|
||||||
|
if len(vout.ScriptPubKey.Addresses) != 1 {
|
||||||
|
return "" // output address not intelligible
|
||||||
|
}
|
||||||
|
return vout.ScriptPubKey.Addresses[0]
|
||||||
|
}
|
||||||
|
|
||||||
type Tx struct {
|
type Tx struct {
|
||||||
Txid string `json:"txid"`
|
Txid string `json:"txid"`
|
||||||
Version int32 `json:"version"`
|
Version int32 `json:"version"`
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user