From cb983ce48460f832faf13928aa61b4dca5adc412 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Wed, 20 Jun 2018 11:25:43 -0700 Subject: [PATCH] Add transactions num and Ids list to block --- aws-code/transaction_generator.go | 2 +- blockchain/block.go | 25 +++++++++++++++++++------ blockchain/blockchain.go | 6 +++--- blockchain/transaction.go | 8 ++++---- blockchain/utxopool.go | 8 ++++---- node/node_handler.go | 18 ++++++------------ 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/aws-code/transaction_generator.go b/aws-code/transaction_generator.go index 1724acb6d..f6b297940 100644 --- a/aws-code/transaction_generator.go +++ b/aws-code/transaction_generator.go @@ -53,7 +53,7 @@ func getNewFakeTransactions(dataNode *node.Node, numTxs int) []*blockchain.Trans // Spend the money of current UTXO to a random address in [1 - 1000] txin := blockchain.TXInput{txId, index, address} txout := blockchain.TXOutput{value, strconv.Itoa(rand.Intn(1000))} - tx := blockchain.Transaction{nil, []blockchain.TXInput{txin}, []blockchain.TXOutput{txout}} + tx := blockchain.Transaction{[32]byte{}, []blockchain.TXInput{txin}, []blockchain.TXOutput{txout}} tx.SetID() if count >= numTxs { diff --git a/blockchain/block.go b/blockchain/block.go index a51059402..794b92ab7 100644 --- a/blockchain/block.go +++ b/blockchain/block.go @@ -9,12 +9,19 @@ import ( "time" ) -// Block keeps block headers. +// Block keeps block headers, transactions and signature. type Block struct { - Timestamp int64 + // Header + Timestamp int64 + PrevBlockHash [32]byte + Hash [32]byte + NumTransactions int32 + TransactionIds [][32]byte + + // Transactions Transactions []*Transaction - PrevBlockHash [32]byte - Hash [32]byte + + // Signature... } // Serialize serializes the block @@ -56,7 +63,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[:] @@ -64,7 +71,13 @@ func (b *Block) HashTransactions() []byte { // NewBlock creates and returns a neew block. func NewBlock(transactions []*Transaction, prevBlockHash [32]byte) *Block { - block := &Block{time.Now().Unix(), transactions, prevBlockHash, [32]byte{}} + numTxs := int32(len(transactions)) + var txIds [][32]byte + + for _, tx := range transactions { + txIds = append(txIds, tx.ID) + } + block := &Block{time.Now().Unix(), prevBlockHash, [32]byte{},numTxs, txIds,transactions} copy(block.Hash[:], block.HashTransactions()[:]) // TODO(Minh): the blockhash should be a hash of everything in the block return block diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index d3fb703cd..3a4a210cb 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -29,7 +29,7 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction { block := bc.Blocks[index] for _, tx := range block.Transactions { - txID := hex.EncodeToString(tx.ID) + txID := hex.EncodeToString(tx.ID[:]) idx := -1 // TODO(minhdoan): Optimize this. @@ -85,7 +85,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 { @@ -132,7 +132,7 @@ func (bc *Blockchain) NewUTXOTransaction(from, to string, amount int) *Transacti outputs = append(outputs, TXOutput{acc - amount, from}) // a change } - tx := Transaction{nil, inputs, outputs} + tx := Transaction{[32]byte{}, inputs, outputs} tx.SetID() return &tx diff --git a/blockchain/transaction.go b/blockchain/transaction.go index dab10cb2a..ee38c78fb 100644 --- a/blockchain/transaction.go +++ b/blockchain/transaction.go @@ -11,7 +11,7 @@ import ( // Transaction represents a Bitcoin transaction type Transaction struct { - ID []byte // 32 byte hash + ID [32]byte // 32 byte hash TxInput []TXInput TxOutput []TXOutput } @@ -43,7 +43,7 @@ func (tx *Transaction) SetID() { log.Panic(err) } hash = sha256.Sum256(encoded.Bytes()) - tx.ID = hash[:] + tx.ID = hash } // NewCoinbaseTX creates a new coinbase transaction @@ -54,7 +54,7 @@ func NewCoinbaseTX(to, data string) *Transaction { txin := TXInput{[]byte{}, -1, data} txout := TXOutput{DefaultCoinbaseValue, to} - tx := Transaction{nil, []TXInput{txin}, []TXOutput{txout}} + tx := Transaction{[32]byte{}, []TXInput{txin}, []TXOutput{txout}} tx.SetID() return &tx } @@ -76,7 +76,7 @@ func (txOutput *TXOutput) String() string { // Used for debuging. func (tx *Transaction) String() string { - res := fmt.Sprintf("ID: %v\n", hex.EncodeToString(tx.ID)) + res := fmt.Sprintf("ID: %v\n", hex.EncodeToString(tx.ID[:])) res += fmt.Sprintf("TxInput:\n") for id, value := range tx.TxInput { res += fmt.Sprintf("%v: %v\n", id, value.String()) diff --git a/blockchain/utxopool.go b/blockchain/utxopool.go index 6ab830546..3a2ab1173 100644 --- a/blockchain/utxopool.go +++ b/blockchain/utxopool.go @@ -79,7 +79,7 @@ func (utxoPool *UTXOPool) VerifyTransactions(transactions []*Transaction) bool { // VerifyOneTransaction verifies if a list of transactions valid. func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { spentTXOs := make(map[string]map[string]map[int]bool) - txID := hex.EncodeToString(tx.ID) + txID := hex.EncodeToString(tx.ID[:]) inTotal := 0 // Calculate the sum of TxInput for _, in := range tx.TxInput { @@ -121,7 +121,7 @@ func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { // UpdateOneTransaction updates utxoPool in respect to the new Transaction. func (utxoPool *UTXOPool) UpdateOneTransaction(tx *Transaction) { if utxoPool != nil { - txID := hex.EncodeToString(tx.ID) + txID := hex.EncodeToString(tx.ID[:]) // Remove for _, in := range tx.TxInput { @@ -166,7 +166,7 @@ func (utxoPool *UTXOPool) VerifyAndUpdate(transactions []*Transaction) bool { func (utxoPool *UTXOPool) Update(transactions []*Transaction) { if utxoPool != nil { for _, tx := range transactions { - curTxID := hex.EncodeToString(tx.ID) + curTxID := hex.EncodeToString(tx.ID[:]) // Remove for _, in := range tx.TxInput { @@ -192,7 +192,7 @@ func (utxoPool *UTXOPool) Update(transactions []*Transaction) { // CreateUTXOPoolFromTransaction a Utxo pool from a genesis transaction. func CreateUTXOPoolFromTransaction(tx *Transaction) *UTXOPool { var utxoPool UTXOPool - txID := hex.EncodeToString(tx.ID) + txID := hex.EncodeToString(tx.ID[:]) utxoPool.UtxoMap = make(map[string]map[string]map[int]int) for index, out := range tx.TxOutput { utxoPool.UtxoMap[out.Address] = make(map[string]map[int]int) diff --git a/node/node_handler.go b/node/node_handler.go index fd50b34d3..c91eacc1c 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -85,19 +85,21 @@ func (node *Node) transactionMessageHandler(msgPayload []byte) { case REQUEST: reader := bytes.NewBuffer(msgPayload[1:]) var txIds map[[32]byte]bool - txId := make([]byte, 32) // 32 byte hash Id + buf := make([]byte, 32) // 32 byte hash Id for { - _, err := reader.Read(txId) + _, err := reader.Read(buf) if err != nil { break } - txIds[getFixedByteTxId(txId)] = true + var txId [32]byte + copy(txId[:], buf) + txIds[txId] = true } var txToReturn []*blockchain.Transaction for _, tx := range node.pendingTransactions { - if txIds[getFixedByteTxId(tx.ID)] { + if txIds[tx.ID] { txToReturn = append(txToReturn, tx) } } @@ -106,14 +108,6 @@ func (node *Node) transactionMessageHandler(msgPayload []byte) { } } -// Copy the txId byte slice over to 32 byte array so the map can key on it -func getFixedByteTxId(txId []byte) [32]byte { - var id [32]byte - for i := range id { - id[i] = txId[i] - } - return id -} func (node *Node) WaitForConsensusReady(readySignal chan int) { node.log.Debug("Waiting for consensus ready", "node", node)