diff --git a/block.go b/blockchain/block.go similarity index 95% rename from block.go rename to blockchain/block.go index bf99487b1..8727ce8db 100644 --- a/block.go +++ b/blockchain/block.go @@ -1,4 +1,4 @@ -package main +package blockchain import ( "bytes" @@ -44,7 +44,7 @@ func (b *Block) HashTransactions() []byte { var txHash [32]byte for _, tx := range b.Transactions { - txHashes = append(txHashes, tx.ID) + txHashes = append(txHashes, tx.id) } txHash = sha256.Sum256(bytes.Join(txHashes, []byte{})) return txHash[:] @@ -52,7 +52,7 @@ func (b *Block) HashTransactions() []byte { // NewBlock creates and returns Block. func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block { - block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}, 0} + block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}} block.Hash = block.HashTransactions() return block } diff --git a/blockchain/block_test.go b/blockchain/block_test.go new file mode 100644 index 000000000..194471681 --- /dev/null +++ b/blockchain/block_test.go @@ -0,0 +1,26 @@ +package blockchain + +import ( + "bytes" + "testing" +) + +func TestBlockSerialize(t *testing.T) { + cbtx := NewCoinbaseTX("minh", genesisCoinbaseData) + block := NewGenesisBlock(cbtx) + + serializedValue := block.Serialize() + deserializedBlock := DeserializeBlock(serializedValue) + + if block.Timestamp != deserializedBlock.Timestamp { + t.Errorf("Serialize or Deserialize incorrect at TimeStamp.") + } + + if bytes.Compare(block.PrevBlockHash, deserializedBlock.PrevBlockHash) != 0 { + t.Errorf("Serialize or Deserialize incorrect at PrevBlockHash.") + } + + if bytes.Compare(block.Hash, deserializedBlock.Hash) != 0 { + t.Errorf("Serialize or Deserialize incorrect at Hash.") + } +} diff --git a/blockchain.go b/blockchain/blockchain.go similarity index 87% rename from blockchain.go rename to blockchain/blockchain.go index 7365bc6d0..aee40b1f0 100644 --- a/blockchain.go +++ b/blockchain/blockchain.go @@ -1,4 +1,4 @@ -package main +package blockchain import ( "encoding/hex" @@ -11,11 +11,6 @@ type Blockchain struct { const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" -// NewBlockchain creates a new Blockchain with genesis Block -func NewBlockchain(address string) *Blockchain { - return &Blockchain{[]*Block{NewGenesisBlock()}} -} - // FindUnspentTransactions returns a list of transactions containing unspent outputs func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction { var unspentTXs []Transaction @@ -26,14 +21,14 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction { BreakTransaction: for _, tx := range block.Transactions { - txId := hex.EncodeToString(tx.Id) + txID := hex.EncodeToString(tx.id) idx := -1 - if spentTXOs[txId] != nil { + if spentTXOs[txID] != nil { idx = 0 } for outIdx, txOutput := range tx.txOutput { - if idx >= 0 && spentTXOs[txId][idx] == outIdx { + if idx >= 0 && spentTXOs[txID][idx] == outIdx { idx++ continue } @@ -73,7 +68,7 @@ func (bc *Blockchain) FindSpendableOutputs(address string, amount int) (int, map Work: for _, tx := range unspentTXs { - txID := hex.EncodeToString(tx.ID) + txID := hex.EncodeToString(tx.id) for outIdx, txOutput := range tx.txOutput { if txOutput.address == address && accumulated < amount { diff --git a/transaction.go b/blockchain/transaction.go similarity index 99% rename from transaction.go rename to blockchain/transaction.go index 51480aad3..5f2495efa 100644 --- a/transaction.go +++ b/blockchain/transaction.go @@ -1,4 +1,4 @@ -package main +package blockchain import ( "bytes" diff --git a/blockchain/transaction_test.go b/blockchain/transaction_test.go new file mode 100644 index 000000000..4f4054e5c --- /dev/null +++ b/blockchain/transaction_test.go @@ -0,0 +1,9 @@ +package blockchain + +import ( + "testing" +) + +func TestNewCoinbaseTX(t *testing.T) { + NewCoinbaseTX("minh", genesisCoinbaseData) +} diff --git a/utils.go b/utils/utils.go similarity index 72% rename from utils.go rename to utils/utils.go index d3cb517b0..4011e1a23 100644 --- a/utils.go +++ b/utils/utils.go @@ -1,4 +1,4 @@ -package main +package utils import ( "bytes" @@ -19,14 +19,15 @@ func IntToHex(num int64) []byte { return buff.Bytes() } -// Helper library to convert '1,2,3,4' into []int{1,2,3,4}. +// ConvertIntoInts is to convert '1,2,3,4' into []int{1,2,3,4}. func ConvertIntoInts(data string) []int { var res = []int{} - items := strings.Split(data, " ") + items := strings.Split(data, ",") for _, value := range items { intValue, err := strconv.Atoi(value) - checkError(err) - res = append(res, intValue) + if err == nil { + res = append(res, intValue) + } } return res } diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 000000000..366936d36 --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,17 @@ +package utils + +import "testing" + +func TestConvertIntoInts(t *testing.T) { + data := "1,2,3,4" + res := ConvertIntoInts(data) + if len(res) != 4 { + t.Errorf("Array length parsed incorrect.") + } + + for id, value := range res { + if value != id+1 { + t.Errorf("Parsing incorrect.") + } + } +} diff --git a/utils_test.go b/utils_test.go deleted file mode 100644 index bc7255297..000000000 --- a/utils_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import "testing" - -func TestConvertIntoMap(t *testing.T) { - data := "minh:3,mike:2" - res := ConvertIntoMap(data) - if len(res) != 2 { - t.Errorf("Result should have 2 pairs (key, value)") - } - if val, ok := res["minh"]; !ok { - t.Errorf("Result should contain key minh") - } else { - if res["minh"] != 3 { - t.Errorf("Value of minh should be 3") - } - } - if val, ok := res["mike"]; !ok { - t.Errorf("Result should contain key mike") - } else { - if res["minh"] != 3 { - t.Errorf("Value of minh should be 2") - } - } -}