fix comments on blockchain and transaction

pull/75/head
Minh Doan 6 years ago
parent dd8728a6f1
commit 061d8e2f7f
  1. 19
      blockchain/blockchain.go
  2. 30
      blockchain/transaction.go

@ -3,6 +3,7 @@ package blockchain
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"github.com/dedis/kyber" "github.com/dedis/kyber"
"github.com/simple-rules/harmony-benchmark/crypto/pki" "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" 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 { func (bc *Blockchain) FindBlock(blockHash []byte) *Block {
if len(blockHash) != 32 { if len(blockHash) != 32 {
return nil return nil
@ -27,6 +28,7 @@ func (bc *Blockchain) FindBlock(blockHash []byte) *Block {
return nil return nil
} }
// FindBlockWithPrevHash fins a block with given prevHash.
func (bc *Blockchain) FindBlockWithPrevHash(prevHash []byte) *Block { func (bc *Blockchain) FindBlockWithPrevHash(prevHash []byte) *Block {
if len(prevHash) != 32 { if len(prevHash) != 32 {
return nil return nil
@ -47,6 +49,7 @@ func (bc *Blockchain) GetLatestBlock() *Block {
return bc.Blocks[len(bc.Blocks)-1] return bc.Blocks[len(bc.Blocks)-1]
} }
// GetBlockHashes returns array of hashes of the given blockchain.
func (bc *Blockchain) GetBlockHashes() [][32]byte { func (bc *Blockchain) GetBlockHashes() [][32]byte {
res := [][32]byte{} res := [][32]byte{}
for _, block := range bc.Blocks { for _, block := range bc.Blocks {
@ -55,7 +58,7 @@ func (bc *Blockchain) GetBlockHashes() [][32]byte {
return res 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 { func (bc *Blockchain) FindUnspentUtxos(address [20]byte) map[TxID]map[uint32]TXOutput {
spentTXOs := make(map[string][]uint32) spentTXOs := make(map[string][]uint32)
result := make(map[TxID]map[uint32]TXOutput) 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 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 { func (bc *Blockchain) FindUTXO(address [20]byte) []TXOutput {
var UTXOs []TXOutput var UTXOs []TXOutput
unspentTXs := bc.FindUnspentUtxos(address) unspentTXs := bc.FindUnspentUtxos(address)
@ -114,7 +117,7 @@ func (bc *Blockchain) FindUTXO(address [20]byte) []TXOutput {
return UTXOs 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) { func (bc *Blockchain) FindSpendableOutputs(address [20]byte, amount int) (int, map[string][]uint32) {
unspentOutputs := make(map[string][]uint32) unspentOutputs := make(map[string][]uint32)
unspentUtxos := bc.FindUnspentUtxos(address) unspentUtxos := bc.FindUnspentUtxos(address)
@ -230,16 +233,16 @@ func CreateBlockchain(address [20]byte, shardId uint32) *Blockchain {
return &bc return &bc
} }
// Create state block based on the utxos. // CreateStateBlock creates state block based on the utxos.
func (bc *Blockchain) CreateStateBlock(utxoPool *UTXOPool) *Block { func (bc *Blockchain) CreateStateBlock(utxoPool *UTXOPool) *Block {
var numBlocks int32 = 0 var numBlocks int32
var numTxs int32 = 0 var numTxs int32
for _, block := range bc.Blocks { for _, block := range bc.Blocks {
if block.IsStateBlock() { if block.IsStateBlock() {
numBlocks += block.State.NumBlocks numBlocks += block.State.NumBlocks
numTxs += block.State.NumTransactions numTxs += block.State.NumTransactions
} else { } else {
numBlocks += 1 numBlocks++
numTxs += block.NumTransactions numTxs += block.NumTransactions
} }
} }

@ -22,10 +22,14 @@ var (
zeroHash TxID zeroHash TxID
) )
const (
// DefaultCoinbaseValue is the default value of coinbase transaction. // DefaultCoinbaseValue is the default value of coinbase transaction.
const DefaultCoinbaseValue = 1 DefaultCoinbaseValue = 1
const DefaultNumUtxos = 100 // DefaultNumUtxos is the default value of number Utxos.
DefaultNumUtxos = 100
)
// Transaction is the struct of a Transaction.
type Transaction struct { type Transaction struct {
ID [32]byte // 32 byte hash ID [32]byte // 32 byte hash
TxInput []TXInput TxInput []TXInput
@ -45,7 +49,7 @@ type TXOutput struct {
type TxID = [32]byte 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. // transaction outputs.
// TxID is the transaction id // TxID is the transaction id
// Index is the index of the transaction ouput in the previous transaction // 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. // 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. // One proof structure is only tied to one shard. Therefore, the utxos in the proof are all with the same shard.
type CrossShardTxProof struct { type CrossShardTxProof struct {
@ -91,6 +95,7 @@ type CrossShardTxProof struct {
// Signatures // 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 // This is a internal data structure that doesn't go across network
type CrossShardTxAndProof struct { type CrossShardTxAndProof struct {
Transaction *Transaction // The cross shard tx Transaction *Transaction // The cross shard tx
@ -111,6 +116,7 @@ func (tx *Transaction) SetID() {
tx.ID = hash tx.ID = hash
} }
// Sign signs the given transaction with a private key.
func (tx *Transaction) Sign(priKey kyber.Scalar) error { func (tx *Transaction) Sign(priKey kyber.Scalar) error {
signature, err := schnorr.Sign(crypto.Ed25519Curve, priKey, tx.GetContentToVerify()) signature, err := schnorr.Sign(crypto.Ed25519Curve, priKey, tx.GetContentToVerify())
if err != nil { if err != nil {
@ -121,6 +127,7 @@ func (tx *Transaction) Sign(priKey kyber.Scalar) error {
return err return err
} }
// IsCrossShard returns if the transaction is a cross transation.
func (tx *Transaction) IsCrossShard() bool { func (tx *Transaction) IsCrossShard() bool {
shardIds := make(map[uint32]bool) shardIds := make(map[uint32]bool)
for _, value := range tx.TxInput { for _, value := range tx.TxInput {
@ -132,6 +139,7 @@ func (tx *Transaction) IsCrossShard() bool {
return len(shardIds) > 1 return len(shardIds) > 1
} }
// GetContentToVerify gets content to verify.
func (tx *Transaction) GetContentToVerify() []byte { func (tx *Transaction) GetContentToVerify() []byte {
tempTx := *tx tempTx := *tx
tempTx.Signature = [64]byte{} tempTx.Signature = [64]byte{}
@ -206,6 +214,7 @@ func (tx *Transaction) String() string {
return res return res
} }
// Serialize return serialized bytes of the transaction.
func (tx *Transaction) Serialize() []byte { func (tx *Transaction) Serialize() []byte {
buffer := bytes.NewBuffer([]byte{}) buffer := bytes.NewBuffer([]byte{})
buffer.Write(tx.ID[:]) buffer.Write(tx.ID[:])
@ -223,6 +232,7 @@ func (tx *Transaction) Serialize() []byte {
return buffer.Bytes() return buffer.Bytes()
} }
// Serialize return serialized bytes of the TXInput.
func (txInput *TXInput) Serialize() []byte { func (txInput *TXInput) Serialize() []byte {
buffer := bytes.NewBuffer([]byte{}) buffer := bytes.NewBuffer([]byte{})
buffer.Write(txInput.Address[:]) buffer.Write(txInput.Address[:])
@ -238,6 +248,7 @@ func (txInput *TXInput) Serialize() []byte {
return buffer.Bytes() return buffer.Bytes()
} }
// Serialize return serialized bytes of the TXOutput.
func (txOutput *TXOutput) Serialize() []byte { func (txOutput *TXOutput) Serialize() []byte {
buffer := bytes.NewBuffer([]byte{}) buffer := bytes.NewBuffer([]byte{})
buffer.Write(txOutput.Address[:]) buffer.Write(txOutput.Address[:])
@ -252,14 +263,15 @@ func (txOutput *TXOutput) Serialize() []byte {
return buffer.Bytes() 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 := bytes.NewBuffer([]byte{})
buffer.Write(crossProof.TxID[:]) buffer.Write(proof.TxID[:])
buffer.Write(crossProof.BlockHash[:]) buffer.Write(proof.BlockHash[:])
for _, value := range crossProof.TxInput { for _, value := range proof.TxInput {
buffer.Write(value.Serialize()) buffer.Write(value.Serialize())
} }
if crossProof.Accept { if proof.Accept {
buffer.WriteByte(byte(1)) buffer.WriteByte(byte(1))
} else { } else {
buffer.WriteByte(byte(0)) buffer.WriteByte(byte(0))

Loading…
Cancel
Save