export DB in blockchain

This commit is contained in:
Wei Yang 2017-10-22 10:39:31 +08:00
parent c72ff7fcee
commit cb4ae18f43
6 changed files with 17 additions and 17 deletions

View File

@ -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 {

View File

@ -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()

View File

@ -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))

View File

@ -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()

View File

@ -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 {

View File

@ -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))