From 061d8e2f7f6591f7805683c79bc6bfb8a462dab9 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Wed, 31 Oct 2018 17:25:58 -0700 Subject: [PATCH] fix comments on blockchain and transaction --- blockchain/blockchain.go | 19 +++++++++++-------- blockchain/transaction.go | 32 ++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 642899d13..630ed9f05 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -3,6 +3,7 @@ package blockchain import ( "bytes" "encoding/hex" + "github.com/dedis/kyber" "github.com/simple-rules/harmony-benchmark/crypto/pki" ) @@ -14,7 +15,7 @@ type Blockchain struct { const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" -// blockHash should have size of 32. +// FindBlock finds a block with given blockHash. func (bc *Blockchain) FindBlock(blockHash []byte) *Block { if len(blockHash) != 32 { return nil @@ -27,6 +28,7 @@ func (bc *Blockchain) FindBlock(blockHash []byte) *Block { return nil } +// FindBlockWithPrevHash fins a block with given prevHash. func (bc *Blockchain) FindBlockWithPrevHash(prevHash []byte) *Block { if len(prevHash) != 32 { return nil @@ -47,6 +49,7 @@ func (bc *Blockchain) GetLatestBlock() *Block { return bc.Blocks[len(bc.Blocks)-1] } +// GetBlockHashes returns array of hashes of the given blockchain. func (bc *Blockchain) GetBlockHashes() [][32]byte { res := [][32]byte{} for _, block := range bc.Blocks { @@ -55,7 +58,7 @@ func (bc *Blockchain) GetBlockHashes() [][32]byte { return res } -// FindUnspentUtxos returns a list of transactions containing unspent outputs +// FindUnspentUtxos returns a list of transactions containing unspent outputs. func (bc *Blockchain) FindUnspentUtxos(address [20]byte) map[TxID]map[uint32]TXOutput { spentTXOs := make(map[string][]uint32) result := make(map[TxID]map[uint32]TXOutput) @@ -97,7 +100,7 @@ func (bc *Blockchain) FindUnspentUtxos(address [20]byte) map[TxID]map[uint32]TXO return result } -// FindUTXO finds and returns all unspent transaction outputs +// FindUTXO finds and returns all unspent transaction outputs. func (bc *Blockchain) FindUTXO(address [20]byte) []TXOutput { var UTXOs []TXOutput unspentTXs := bc.FindUnspentUtxos(address) @@ -114,7 +117,7 @@ func (bc *Blockchain) FindUTXO(address [20]byte) []TXOutput { return UTXOs } -// FindSpendableOutputs finds and returns unspent outputs to reference in inputs +// FindSpendableOutputs finds and returns unspent outputs to reference in inputs. func (bc *Blockchain) FindSpendableOutputs(address [20]byte, amount int) (int, map[string][]uint32) { unspentOutputs := make(map[string][]uint32) unspentUtxos := bc.FindUnspentUtxos(address) @@ -230,16 +233,16 @@ func CreateBlockchain(address [20]byte, shardId uint32) *Blockchain { return &bc } -// Create state block based on the utxos. +// CreateStateBlock creates state block based on the utxos. func (bc *Blockchain) CreateStateBlock(utxoPool *UTXOPool) *Block { - var numBlocks int32 = 0 - var numTxs int32 = 0 + var numBlocks int32 + var numTxs int32 for _, block := range bc.Blocks { if block.IsStateBlock() { numBlocks += block.State.NumBlocks numTxs += block.State.NumTransactions } else { - numBlocks += 1 + numBlocks++ numTxs += block.NumTransactions } } diff --git a/blockchain/transaction.go b/blockchain/transaction.go index 7327bef84..a97cc2fe7 100644 --- a/blockchain/transaction.go +++ b/blockchain/transaction.go @@ -22,10 +22,14 @@ var ( zeroHash TxID ) -// DefaultCoinbaseValue is the default value of coinbase transaction. -const DefaultCoinbaseValue = 1 -const DefaultNumUtxos = 100 +const ( + // DefaultCoinbaseValue is the default value of coinbase transaction. + DefaultCoinbaseValue = 1 + // DefaultNumUtxos is the default value of number Utxos. + DefaultNumUtxos = 100 +) +// Transaction is the struct of a Transaction. type Transaction struct { ID [32]byte // 32 byte hash TxInput []TXInput @@ -45,7 +49,7 @@ type TXOutput struct { type TxID = [32]byte -// Output defines a data type that is used to track previous +// OutPoint defines a data type that is used to track previous // transaction outputs. // TxID is the transaction id // Index is the index of the transaction ouput in the previous transaction @@ -80,7 +84,7 @@ func NewTXInput(prevOut *OutPoint, address [20]byte, shardID uint32) *TXInput { } } -// The proof of accept or reject in the cross shard transaction locking phase. +// CrossShardTxProof is the proof of accept or reject in the cross shard transaction locking phase. // This is created by the shard leader, filled with proof signatures after consensus, and returned back to the client. // One proof structure is only tied to one shard. Therefore, the utxos in the proof are all with the same shard. type CrossShardTxProof struct { @@ -91,6 +95,7 @@ type CrossShardTxProof struct { // Signatures } +// CrossShardTxAndProof is the proof of accept or reject in the cross shard transaction locking phase. // This is a internal data structure that doesn't go across network type CrossShardTxAndProof struct { Transaction *Transaction // The cross shard tx @@ -111,6 +116,7 @@ func (tx *Transaction) SetID() { tx.ID = hash } +// Sign signs the given transaction with a private key. func (tx *Transaction) Sign(priKey kyber.Scalar) error { signature, err := schnorr.Sign(crypto.Ed25519Curve, priKey, tx.GetContentToVerify()) if err != nil { @@ -121,6 +127,7 @@ func (tx *Transaction) Sign(priKey kyber.Scalar) error { return err } +// IsCrossShard returns if the transaction is a cross transation. func (tx *Transaction) IsCrossShard() bool { shardIds := make(map[uint32]bool) for _, value := range tx.TxInput { @@ -132,6 +139,7 @@ func (tx *Transaction) IsCrossShard() bool { return len(shardIds) > 1 } +// GetContentToVerify gets content to verify. func (tx *Transaction) GetContentToVerify() []byte { tempTx := *tx tempTx.Signature = [64]byte{} @@ -206,6 +214,7 @@ func (tx *Transaction) String() string { return res } +// Serialize return serialized bytes of the transaction. func (tx *Transaction) Serialize() []byte { buffer := bytes.NewBuffer([]byte{}) buffer.Write(tx.ID[:]) @@ -223,6 +232,7 @@ func (tx *Transaction) Serialize() []byte { return buffer.Bytes() } +// Serialize return serialized bytes of the TXInput. func (txInput *TXInput) Serialize() []byte { buffer := bytes.NewBuffer([]byte{}) buffer.Write(txInput.Address[:]) @@ -238,6 +248,7 @@ func (txInput *TXInput) Serialize() []byte { return buffer.Bytes() } +// Serialize return serialized bytes of the TXOutput. func (txOutput *TXOutput) Serialize() []byte { buffer := bytes.NewBuffer([]byte{}) buffer.Write(txOutput.Address[:]) @@ -252,14 +263,15 @@ func (txOutput *TXOutput) Serialize() []byte { return buffer.Bytes() } -func (crossProof *CrossShardTxProof) Serialize() []byte { +// Serialize returns serialized bytes of the CrossShardTxProof. +func (proof *CrossShardTxProof) Serialize() []byte { buffer := bytes.NewBuffer([]byte{}) - buffer.Write(crossProof.TxID[:]) - buffer.Write(crossProof.BlockHash[:]) - for _, value := range crossProof.TxInput { + buffer.Write(proof.TxID[:]) + buffer.Write(proof.BlockHash[:]) + for _, value := range proof.TxInput { buffer.Write(value.Serialize()) } - if crossProof.Accept { + if proof.Accept { buffer.WriteByte(byte(1)) } else { buffer.WriteByte(byte(0))