From 7416e9a71d7791f0645667f6d76f1f961669ddb6 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 30 May 2013 17:27:39 -0500 Subject: [PATCH] Rename funcs and variables for Idx to Height. The Db interface is intended to work with block heights as opposed to specific database ids which may or may not be the same as the block height. This commits changes the function names to make that distinction a little more clear. --- db.go | 16 ++++++++-------- sqlite3/sqliteblock.go | 25 +++++++++++++------------ sqlite3/sqliteblock_test.go | 4 ++-- sqlite3/sqlitedbcache.go | 2 +- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/db.go b/db.go index 91054895..5e1526bf 100644 --- a/db.go +++ b/db.go @@ -54,15 +54,15 @@ type Db interface { // cache the underlying object if desired. FetchBlockBySha(sha *btcwire.ShaHash) (blk *btcutil.Block, err error) - // FetchBlockShaByIdx returns a block hash based on its height in the - // blockchain. - FetchBlockShaByIdx(blkid int64) (sha *btcwire.ShaHash, err error) + // FetchBlockShaByHeight returns a block hash based on its height in the + // block chain. + FetchBlockShaByHeight(height int64) (sha *btcwire.ShaHash, err error) - // FetchIdxRange looks up a range of blocks by the start and ending + // FetchHeightRange looks up a range of blocks by the start and ending // heights. Fetch is inclusive of the start height and exclusive of the // ending height. To fetch all hashes from the start height until no // more are present, use the special id `AllShas'. - FetchIdxRange(startid, endid int64) (rshalist []btcwire.ShaHash, err error) + FetchHeightRange(startHeight, endHeight int64) (rshalist []btcwire.ShaHash, err error) // FetchTxAllBySha returns several pieces of data for a given // transaction hash. @@ -85,7 +85,7 @@ type Db interface { // InsertBlock inserts raw block and transaction data from a block // into the database. - InsertBlock(block *btcutil.Block) (blockid int64, err error) + InsertBlock(block *btcutil.Block) (height int64, err error) // InsertBlockData stores a block hash and its associated data block // with the given previous hash and protocol version into the database. @@ -93,7 +93,7 @@ type Db interface { // InsertTx stores a transaction hash and its associated data into the // database. - InsertTx(txsha *btcwire.ShaHash, blockidx int64, txoff int, txlen int, usedbuf []byte) (err error) + InsertTx(txsha *btcwire.ShaHash, blockHeight int64, txoff int, txlen int, usedbuf []byte) (err error) // InvalidateBlockCache releases all cached blocks. InvalidateBlockCache() @@ -109,7 +109,7 @@ type Db interface { // NewestSha provides an interface to quickly look up the hash of // the most recent (end) block of the block chain. - NewestSha() (sha *btcwire.ShaHash, blkid int64, err error) + NewestSha() (sha *btcwire.ShaHash, height int64, err error) // RollbackClose discards the recent database changes to the previously // saved data at last Sync and closes the database. diff --git a/sqlite3/sqliteblock.go b/sqlite3/sqliteblock.go index 5416e160..b2c2b84e 100644 --- a/sqlite3/sqliteblock.go +++ b/sqlite3/sqliteblock.go @@ -151,13 +151,14 @@ func (db *SqliteDb) blkExistsSha(sha *btcwire.ShaHash) bool { return true } -// FetchBlockShaByIdx returns a block sha based on its height in the blockchain. -func (db *SqliteDb) FetchBlockShaByIdx(blkid int64) (sha *btcwire.ShaHash, err error) { +// FetchBlockShaByHeight returns a block hash based on its height in the +// block chain. +func (db *SqliteDb) FetchBlockShaByHeight(height int64) (sha *btcwire.ShaHash, err error) { var row *sql.Row db.dbLock.Lock() defer db.dbLock.Unlock() - blockidx := blkid + 1 // skew between btc blockid and sql + blockidx := height + 1 // skew between btc blockid and sql row = db.blkStmts[blkFetchIdx].QueryRow(blockidx) @@ -171,21 +172,21 @@ func (db *SqliteDb) FetchBlockShaByIdx(blkid int64) (sha *btcwire.ShaHash, err e return &shaval, nil } -// FetchIdxRange looks up a range of block by the start and ending ids. -// Fetch is inclusive of the start id and exclusive of the ending id. If the -// special id `AllShas' is provided as endid then FetchIdxRange will fetch all -// shas from startid until no more shas are present. -func (db *SqliteDb) FetchIdxRange(startid, endid int64) (rshalist []btcwire.ShaHash, err error) { +// FetchHeightRange looks up a range of blocks by the start and ending +// heights. Fetch is inclusive of the start height and exclusive of the +// ending height. To fetch all hashes from the start height until no +// more are present, use the special id `AllShas'. +func (db *SqliteDb) FetchHeightRange(startHeight, endHeight int64) (rshalist []btcwire.ShaHash, err error) { db.dbLock.Lock() defer db.dbLock.Unlock() - startidx := startid + 1 // skew between btc blockid and sql + startidx := startHeight + 1 // skew between btc block height and sql var endidx int64 - if endid == btcdb.AllShas { + if endHeight == btcdb.AllShas { endidx = btcdb.AllShas // no skew if asking for all } else { - endidx = endid + 1 // skew between btc blockid and sql + endidx = endHeight + 1 // skew between btc block height and sql } rows, err := db.blkStmts[blkFetchIdxList].Query(startidx, endidx) if err != nil { @@ -209,7 +210,7 @@ func (db *SqliteDb) FetchIdxRange(startid, endid int64) (rshalist []btcwire.ShaH if err == nil { rshalist = shalist } - log.Tracef("FetchIdxRange idx %v %v returned %v shas err %v", startid, endid, len(shalist), err) + log.Tracef("FetchIdxRange idx %v %v returned %v shas err %v", startHeight, endHeight, len(shalist), err) return } diff --git a/sqlite3/sqliteblock_test.go b/sqlite3/sqliteblock_test.go index 80622436..2b2d17b7 100644 --- a/sqlite3/sqliteblock_test.go +++ b/sqlite3/sqliteblock_test.go @@ -141,7 +141,7 @@ func testFetch(t *testing.T, db btcdb.Db, shas []btcwire.ShaHash, } // Fetch the sha by index and ensure it matches. - tsha, err := db.FetchBlockShaByIdx(int64(i)) + tsha, err := db.FetchBlockShaByHeight(int64(i)) if err != nil { t.Errorf("can't fetch sha at index %d: %v", i, err) continue @@ -174,7 +174,7 @@ func testFetch(t *testing.T, db btcdb.Db, shas []btcwire.ShaHash, for _, test := range fetchIdxTests { t.Logf("numSha: %d - Fetch from %d to %d\n", numShas, test.start, test.end) - if shalist, err := db.FetchIdxRange(test.start, test.end); err == nil { + if shalist, err := db.FetchHeightRange(test.start, test.end); err == nil { compareArray(t, shalist, test.exp, test.test, sync) } else { t.Errorf("failed to fetch index range for %s (%s)", diff --git a/sqlite3/sqlitedbcache.go b/sqlite3/sqlitedbcache.go index feba026d..45de6cee 100644 --- a/sqlite3/sqlitedbcache.go +++ b/sqlite3/sqlitedbcache.go @@ -137,7 +137,7 @@ func (db *SqliteDb) FetchTxAllBySha(txsha *btcwire.ShaHash) (rtx *btcwire.MsgTx, return } - blksha, err := db.FetchBlockShaByIdx(bidx) + blksha, err := db.FetchBlockShaByHeight(bidx) if err != nil { log.Warnf("block idx lookup %v to %v", bidx, err) return