From cb4ae18f437ccc3cf53284313404f08c9bee28ae Mon Sep 17 00:00:00 2001 From: Wei Yang Date: Sun, 22 Oct 2017 10:39:31 +0800 Subject: [PATCH] export DB in blockchain --- blockchain.go | 14 +++++++------- cli_createblockchain.go | 2 +- cli_getbalance.go | 2 +- cli_printchain.go | 4 ++-- cli_send.go | 2 +- utxo_set.go | 10 +++++----- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/blockchain.go b/blockchain.go index 28b01c0..7135732 100644 --- a/blockchain.go +++ b/blockchain.go @@ -19,7 +19,7 @@ const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second // Blockchain implements interactions with a DB type Blockchain struct { tip []byte - db *bolt.DB + DB *bolt.DB } // CreateBlockchain creates a new blockchain DB @@ -99,7 +99,7 @@ func NewBlockchain(nodeID string) *Blockchain { // AddBlock saves the block into the blockchain func (bc *Blockchain) AddBlock(block *Block) { - err := bc.db.Update(func(tx *bolt.Tx) error { + err := bc.DB.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(blocksBucket)) blockInDb := b.Get(block.Hash) @@ -199,7 +199,7 @@ func (bc *Blockchain) FindUTXO() map[string]TXOutputs { // Iterator returns a BlockchainIterat func (bc *Blockchain) Iterator() *BlockchainIterator { - bci := &BlockchainIterator{bc.tip, bc.db} + bci := &BlockchainIterator{bc.tip, bc.DB} return bci } @@ -208,7 +208,7 @@ func (bc *Blockchain) Iterator() *BlockchainIterator { func (bc *Blockchain) GetBestHeight() int { var lastBlock Block - err := bc.db.View(func(tx *bolt.Tx) error { + err := bc.DB.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(blocksBucket)) lastHash := b.Get([]byte("l")) blockData := b.Get(lastHash) @@ -227,7 +227,7 @@ func (bc *Blockchain) GetBestHeight() int { func (bc *Blockchain) GetBlock(blockHash []byte) (Block, error) { var block Block - err := bc.db.View(func(tx *bolt.Tx) error { + err := bc.DB.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(blocksBucket)) blockData := b.Get(blockHash) @@ -277,7 +277,7 @@ func (bc *Blockchain) MineBlock(transactions []*Transaction) *Block { } } - err := bc.db.View(func(tx *bolt.Tx) error { + err := bc.DB.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(blocksBucket)) lastHash = b.Get([]byte("l")) @@ -294,7 +294,7 @@ func (bc *Blockchain) MineBlock(transactions []*Transaction) *Block { newBlock := NewBlock(transactions, lastHash, lastHeight+1) - err = bc.db.Update(func(tx *bolt.Tx) error { + err = bc.DB.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(blocksBucket)) err := b.Put(newBlock.Hash, newBlock.Serialize()) if err != nil { diff --git a/cli_createblockchain.go b/cli_createblockchain.go index 5b86845..bc77fe9 100644 --- a/cli_createblockchain.go +++ b/cli_createblockchain.go @@ -10,7 +10,7 @@ func (cli *CLI) createBlockchain(address, nodeID string) { log.Panic("ERROR: Address is not valid") } bc := CreateBlockchain(address, nodeID) - defer bc.db.Close() + defer bc.DB.Close() UTXOSet := UTXOSet{bc} UTXOSet.Reindex() diff --git a/cli_getbalance.go b/cli_getbalance.go index 6c8b7fa..9aa7c0f 100644 --- a/cli_getbalance.go +++ b/cli_getbalance.go @@ -11,7 +11,7 @@ func (cli *CLI) getBalance(address, nodeID string) { } bc := NewBlockchain(nodeID) UTXOSet := UTXOSet{bc} - defer bc.db.Close() + defer bc.DB.Close() balance := 0 pubKeyHash := Base58Decode([]byte(address)) diff --git a/cli_printchain.go b/cli_printchain.go index bcb66ba..7aaa63f 100644 --- a/cli_printchain.go +++ b/cli_printchain.go @@ -8,7 +8,7 @@ import ( func (cli *CLI) printChain(nodeID string) { bc := NewBlockchain(nodeID) - defer bc.db.Close() + defer bc.DB.Close() bci := bc.Iterator() @@ -34,7 +34,7 @@ func (cli *CLI) printChain(nodeID string) { func (cli *CLI) printBlock(blockHash, nodeID string) { bc := NewBlockchain(nodeID) - defer bc.db.Close() + defer bc.DB.Close() bci := bc.Iterator() diff --git a/cli_send.go b/cli_send.go index 1f22cbb..f2f5876 100644 --- a/cli_send.go +++ b/cli_send.go @@ -15,7 +15,7 @@ func (cli *CLI) send(from, to string, amount int, nodeID string, mineNow bool) { bc := NewBlockchain(nodeID) UTXOSet := UTXOSet{bc} - defer bc.db.Close() + defer bc.DB.Close() wallets, err := NewWallets(nodeID) if err != nil { diff --git a/utxo_set.go b/utxo_set.go index 902b530..7adbad9 100644 --- a/utxo_set.go +++ b/utxo_set.go @@ -18,7 +18,7 @@ type UTXOSet struct { func (u UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[string][]int) { unspentOutputs := make(map[string][]int) accumulated := 0 - db := u.Blockchain.db + db := u.Blockchain.DB err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(utxoBucket)) @@ -48,7 +48,7 @@ func (u UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[s // FindUTXO finds UTXO for a public key hash func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TXOutput { var UTXOs []TXOutput - db := u.Blockchain.db + db := u.Blockchain.DB err := db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(utxoBucket)) @@ -75,7 +75,7 @@ func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TXOutput { // CountTransactions returns the number of transactions in the UTXO set func (u UTXOSet) CountTransactions() int { - db := u.Blockchain.db + db := u.Blockchain.DB counter := 0 err := db.View(func(tx *bolt.Tx) error { @@ -97,7 +97,7 @@ func (u UTXOSet) CountTransactions() int { // Reindex rebuilds the UTXO set func (u UTXOSet) Reindex() { - db := u.Blockchain.db + db := u.Blockchain.DB bucketName := []byte(utxoBucket) err := db.Update(func(tx *bolt.Tx) error { @@ -141,7 +141,7 @@ func (u UTXOSet) Reindex() { // Update updates the UTXO set with transactions from the Block // The Block is considered to be the tip of a blockchain func (u UTXOSet) Update(block *Block) { - db := u.Blockchain.db + db := u.Blockchain.DB err := db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(utxoBucket))