Skip to content

Commit

Permalink
export DB in blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardWeiYang committed Oct 22, 2017
1 parent c72ff7f commit cb4ae18
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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"))

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli_createblockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion cli_getbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions cli_printchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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

Expand Down
2 changes: 1 addition & 1 deletion cli_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions utxo_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit cb4ae18

Please sign in to comment.