From 3a49816e6ad3f53441aaefb9a6e0505c4e35f52a Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Sun, 10 Jun 2018 11:33:04 -0700 Subject: [PATCH] modify Transaction data structure --- benchmark_node.go | 5 +++-- block.go | 24 ++++++++++-------------- blockchain.go | 2 +- transaction.go | 32 ++++++++++++-------------------- 4 files changed, 26 insertions(+), 37 deletions(-) diff --git a/benchmark_node.go b/benchmark_node.go index 0db0d28f8..612312866 100644 --- a/benchmark_node.go +++ b/benchmark_node.go @@ -1,8 +1,6 @@ package main import ( - "./consensus" - "./p2p" "bufio" "flag" "fmt" @@ -11,6 +9,9 @@ import ( "os" "strconv" "strings" + + "./consensus" + "./p2p" ) // Consts diff --git a/block.go b/block.go index d1b2a8035..ba33faa9a 100644 --- a/block.go +++ b/block.go @@ -3,31 +3,27 @@ package main import ( "bytes" "crypto/sha256" - "strconv" "time" ) -// Constants -const ( - TOTAL_COINS = 21000000 -) - // Block keeps block headers. type Block struct { Timestamp int64 - utxoPool []UTXOPool + Transactions []*Transaction PrevBlockHash []byte Hash []byte } -//SetHash calculates and sets block hash. -func (b *Block) SetHash() { - timestamp := []byte(strconv.FormatInt(b.Timestamp, 10)) - // headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{}) - headers := bytes.Join([][]byte{b.PrevBlockHash, timestamp}, []byte{}) - hash := sha256.Sum256(headers) +// HashTransactions returns a hash of the transactions in the block +func (b *Block) HashTransactions() []byte { + var txHashes [][]byte + var txHash [32]byte - b.Hash = hash[:] + for _, tx := range b.Transactions { + txHashes = append(txHashes, tx.ID) + } + txHash = sha256.Sum256(bytes.Join(txHashes, []byte{})) + return txHash[:] } // NewBlock creates and returns Block. diff --git a/blockchain.go b/blockchain.go index 3662f6868..ca77f50c6 100644 --- a/blockchain.go +++ b/blockchain.go @@ -17,4 +17,4 @@ func (bc *Blockchain) AddBlock(data string) { // NewBlockchain creates a new Blockchain with genesis Block func NewBlockchain() *Blockchain { return &Blockchain{[]*Block{NewGenesisBlock()}} -} +} \ No newline at end of file diff --git a/transaction.go b/transaction.go index fe04ff6e8..0c44dde1f 100644 --- a/transaction.go +++ b/transaction.go @@ -5,32 +5,24 @@ import ( "crypto/sha256" "encoding/gob" "log" - "strconv" - "strings" ) // Transaction represents a Bitcoin transaction type Transaction struct { - ID []byte - inputAddresss []string - outputAddress []string - value []int + ID []byte + Vin []TXInput + Vout []TXOutput } -func (tx *Transaction) Parse(data string) { - items := strings.Split(data, ",") - for _, value := range items { - pair := strings.Split(value, " ") - if len(pair) == 3 { - intValue, err := strconv.Atoi(pair[2]) - if err != nil { - tx.inputAddress = append(tx.inputAddresss, strings.Trim(pair[0])) - tx.outputAddress = append(tx.outputAddress, strings.Trim(pair[1])) - tx.value = append(tx.value, intValue) - } - } - } - return res +type TXOutput struct { + Addresss string + Value int +} + +type TXInput struct { + Txid []byte + outIndex int + Address string } // SetID sets ID of a transaction