- Transactions
This commit is contained in:
Brian Buller 2017-09-22 14:22:11 -05:00
parent a198d9da89
commit 3d8e28a725
7 changed files with 450 additions and 91 deletions

View File

@ -2,8 +2,8 @@ package main
import ( import (
"bytes" "bytes"
"crypto/sha256"
"encoding/gob" "encoding/gob"
"log"
"math" "math"
"time" "time"
) )
@ -14,17 +14,17 @@ var (
type Block struct { type Block struct {
Timestamp int64 Timestamp int64
Data []byte Transactions []*Transaction
PrevBlockHash []byte PrevBlockHash []byte
Hash []byte Hash []byte
Nonce int Nonce int
} }
// NewBlock returns a new block, ready to be added to the chain // NewBlock returns a new block, ready to be added to the chain
func NewBlock(data string, prevBlockHash []byte) *Block { func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block {
block := &Block{ block := &Block{
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
Data: []byte(data), Transactions: transactions,
PrevBlockHash: prevBlockHash, PrevBlockHash: prevBlockHash,
Hash: []byte{}, Hash: []byte{},
} }
@ -41,7 +41,7 @@ func (b *Block) Serialize() []byte {
var result bytes.Buffer var result bytes.Buffer
encoder := gob.NewEncoder(&result) encoder := gob.NewEncoder(&result)
if err := encoder.Encode(b); err != nil { if err := encoder.Encode(b); err != nil {
log.Panic(err) exitWithError(err)
} }
return result.Bytes() return result.Bytes()
} }
@ -50,7 +50,19 @@ func DeserializeBlock(d []byte) *Block {
var block Block var block Block
decoder := gob.NewDecoder(bytes.NewReader(d)) decoder := gob.NewDecoder(bytes.NewReader(d))
if err := decoder.Decode(&block); err != nil { if err := decoder.Decode(&block); err != nil {
log.Panic(err) exitWithError(err)
} }
return &block return &block
} }
func (b *Block) HashTransactions() []byte {
var txHashes [][]byte
var txHash [32]byte
for _, tx := range b.Transactions {
txHashes = append(txHashes, tx.ID)
}
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
return txHash[:]
}

View File

@ -1,108 +1,241 @@
package main package main
import ( import (
"encoding/hex"
"fmt" "fmt"
"log" "os"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
) )
var dbFile = "blockchain.db" const (
dbFile = "blockchain.db"
const blocksBucket = "blocks" blocksBucket = "blocks"
genesisCoinbaseData = "Arbitrary Genesis Data"
)
type Blockchain struct { type Blockchain struct {
tip []byte tip []byte
db *bolt.DB db *bolt.DB
} }
type BlockchainIterator struct { // Return whether the database file already exists
currentHash []byte func dbExists() bool {
db *bolt.DB if _, err := os.Stat(dbFile); os.IsNotExist(err) {
return false
}
return true
} }
// NewBlockchain creates a new Blockchain object from the DB
func NewBlockchain() *Blockchain { func NewBlockchain() *Blockchain {
if dbExists() == false {
fmt.Println("No existing blockchain found. Create one first.")
os.Exit(1)
}
var tip []byte var tip []byte
db, err := bolt.Open(dbFile, 0600, nil) db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
exitWithError(err)
}
err = db.Update(func(tx *bolt.Tx) error { err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket)) b := tx.Bucket([]byte(blocksBucket))
tip = b.Get([]byte("1"))
if b == nil { return nil
fmt.Println("No existing blockchain found. Creating a new one...") })
genesis := NewGenesisBlock() if err != nil {
exitWithError(err)
}
bc := Blockchain{
tip: tip,
db: db,
}
return &bc
}
// CreateBlockchain creates the blockchain DB and generate a new genesis block
func CreateBlockchain(address string) *Blockchain {
if dbExists() {
fmt.Println("Blockchain already exists.")
os.Exit(1)
}
var tip []byte
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
exitWithError(err)
}
err = db.Update(func(tx *bolt.Tx) error {
cbtx := NewCoinbaseTX(address, genesisCoinbaseData)
genesis := NewGenesisBlock(cbtx)
b, err := tx.CreateBucket([]byte(blocksBucket)) b, err := tx.CreateBucket([]byte(blocksBucket))
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
err = b.Put(genesis.Hash, genesis.Serialize()) err = b.Put(genesis.Hash, genesis.Serialize())
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
err = b.Put([]byte("1"), genesis.Hash) err = b.Put([]byte("1"), genesis.Hash)
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
tip = genesis.Hash tip = genesis.Hash
} else {
tip = b.Get([]byte("1"))
}
return nil return nil
}) })
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
bc := Blockchain{tip, db} bc := Blockchain{
tip: tip,
db: db,
}
return &bc return &bc
} }
// NewGenesisBlock creates a new Genesis Block to start the blockchain // NewGenesisBlock creates a new Genesis Block to start the blockchain
func NewGenesisBlock() *Block { func NewGenesisBlock(coinbase *Transaction) *Block {
return NewBlock("Genesis Block", []byte{}) return NewBlock([]*Transaction{coinbase}, []byte{})
} }
// AddBlock adds a block to the blockchain // FindUnspentTransactions returns a list of transactions containing unspent outputs
func (bc *Blockchain) AddBlock(data string) { func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
var unspentTXs []Transaction
spentTXOs := make(map[string][]int)
bci := bc.Iterator()
for {
block := bci.Next()
for _, tx := range block.Transactions {
txID := hex.EncodeToString(tx.ID)
Outputs:
for outIdx, out := range tx.Vout {
// Was the output spent?
if spentTXOs[txID] != nil {
for _, spentOut := range spentTXOs[txID] {
if spentOut == outIdx {
continue Outputs
}
}
}
if out.CanBeUnlockedWith(address) {
unspentTXs = append(unspentTXs, *tx)
}
}
if tx.IsCoinbase() == false {
for _, in := range tx.Vin {
if in.CanUnlockOutputWith(address) {
inTxID := hex.EncodeToString(in.Txid)
spentTXOs[inTxID] = append(spentTXOs[inTxID], in.Vout)
}
}
}
}
if len(block.PrevBlockHash) == 0 {
break
}
}
return unspentTXs
}
// FindUTXO finds and returns all unspent transaction outputs
func (bc *Blockchain) FindUTXO(address string) []TXOutput {
var UTXOs []TXOutput
unspentTransactions := bc.FindUnspentTransactions(address)
for _, tx := range unspentTransactions {
for _, out := range tx.Vout {
if out.CanBeUnlockedWith(address) {
UTXOs = append(UTXOs, out)
}
}
}
return UTXOs
}
// FindSpendableOutputs finds and returns unspent outputs to reference in inputs
func (bc *Blockchain) FindSpendableOutputs(address string, amount int) (int, map[string][]int) {
unspentOutputs := make(map[string][]int)
unspentTXs := bc.FindUnspentTransactions(address)
accumulated := 0
Work:
for _, tx := range unspentTXs {
txID := hex.EncodeToString(tx.ID)
for outIdx, out := range tx.Vout {
if out.CanBeUnlockedWith(address) && accumulated < amount {
accumulated += out.Value
unspentOutputs[txID] = append(unspentOutputs[txID], outIdx)
if accumulated >= amount {
break Work
}
}
}
}
return accumulated, unspentOutputs
}
// MineBlock mines a new block with the provided transactions
func (bc *Blockchain) MineBlock(transactions []*Transaction) {
var lastHash []byte var lastHash []byte
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("1")) lastHash = b.Get([]byte("1"))
return nil return nil
}) })
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
newBlock := NewBlock(data, lastHash) newBlock := NewBlock(transactions, lastHash)
err = bc.db.Update(func(tx *bolt.Tx) error { err = bc.db.Update(func(tx *bolt.Tx) error {
var err error
b := tx.Bucket([]byte(blocksBucket)) b := tx.Bucket([]byte(blocksBucket))
if err = b.Put(newBlock.Hash, newBlock.Serialize()); err != nil { err := b.Put(newBlock.Hash, newBlock.Serialize())
log.Panic(err) if err != nil {
exitWithError(err)
} }
if err = b.Put([]byte("1"), newBlock.Hash); err != nil {
log.Panic(err) err = b.Put([]byte("1"), newBlock.Hash)
if err != nil {
exitWithError(err)
} }
bc.tip = newBlock.Hash bc.tip = newBlock.Hash
return nil return nil
}) })
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
} }
// Iterating the Blockchain
type BlockchainIterator struct {
currentHash []byte
db *bolt.DB
}
// Iterator returns a blockchain iterator
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
} }
// next returns the next block (starting from the tip)
func (bci *BlockchainIterator) Next() *Block { func (bci *BlockchainIterator) Next() *Block {
var block *Block var block *Block
@ -113,7 +246,7 @@ func (bci *BlockchainIterator) Next() *Block {
return nil return nil
}) })
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
bci.currentHash = block.PrevBlockHash bci.currentHash = block.PrevBlockHash

144
cli.go
View File

@ -7,66 +7,99 @@ import (
"strconv" "strconv"
) )
type CLI struct { const (
bc *Blockchain flagBalance = "balance"
} flagCreate = "create"
flagSend = "send"
flagLedger = "ledger"
)
func (cli *CLI) printUsage() { type CLI struct{}
fmt.Println("Usage:")
fmt.Println("\taddblock -data BLOCK_DATA\tadd a block to the blockchain")
fmt.Println("\tprintchain\t\t\tprint all the blocks of the blockchain")
}
func (cli *CLI) validateArgs() {
if len(os.Args) < 2 {
cli.printUsage()
os.Exit(1)
}
}
// Run parses command line arguments and processes commands
func (cli *CLI) Run() { func (cli *CLI) Run() {
cli.validateArgs() cli.validateArgs()
// Valid CLI flags // Valid CLI flags
addBlockCmd := flag.NewFlagSet("addblock", flag.ExitOnError) getBalanceCmd := flag.NewFlagSet(flagBalance, flag.ExitOnError)
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError) createBlockchainCmd := flag.NewFlagSet(flagCreate, flag.ExitOnError)
// CLI Flag Data sendCmd := flag.NewFlagSet(flagSend, flag.ExitOnError)
addBlockData := addBlockCmd.String("data", "", "Block data") printChainCmd := flag.NewFlagSet(flagLedger, flag.ExitOnError)
getBalanceAddress := getBalanceCmd.String("address", "", "The address to get the balance of")
createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send the genesis block reward to")
sendFrom := sendCmd.String("from", "", "Source wallet address")
sendTo := sendCmd.String("to", "", "Destination wallet address")
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
switch os.Args[1] { switch os.Args[1] {
case "addblock": case flagBalance:
addBlockCmd.Parse(os.Args[2:]) if err := getBalanceCmd.Parse(os.Args[2:]); err != nil {
case "printchain": exitWithError(err)
printChainCmd.Parse(os.Args[2:]) }
case flagCreate:
if err := createBlockchainCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
case flagLedger:
if err := printChainCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
case flagSend:
if err := sendCmd.Parse(os.Args[2:]); err != nil {
exitWithError(err)
}
default: default:
cli.printUsage() cli.printUsage()
os.Exit(1) os.Exit(1)
} }
if addBlockCmd.Parsed() { if getBalanceCmd.Parsed() {
if *addBlockData == "" { if *getBalanceAddress == "" {
addBlockCmd.Usage() getBalanceCmd.Usage()
os.Exit(1) os.Exit(1)
} }
cli.addBlock(*addBlockData) cli.getBalance(*getBalanceAddress)
}
if createBlockchainCmd.Parsed() {
if *createBlockchainAddress == "" {
createBlockchainCmd.Usage()
os.Exit(1)
}
cli.createBlockchain(*createBlockchainAddress)
} }
if printChainCmd.Parsed() { if printChainCmd.Parsed() {
cli.printChain() cli.printChain()
} }
if sendCmd.Parsed() {
if *sendFrom == "" || *sendTo == "" || *sendAmount <= 0 {
sendCmd.Usage()
os.Exit(1)
}
cli.send(*sendFrom, *sendTo, *sendAmount)
}
} }
func (cli *CLI) addBlock(data string) { // createBlockchain creates a new blockchain with a genesis block
cli.bc.AddBlock(data) func (cli *CLI) createBlockchain(address string) {
fmt.Println("Success!") bc := CreateBlockchain(address)
bc.db.Close()
fmt.Println("Done!")
} }
// printChain outputs the entire ledger
func (cli *CLI) printChain() { func (cli *CLI) printChain() {
bci := cli.bc.Iterator() bc := NewBlockchain()
defer bc.db.Close()
bci := bc.Iterator()
for { for {
block := bci.Next() block := bci.Next()
fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash) fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash)
fmt.Printf("Data: %s\n", block.Data)
fmt.Printf("Hash: %x\n", block.Hash) fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block) pow := NewProofOfWork(block)
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate())) fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
@ -77,3 +110,50 @@ func (cli *CLI) printChain() {
} }
} }
} }
// getBalance retrieves the transaction balance for an address
func (cli *CLI) getBalance(address string) {
bc := NewBlockchain()
defer bc.db.Close()
balance := 0
UTXOs := bc.FindUTXO(address)
for _, out := range UTXOs {
balance += out.Value
}
fmt.Printf("Balance of '%s': %d\n", address, balance)
}
// send Triggers a transaction exchange
func (cli *CLI) send(from, to string, amount int) {
bc := NewBlockchain()
defer bc.db.Close()
tx := NewUTXOTransaction(from, to, amount, bc)
bc.MineBlock([]*Transaction{tx})
fmt.Println("Success!")
}
// printUsage prints the usage of the cli app
func (cli *CLI) printUsage() {
fmt.Println("Usage:")
fmt.Println("\t" + flagBalance + " -address ADDRESS\tGet balance of ADDRESS")
fmt.Println("\t" + flagCreate + " -address ADDRESS\tCreate a blockchain and send genesis block reward to ADDRESS")
fmt.Println("\t" + flagLedger + "\t\t\tprint all the blocks of the blockchain")
fmt.Println("\t" + flagSend + " -from FROM -to TO -amount AMOUNT\tSend AMOUNT of coins from FROM address to TO address")
}
// validateArgs just makes sure that we've got a couple arguments
func (cli *CLI) validateArgs() {
if len(os.Args) < 2 {
cli.printUsage()
os.Exit(1)
}
}
func exitWithError(err error) {
fmt.Println(err)
os.Exit(1)
}

View File

@ -1,9 +1,6 @@
package main package main
func main() { func main() {
bc := NewBlockchain() cli := CLI{}
defer bc.db.Close()
cli := CLI{bc}
cli.Run() cli.Run()
} }

View File

@ -27,7 +27,7 @@ func (pow *ProofOfWork) prepareData(nonce int) []byte {
data := bytes.Join( data := bytes.Join(
[][]byte{ [][]byte{
pow.block.PrevBlockHash, pow.block.PrevBlockHash,
pow.block.Data, pow.block.HashTransactions(),
IntToHex(pow.block.Timestamp), IntToHex(pow.block.Timestamp),
IntToHex(int64(proofTargetBits)), IntToHex(int64(proofTargetBits)),
IntToHex(int64(nonce)), IntToHex(int64(nonce)),
@ -42,7 +42,7 @@ func (pow *ProofOfWork) Run() (int, []byte) {
var hash [32]byte var hash [32]byte
nonce := 0 nonce := 0
fmt.Printf("Mining the block containing \"%s\"\n", pow.block.Data) fmt.Printf("Mining a new block")
for nonce < maxNonce { for nonce < maxNonce {
data := pow.prepareData(nonce) data := pow.prepareData(nonce)
hash = sha256.Sum256(data) hash = sha256.Sum256(data)

138
transaction.go Normal file
View File

@ -0,0 +1,138 @@
package main
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"fmt"
"os"
)
const subsidy = 10
// Transaction represents a blockchain transaction
type Transaction struct {
ID []byte
Vin []TXInput
Vout []TXOutput
}
// IsCoinbase checks whether the transaction is a coinbase
func (tx Transaction) IsCoinbase() bool {
return len(tx.Vin) == 1 && len(tx.Vin[0].Txid) == 0 && tx.Vin[0].Vout == -1
}
// SetID sets the ID of a transaction
func (tx Transaction) SetID() {
var encoded bytes.Buffer
var hash [32]byte
enc := gob.NewEncoder(&encoded)
err := enc.Encode(tx)
if err != nil {
exitWithError(err)
}
hash = sha256.Sum256(encoded.Bytes())
tx.ID = hash[:]
}
// NewCoinbaseTX creates a new coinbase transaction
func NewCoinbaseTX(to, data string) *Transaction {
if data == "" {
data = fmt.Sprintf("Reward to '%s'", to)
}
txin := TXInput{
Txid: []byte{},
Vout: -1,
ScriptSig: data,
}
txout := TXOutput{
Value: subsidy,
ScriptPubKey: to,
}
tx := Transaction{nil, []TXInput{txin}, []TXOutput{txout}}
tx.SetID()
return &tx
}
// NewUTXOTransaction creates a new transaction
func NewUTXOTransaction(from, to string, amount int, bc *Blockchain) *Transaction {
var inputs []TXInput
var outputs []TXOutput
acc, validOutputs := bc.FindSpendableOutputs(from, amount)
if acc < amount {
fmt.Println("ERROR: Not enough funds")
os.Exit(1)
}
// Build a list of inputs
for txid, outs := range validOutputs {
txID, err := hex.DecodeString(txid)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, out := range outs {
input := TXInput{
Txid: txID,
Vout: out,
ScriptSig: from,
}
inputs = append(inputs, input)
}
}
// Build a list of outputs
output := TXOutput{
Value: amount,
ScriptPubKey: to,
}
outputs = append(outputs, output)
if acc > amount {
// There is some change
chgOutput := TXOutput{
Value: acc - amount,
ScriptPubKey: from,
}
outputs = append(outputs, chgOutput)
}
tx := Transaction{
ID: nil,
Vin: inputs,
Vout: outputs,
}
tx.SetID()
return &tx
}
// TXInput represents a transaction input
type TXInput struct {
Txid []byte
Vout int
ScriptSig string
}
// CanUnlockOutputWith checks whether the unlockingData (address) initiated the transaction
func (in *TXInput) CanUnlockOutputWith(unlockingData string) bool {
return in.ScriptSig == unlockingData
}
// TXOutput represents a transaction output
type TXOutput struct {
Value int
ScriptPubKey string
}
// CanBeUnlockedWith checks if the output can be unlocked with the provided data
func (out *TXOutput) CanBeUnlockedWith(unlockingData string) bool {
return out.ScriptPubKey == unlockingData
}

View File

@ -3,7 +3,6 @@ package main
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"log"
) )
// IntToHex converts an int64 to a byte array // IntToHex converts an int64 to a byte array
@ -11,7 +10,7 @@ func IntToHex(num int64) []byte {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, num) err := binary.Write(buf, binary.BigEndian, num)
if err != nil { if err != nil {
log.Panic(err) exitWithError(err)
} }
return buf.Bytes() return buf.Bytes()
} }