diff --git a/.vscode/settings.json b/.vscode/settings.json index e4062af53..8516d1f5c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,5 @@ { "workbench.colorTheme": "Solarized Light", "npm.enableScriptExplorer": true, - "window.zoomLevel": 3, - "editor.tabSize": 4, - "editor.insertSpaces": true, - "editor.detectIndentation": false, - "editor.tabCompletion": true, - } - \ No newline at end of file + "window.zoomLevel": 1, +} \ No newline at end of file diff --git a/block.go b/block.go index 552063422..1972dbd30 100644 --- a/block.go +++ b/block.go @@ -26,18 +26,37 @@ func (b *Block) HashTransactions() []byte { return txHash[:] } -// NewBlock creates and returns Block. -func NewBlock(utxoPool []UTXOPool, prevBlockHash []byte) *Block { +// Serialize serializes the block +func (b *Block) Serialize() []byte { + var result bytes.Buffer + encoder := gob.NewEncoder(&result) + err := encoder.Encode(b) + if err != nil { + log.Panic(err) + } + return result.Bytes() +} - block := &Block{time.Now().Unix(), utxoPool, prevBlockHash, []byte{}} - block.SetHash() +// NewBlock creates and returns Block. +func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block { + block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}, 0} + block.Hash = block.HashTransactions() return block } // NewGenesisBlock creates and returns genesis Block. -func NewGenesisBlock() *Block { - genesisUTXOPool := UTXOPool{} - genesisUTXOPool.utxos["genesis"] = TOTAL_COINS +func NewGenesisBlock(coinbase *Transaction) *Block { + return NewBlock([]*Transaction{coinbase}, []byte{}) +} - return NewBlock(genesisUTXOPool, []byte{}) +// DeserializeBlock deserializes a block +func DeserializeBlock(d []byte) *Block { + var block Block + decoder := gob.NewDecoder(bytes.NewReader(d)) + err := decoder.Decode(&block) + if err != nil { + log.Panic(err) + } + return &block } + diff --git a/transaction.go b/transaction.go index f37fdc7f4..45dfea76f 100644 --- a/transaction.go +++ b/transaction.go @@ -1,40 +1,41 @@ package main import ( - "bytes" - "crypto/sha256" - "encoding/gob" - "log" + "bytes" + "crypto/sha256" + "encoding/gob" + "log" ) // Transaction represents a Bitcoin transaction type Transaction struct { - id []byte - txInput []TXInput - txOutput []TXOutput + id []byte + txInput []TXInput + txOutput []TXOutput } type TXOutput struct { - address string - value int + address string + value int } type TXInput struct { - txId []byte - txOutputIndex int - address string + txId []byte + txOutputIndex int + address string } // SetID sets ID of a transaction func (tx *Transaction) SetId() { - var encoded bytes.Buffer - var hash [32]byte + var encoded bytes.Buffer + var hash [32]byte - enc := gob.NewEncoder(&encoded) - err := enc.Encode(tx) - if err != nil { - log.Panic(err) - } - hash = sha256.Sum256(encoded.Bytes()) - tx.ID = hash[:] + enc := gob.NewEncoder(&encoded) + err := enc.Encode(tx) + if err != nil { + log.Panic(err) + } + hash = sha256.Sum256(encoded.Bytes()) + tx.ID = hash[:] } +