export DB in blockchain
This commit is contained in:
parent
c72ff7fcee
commit
cb4ae18f43
|
@ -19,7 +19,7 @@ const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second
|
||||||
// Blockchain implements interactions with a DB
|
// Blockchain implements interactions with a DB
|
||||||
type Blockchain struct {
|
type Blockchain struct {
|
||||||
tip []byte
|
tip []byte
|
||||||
db *bolt.DB
|
DB *bolt.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateBlockchain creates a new blockchain DB
|
// CreateBlockchain creates a new blockchain DB
|
||||||
|
@ -99,7 +99,7 @@ func NewBlockchain(nodeID string) *Blockchain {
|
||||||
|
|
||||||
// AddBlock saves the block into the blockchain
|
// AddBlock saves the block into the blockchain
|
||||||
func (bc *Blockchain) AddBlock(block *Block) {
|
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))
|
b := tx.Bucket([]byte(blocksBucket))
|
||||||
blockInDb := b.Get(block.Hash)
|
blockInDb := b.Get(block.Hash)
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ func (bc *Blockchain) FindUTXO() map[string]TXOutputs {
|
||||||
|
|
||||||
// Iterator returns a BlockchainIterat
|
// Iterator returns a BlockchainIterat
|
||||||
func (bc *Blockchain) Iterator() *BlockchainIterator {
|
func (bc *Blockchain) Iterator() *BlockchainIterator {
|
||||||
bci := &BlockchainIterator{bc.tip, bc.db}
|
bci := &BlockchainIterator{bc.tip, bc.DB}
|
||||||
|
|
||||||
return bci
|
return bci
|
||||||
}
|
}
|
||||||
|
@ -208,7 +208,7 @@ func (bc *Blockchain) Iterator() *BlockchainIterator {
|
||||||
func (bc *Blockchain) GetBestHeight() int {
|
func (bc *Blockchain) GetBestHeight() int {
|
||||||
var lastBlock Block
|
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))
|
b := tx.Bucket([]byte(blocksBucket))
|
||||||
lastHash := b.Get([]byte("l"))
|
lastHash := b.Get([]byte("l"))
|
||||||
blockData := b.Get(lastHash)
|
blockData := b.Get(lastHash)
|
||||||
|
@ -227,7 +227,7 @@ func (bc *Blockchain) GetBestHeight() int {
|
||||||
func (bc *Blockchain) GetBlock(blockHash []byte) (Block, error) {
|
func (bc *Blockchain) GetBlock(blockHash []byte) (Block, error) {
|
||||||
var block Block
|
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))
|
b := tx.Bucket([]byte(blocksBucket))
|
||||||
|
|
||||||
blockData := b.Get(blockHash)
|
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))
|
b := tx.Bucket([]byte(blocksBucket))
|
||||||
lastHash = b.Get([]byte("l"))
|
lastHash = b.Get([]byte("l"))
|
||||||
|
|
||||||
|
@ -294,7 +294,7 @@ func (bc *Blockchain) MineBlock(transactions []*Transaction) *Block {
|
||||||
|
|
||||||
newBlock := NewBlock(transactions, lastHash, lastHeight+1)
|
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))
|
b := tx.Bucket([]byte(blocksBucket))
|
||||||
err := b.Put(newBlock.Hash, newBlock.Serialize())
|
err := b.Put(newBlock.Hash, newBlock.Serialize())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,7 +10,7 @@ func (cli *CLI) createBlockchain(address, nodeID string) {
|
||||||
log.Panic("ERROR: Address is not valid")
|
log.Panic("ERROR: Address is not valid")
|
||||||
}
|
}
|
||||||
bc := CreateBlockchain(address, nodeID)
|
bc := CreateBlockchain(address, nodeID)
|
||||||
defer bc.db.Close()
|
defer bc.DB.Close()
|
||||||
|
|
||||||
UTXOSet := UTXOSet{bc}
|
UTXOSet := UTXOSet{bc}
|
||||||
UTXOSet.Reindex()
|
UTXOSet.Reindex()
|
||||||
|
|
|
@ -11,7 +11,7 @@ func (cli *CLI) getBalance(address, nodeID string) {
|
||||||
}
|
}
|
||||||
bc := NewBlockchain(nodeID)
|
bc := NewBlockchain(nodeID)
|
||||||
UTXOSet := UTXOSet{bc}
|
UTXOSet := UTXOSet{bc}
|
||||||
defer bc.db.Close()
|
defer bc.DB.Close()
|
||||||
|
|
||||||
balance := 0
|
balance := 0
|
||||||
pubKeyHash := Base58Decode([]byte(address))
|
pubKeyHash := Base58Decode([]byte(address))
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
func (cli *CLI) printChain(nodeID string) {
|
func (cli *CLI) printChain(nodeID string) {
|
||||||
bc := NewBlockchain(nodeID)
|
bc := NewBlockchain(nodeID)
|
||||||
defer bc.db.Close()
|
defer bc.DB.Close()
|
||||||
|
|
||||||
bci := bc.Iterator()
|
bci := bc.Iterator()
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ func (cli *CLI) printChain(nodeID string) {
|
||||||
|
|
||||||
func (cli *CLI) printBlock(blockHash, nodeID string) {
|
func (cli *CLI) printBlock(blockHash, nodeID string) {
|
||||||
bc := NewBlockchain(nodeID)
|
bc := NewBlockchain(nodeID)
|
||||||
defer bc.db.Close()
|
defer bc.DB.Close()
|
||||||
|
|
||||||
bci := bc.Iterator()
|
bci := bc.Iterator()
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ func (cli *CLI) send(from, to string, amount int, nodeID string, mineNow bool) {
|
||||||
|
|
||||||
bc := NewBlockchain(nodeID)
|
bc := NewBlockchain(nodeID)
|
||||||
UTXOSet := UTXOSet{bc}
|
UTXOSet := UTXOSet{bc}
|
||||||
defer bc.db.Close()
|
defer bc.DB.Close()
|
||||||
|
|
||||||
wallets, err := NewWallets(nodeID)
|
wallets, err := NewWallets(nodeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
10
utxo_set.go
10
utxo_set.go
|
@ -18,7 +18,7 @@ type UTXOSet struct {
|
||||||
func (u UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[string][]int) {
|
func (u UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[string][]int) {
|
||||||
unspentOutputs := make(map[string][]int)
|
unspentOutputs := make(map[string][]int)
|
||||||
accumulated := 0
|
accumulated := 0
|
||||||
db := u.Blockchain.db
|
db := u.Blockchain.DB
|
||||||
|
|
||||||
err := db.View(func(tx *bolt.Tx) error {
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
b := tx.Bucket([]byte(utxoBucket))
|
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
|
// FindUTXO finds UTXO for a public key hash
|
||||||
func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TXOutput {
|
func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TXOutput {
|
||||||
var UTXOs []TXOutput
|
var UTXOs []TXOutput
|
||||||
db := u.Blockchain.db
|
db := u.Blockchain.DB
|
||||||
|
|
||||||
err := db.View(func(tx *bolt.Tx) error {
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
b := tx.Bucket([]byte(utxoBucket))
|
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
|
// CountTransactions returns the number of transactions in the UTXO set
|
||||||
func (u UTXOSet) CountTransactions() int {
|
func (u UTXOSet) CountTransactions() int {
|
||||||
db := u.Blockchain.db
|
db := u.Blockchain.DB
|
||||||
counter := 0
|
counter := 0
|
||||||
|
|
||||||
err := db.View(func(tx *bolt.Tx) error {
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
|
@ -97,7 +97,7 @@ func (u UTXOSet) CountTransactions() int {
|
||||||
|
|
||||||
// Reindex rebuilds the UTXO set
|
// Reindex rebuilds the UTXO set
|
||||||
func (u UTXOSet) Reindex() {
|
func (u UTXOSet) Reindex() {
|
||||||
db := u.Blockchain.db
|
db := u.Blockchain.DB
|
||||||
bucketName := []byte(utxoBucket)
|
bucketName := []byte(utxoBucket)
|
||||||
|
|
||||||
err := db.Update(func(tx *bolt.Tx) error {
|
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
|
// Update updates the UTXO set with transactions from the Block
|
||||||
// The Block is considered to be the tip of a blockchain
|
// The Block is considered to be the tip of a blockchain
|
||||||
func (u UTXOSet) Update(block *Block) {
|
func (u UTXOSet) Update(block *Block) {
|
||||||
db := u.Blockchain.db
|
db := u.Blockchain.DB
|
||||||
|
|
||||||
err := db.Update(func(tx *bolt.Tx) error {
|
err := db.Update(func(tx *bolt.Tx) error {
|
||||||
b := tx.Bucket([]byte(utxoBucket))
|
b := tx.Bucket([]byte(utxoBucket))
|
||||||
|
|
Loading…
Reference in New Issue