Merge pull request #171 from harmony-one/rj_branch
Remove UTXO model (the block syncing code needs to be reworked @minh)pull/172/head
commit
d0463a566c
@ -1,172 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/sha256" |
||||
"encoding/gob" |
||||
"fmt" |
||||
"log" |
||||
"time" |
||||
|
||||
"github.com/harmony-one/harmony/db" |
||||
"github.com/harmony-one/harmony/utils" |
||||
) |
||||
|
||||
const ( |
||||
// TimeStampForGenesisBlock is the constant timestamp for the genesis block.
|
||||
TimeStampForGenesisBlock = 0 |
||||
) |
||||
|
||||
// Block is a block in the blockchain that contains block headers, transactions and signature etc.
|
||||
type Block struct { |
||||
// Header
|
||||
Timestamp int64 |
||||
PrevBlockHash [32]byte |
||||
NumTransactions int32 |
||||
TransactionIds [][32]byte |
||||
Transactions []*Transaction // Transactions.
|
||||
ShardID uint32 |
||||
Hash [32]byte |
||||
MerkleRootData []byte |
||||
State *State // If present, this block is state block.
|
||||
// Signature...
|
||||
Bitmap []byte // Contains which validator signed the block.
|
||||
Signature [66]byte // Schnorr collective signature.
|
||||
|
||||
AccountBlock []byte // Temporary piggy-back.
|
||||
} |
||||
|
||||
// State is used in Block to indicate that block is a state block.
|
||||
type State struct { |
||||
NumBlocks int32 // Total number of blocks.
|
||||
NumTransactions int32 // Total number of transactions.
|
||||
} |
||||
|
||||
// IsStateBlock is used to check if a block is a state block.
|
||||
func (b *Block) IsStateBlock() bool { |
||||
// TODO: think of a better indicator to check.
|
||||
return b.State != nil && bytes.Equal(b.PrevBlockHash[:], (&[32]byte{})[:]) |
||||
} |
||||
|
||||
// Serialize serializes the block.
|
||||
func (b *Block) Serialize() []byte { |
||||
var result bytes.Buffer |
||||
encoder := gob.NewEncoder(&result) |
||||
err := encoder.Encode(b) |
||||
if err != nil { |
||||
log.Panic(err) |
||||
} |
||||
return result.Bytes() |
||||
} |
||||
|
||||
// DeserializeBlock deserializes a block.
|
||||
func DeserializeBlock(d []byte) (*Block, error) { |
||||
var block Block |
||||
decoder := gob.NewDecoder(bytes.NewReader(d)) |
||||
err := decoder.Decode(&block) |
||||
if err != nil { |
||||
log.Panic(err) |
||||
} |
||||
return &block, err |
||||
} |
||||
|
||||
// Used for debuging.
|
||||
func (b *Block) String() string { |
||||
res := fmt.Sprintf("Block created at %v\n", b.Timestamp) |
||||
for id, tx := range b.Transactions { |
||||
res += fmt.Sprintf("Transaction %v: %v\n", id, *tx) |
||||
} |
||||
res += fmt.Sprintf("previous blockhash: %v\n", b.PrevBlockHash) |
||||
res += fmt.Sprintf("hash: %v\n", b.Hash) |
||||
return res |
||||
} |
||||
|
||||
func (b *Block) generateMerkleRootData() { |
||||
var data [][]byte |
||||
|
||||
for _, txID := range b.TransactionIds { |
||||
data = append(data, txID[:]) |
||||
} |
||||
merkleTre := NewMerkleTree(data) |
||||
b.MerkleRootData = merkleTre.RootNode.Data |
||||
} |
||||
|
||||
func (b *Block) Write(db db.Database, key string) error { |
||||
return db.Put([]byte(key), b.Serialize()) |
||||
} |
||||
|
||||
// Delete deletes the given key in the given databse.
|
||||
func Delete(db db.Database, key string) error { |
||||
return db.Delete([]byte(key)) |
||||
} |
||||
|
||||
// CalculateBlockHash returns a hash of the block
|
||||
func (b *Block) CalculateBlockHash() []byte { |
||||
var hashes [][]byte |
||||
var blockHash [32]byte |
||||
|
||||
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.Timestamp)) |
||||
hashes = append(hashes, b.PrevBlockHash[:]) |
||||
for _, id := range b.TransactionIds { |
||||
hashes = append(hashes, id[:]) |
||||
} |
||||
b.generateMerkleRootData() |
||||
|
||||
hashes = append(hashes, b.MerkleRootData) |
||||
hashes = append(hashes, utils.ConvertFixedDataIntoByteArray(b.ShardID)) |
||||
|
||||
blockHash = sha256.Sum256(bytes.Join(hashes, []byte{})) |
||||
return blockHash[:] |
||||
} |
||||
|
||||
// NewBlock creates and returns a new block.
|
||||
func NewBlock(transactions []*Transaction, prevBlockHash [32]byte, shardID uint32, isGenesisBlock bool) *Block { |
||||
numTxs := int32(len(transactions)) |
||||
var txIDs [][32]byte |
||||
|
||||
for _, tx := range transactions { |
||||
txIDs = append(txIDs, tx.ID) |
||||
} |
||||
timestamp := time.Now().Unix() |
||||
if isGenesisBlock { |
||||
timestamp = TimeStampForGenesisBlock |
||||
} |
||||
block := &Block{Timestamp: timestamp, PrevBlockHash: prevBlockHash, NumTransactions: numTxs, TransactionIds: txIDs, Transactions: transactions, ShardID: shardID, Hash: [32]byte{}} |
||||
copy(block.Hash[:], block.CalculateBlockHash()[:]) |
||||
|
||||
return block |
||||
} |
||||
|
||||
// NewGenesisBlock creates and returns genesis Block.
|
||||
func NewGenesisBlock(coinbase *Transaction, shardID uint32) *Block { |
||||
return NewBlock([]*Transaction{coinbase}, [32]byte{}, shardID, true) |
||||
} |
||||
|
||||
// NewStateBlock creates and returns a state Block based on utxo pool.
|
||||
// TODO(RJ): take care of dangling cross shard transaction
|
||||
func NewStateBlock(utxoPool *UTXOPool, numBlocks, numTxs int32) *Block { |
||||
stateTransactions := []*Transaction{} |
||||
stateTransactionIds := [][32]byte{} |
||||
for address, txHash2Vout2AmountMap := range utxoPool.UtxoMap { |
||||
stateTransaction := &Transaction{} |
||||
for txHash, vout2AmountMap := range txHash2Vout2AmountMap { |
||||
for index, amount := range vout2AmountMap { |
||||
txHashBytes, err := utils.Get32BytesFromString(txHash) |
||||
if err == nil { |
||||
stateTransaction.TxInput = append(stateTransaction.TxInput, *NewTXInput(NewOutPoint(&txHashBytes, index), address, utxoPool.ShardID)) |
||||
stateTransaction.TxOutput = append(stateTransaction.TxOutput, TXOutput{Amount: amount, Address: address, ShardID: utxoPool.ShardID}) |
||||
} else { |
||||
return nil |
||||
} |
||||
} |
||||
} |
||||
if len(stateTransaction.TxOutput) != 0 { |
||||
stateTransaction.SetID() |
||||
stateTransactionIds = append(stateTransactionIds, stateTransaction.ID) |
||||
stateTransactions = append(stateTransactions, stateTransaction) |
||||
} |
||||
} |
||||
newBlock := NewBlock(stateTransactions, [32]byte{}, utxoPool.ShardID, false) |
||||
newBlock.State = &State{NumBlocks: numBlocks, NumTransactions: numTxs} |
||||
return newBlock |
||||
} |
@ -1,21 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
func TestBlockSerialize(t *testing.T) { |
||||
cbtx := NewCoinbaseTX(TestAddressOne, genesisCoinbaseData, 0) |
||||
if cbtx == nil { |
||||
t.Errorf("Failed to create a coinbase transaction.") |
||||
} |
||||
block := NewGenesisBlock(cbtx, 0) |
||||
|
||||
serializedValue := block.Serialize() |
||||
deserializedBlock, _ := DeserializeBlock(serializedValue) |
||||
|
||||
if !reflect.DeepEqual(block, deserializedBlock) { |
||||
t.Errorf("Original block and the deserialized block not equal.") |
||||
} |
||||
} |
@ -1,265 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/hex" |
||||
|
||||
"github.com/dedis/kyber" |
||||
"github.com/harmony-one/harmony/crypto/pki" |
||||
) |
||||
|
||||
// Blockchain keeps a sequence of Blocks
|
||||
type Blockchain struct { |
||||
Blocks []*Block |
||||
} |
||||
|
||||
const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" |
||||
|
||||
// FindBlock finds a block with given blockHash.
|
||||
func (bc *Blockchain) FindBlock(blockHash []byte) *Block { |
||||
if len(blockHash) != 32 { |
||||
return nil |
||||
} |
||||
for _, block := range bc.Blocks { |
||||
if bytes.Equal(block.Hash[:], blockHash[:]) { |
||||
return block |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// FindBlockWithPrevHash fins a block with given prevHash.
|
||||
func (bc *Blockchain) FindBlockWithPrevHash(prevHash []byte) *Block { |
||||
if len(prevHash) != 32 { |
||||
return nil |
||||
} |
||||
for _, block := range bc.Blocks { |
||||
if bytes.Equal(block.PrevBlockHash[:], prevHash[:]) { |
||||
return block |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// GetLatestBlock gests the latest block at the end of the chain
|
||||
func (bc *Blockchain) GetLatestBlock() *Block { |
||||
if len(bc.Blocks) == 0 { |
||||
return nil |
||||
} |
||||
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 { |
||||
res = append(res, block.Hash) |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// 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) |
||||
|
||||
for index := len(bc.Blocks) - 1; index >= 0; index-- { |
||||
block := bc.Blocks[index] |
||||
|
||||
for _, tx := range block.Transactions { |
||||
txID := hex.EncodeToString(tx.ID[:]) |
||||
|
||||
for outIdx, txOutput := range tx.TxOutput { |
||||
shouldContinue := false |
||||
for index := range spentTXOs[txID] { |
||||
if spentTXOs[txID][index] == uint32(outIdx) { |
||||
shouldContinue = true |
||||
break |
||||
} |
||||
} |
||||
if shouldContinue { |
||||
continue |
||||
} |
||||
if txOutput.Address == address { |
||||
_, ok := result[tx.ID] |
||||
if !ok { |
||||
result[tx.ID] = make(map[uint32]TXOutput) |
||||
} |
||||
result[tx.ID][uint32(outIdx)] = txOutput |
||||
} |
||||
} |
||||
|
||||
for _, txInput := range tx.TxInput { |
||||
if address == txInput.Address { |
||||
ID := hex.EncodeToString(txInput.PreviousOutPoint.TxID[:]) |
||||
spentTXOs[ID] = append(spentTXOs[ID], txInput.PreviousOutPoint.Index) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return result |
||||
} |
||||
|
||||
// FindUTXO finds and returns all unspent transaction outputs.
|
||||
func (bc *Blockchain) FindUTXO(address [20]byte) []TXOutput { |
||||
var UTXOs []TXOutput |
||||
unspentTXs := bc.FindUnspentUtxos(address) |
||||
|
||||
for _, utxos := range unspentTXs { |
||||
for _, txOutput := range utxos { |
||||
if txOutput.Address == address { |
||||
UTXOs = append(UTXOs, txOutput) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
|
||||
return UTXOs |
||||
} |
||||
|
||||
// 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) |
||||
accumulated := 0 |
||||
|
||||
Work: |
||||
for txID, txOutputs := range unspentUtxos { |
||||
txID := hex.EncodeToString(txID[:]) |
||||
|
||||
for outIdx, txOutput := range txOutputs { |
||||
if txOutput.Address == address && accumulated < amount { |
||||
accumulated += txOutput.Amount |
||||
unspentOutputs[txID] = append(unspentOutputs[txID], uint32(outIdx)) |
||||
|
||||
if accumulated >= amount { |
||||
break Work |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return accumulated, unspentOutputs |
||||
} |
||||
|
||||
// NewUTXOTransaction creates a new transaction
|
||||
func (bc *Blockchain) NewUTXOTransaction(priKey kyber.Scalar, from, to [20]byte, amount int, shardID uint32) *Transaction { |
||||
var inputs []TXInput |
||||
var outputs []TXOutput |
||||
|
||||
acc, validOutputs := bc.FindSpendableOutputs(from, amount) |
||||
|
||||
if acc < amount { |
||||
return nil |
||||
} |
||||
|
||||
// Build a list of inputs
|
||||
for txid, outs := range validOutputs { |
||||
id, err := hex.DecodeString(txid) |
||||
if err != nil { |
||||
return nil |
||||
} |
||||
|
||||
txID := TxID{} |
||||
copy(txID[:], id[:]) |
||||
for _, out := range outs { |
||||
input := NewTXInput(NewOutPoint(&txID, out), from, shardID) |
||||
inputs = append(inputs, *input) |
||||
} |
||||
} |
||||
|
||||
// Build a list of outputs
|
||||
outputs = append(outputs, TXOutput{amount, to, shardID}) |
||||
if acc > amount { |
||||
outputs = append(outputs, TXOutput{acc - amount, from, shardID}) // a change
|
||||
} |
||||
|
||||
tx := Transaction{ID: [32]byte{}, TxInput: inputs, TxOutput: outputs, Proofs: nil} |
||||
tx.SetID() |
||||
|
||||
pubKey := pki.GetPublicKeyFromScalar(priKey) |
||||
bytes, err := pubKey.MarshalBinary() |
||||
if err == nil { |
||||
copy(tx.PublicKey[:], bytes) |
||||
} else { |
||||
panic("Failed to serialize public key") |
||||
} |
||||
tx.SetID() // TODO(RJ): figure out the correct way to set Tx ID.
|
||||
tx.Sign(priKey) |
||||
|
||||
return &tx |
||||
} |
||||
|
||||
// AddNewUserTransfer creates a new transaction and a block of that transaction.
|
||||
// Mostly used for testing.
|
||||
func (bc *Blockchain) AddNewUserTransfer(utxoPool *UTXOPool, priKey kyber.Scalar, from, to [20]byte, amount int, shardID uint32) bool { |
||||
tx := bc.NewUTXOTransaction(priKey, from, to, amount, shardID) |
||||
if tx != nil { |
||||
newBlock := NewBlock([]*Transaction{tx}, bc.Blocks[len(bc.Blocks)-1].Hash, shardID, false) |
||||
if bc.VerifyNewBlockAndUpdate(utxoPool, newBlock) { |
||||
return true |
||||
} |
||||
} |
||||
return false |
||||
} |
||||
|
||||
// VerifyNewBlockAndUpdate verifies if the new coming block is valid for the current blockchain.
|
||||
func (bc *Blockchain) VerifyNewBlockAndUpdate(utxopool *UTXOPool, block *Block) bool { |
||||
length := len(bc.Blocks) |
||||
if !bytes.Equal(block.PrevBlockHash[:], bc.Blocks[length-1].Hash[:]) { |
||||
return false |
||||
} |
||||
if block.Timestamp < bc.Blocks[length-1].Timestamp { |
||||
return false |
||||
} |
||||
|
||||
if utxopool != nil && !utxopool.VerifyAndUpdate(block.Transactions) { |
||||
return false |
||||
} |
||||
bc.Blocks = append(bc.Blocks, block) |
||||
return true |
||||
} |
||||
|
||||
// CreateBlockchain creates a new blockchain DB
|
||||
// TODO(minhdoan): This func is not used, consider to remove.
|
||||
func CreateBlockchain(address [20]byte, shardID uint32) *Blockchain { |
||||
// TODO: We assume we have not created any blockchain before.
|
||||
// In current bitcoin, we can check if we created a blockchain before accessing local db.
|
||||
cbtx := NewCoinbaseTX(address, genesisCoinbaseData, shardID) |
||||
genesis := NewGenesisBlock(cbtx, shardID) |
||||
|
||||
bc := Blockchain{[]*Block{genesis}} |
||||
|
||||
return &bc |
||||
} |
||||
|
||||
// CreateBlockchainWithMoreBlocks is used for syncing testing.
|
||||
func CreateBlockchainWithMoreBlocks(addresses [][20]byte, shardID uint32) *Blockchain { |
||||
return &Blockchain{CreateMoreBlocks(addresses, shardID)} |
||||
} |
||||
|
||||
// CreateMoreBlocks is used for syncing testing.
|
||||
func CreateMoreBlocks(addresses [][20]byte, shardID uint32) []*Block { |
||||
blocks := []*Block{} |
||||
for _, address := range addresses { |
||||
cbtx := NewCoinbaseTX(address, genesisCoinbaseData, shardID) |
||||
blocks = append(blocks, NewGenesisBlock(cbtx, shardID)) |
||||
} |
||||
return blocks |
||||
} |
||||
|
||||
// CreateStateBlock creates state block based on the utxos.
|
||||
func (bc *Blockchain) CreateStateBlock(utxoPool *UTXOPool) *Block { |
||||
var numBlocks int32 |
||||
var numTxs int32 |
||||
for _, block := range bc.Blocks { |
||||
if block.IsStateBlock() { |
||||
numBlocks += block.State.NumBlocks |
||||
numTxs += block.State.NumTransactions |
||||
} else { |
||||
numBlocks++ |
||||
numTxs += block.NumTransactions |
||||
} |
||||
} |
||||
return NewStateBlock(utxoPool, numBlocks, numTxs) |
||||
} |
@ -1,92 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/harmony-one/harmony/crypto/pki" |
||||
) |
||||
|
||||
var ( |
||||
PriIntOne = 111 |
||||
PriIntTwo = 2 |
||||
PriIntThree = 3 |
||||
PriIntFour = 4 |
||||
PriKeyOne = pki.GetPrivateKeyScalarFromInt(PriIntOne) |
||||
PriKeyTwo = pki.GetPrivateKeyScalarFromInt(PriIntTwo) |
||||
PriKeyThree = pki.GetPrivateKeyScalarFromInt(PriIntThree) |
||||
PriKeyFour = pki.GetPrivateKeyScalarFromInt(PriIntFour) |
||||
TestAddressOne = pki.GetAddressFromInt(PriIntOne) |
||||
TestAddressTwo = pki.GetAddressFromInt(PriIntTwo) |
||||
TestAddressThree = pki.GetAddressFromInt(PriIntThree) |
||||
TestAddressFour = pki.GetAddressFromInt(PriIntFour) |
||||
) |
||||
|
||||
func TestCreateBlockchain(t *testing.T) { |
||||
if bc := CreateBlockchain(TestAddressOne, 0); bc == nil { |
||||
t.Errorf("failed to create a blockchain") |
||||
} |
||||
} |
||||
|
||||
func TestFindSpendableOutputs(t *testing.T) { |
||||
requestAmount := 3 |
||||
bc := CreateBlockchain(TestAddressOne, 0) |
||||
accumulated, unspentOutputs := bc.FindSpendableOutputs(TestAddressOne, requestAmount) |
||||
if accumulated < DefaultCoinbaseValue { |
||||
t.Error("Failed to find enough unspent ouptuts") |
||||
} |
||||
|
||||
if len(unspentOutputs) <= 0 { |
||||
t.Error("Failed to find enough unspent ouptuts") |
||||
} |
||||
} |
||||
|
||||
func TestFindUTXO(t *testing.T) { |
||||
bc := CreateBlockchain(TestAddressOne, 0) |
||||
utxo := bc.FindUTXO(TestAddressOne) |
||||
|
||||
total := 0 |
||||
for _, value := range utxo { |
||||
total += value.Amount |
||||
if value.Address != TestAddressOne { |
||||
t.Error("FindUTXO failed.") |
||||
} |
||||
} |
||||
|
||||
if total != DefaultCoinbaseValue { |
||||
t.Error("FindUTXO failed.") |
||||
} |
||||
} |
||||
|
||||
func TestAddNewUserTransfer(t *testing.T) { |
||||
bc := CreateBlockchain(TestAddressOne, 0) |
||||
utxoPool := CreateUTXOPoolFromGenesisBlock(bc.Blocks[0]) |
||||
|
||||
if !bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressThree, 3, 0) { |
||||
t.Error("Failed to add new transfer to alok.") |
||||
} |
||||
if !bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressTwo, 3, 0) { |
||||
t.Error("Failed to add new transfer to rj.") |
||||
} |
||||
|
||||
if bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressFour, 100, 0) { |
||||
t.Error("minh should not have enough fun to make the transfer.") |
||||
} |
||||
} |
||||
|
||||
func TestVerifyNewBlock(t *testing.T) { |
||||
bc := CreateBlockchain(TestAddressOne, 0) |
||||
utxoPool := CreateUTXOPoolFromGenesisBlock(bc.Blocks[0]) |
||||
|
||||
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressThree, 3, 0) |
||||
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressTwo, 10, 0) |
||||
|
||||
tx := bc.NewUTXOTransaction(PriKeyOne, TestAddressOne, TestAddressFour, 10, 0) |
||||
if tx == nil { |
||||
t.Error("failed to create a new transaction.") |
||||
} |
||||
newBlock := NewBlock([]*Transaction{tx}, bc.Blocks[len(bc.Blocks)-1].Hash, 0, false) |
||||
|
||||
if !bc.VerifyNewBlockAndUpdate(utxoPool, newBlock) { |
||||
t.Error("failed to add a new valid block.") |
||||
} |
||||
} |
@ -1 +0,0 @@ |
||||
package blockchain |
@ -1,69 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"crypto/sha256" |
||||
) |
||||
|
||||
// MerkleTree represent a Merkle tree
|
||||
type MerkleTree struct { |
||||
RootNode *MerkleNode |
||||
} |
||||
|
||||
// MerkleNode represent a Merkle tree node
|
||||
type MerkleNode struct { |
||||
Left *MerkleNode |
||||
Right *MerkleNode |
||||
Data []byte |
||||
} |
||||
|
||||
// NewMerkleTree creates a new Merkle tree from a sequence of data
|
||||
func NewMerkleTree(data [][]byte) *MerkleTree { |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
var nodes []*MerkleNode |
||||
|
||||
for _, datum := range data { |
||||
node := NewMerkleNode(nil, nil, datum) |
||||
nodes = append(nodes, node) |
||||
} |
||||
|
||||
for len(nodes) > 1 { |
||||
var newLevel []*MerkleNode |
||||
|
||||
if len(nodes)%2 != 0 { |
||||
nodes = append(nodes, nodes[len(nodes)-1]) |
||||
} |
||||
|
||||
for j := 0; j < len(nodes); j += 2 { |
||||
node := NewMerkleNode(nodes[j], nodes[j+1], nil) |
||||
newLevel = append(newLevel, node) |
||||
} |
||||
|
||||
nodes = newLevel |
||||
} |
||||
|
||||
mTree := MerkleTree{nodes[0]} |
||||
|
||||
return &mTree |
||||
} |
||||
|
||||
// NewMerkleNode creates a new Merkle tree node
|
||||
func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode { |
||||
mNode := MerkleNode{} |
||||
|
||||
prevHashes := []byte{} |
||||
if left != nil { |
||||
prevHashes = append(prevHashes, left.Data...) |
||||
} |
||||
if right != nil { |
||||
prevHashes = append(prevHashes, right.Data...) |
||||
} |
||||
prevHashes = append(prevHashes, data...) |
||||
hash := sha256.Sum256(prevHashes) |
||||
mNode.Data = hash[:] |
||||
mNode.Left = left |
||||
mNode.Right = right |
||||
|
||||
return &mNode |
||||
} |
@ -1,82 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"encoding/hex" |
||||
"fmt" |
||||
"testing" |
||||
) |
||||
|
||||
func TestNewMerkleNode(t *testing.T) { |
||||
data := [][]byte{ |
||||
[]byte("node1"), |
||||
[]byte("node2"), |
||||
[]byte("node3"), |
||||
} |
||||
|
||||
fmt.Println("Testing") |
||||
// Level 1
|
||||
|
||||
n1 := NewMerkleNode(nil, nil, data[0]) |
||||
n2 := NewMerkleNode(nil, nil, data[1]) |
||||
n3 := NewMerkleNode(nil, nil, data[2]) |
||||
n4 := NewMerkleNode(nil, nil, data[2]) |
||||
|
||||
// Level 2
|
||||
n5 := NewMerkleNode(n1, n2, nil) |
||||
n6 := NewMerkleNode(n3, n4, nil) |
||||
|
||||
// Level 3
|
||||
n7 := NewMerkleNode(n5, n6, nil) |
||||
|
||||
if hex.EncodeToString(n7.Data) != "4e3e44e55926330ab6c31892f980f8bfd1a6e910ff1ebc3f778211377f35227e" { |
||||
t.Errorf("merkle tree is not built correctly.") |
||||
} |
||||
} |
||||
|
||||
func TestNewMerkleTree(t *testing.T) { |
||||
data := [][]byte{ |
||||
[]byte("node1"), |
||||
[]byte("node2"), |
||||
[]byte("node3"), |
||||
[]byte("node4"), |
||||
} |
||||
// Level 1
|
||||
n1 := NewMerkleNode(nil, nil, data[0]) |
||||
n2 := NewMerkleNode(nil, nil, data[1]) |
||||
n3 := NewMerkleNode(nil, nil, data[2]) |
||||
n4 := NewMerkleNode(nil, nil, data[3]) |
||||
|
||||
// Level 2
|
||||
n5 := NewMerkleNode(n1, n2, nil) |
||||
n6 := NewMerkleNode(n3, n4, nil) |
||||
|
||||
// Level 3
|
||||
n7 := NewMerkleNode(n5, n6, nil) |
||||
|
||||
rootHash := fmt.Sprintf("%x", n7.Data) |
||||
mTree := NewMerkleTree(data) |
||||
|
||||
if rootHash != fmt.Sprintf("%x", mTree.RootNode.Data) { |
||||
t.Errorf("Merkle tree root hash is incorrect") |
||||
} |
||||
} |
||||
|
||||
func TestNewMerkleTree2(t *testing.T) { |
||||
data := [][]byte{ |
||||
[]byte("node1"), |
||||
[]byte("node2"), |
||||
} |
||||
// Level 1
|
||||
n1 := NewMerkleNode(nil, nil, data[0]) |
||||
n2 := NewMerkleNode(nil, nil, data[1]) |
||||
|
||||
// Level 2
|
||||
n3 := NewMerkleNode(n1, n2, nil) |
||||
|
||||
rootHash := fmt.Sprintf("%x", n3.Data) |
||||
mTree := NewMerkleTree(data) |
||||
|
||||
if rootHash != fmt.Sprintf("%x", mTree.RootNode.Data) { |
||||
t.Errorf("Merkle tree root hash is incorrect") |
||||
} |
||||
} |
@ -1,281 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/sha256" |
||||
"encoding/binary" |
||||
"encoding/gob" |
||||
"encoding/hex" |
||||
"fmt" |
||||
"log" |
||||
"math" |
||||
|
||||
"github.com/dedis/kyber" |
||||
"github.com/dedis/kyber/sign/schnorr" |
||||
"github.com/harmony-one/harmony/crypto" |
||||
) |
||||
|
||||
var ( |
||||
// zeroHash is the zero value for a Hash and is defined as
|
||||
// a package level variable to avoid the need to create a new instance
|
||||
// every time a check is needed.
|
||||
zeroHash TxID |
||||
) |
||||
|
||||
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 |
||||
TxOutput []TXOutput |
||||
PublicKey [32]byte |
||||
Signature [64]byte |
||||
|
||||
Proofs []CrossShardTxProof // The proofs for crossShard tx unlock-to-commit/abort
|
||||
} |
||||
|
||||
// TXOutput is the struct of transaction output in a transaction.
|
||||
type TXOutput struct { |
||||
Amount int // TODO: Switch to big int or uint32
|
||||
Address [20]byte // last 20 bytes of the hash of public key
|
||||
ShardID uint32 // The Id of the shard where this UTXO belongs
|
||||
} |
||||
|
||||
// TxID structure type.
|
||||
type TxID = [32]byte |
||||
|
||||
// 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
|
||||
type OutPoint struct { |
||||
TxID TxID |
||||
Index uint32 |
||||
} |
||||
|
||||
// NewOutPoint returns a new transaction outpoint point with the
|
||||
// provided txID and index.
|
||||
func NewOutPoint(txID *TxID, index uint32) *OutPoint { |
||||
return &OutPoint{ |
||||
TxID: *txID, |
||||
Index: index, |
||||
} |
||||
} |
||||
|
||||
// TXInput is the struct of transaction input (a UTXO) in a transaction.
|
||||
type TXInput struct { |
||||
PreviousOutPoint OutPoint |
||||
Address [20]byte // TODO: @minh do we really need this?
|
||||
ShardID uint32 // The Id of the shard where this UTXO belongs
|
||||
} |
||||
|
||||
// NewTXInput returns a new transaction input with the provided
|
||||
// previous outpoint point, output address and shardID
|
||||
func NewTXInput(prevOut *OutPoint, address [20]byte, shardID uint32) *TXInput { |
||||
return &TXInput{ |
||||
PreviousOutPoint: *prevOut, |
||||
Address: address, |
||||
ShardID: shardID, |
||||
} |
||||
} |
||||
|
||||
// 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 { |
||||
Accept bool // false means proof-of-reject, true means proof-of-accept
|
||||
TxID [32]byte // Id of the transaction which this proof is on
|
||||
TxInput []TXInput // The list of Utxo that this proof is on. They should be in the same shard.
|
||||
BlockHash [32]byte // The hash of the block where the proof is registered
|
||||
// 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
|
||||
Proof *CrossShardTxProof // The proof
|
||||
} |
||||
|
||||
// SetID sets ID of a transaction (32 byte hash of the whole transaction)
|
||||
func (tx *Transaction) SetID() { |
||||
var encoded bytes.Buffer |
||||
var hash [32]byte |
||||
|
||||
enc := gob.NewEncoder(&encoded) |
||||
err := enc.Encode(tx) |
||||
if err != nil { |
||||
log.Panic(err) |
||||
} |
||||
hash = sha256.Sum256(encoded.Bytes()) |
||||
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 { |
||||
log.Panic(err) |
||||
} |
||||
|
||||
copy(tx.Signature[:], signature) |
||||
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 { |
||||
shardIDs[value.ShardID] = true |
||||
} |
||||
for _, value := range tx.TxOutput { |
||||
shardIDs[value.ShardID] = true |
||||
} |
||||
return len(shardIDs) > 1 |
||||
} |
||||
|
||||
// GetContentToVerify gets content to verify.
|
||||
func (tx *Transaction) GetContentToVerify() []byte { |
||||
tempTx := *tx |
||||
tempTx.Signature = [64]byte{} |
||||
tempTx.Proofs = []CrossShardTxProof{} |
||||
|
||||
return tempTx.Serialize() |
||||
} |
||||
|
||||
// NewCoinbaseTX creates a new coinbase transaction
|
||||
func NewCoinbaseTX(toAddress [20]byte, data string, shardID uint32) *Transaction { |
||||
if data == "" { |
||||
data = fmt.Sprintf("Reward to '%b'", toAddress) |
||||
} |
||||
|
||||
txin := NewTXInput(NewOutPoint(&TxID{}, math.MaxUint32), toAddress, shardID) |
||||
outputs := []TXOutput{} |
||||
for i := 0; i < DefaultNumUtxos; i++ { |
||||
outputs = append(outputs, TXOutput{DefaultCoinbaseValue, toAddress, shardID}) |
||||
} |
||||
tx := Transaction{ID: [32]byte{}, TxInput: []TXInput{*txin}, TxOutput: outputs, Proofs: nil} |
||||
// TODO: take care of the signature of coinbase transaction.
|
||||
tx.SetID() |
||||
return &tx |
||||
} |
||||
|
||||
// Used for debuging.
|
||||
func (txInput *TXInput) String() string { |
||||
res := fmt.Sprintf("TxID: %v, ", hex.EncodeToString(txInput.PreviousOutPoint.TxID[:])) |
||||
res += fmt.Sprintf("TxOutputIndex: %v, ", txInput.PreviousOutPoint.Index) |
||||
res += fmt.Sprintf("Address: %v, ", txInput.Address) |
||||
res += fmt.Sprintf("ShardID: %v", txInput.ShardID) |
||||
return res |
||||
} |
||||
|
||||
// Used for debuging.
|
||||
func (txOutput *TXOutput) String() string { |
||||
res := fmt.Sprintf("Amount: %v, ", txOutput.Amount) |
||||
res += fmt.Sprintf("Address: %v", txOutput.Address) |
||||
res += fmt.Sprintf("ShardID: %v", txOutput.ShardID) |
||||
return res |
||||
} |
||||
|
||||
// Used for debuging.
|
||||
func (proof *CrossShardTxProof) String() string { |
||||
res := fmt.Sprintf("Accept: %v, ", proof.Accept) |
||||
res += fmt.Sprintf("TxId: %v, ", hex.EncodeToString(proof.TxID[:])) |
||||
res += fmt.Sprintf("BlockHash: %v, ", hex.EncodeToString(proof.BlockHash[:])) |
||||
res += fmt.Sprintf("TxInput:\n") |
||||
for id, value := range proof.TxInput { |
||||
res += fmt.Sprintf("%v: %v\n", id, value.String()) |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// Used for debuging.
|
||||
func (tx *Transaction) String() string { |
||||
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()) |
||||
} |
||||
res += fmt.Sprintf("TxOutput:\n") |
||||
for id, value := range tx.TxOutput { |
||||
res += fmt.Sprintf("%v: %v\n", id, value.String()) |
||||
} |
||||
for id, value := range tx.Proofs { |
||||
res += fmt.Sprintf("Proof:\n") |
||||
res += fmt.Sprintf("%v: %v\n", id, value.String()) |
||||
} |
||||
res += fmt.Sprintf("PublicKey: %v\n", hex.EncodeToString(tx.PublicKey[:])) |
||||
res += fmt.Sprintf("Sig: %v\n", hex.EncodeToString(tx.Signature[:])) |
||||
return res |
||||
} |
||||
|
||||
// Serialize return serialized bytes of the transaction.
|
||||
func (tx *Transaction) Serialize() []byte { |
||||
buffer := bytes.NewBuffer([]byte{}) |
||||
buffer.Write(tx.ID[:]) |
||||
for _, value := range tx.TxInput { |
||||
buffer.Write(value.Serialize()) |
||||
} |
||||
for _, value := range tx.TxOutput { |
||||
buffer.Write(value.Serialize()) |
||||
} |
||||
for _, value := range tx.Proofs { |
||||
buffer.Write(value.Serialize()) |
||||
} |
||||
buffer.Write(tx.PublicKey[:]) |
||||
buffer.Write(tx.Signature[:]) |
||||
return buffer.Bytes() |
||||
} |
||||
|
||||
// Serialize return serialized bytes of the TXInput.
|
||||
func (txInput *TXInput) Serialize() []byte { |
||||
buffer := bytes.NewBuffer([]byte{}) |
||||
buffer.Write(txInput.Address[:]) |
||||
|
||||
fourBytes := make([]byte, 4) |
||||
binary.BigEndian.PutUint32(fourBytes, txInput.ShardID) |
||||
buffer.Write(fourBytes) |
||||
|
||||
binary.BigEndian.PutUint32(fourBytes, txInput.PreviousOutPoint.Index) |
||||
buffer.Write(fourBytes) |
||||
|
||||
buffer.Write(txInput.PreviousOutPoint.TxID[:]) |
||||
return buffer.Bytes() |
||||
} |
||||
|
||||
// Serialize return serialized bytes of the TXOutput.
|
||||
func (txOutput *TXOutput) Serialize() []byte { |
||||
buffer := bytes.NewBuffer([]byte{}) |
||||
buffer.Write(txOutput.Address[:]) |
||||
|
||||
fourBytes := make([]byte, 4) |
||||
binary.BigEndian.PutUint32(fourBytes, txOutput.ShardID) |
||||
buffer.Write(fourBytes) |
||||
|
||||
binary.BigEndian.PutUint32(fourBytes, uint32(txOutput.Amount)) // TODO(RJ): make amount a bigInt
|
||||
buffer.Write(fourBytes) |
||||
|
||||
return buffer.Bytes() |
||||
} |
||||
|
||||
// Serialize returns serialized bytes of the CrossShardTxProof.
|
||||
func (proof *CrossShardTxProof) Serialize() []byte { |
||||
buffer := bytes.NewBuffer([]byte{}) |
||||
buffer.Write(proof.TxID[:]) |
||||
buffer.Write(proof.BlockHash[:]) |
||||
for _, value := range proof.TxInput { |
||||
buffer.Write(value.Serialize()) |
||||
} |
||||
if proof.Accept { |
||||
buffer.WriteByte(byte(1)) |
||||
} else { |
||||
buffer.WriteByte(byte(0)) |
||||
} |
||||
return buffer.Bytes() |
||||
} |
@ -1,11 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func TestNewCoinbaseTX(t *testing.T) { |
||||
if cbtx := NewCoinbaseTX(TestAddressOne, genesisCoinbaseData, 0); cbtx == nil { |
||||
t.Errorf("failed to create a coinbase transaction.") |
||||
} |
||||
} |
@ -1,564 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/gob" |
||||
"encoding/hex" |
||||
"errors" |
||||
"fmt" |
||||
"sync" |
||||
|
||||
"github.com/dedis/kyber/sign/schnorr" |
||||
"github.com/harmony-one/harmony/crypto" |
||||
"github.com/harmony-one/harmony/log" |
||||
) |
||||
|
||||
// Vout2AmountMap is a TODO type.
|
||||
type Vout2AmountMap = map[uint32]int |
||||
|
||||
// TXHash2Vout2AmountMap is a TODO type.
|
||||
type TXHash2Vout2AmountMap = map[string]Vout2AmountMap |
||||
|
||||
// UtxoMap is a TODO type.
|
||||
type UtxoMap = map[[20]byte]TXHash2Vout2AmountMap |
||||
|
||||
// UTXOPool stores transactions and balance associated with each address.
|
||||
type UTXOPool struct { |
||||
// Mapping from address to a map of transaction id to a map of the index of output
|
||||
// array in that transaction to that balance.
|
||||
/* |
||||
The 3-d map's structure: |
||||
address - [ |
||||
txID1 - [ |
||||
outputIndex1 - value1 |
||||
outputIndex2 - value2 |
||||
] |
||||
txID2 - [ |
||||
outputIndex1 - value1 |
||||
outputIndex2 - value2 |
||||
] |
||||
] |
||||
*/ |
||||
UtxoMap UtxoMap |
||||
LockedUtxoMap UtxoMap |
||||
ShardID uint32 |
||||
mutex sync.Mutex |
||||
} |
||||
|
||||
// MergeUtxoMap merges the utxoMap into that of the UtxoPool
|
||||
func (utxoPool *UTXOPool) MergeUtxoMap(utxoMap UtxoMap) { |
||||
for address, txHash2Vout2AmountMap := range utxoMap { |
||||
clientTxHashMap, ok := utxoPool.UtxoMap[address] |
||||
if ok { |
||||
for txHash, vout2AmountMap := range txHash2Vout2AmountMap { |
||||
clientVout2AmountMap, ok := clientTxHashMap[txHash] |
||||
if ok { |
||||
for vout, amount := range vout2AmountMap { |
||||
clientVout2AmountMap[vout] = amount |
||||
} |
||||
} else { |
||||
clientTxHashMap[txHash] = vout2AmountMap |
||||
} |
||||
|
||||
} |
||||
} else { |
||||
utxoPool.UtxoMap[address] = txHash2Vout2AmountMap |
||||
} |
||||
} |
||||
} |
||||
|
||||
// GetUtxoMapByAddresses gets the Utxo map for specific addresses
|
||||
func (utxoPool *UTXOPool) GetUtxoMapByAddresses(addresses [][20]byte) UtxoMap { |
||||
result := make(UtxoMap) |
||||
for _, address := range addresses { |
||||
utxos, ok := utxoPool.UtxoMap[address] |
||||
if ok { |
||||
result[address] = utxos |
||||
} |
||||
} |
||||
return result |
||||
} |
||||
|
||||
// VerifyTransactions verifies if a list of transactions valid for this shard.
|
||||
func (utxoPool *UTXOPool) VerifyTransactions(transactions []*Transaction) bool { |
||||
spentTXOs := make(map[[20]byte]map[string]map[uint32]bool) |
||||
if utxoPool != nil { |
||||
for _, tx := range transactions { |
||||
if crossShard, err := utxoPool.VerifyOneTransaction(tx, &spentTXOs); !crossShard && err != nil { |
||||
return false |
||||
} |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
// VerifyStateBlock verifies if the given state block matches the current utxo pool.
|
||||
func (utxoPool *UTXOPool) VerifyStateBlock(stateBlock *Block) bool { |
||||
accountBalanceInUtxoPool := make(map[[20]byte]int) |
||||
|
||||
for address, txHash2Vout2AmountMap := range utxoPool.UtxoMap { |
||||
for _, vout2AmountMap := range txHash2Vout2AmountMap { |
||||
for _, amount := range vout2AmountMap { |
||||
accountBalanceInUtxoPool[address] = accountBalanceInUtxoPool[address] + amount |
||||
} |
||||
} |
||||
} |
||||
for _, transaction := range stateBlock.Transactions { |
||||
for _, txOutput := range transaction.TxOutput { |
||||
if txOutput.ShardID != utxoPool.ShardID { |
||||
return false |
||||
} |
||||
accountBalanceInUtxoPool[txOutput.Address] = accountBalanceInUtxoPool[txOutput.Address] - txOutput.Amount |
||||
} |
||||
} |
||||
|
||||
for _, amount := range accountBalanceInUtxoPool { |
||||
if amount != 0 { |
||||
return false |
||||
} |
||||
} |
||||
return true |
||||
} |
||||
|
||||
// VerifyOneTransaction verifies if a list of transactions valid.
|
||||
// Add another sanity check function (e.g. spending the same utxo) called before this one.
|
||||
func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction, spentTXOs *map[[20]byte]map[string]map[uint32]bool) (crossShard bool, err error) { |
||||
var nilPubKey [32]byte |
||||
// TODO(ricl): remove. just for btc replay.
|
||||
if tx.PublicKey == nilPubKey { |
||||
return false, nil |
||||
} |
||||
if len(tx.Proofs) > 1 { |
||||
return utxoPool.VerifyUnlockTransaction(tx) |
||||
} |
||||
|
||||
if spentTXOs == nil { |
||||
spentTXOs = &map[[20]byte]map[string]map[uint32]bool{} |
||||
} |
||||
inTotal := 0 |
||||
// Calculate the sum of TxInput
|
||||
for _, in := range tx.TxInput { |
||||
// Only check the input for my own shard.
|
||||
if in.ShardID != utxoPool.ShardID { |
||||
crossShard = true |
||||
continue |
||||
} |
||||
|
||||
inTxID := hex.EncodeToString(in.PreviousOutPoint.TxID[:]) |
||||
index := in.PreviousOutPoint.Index |
||||
// Check if the transaction with the address is spent or not.
|
||||
if val, ok := (*spentTXOs)[in.Address][inTxID][index]; ok { |
||||
if val { |
||||
return crossShard, errors.New("TxInput is already spent") |
||||
} |
||||
} |
||||
// Mark the transactions with the address and index spent.
|
||||
if _, ok := (*spentTXOs)[in.Address]; !ok { |
||||
(*spentTXOs)[in.Address] = make(map[string]map[uint32]bool) |
||||
} |
||||
if _, ok := (*spentTXOs)[in.Address][inTxID]; !ok { |
||||
(*spentTXOs)[in.Address][inTxID] = make(map[uint32]bool) |
||||
} |
||||
(*spentTXOs)[in.Address][inTxID][index] = true |
||||
|
||||
// Sum the balance up to the inTotal.
|
||||
utxoPool.mutex.Lock() |
||||
if val, ok := utxoPool.UtxoMap[in.Address][inTxID][index]; ok { |
||||
inTotal += val |
||||
} else { |
||||
utxoPool.mutex.Unlock() |
||||
return crossShard, errors.New("Specified TxInput does not exist in utxo pool") |
||||
} |
||||
utxoPool.mutex.Unlock() |
||||
} |
||||
|
||||
outTotal := 0 |
||||
// Calculate the sum of TxOutput
|
||||
for _, out := range tx.TxOutput { |
||||
outTotal += out.Amount |
||||
|
||||
if out.ShardID != utxoPool.ShardID { |
||||
crossShard = true |
||||
} |
||||
} |
||||
|
||||
// TODO: improve this checking logic
|
||||
if (crossShard && inTotal > outTotal) || (!crossShard && inTotal != outTotal) { |
||||
return crossShard, errors.New("Input and output amount doesn't match") |
||||
} |
||||
|
||||
if inTotal == 0 { |
||||
return false, errors.New("Input amount is 0") // Here crossShard is false, because if there is no business for this shard, it's effectively not crossShard no matter what.
|
||||
} |
||||
|
||||
// Verify the signature
|
||||
pubKey := crypto.Ed25519Curve.Point() |
||||
tempErr := pubKey.UnmarshalBinary(tx.PublicKey[:]) |
||||
if tempErr != nil { |
||||
log.Error("Failed to deserialize public key", "error", tempErr) |
||||
} |
||||
tempErr = schnorr.Verify(crypto.Ed25519Curve, pubKey, tx.GetContentToVerify(), tx.Signature[:]) |
||||
if tempErr != nil { |
||||
log.Error("Failed to verify signature", "error", tempErr, "public key", pubKey, "pubKey in bytes", tx.PublicKey[:]) |
||||
return crossShard, errors.New("Invalid signature") |
||||
} |
||||
return crossShard, nil |
||||
} |
||||
|
||||
// VerifyUnlockTransaction verifies a cross shard transaction that contains proofs for unlock-to-commit/abort.
|
||||
func (utxoPool *UTXOPool) VerifyUnlockTransaction(tx *Transaction) (crossShard bool, err error) { |
||||
err = nil |
||||
crossShard = false // unlock transaction is treated as crossShard=false because it will be finalized now (doesn't need more steps)
|
||||
txInputs := make(map[TXInput]bool) |
||||
for _, curProof := range tx.Proofs { |
||||
for _, txInput := range curProof.TxInput { |
||||
txInputs[txInput] = true |
||||
} |
||||
} |
||||
for _, txInput := range tx.TxInput { |
||||
val, ok := txInputs[txInput] |
||||
if !ok || !val { |
||||
err = errors.New("Invalid unlock transaction: not all proofs are provided for tx inputs") |
||||
} |
||||
} |
||||
return |
||||
} |
||||
|
||||
// Update updates Utxo balances with a list of new transactions.
|
||||
func (utxoPool *UTXOPool) Update(transactions []*Transaction) { |
||||
if utxoPool != nil { |
||||
for _, tx := range transactions { |
||||
utxoPool.UpdateOneTransaction(tx) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// UpdateOneTransaction updates utxoPool in respect to the new Transaction.
|
||||
func (utxoPool *UTXOPool) UpdateOneTransaction(tx *Transaction) { |
||||
isUnlockTx := len(tx.Proofs) > 1 |
||||
unlockToCommit := true |
||||
if isUnlockTx { |
||||
for _, proof := range tx.Proofs { |
||||
if !proof.Accept { |
||||
unlockToCommit = false // if any proof is a rejection, they it's a unlock-to-abort tx. Otherwise, it's unlock-to-commit
|
||||
} |
||||
} |
||||
} |
||||
|
||||
isCrossShard := false |
||||
// check whether it's a cross shard tx.
|
||||
for _, in := range tx.TxInput { |
||||
if in.ShardID != utxoPool.ShardID { |
||||
isCrossShard = true |
||||
break |
||||
} |
||||
} |
||||
for _, out := range tx.TxOutput { |
||||
if out.ShardID != utxoPool.ShardID { |
||||
isCrossShard = true |
||||
break |
||||
} |
||||
} |
||||
|
||||
isValidCrossShard := true |
||||
if isCrossShard { |
||||
// Check whether for this cross shard transaction is valid or not.
|
||||
for _, in := range tx.TxInput { |
||||
// Only check the input for my own shard.
|
||||
if in.ShardID != utxoPool.ShardID { |
||||
continue |
||||
} |
||||
inTxID := hex.EncodeToString(in.PreviousOutPoint.TxID[:]) |
||||
if _, ok := utxoPool.UtxoMap[in.Address][inTxID][in.PreviousOutPoint.Index]; !ok { |
||||
isValidCrossShard = false |
||||
} |
||||
} |
||||
} |
||||
|
||||
utxoPool.mutex.Lock() |
||||
defer utxoPool.mutex.Unlock() |
||||
if utxoPool != nil { |
||||
txID := hex.EncodeToString(tx.ID[:]) |
||||
|
||||
// Remove
|
||||
if !isUnlockTx { |
||||
if isValidCrossShard { |
||||
for _, in := range tx.TxInput { |
||||
// Only check the input for my own shard.
|
||||
if in.ShardID != utxoPool.ShardID { |
||||
continue |
||||
} |
||||
|
||||
// NOTE: for the locking phase of cross tx, the utxo is simply removed from the pool.
|
||||
inTxID := hex.EncodeToString(in.PreviousOutPoint.TxID[:]) |
||||
value := utxoPool.UtxoMap[in.Address][inTxID][in.PreviousOutPoint.Index] |
||||
utxoPool.DeleteOneUtxo(in.Address, inTxID, in.PreviousOutPoint.Index) |
||||
if isCrossShard { |
||||
// put the delete (locked) utxo into a separate locked utxo pool
|
||||
inTxID := hex.EncodeToString(in.PreviousOutPoint.TxID[:]) |
||||
if _, ok := utxoPool.LockedUtxoMap[in.Address]; !ok { |
||||
utxoPool.LockedUtxoMap[in.Address] = make(TXHash2Vout2AmountMap) |
||||
} |
||||
if _, ok := utxoPool.LockedUtxoMap[in.Address][inTxID]; !ok { |
||||
utxoPool.LockedUtxoMap[in.Address][inTxID] = make(Vout2AmountMap) |
||||
} |
||||
utxoPool.LockedUtxoMap[in.Address][inTxID][in.PreviousOutPoint.Index] = value |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Update
|
||||
if !isCrossShard || isUnlockTx { |
||||
if !unlockToCommit { |
||||
// unlock-to-abort, bring back (unlock) the utxo input
|
||||
for _, in := range tx.TxInput { |
||||
// Only unlock the input for my own shard.
|
||||
if in.ShardID != utxoPool.ShardID { |
||||
continue |
||||
} |
||||
|
||||
inTxID := hex.EncodeToString(in.PreviousOutPoint.TxID[:]) |
||||
|
||||
if utxoPool.LockedUtxoExists(in.Address, inTxID, in.PreviousOutPoint.Index) { |
||||
// bring back the locked (removed) utxo
|
||||
if _, ok := utxoPool.UtxoMap[in.Address]; !ok { |
||||
utxoPool.UtxoMap[in.Address] = make(TXHash2Vout2AmountMap) |
||||
utxoPool.UtxoMap[in.Address][inTxID] = make(Vout2AmountMap) |
||||
} |
||||
if _, ok := utxoPool.UtxoMap[in.Address][inTxID]; !ok { |
||||
utxoPool.UtxoMap[in.Address][inTxID] = make(Vout2AmountMap) |
||||
} |
||||
value := utxoPool.LockedUtxoMap[in.Address][inTxID][in.PreviousOutPoint.Index] |
||||
utxoPool.UtxoMap[in.Address][inTxID][in.PreviousOutPoint.Index] = value |
||||
|
||||
utxoPool.DeleteOneLockedUtxo(in.Address, inTxID, in.PreviousOutPoint.Index) |
||||
} |
||||
} |
||||
} else { |
||||
// normal utxo output update
|
||||
for index, out := range tx.TxOutput { |
||||
// Only check the input for my own shard.
|
||||
if out.ShardID != utxoPool.ShardID { |
||||
continue |
||||
} |
||||
if _, ok := utxoPool.UtxoMap[out.Address]; !ok { |
||||
utxoPool.UtxoMap[out.Address] = make(TXHash2Vout2AmountMap) |
||||
utxoPool.UtxoMap[out.Address][txID] = make(Vout2AmountMap) |
||||
} |
||||
if _, ok := utxoPool.UtxoMap[out.Address][txID]; !ok { |
||||
utxoPool.UtxoMap[out.Address][txID] = make(Vout2AmountMap) |
||||
} |
||||
utxoPool.UtxoMap[out.Address][txID][uint32(index)] = out.Amount |
||||
} |
||||
if isUnlockTx { // for unlock-to-commit transaction, also need to delete the locked utxo
|
||||
for _, in := range tx.TxInput { |
||||
// Only unlock the input for my own shard.
|
||||
if in.ShardID != utxoPool.ShardID { |
||||
continue |
||||
} |
||||
|
||||
inTxID := hex.EncodeToString(in.PreviousOutPoint.TxID[:]) |
||||
utxoPool.DeleteOneLockedUtxo(in.Address, inTxID, in.PreviousOutPoint.Index) |
||||
} |
||||
} |
||||
} |
||||
} // If it's a cross shard locking Tx, then don't update so the input UTXOs are locked (removed), and the money is not spendable until unlock-to-commit or unlock-to-abort
|
||||
} |
||||
} |
||||
|
||||
// VerifyOneTransactionAndUpdate verifies and update a valid transaction.
|
||||
// Return false if the transaction is not valid.
|
||||
func (utxoPool *UTXOPool) VerifyOneTransactionAndUpdate(tx *Transaction) bool { |
||||
if _, err := utxoPool.VerifyOneTransaction(tx, nil); err == nil { |
||||
utxoPool.UpdateOneTransaction(tx) |
||||
return true |
||||
} |
||||
return false |
||||
} |
||||
|
||||
// VerifyAndUpdate verifies a list of transactions and update utxoPool.
|
||||
func (utxoPool *UTXOPool) VerifyAndUpdate(transactions []*Transaction) bool { |
||||
if utxoPool.VerifyTransactions(transactions) { |
||||
utxoPool.Update(transactions) |
||||
return true |
||||
} |
||||
return false |
||||
} |
||||
|
||||
// CreateUTXOPoolFromGenesisBlock a Utxo pool from a genesis block.
|
||||
func CreateUTXOPoolFromGenesisBlock(block *Block) *UTXOPool { |
||||
shardID := block.ShardID |
||||
var utxoPool UTXOPool |
||||
utxoPool.UtxoMap = make(UtxoMap) |
||||
utxoPool.LockedUtxoMap = make(UtxoMap) |
||||
for _, tx := range block.Transactions { |
||||
txID := hex.EncodeToString(tx.ID[:]) |
||||
for index, out := range tx.TxOutput { |
||||
_, ok := utxoPool.UtxoMap[out.Address] |
||||
if !ok { |
||||
utxoPool.UtxoMap[out.Address] = make(TXHash2Vout2AmountMap) |
||||
} |
||||
|
||||
_, ok = utxoPool.UtxoMap[out.Address][txID] |
||||
if !ok { |
||||
utxoPool.UtxoMap[out.Address][txID] = make(Vout2AmountMap) |
||||
} |
||||
utxoPool.UtxoMap[out.Address][txID][uint32(index)] = out.Amount |
||||
} |
||||
} |
||||
utxoPool.ShardID = shardID |
||||
return &utxoPool |
||||
} |
||||
|
||||
// SelectTransactionsForNewBlock returns a list of index of valid transactions for the new block.
|
||||
func (utxoPool *UTXOPool) SelectTransactionsForNewBlock(transactions []*Transaction, maxNumTxs int) ([]*Transaction, []*Transaction, []*Transaction, []*CrossShardTxAndProof) { |
||||
selected, unselected, invalid, crossShardTxs := []*Transaction{}, []*Transaction{}, []*Transaction{}, []*CrossShardTxAndProof{} |
||||
spentTXOs := make(map[[20]byte]map[string]map[uint32]bool) |
||||
for _, tx := range transactions { |
||||
crossShard, err := utxoPool.VerifyOneTransaction(tx, &spentTXOs) |
||||
|
||||
if len(selected) < maxNumTxs { |
||||
//if err != nil && rand.Intn(10) < 1 {
|
||||
// log.Warn("Invalid Transaction", "Reason", err)
|
||||
//}
|
||||
if err == nil || crossShard { |
||||
if crossShard { |
||||
proof := CrossShardTxProof{Accept: err == nil, TxID: tx.ID, TxInput: getShardTxInput(tx, utxoPool.ShardID)} |
||||
txAndProof := CrossShardTxAndProof{tx, &proof} |
||||
crossShardTxs = append(crossShardTxs, &txAndProof) |
||||
tx.Proofs = append(tx.Proofs, proof) |
||||
} |
||||
selected = append(selected, tx) |
||||
} else { |
||||
invalid = append(invalid, tx) |
||||
} |
||||
} else { |
||||
unselected = append(unselected, tx) |
||||
} |
||||
} |
||||
return selected, unselected, invalid, crossShardTxs |
||||
} |
||||
|
||||
func getShardTxInput(transaction *Transaction, shardID uint32) []TXInput { |
||||
result := []TXInput{} |
||||
for _, txInput := range transaction.TxInput { |
||||
if txInput.ShardID == shardID { |
||||
result = append(result, txInput) |
||||
} |
||||
} |
||||
return result |
||||
} |
||||
|
||||
// DeleteOneUtxo deletes TODO.
|
||||
func (utxoPool *UTXOPool) DeleteOneUtxo(address [20]byte, txID string, index uint32) { |
||||
delete(utxoPool.UtxoMap[address][txID], index) |
||||
if len(utxoPool.UtxoMap[address][txID]) == 0 { |
||||
delete(utxoPool.UtxoMap[address], txID) |
||||
if len(utxoPool.UtxoMap[address]) == 0 { |
||||
delete(utxoPool.UtxoMap, address) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// LockedUtxoExists checks if the looked utxo exists.
|
||||
func (utxoPool *UTXOPool) LockedUtxoExists(address [20]byte, txID string, index uint32) bool { |
||||
_, ok := utxoPool.LockedUtxoMap[address] |
||||
if !ok { |
||||
return false |
||||
} |
||||
_, ok = utxoPool.LockedUtxoMap[address][txID] |
||||
if !ok { |
||||
return false |
||||
} |
||||
_, ok = utxoPool.LockedUtxoMap[address][txID][index] |
||||
return ok |
||||
} |
||||
|
||||
// DeleteOneLockedUtxo deletes one balance item of UTXOPool and clean up if possible.
|
||||
func (utxoPool *UTXOPool) DeleteOneLockedUtxo(address [20]byte, txID string, index uint32) { |
||||
delete(utxoPool.LockedUtxoMap[address][txID], index) |
||||
if len(utxoPool.LockedUtxoMap[address][txID]) == 0 { |
||||
delete(utxoPool.LockedUtxoMap[address], txID) |
||||
if len(utxoPool.LockedUtxoMap[address]) == 0 { |
||||
delete(utxoPool.LockedUtxoMap, address) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// CleanUp cleans up UTXOPool.
|
||||
func (utxoPool *UTXOPool) CleanUp() { |
||||
for address, txMap := range utxoPool.UtxoMap { |
||||
for txid, outIndexes := range txMap { |
||||
for index, value := range outIndexes { |
||||
if value == 0 { |
||||
delete(utxoPool.UtxoMap[address][txid], index) |
||||
} |
||||
} |
||||
if len(utxoPool.UtxoMap[address][txid]) == 0 { |
||||
delete(utxoPool.UtxoMap[address], txid) |
||||
} |
||||
} |
||||
if len(utxoPool.UtxoMap[address]) == 0 { |
||||
delete(utxoPool.UtxoMap, address) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Used for debugging.
|
||||
func (utxoPool *UTXOPool) String() string { |
||||
return printUtxos(&utxoPool.UtxoMap) |
||||
} |
||||
|
||||
// StringOfLockedUtxos is used for debugging.
|
||||
func (utxoPool *UTXOPool) StringOfLockedUtxos() string { |
||||
return printUtxos(&utxoPool.LockedUtxoMap) |
||||
} |
||||
|
||||
func printUtxos(utxos *UtxoMap) string { |
||||
res := "" |
||||
for address, v1 := range *utxos { |
||||
for txid, v2 := range v1 { |
||||
for index, value := range v2 { |
||||
res += fmt.Sprintf("address: %v, tx id: %v, index: %v, value: %v\n", address, txid, index, value) |
||||
|
||||
} |
||||
} |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// GetSizeInByteOfUtxoMap gets a snapshot copy of the current pool
|
||||
func (utxoPool *UTXOPool) GetSizeInByteOfUtxoMap() int { |
||||
utxoPool.mutex.Lock() |
||||
defer utxoPool.mutex.Unlock() |
||||
byteBuffer := bytes.NewBuffer([]byte{}) |
||||
encoder := gob.NewEncoder(byteBuffer) |
||||
encoder.Encode(utxoPool.UtxoMap) |
||||
return len(byteBuffer.Bytes()) |
||||
} |
||||
|
||||
// CountNumOfUtxos counts the total number of utxos in a pool.
|
||||
func (utxoPool *UTXOPool) CountNumOfUtxos() int { |
||||
return countNumOfUtxos(&utxoPool.UtxoMap) |
||||
} |
||||
|
||||
// CountNumOfLockedUtxos counts the total number of locked utxos in a pool.
|
||||
func (utxoPool *UTXOPool) CountNumOfLockedUtxos() int { |
||||
return countNumOfUtxos(&utxoPool.LockedUtxoMap) |
||||
} |
||||
|
||||
func countNumOfUtxos(utxos *UtxoMap) int { |
||||
countAll := 0 |
||||
for _, utxoMap := range *utxos { |
||||
for txIDStr, val := range utxoMap { |
||||
_, err := hex.DecodeString(txIDStr) |
||||
if err != nil { |
||||
continue |
||||
} |
||||
|
||||
countAll += len(val) |
||||
} |
||||
} |
||||
return countAll |
||||
} |
@ -1,68 +0,0 @@ |
||||
package blockchain |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func TestVerifyOneTransactionAndUpdate(t *testing.T) { |
||||
bc := CreateBlockchain(TestAddressOne, 0) |
||||
utxoPool := CreateUTXOPoolFromGenesisBlock(bc.Blocks[0]) |
||||
|
||||
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressThree, 3, 0) |
||||
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressTwo, 100, 0) |
||||
|
||||
tx := bc.NewUTXOTransaction(PriKeyOne, TestAddressOne, TestAddressFour, 10, 0) |
||||
if tx == nil { |
||||
t.Error("failed to create a new transaction.") |
||||
} |
||||
|
||||
if _, err := utxoPool.VerifyOneTransaction(tx, nil); err != nil { |
||||
t.Error("failed to verify a valid transaction.") |
||||
} |
||||
utxoPool.VerifyOneTransactionAndUpdate(tx) |
||||
} |
||||
|
||||
func TestVerifyOneTransactionFail(t *testing.T) { |
||||
bc := CreateBlockchain(TestAddressOne, 0) |
||||
utxoPool := CreateUTXOPoolFromGenesisBlock(bc.Blocks[0]) |
||||
|
||||
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressThree, 3, 0) |
||||
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressTwo, 100, 0) |
||||
|
||||
tx := bc.NewUTXOTransaction(PriKeyOne, TestAddressOne, TestAddressFour, 10, 0) |
||||
if tx == nil { |
||||
t.Error("failed to create a new transaction.") |
||||
} |
||||
|
||||
tx.TxInput = append(tx.TxInput, tx.TxInput[0]) |
||||
if _, err := utxoPool.VerifyOneTransaction(tx, nil); err == nil { |
||||
t.Error("Tx with multiple identical TxInput shouldn't be valid") |
||||
} |
||||
} |
||||
|
||||
func TestDeleteOneBalanceItem(t *testing.T) { |
||||
bc := CreateBlockchain(TestAddressOne, 0) |
||||
utxoPool := CreateUTXOPoolFromGenesisBlock(bc.Blocks[0]) |
||||
|
||||
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressThree, 3, 0) |
||||
bc.AddNewUserTransfer(utxoPool, PriKeyThree, TestAddressThree, TestAddressTwo, 3, 0) |
||||
|
||||
if _, ok := utxoPool.UtxoMap[TestAddressThree]; ok { |
||||
t.Errorf("alok should not be contained in the balance map") |
||||
} |
||||
} |
||||
|
||||
func TestCleanUp(t *testing.T) { |
||||
var utxoPool UTXOPool |
||||
utxoPool.UtxoMap = make(UtxoMap) |
||||
utxoPool.UtxoMap[TestAddressOne] = make(TXHash2Vout2AmountMap) |
||||
utxoPool.UtxoMap[TestAddressTwo] = TXHash2Vout2AmountMap{ |
||||
"abcd": { |
||||
0: 1, |
||||
}, |
||||
} |
||||
utxoPool.CleanUp() |
||||
if _, ok := utxoPool.UtxoMap[TestAddressOne]; ok { |
||||
t.Errorf("minh should not be contained in the balance map") |
||||
} |
||||
} |
@ -1,214 +0,0 @@ |
||||
package txgen |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"encoding/hex" |
||||
"math/rand" |
||||
|
||||
"github.com/harmony-one/harmony/blockchain" |
||||
"github.com/harmony-one/harmony/client" |
||||
"github.com/harmony-one/harmony/crypto/pki" |
||||
"github.com/harmony-one/harmony/log" |
||||
"github.com/harmony-one/harmony/node" |
||||
) |
||||
|
||||
// TxInfo is the transaction info.
|
||||
type TxInfo struct { |
||||
// Global Input
|
||||
shardID int |
||||
dataNodes []*node.Node |
||||
// Temp Input
|
||||
id [32]byte |
||||
index uint32 |
||||
value int |
||||
address [20]byte |
||||
// Output
|
||||
txs []*blockchain.Transaction |
||||
crossTxs []*blockchain.Transaction |
||||
txCount int |
||||
} |
||||
|
||||
// GenerateSimulatedTransactions generates at most "maxNumTxs" number of simulated transactions based on the current UtxoPools of all shards.
|
||||
// The transactions are generated by going through the existing utxos and
|
||||
// randomly select a subset of them as the input for each new transaction. The output
|
||||
// address of the new transaction are randomly selected from [0 - N), where N is the total number of fake addresses.
|
||||
//
|
||||
// When crossShard=true, besides the selected utxo input, select another valid utxo as input from the same address in a second shard.
|
||||
// Similarly, generate another utxo output in that second shard.
|
||||
//
|
||||
// NOTE: the genesis block should contain N coinbase transactions which add
|
||||
// token (1000) to each address in [0 - N). See node.AddTestingAddresses()
|
||||
//
|
||||
// Params:
|
||||
// subsetID - the which subset of the utxo to work on (used to select addresses)
|
||||
// shardID - the shardID for current shard
|
||||
// dataNodes - nodes containing utxopools of all shards
|
||||
// Returns:
|
||||
// all single-shard txs
|
||||
// all cross-shard txs
|
||||
func GenerateSimulatedTransactions(subsetID, numSubset int, shardID int, dataNodes []*node.Node, setting Settings) ([]*blockchain.Transaction, []*blockchain.Transaction) { |
||||
/* |
||||
UTXO map structure: |
||||
address - [ |
||||
txID1 - [ |
||||
outputIndex1 - value1 |
||||
outputIndex2 - value2 |
||||
] |
||||
txID2 - [ |
||||
outputIndex1 - value1 |
||||
outputIndex2 - value2 |
||||
] |
||||
] |
||||
*/ |
||||
|
||||
txInfo := TxInfo{} |
||||
txInfo.shardID = shardID |
||||
txInfo.dataNodes = dataNodes |
||||
txInfo.txCount = 0 |
||||
|
||||
UTXOLOOP: |
||||
// Loop over all addresses
|
||||
for address, txMap := range dataNodes[shardID].UtxoPool.UtxoMap { |
||||
if int(binary.BigEndian.Uint32(address[:]))%numSubset == subsetID%numSubset { // Work on one subset of utxo at a time
|
||||
txInfo.address = address |
||||
// Loop over all txIDs for the address
|
||||
for txIDStr, utxoMap := range txMap { |
||||
// Parse TxId
|
||||
id, err := hex.DecodeString(txIDStr) |
||||
if err != nil { |
||||
continue |
||||
} |
||||
copy(txInfo.id[:], id[:]) |
||||
|
||||
// Loop over all utxos for the txID
|
||||
utxoSize := len(utxoMap) |
||||
batchSize := utxoSize / numSubset |
||||
i := subsetID % numSubset |
||||
counter := 0 |
||||
for index, value := range utxoMap { |
||||
counter++ |
||||
if batchSize*i < counter && counter > batchSize*(i+1) { |
||||
continue |
||||
} |
||||
txInfo.index = index |
||||
txInfo.value = value |
||||
|
||||
randNum := rand.Intn(100) |
||||
|
||||
subsetRatio := 100 // / numSubset
|
||||
if randNum < subsetRatio { // Sample based on batch size
|
||||
if setting.CrossShard && randNum < subsetRatio*setting.CrossShardRatio/100 { // 30% cross shard transactions: add another txinput from another shard
|
||||
generateCrossShardTx(&txInfo, setting) |
||||
} else { |
||||
generateSingleShardTx(&txInfo, setting) |
||||
} |
||||
if txInfo.txCount >= setting.MaxNumTxsPerBatch { |
||||
break UTXOLOOP |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
log.Info("UTXO CLIENT", "numUtxo", dataNodes[shardID].UtxoPool.CountNumOfUtxos(), "shardID", shardID) |
||||
log.Debug("[Generator] generated transations", "single-shard", len(txInfo.txs), "cross-shard", len(txInfo.crossTxs)) |
||||
return txInfo.txs, txInfo.crossTxs |
||||
} |
||||
|
||||
func generateCrossShardTx(txInfo *TxInfo, setting Settings) { |
||||
nodeShardID := txInfo.dataNodes[txInfo.shardID].Consensus.ShardID |
||||
crossShardID := nodeShardID |
||||
// a random shard to spend money to
|
||||
for { |
||||
crossShardID = uint32(rand.Intn(len(txInfo.dataNodes))) |
||||
if crossShardID != nodeShardID { |
||||
break |
||||
} |
||||
} |
||||
|
||||
//crossShardNode := txInfo.dataNodes[crossShardID]
|
||||
//crossShardUtxosMap := crossShardNode.UtxoPool.UtxoMap[txInfo.address]
|
||||
//
|
||||
//// Get the cross shard utxo from another shard
|
||||
//var crossTxin *blockchain.TXInput
|
||||
//crossUtxoValue := 0
|
||||
//// Loop over utxos for the same address from the other shard and use the first utxo as the second cross tx input
|
||||
//for crossTxIdStr, crossShardUtxos := range crossShardUtxosMap {
|
||||
// // Parse TxId
|
||||
// id, err := hex.DecodeString(crossTxIdStr)
|
||||
// if err != nil {
|
||||
// continue
|
||||
// }
|
||||
// crossTxId := [32]byte{}
|
||||
// copy(crossTxId[:], id[:])
|
||||
//
|
||||
// for crossShardIndex, crossShardValue := range crossShardUtxos {
|
||||
// crossUtxoValue = crossShardValue
|
||||
// crossTxin = blockchain.NewTXInput(blockchain.NewOutPoint(&crossTxId, crossShardIndex), txInfo.address, crossShardID)
|
||||
// break
|
||||
// }
|
||||
// if crossTxin != nil {
|
||||
// break
|
||||
// }
|
||||
//}
|
||||
|
||||
// Add the utxo from current shard
|
||||
txIn := blockchain.NewTXInput(blockchain.NewOutPoint(&txInfo.id, txInfo.index), txInfo.address, nodeShardID) |
||||
txInputs := []blockchain.TXInput{*txIn} |
||||
|
||||
// Add the utxo from the other shard, if any
|
||||
//if crossTxin != nil { // This means the ratio of cross shard tx could be lower than 1/3
|
||||
// txInputs = append(txInputs, *crossTxin)
|
||||
//}
|
||||
|
||||
// Spend the utxo from the current shard to a random address in [0 - N)
|
||||
txout := blockchain.TXOutput{Amount: txInfo.value, Address: pki.GetAddressFromInt(rand.Intn(setting.NumOfAddress) + 1), ShardID: crossShardID} |
||||
|
||||
txOutputs := []blockchain.TXOutput{txout} |
||||
|
||||
// Spend the utxo from the other shard, if any, to a random address in [0 - N)
|
||||
//if crossTxin != nil {
|
||||
// crossTxout := blockchain.TXOutput{Amount: crossUtxoValue, Address: pki.GetAddressFromInt(rand.Intn(setting.numOfAddress) + 1), ShardID: crossShardID}
|
||||
// txOutputs = append(txOutputs, crossTxout)
|
||||
//}
|
||||
|
||||
// Construct the new transaction
|
||||
tx := blockchain.Transaction{ID: [32]byte{}, TxInput: txInputs, TxOutput: txOutputs, Proofs: nil} |
||||
|
||||
priKeyInt, ok := client.LookUpIntPriKey(txInfo.address) |
||||
if ok { |
||||
tx.PublicKey = pki.GetBytesFromPublicKey(pki.GetPublicKeyFromScalar(pki.GetPrivateKeyScalarFromInt(priKeyInt))) |
||||
|
||||
tx.SetID() // TODO(RJ): figure out the correct way to set Tx ID.
|
||||
tx.Sign(pki.GetPrivateKeyScalarFromInt(priKeyInt)) |
||||
} else { |
||||
log.Error("Failed to look up the corresponding private key from address", "Address", txInfo.address) |
||||
return |
||||
} |
||||
|
||||
txInfo.crossTxs = append(txInfo.crossTxs, &tx) |
||||
txInfo.txCount++ |
||||
} |
||||
|
||||
func generateSingleShardTx(txInfo *TxInfo, setting Settings) { |
||||
nodeShardID := txInfo.dataNodes[txInfo.shardID].Consensus.ShardID |
||||
// Add the utxo as new tx input
|
||||
txin := blockchain.NewTXInput(blockchain.NewOutPoint(&txInfo.id, txInfo.index), txInfo.address, nodeShardID) |
||||
|
||||
// Spend the utxo to a random address in [0 - N)
|
||||
txout := blockchain.TXOutput{Amount: txInfo.value, Address: pki.GetAddressFromInt(rand.Intn(setting.NumOfAddress) + 1), ShardID: nodeShardID} |
||||
tx := blockchain.Transaction{ID: [32]byte{}, TxInput: []blockchain.TXInput{*txin}, TxOutput: []blockchain.TXOutput{txout}, Proofs: nil} |
||||
|
||||
priKeyInt, ok := client.LookUpIntPriKey(txInfo.address) |
||||
if ok { |
||||
tx.PublicKey = pki.GetBytesFromPublicKey(pki.GetPublicKeyFromScalar(pki.GetPrivateKeyScalarFromInt(priKeyInt))) |
||||
tx.SetID() // TODO(RJ): figure out the correct way to set Tx ID.
|
||||
tx.Sign(pki.GetPrivateKeyScalarFromInt(priKeyInt)) |
||||
} else { |
||||
log.Error("Failed to look up the corresponding private key from address", "Address", txInfo.address) |
||||
return |
||||
} |
||||
|
||||
txInfo.txs = append(txInfo.txs, &tx) |
||||
txInfo.txCount++ |
||||
} |
@ -1,394 +0,0 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"crypto/rand" |
||||
"encoding/hex" |
||||
"errors" |
||||
"flag" |
||||
"fmt" |
||||
"log" |
||||
"strings" |
||||
|
||||
"github.com/harmony-one/harmony/p2p/p2pimpl" |
||||
|
||||
"io" |
||||
"io/ioutil" |
||||
math_rand "math/rand" |
||||
"os" |
||||
"strconv" |
||||
"time" |
||||
|
||||
"github.com/dedis/kyber" |
||||
"github.com/harmony-one/harmony/blockchain" |
||||
"github.com/harmony-one/harmony/client" |
||||
client_config "github.com/harmony-one/harmony/client/config" |
||||
"github.com/harmony-one/harmony/crypto" |
||||
"github.com/harmony-one/harmony/crypto/pki" |
||||
"github.com/harmony-one/harmony/node" |
||||
"github.com/harmony-one/harmony/p2p" |
||||
proto_node "github.com/harmony-one/harmony/proto/node" |
||||
"github.com/harmony-one/harmony/utils" |
||||
) |
||||
|
||||
func main() { |
||||
// Account subcommands
|
||||
accountImportCommand := flag.NewFlagSet("import", flag.ExitOnError) |
||||
accountImportPtr := accountImportCommand.String("privateKey", "", "Specify the private key to import") |
||||
|
||||
// Transfer subcommands
|
||||
transferCommand := flag.NewFlagSet("transfer", flag.ExitOnError) |
||||
transferSenderPtr := transferCommand.String("sender", "0", "Specify the sender account address or index") |
||||
transferReceiverPtr := transferCommand.String("receiver", "", "Specify the receiver account") |
||||
transferAmountPtr := transferCommand.Int("amount", 0, "Specify the amount to transfer") |
||||
|
||||
// Verify that a subcommand has been provided
|
||||
// os.Arg[0] is the main command
|
||||
// os.Arg[1] will be the subcommand
|
||||
if len(os.Args) < 2 { |
||||
fmt.Println("account or transfer subcommand is required") |
||||
os.Exit(1) |
||||
} |
||||
|
||||
// Switch on the subcommand
|
||||
switch os.Args[1] { |
||||
case "account": |
||||
switch os.Args[2] { |
||||
case "new": |
||||
randomBytes := [32]byte{} |
||||
_, err := io.ReadFull(rand.Reader, randomBytes[:]) |
||||
|
||||
if err != nil { |
||||
fmt.Println("Failed to create a new private key...") |
||||
return |
||||
} |
||||
priKey := crypto.Ed25519Curve.Scalar().SetBytes(randomBytes[:]) |
||||
priKeyBytes, err := priKey.MarshalBinary() |
||||
if err != nil { |
||||
panic("Failed to serialize the private key") |
||||
} |
||||
pubKey := pki.GetPublicKeyFromScalar(priKey) |
||||
address := pki.GetAddressFromPublicKey(pubKey) |
||||
StorePrivateKey(priKeyBytes) |
||||
fmt.Printf("New account created:\nAddress: {%x}\n", address) |
||||
case "list": |
||||
for i, address := range ReadAddresses() { |
||||
fmt.Printf("Account %d:\n {%x}\n", i+1, address) |
||||
} |
||||
case "clearAll": |
||||
ClearKeystore() |
||||
fmt.Println("All existing accounts deleted...") |
||||
case "import": |
||||
accountImportCommand.Parse(os.Args[3:]) |
||||
priKey := *accountImportPtr |
||||
if priKey == "" { |
||||
fmt.Println("Error: --privateKey is required") |
||||
return |
||||
} |
||||
if !accountImportCommand.Parsed() { |
||||
fmt.Println("Failed to parse flags") |
||||
} |
||||
priKeyBytes, err := hex.DecodeString(priKey) |
||||
if err != nil { |
||||
panic("Failed to parse the private key into bytes") |
||||
} |
||||
StorePrivateKey(priKeyBytes) |
||||
fmt.Println("Private key imported...") |
||||
case "showBalance": |
||||
walletNode := CreateWalletServerNode() |
||||
go walletNode.StartServer() |
||||
|
||||
shardUtxoMap, err := FetchUtxos(ReadAddresses(), walletNode) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
} |
||||
PrintUtxoBalance(shardUtxoMap) |
||||
case "test": |
||||
// Testing code
|
||||
priKey := pki.GetPrivateKeyScalarFromInt(444) |
||||
address := pki.GetAddressFromPrivateKey(priKey) |
||||
priKeyBytes, err := priKey.MarshalBinary() |
||||
if err != nil { |
||||
panic("Failed to deserialize private key scalar.") |
||||
} |
||||
fmt.Printf("Private Key :\n {%x}\n", priKeyBytes) |
||||
fmt.Printf("Address :\n {%x}\n", address) |
||||
} |
||||
case "transfer": |
||||
transferCommand.Parse(os.Args[2:]) |
||||
if !transferCommand.Parsed() { |
||||
fmt.Println("Failed to parse flags") |
||||
} |
||||
sender := *transferSenderPtr |
||||
receiver := *transferReceiverPtr |
||||
amount := *transferAmountPtr |
||||
|
||||
if amount <= 0 { |
||||
fmt.Println("Please specify positive amount to transfer") |
||||
} |
||||
priKeys := ReadPrivateKeys() |
||||
if len(priKeys) == 0 { |
||||
fmt.Println("No imported account to use.") |
||||
return |
||||
} |
||||
senderIndex, err := strconv.Atoi(sender) |
||||
senderAddress := "" |
||||
addresses := ReadAddresses() |
||||
if err != nil { |
||||
senderIndex = -1 |
||||
for i, address := range addresses { |
||||
if fmt.Sprintf("%x", address) == senderAddress { |
||||
senderIndex = i |
||||
break |
||||
} |
||||
} |
||||
if senderIndex == -1 { |
||||
fmt.Println("The specified sender account is not imported yet.") |
||||
break |
||||
} |
||||
} |
||||
if senderIndex >= len(priKeys) { |
||||
fmt.Println("Sender account index out of bounds.") |
||||
return |
||||
} |
||||
receiverAddress, err := hex.DecodeString(receiver) |
||||
if err != nil || len(receiverAddress) != 20 { |
||||
fmt.Println("The receiver address is not a valid.") |
||||
return |
||||
} |
||||
|
||||
// Generate transaction
|
||||
trimmedReceiverAddress := [20]byte{} |
||||
copy(trimmedReceiverAddress[:], receiverAddress[:20]) |
||||
|
||||
senderPriKey := priKeys[senderIndex] |
||||
senderAddressBytes := pki.GetAddressFromPrivateKey(senderPriKey) |
||||
|
||||
// Start client server
|
||||
walletNode := CreateWalletServerNode() |
||||
go walletNode.StartServer() |
||||
|
||||
shardUtxoMap, err := FetchUtxos([][20]byte{senderAddressBytes}, walletNode) |
||||
if err != nil { |
||||
fmt.Printf("Failed to fetch utxos: %s\n", err) |
||||
} |
||||
|
||||
cummulativeBalance := 0 |
||||
txInputs := []blockchain.TXInput{} |
||||
LOOP: |
||||
for shardID, utxoMap := range shardUtxoMap { |
||||
for txID, vout2AmountMap := range utxoMap[senderAddressBytes] { |
||||
txIDBytes, err := utils.Get32BytesFromString(txID) |
||||
if err != nil { |
||||
fmt.Println("Failed to parse txID") |
||||
continue |
||||
} |
||||
for voutIndex, utxoAmount := range vout2AmountMap { |
||||
cummulativeBalance += utxoAmount |
||||
txIn := blockchain.NewTXInput(blockchain.NewOutPoint(&txIDBytes, voutIndex), senderAddressBytes, shardID) |
||||
txInputs = append(txInputs, *txIn) |
||||
if cummulativeBalance >= amount { |
||||
break LOOP |
||||
} |
||||
} |
||||
} |
||||
} |
||||
txout := blockchain.TXOutput{Amount: amount, Address: trimmedReceiverAddress, ShardID: uint32(math_rand.Intn(len(shardUtxoMap)))} |
||||
|
||||
txOutputs := []blockchain.TXOutput{txout} |
||||
if cummulativeBalance > amount { |
||||
changeTxOut := blockchain.TXOutput{Amount: cummulativeBalance - amount, Address: senderAddressBytes, ShardID: uint32(math_rand.Intn(len(shardUtxoMap)))} |
||||
txOutputs = append(txOutputs, changeTxOut) |
||||
} |
||||
|
||||
tx := blockchain.Transaction{ID: [32]byte{}, PublicKey: pki.GetBytesFromPublicKey(pki.GetPublicKeyFromScalar(senderPriKey)), TxInput: txInputs, TxOutput: txOutputs, Proofs: nil} |
||||
tx.SetID() // TODO(RJ): figure out the correct way to set Tx ID.
|
||||
tx.Sign(senderPriKey) |
||||
|
||||
pubKey := crypto.Ed25519Curve.Point() |
||||
err = pubKey.UnmarshalBinary(tx.PublicKey[:]) |
||||
if err != nil { |
||||
fmt.Println("Failed to deserialize public key", "error", err) |
||||
} |
||||
|
||||
err = ExecuteTransaction(tx, walletNode) |
||||
|
||||
if err != nil { |
||||
fmt.Println(err) |
||||
} else { |
||||
fmt.Println("Transaction submitted successfully") |
||||
} |
||||
default: |
||||
flag.PrintDefaults() |
||||
os.Exit(1) |
||||
} |
||||
} |
||||
|
||||
func getShardIDToLeaderMap() map[uint32]p2p.Peer { |
||||
// TODO(ricl): Later use data.harmony.one for API.
|
||||
str, _ := client.DownloadURLAsString("https://s3-us-west-2.amazonaws.com/unique-bucket-bin/leaders.txt") |
||||
lines := strings.Split(str, "\n") |
||||
shardIDLeaderMap := map[uint32]p2p.Peer{} |
||||
log.Print(lines) |
||||
for _, line := range lines { |
||||
if line == "" { |
||||
continue |
||||
} |
||||
parts := strings.Split(line, " ") |
||||
|
||||
shardID := parts[3] |
||||
id, err := strconv.Atoi(shardID) |
||||
if err == nil { |
||||
shardIDLeaderMap[uint32(id)] = p2p.Peer{IP: parts[0], Port: parts[1]} |
||||
} else { |
||||
log.Print("[Generator] Error parsing the shard Id ", shardID) |
||||
} |
||||
} |
||||
return shardIDLeaderMap |
||||
} |
||||
|
||||
// CreateWalletServerNode creates wallet server node.
|
||||
func CreateWalletServerNode() *node.Node { |
||||
configr := client_config.NewConfig() |
||||
var shardIDLeaderMap map[uint32]p2p.Peer |
||||
var clientPeer *p2p.Peer |
||||
if true { |
||||
configr.ReadConfigFile("local_config2.txt") |
||||
shardIDLeaderMap = configr.GetShardIDToLeaderMap() |
||||
clientPeer = configr.GetClientPeer() |
||||
} else { |
||||
shardIDLeaderMap = getShardIDToLeaderMap() |
||||
clientPeer = &p2p.Peer{Port: "127.0.0.1", IP: "1234"} |
||||
} |
||||
host := p2pimpl.NewHost(*clientPeer) |
||||
walletNode := node.New(host, nil, nil) |
||||
walletNode.Client = client.NewClient(walletNode.GetHost(), &shardIDLeaderMap) |
||||
return walletNode |
||||
} |
||||
|
||||
// ExecuteTransaction issues the transaction to the Harmony network
|
||||
func ExecuteTransaction(tx blockchain.Transaction, walletNode *node.Node) error { |
||||
if tx.IsCrossShard() { |
||||
walletNode.Client.PendingCrossTxsMutex.Lock() |
||||
walletNode.Client.PendingCrossTxs[tx.ID] = &tx |
||||
walletNode.Client.PendingCrossTxsMutex.Unlock() |
||||
} |
||||
|
||||
msg := proto_node.ConstructTransactionListMessage([]*blockchain.Transaction{&tx}) |
||||
walletNode.BroadcastMessage(walletNode.Client.GetLeaders(), msg) |
||||
|
||||
doneSignal := make(chan int) |
||||
go func() { |
||||
for { |
||||
if len(walletNode.Client.PendingCrossTxs) == 0 { |
||||
doneSignal <- 0 |
||||
break |
||||
} |
||||
} |
||||
}() |
||||
|
||||
select { |
||||
case <-doneSignal: |
||||
time.Sleep(100 * time.Millisecond) |
||||
return nil |
||||
case <-time.After(5 * time.Second): |
||||
return errors.New("Cross-shard Transaction processing timed out") |
||||
} |
||||
} |
||||
|
||||
// FetchUtxos fetches utxos of specified address from the Harmony network
|
||||
func FetchUtxos(addresses [][20]byte, walletNode *node.Node) (map[uint32]blockchain.UtxoMap, error) { |
||||
fmt.Println("Fetching account balance...") |
||||
walletNode.Client.ShardUtxoMap = make(map[uint32]blockchain.UtxoMap) |
||||
walletNode.BroadcastMessage(walletNode.Client.GetLeaders(), proto_node.ConstructFetchUtxoMessage(*walletNode.ClientPeer, addresses)) |
||||
|
||||
doneSignal := make(chan int) |
||||
go func() { |
||||
for { |
||||
if len(walletNode.Client.ShardUtxoMap) == len(*walletNode.Client.Leaders) { |
||||
doneSignal <- 0 |
||||
break |
||||
} |
||||
} |
||||
}() |
||||
|
||||
select { |
||||
case <-doneSignal: |
||||
return walletNode.Client.ShardUtxoMap, nil |
||||
case <-time.After(3 * time.Second): |
||||
return nil, errors.New("Utxo fetch timed out") |
||||
} |
||||
} |
||||
|
||||
// PrintUtxoBalance prints UTXO balance.
|
||||
func PrintUtxoBalance(shardUtxoMap map[uint32]blockchain.UtxoMap) { |
||||
addressBalance := make(map[[20]byte]int) |
||||
for _, utxoMap := range shardUtxoMap { |
||||
for address, txHash2Vout2AmountMap := range utxoMap { |
||||
for _, vout2AmountMap := range txHash2Vout2AmountMap { |
||||
for _, amount := range vout2AmountMap { |
||||
value, ok := addressBalance[address] |
||||
if ok { |
||||
addressBalance[address] = value + amount |
||||
} else { |
||||
addressBalance[address] = amount |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
for address, balance := range addressBalance { |
||||
fmt.Printf("Address: {%x}\n", address) |
||||
fmt.Printf("Balance: %d\n", balance) |
||||
} |
||||
} |
||||
|
||||
// ReadAddresses reads the addresses stored in local keystore
|
||||
func ReadAddresses() [][20]byte { |
||||
priKeys := ReadPrivateKeys() |
||||
addresses := [][20]byte{} |
||||
for _, key := range priKeys { |
||||
addresses = append(addresses, pki.GetAddressFromPrivateKey(key)) |
||||
} |
||||
return addresses |
||||
} |
||||
|
||||
// StorePrivateKey stores the specified private key in local keystore
|
||||
func StorePrivateKey(priKey []byte) { |
||||
for _, address := range ReadAddresses() { |
||||
if address == pki.GetAddressFromPrivateKey(crypto.Ed25519Curve.Scalar().SetBytes(priKey)) { |
||||
fmt.Println("The key already exists in the keystore") |
||||
return |
||||
} |
||||
} |
||||
f, err := os.OpenFile("keystore", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
||||
|
||||
if err != nil { |
||||
panic("Failed to open keystore") |
||||
} |
||||
_, err = f.Write(priKey) |
||||
|
||||
if err != nil { |
||||
panic("Failed to write to keystore") |
||||
} |
||||
f.Close() |
||||
} |
||||
|
||||
// ClearKeystore deletes all data in the local keystore
|
||||
func ClearKeystore() { |
||||
ioutil.WriteFile("keystore", []byte{}, 0644) |
||||
} |
||||
|
||||
// ReadPrivateKeys reads all the private key stored in local keystore
|
||||
func ReadPrivateKeys() []kyber.Scalar { |
||||
keys, err := ioutil.ReadFile("keystore") |
||||
if err != nil { |
||||
return []kyber.Scalar{} |
||||
} |
||||
keyScalars := []kyber.Scalar{} |
||||
for i := 0; i < len(keys); i += 32 { |
||||
priKey := crypto.Ed25519Curve.Scalar() |
||||
priKey.UnmarshalBinary(keys[i : i+32]) |
||||
keyScalars = append(keyScalars, priKey) |
||||
} |
||||
return keyScalars |
||||
} |
@ -1,20 +1,34 @@ |
||||
package node |
||||
|
||||
import ( |
||||
"github.com/harmony-one/harmony/blockchain" |
||||
"github.com/harmony-one/harmony/crypto/pki" |
||||
"crypto/ecdsa" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/params" |
||||
"github.com/harmony-one/harmony/core" |
||||
"math/big" |
||||
"math/rand" |
||||
"strings" |
||||
) |
||||
|
||||
// AddTestingAddresses creates in genesis block numAddress transactions which assign k token to each address in [0 - numAddress)
|
||||
// k = DefaultCoinbaseValue * DefaultNumUtxos
|
||||
// Assume we have S shards, then each account possesses k*S tokens
|
||||
// This is used by client code.
|
||||
// TODO: Consider to remove it later when moving to production.
|
||||
func (node *Node) AddTestingAddresses(numAddress int) { |
||||
txs := make([]*blockchain.Transaction, numAddress) |
||||
for i := range txs { |
||||
txs[i] = blockchain.NewCoinbaseTX(pki.GetAddressFromInt(i+1), "", node.Consensus.ShardID) |
||||
// CreateGenesisAllocWithTestingAddresses create the genesis block allocation that contains deterministically
|
||||
// generated testing addressess with tokens.
|
||||
// TODO: Consider to remove it later when moving to production.a
|
||||
func (node *Node) CreateGenesisAllocWithTestingAddresses(numAddress int) core.GenesisAlloc { |
||||
rand.Seed(0) |
||||
len := 1000000 |
||||
bytes := make([]byte, len) |
||||
for i := 0; i < len; i++ { |
||||
bytes[i] = byte(rand.Intn(100)) |
||||
} |
||||
node.blockchain.Blocks[0].Transactions = append(node.blockchain.Blocks[0].Transactions, txs...) |
||||
node.UtxoPool.Update(txs) |
||||
reader := strings.NewReader(string(bytes)) |
||||
genesisAloc := make(core.GenesisAlloc) |
||||
for i := 0; i < numAddress; i++ { |
||||
testBankKey, _ := ecdsa.GenerateKey(crypto.S256(), reader) |
||||
testBankAddress := crypto.PubkeyToAddress(testBankKey.PublicKey) |
||||
testBankFunds := big.NewInt(1000) |
||||
testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(params.Ether)) |
||||
genesisAloc[testBankAddress] = core.GenesisAccount{Balance: testBankFunds} |
||||
node.TestBankKeys = append(node.TestBankKeys, testBankKey) |
||||
} |
||||
return genesisAloc |
||||
} |
||||
|
@ -1,55 +0,0 @@ |
||||
package client |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/gob" |
||||
|
||||
"github.com/harmony-one/harmony/blockchain" |
||||
"github.com/harmony-one/harmony/proto" |
||||
) |
||||
|
||||
// MessageType is the specific types of message under Client category
|
||||
type MessageType byte |
||||
|
||||
// Message type supported by client
|
||||
const ( |
||||
Transaction MessageType = iota |
||||
// TODO: add more types
|
||||
) |
||||
|
||||
// TransactionMessageType defines the types of messages used for Client/Transaction
|
||||
type TransactionMessageType int |
||||
|
||||
// The proof of accept or reject returned by the leader to the client tnat issued cross shard transactions
|
||||
const ( |
||||
ProofOfLock TransactionMessageType = iota |
||||
UtxoResponse |
||||
) |
||||
|
||||
// FetchUtxoResponseMessage is the data structure of UTXO map
|
||||
type FetchUtxoResponseMessage struct { |
||||
UtxoMap blockchain.UtxoMap |
||||
ShardID uint32 |
||||
} |
||||
|
||||
// ConstructProofOfAcceptOrRejectMessage constructs the proof of accept or reject message that will be sent to client
|
||||
func ConstructProofOfAcceptOrRejectMessage(proofs []blockchain.CrossShardTxProof) []byte { |
||||
byteBuffer := bytes.NewBuffer([]byte{byte(proto.Client)}) |
||||
byteBuffer.WriteByte(byte(Transaction)) |
||||
byteBuffer.WriteByte(byte(ProofOfLock)) |
||||
encoder := gob.NewEncoder(byteBuffer) |
||||
|
||||
encoder.Encode(proofs) |
||||
return byteBuffer.Bytes() |
||||
} |
||||
|
||||
// ConstructFetchUtxoResponseMessage constructs the response message to fetch utxo message
|
||||
func ConstructFetchUtxoResponseMessage(utxoMap *blockchain.UtxoMap, shardID uint32) []byte { |
||||
byteBuffer := bytes.NewBuffer([]byte{byte(proto.Client)}) |
||||
byteBuffer.WriteByte(byte(Transaction)) |
||||
byteBuffer.WriteByte(byte(UtxoResponse)) |
||||
encoder := gob.NewEncoder(byteBuffer) |
||||
|
||||
encoder.Encode(FetchUtxoResponseMessage{*utxoMap, shardID}) |
||||
return byteBuffer.Bytes() |
||||
} |
@ -1,111 +0,0 @@ |
||||
package downloader_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
bc "github.com/harmony-one/harmony/blockchain" |
||||
"github.com/harmony-one/harmony/crypto/pki" |
||||
"github.com/harmony-one/harmony/services/syncing/downloader" |
||||
pb "github.com/harmony-one/harmony/services/syncing/downloader/proto" |
||||
) |
||||
|
||||
const ( |
||||
serverPort = "9997" |
||||
serverIP = "127.0.0.1" |
||||
clientPort = "9999" |
||||
) |
||||
|
||||
var ( |
||||
PriIntOne = 111 |
||||
PriIntTwo = 222 |
||||
TestAddressOne = pki.GetAddressFromInt(PriIntOne) |
||||
TestAddressTwo = pki.GetAddressFromInt(PriIntTwo) |
||||
ShardID = uint32(0) |
||||
) |
||||
|
||||
type FakeNode struct { |
||||
bc *bc.Blockchain |
||||
} |
||||
|
||||
// GetBlockHashes used for state download.
|
||||
func (node *FakeNode) GetBlockHashes() [][]byte { |
||||
res := [][]byte{} |
||||
for _, block := range node.bc.Blocks { |
||||
res = append(res, block.Hash[:]) |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// GetBlocks used for state download.
|
||||
func (node *FakeNode) GetBlocks() [][]byte { |
||||
res := [][]byte{} |
||||
for _, block := range node.bc.Blocks { |
||||
res = append(res, block.Serialize()) |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// SetBlockchain is used for testing
|
||||
func (node *FakeNode) Init() { |
||||
addresses := [][20]byte{TestAddressOne, TestAddressTwo} |
||||
node.bc = bc.CreateBlockchainWithMoreBlocks(addresses, ShardID) |
||||
} |
||||
|
||||
// CalculateResponse is the implementation for DownloadInterface.
|
||||
func (node *FakeNode) CalculateResponse(request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) { |
||||
response := &pb.DownloaderResponse{} |
||||
if request.Type == pb.DownloaderRequest_HEADER { |
||||
for _, block := range node.bc.Blocks { |
||||
response.Payload = append(response.Payload, block.Hash[:]) |
||||
} |
||||
} else { |
||||
for i := range request.Hashes { |
||||
block := node.bc.FindBlock(request.Hashes[i]) |
||||
response.Payload = append(response.Payload, block.Serialize()) |
||||
} |
||||
} |
||||
return response, nil |
||||
} |
||||
|
||||
// TestGetBlockHashes tests GetBlockHashes function.
|
||||
func TestGetBlockHashes(t *testing.T) { |
||||
fakeNode := &FakeNode{} |
||||
fakeNode.Init() |
||||
s := downloader.NewServer(fakeNode) |
||||
grcpServer, err := s.Start(serverIP, serverPort) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
defer grcpServer.Stop() |
||||
|
||||
client := downloader.ClientSetup(serverIP, serverPort) |
||||
defer client.Close() |
||||
response := client.GetBlockHashes() |
||||
if !reflect.DeepEqual(response.Payload, fakeNode.GetBlockHashes()) { |
||||
t.Error("not equal") |
||||
} |
||||
} |
||||
|
||||
// TestGetBlocks tests GetBlocks function.
|
||||
func TestGetBlocks(t *testing.T) { |
||||
fakeNode := &FakeNode{} |
||||
fakeNode.Init() |
||||
s := downloader.NewServer(fakeNode) |
||||
grcpServer, err := s.Start(serverIP, serverPort) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
defer grcpServer.Stop() |
||||
|
||||
client := downloader.ClientSetup(serverIP, serverPort) |
||||
defer client.Close() |
||||
response := client.GetBlockHashes() |
||||
if !reflect.DeepEqual(response.Payload, fakeNode.GetBlockHashes()) { |
||||
t.Error("not equal") |
||||
} |
||||
response = client.GetBlocks([][]byte{response.Payload[0], response.Payload[1]}) |
||||
if !reflect.DeepEqual(response.Payload, fakeNode.GetBlocks()) { |
||||
t.Error("not equal") |
||||
} |
||||
} |
@ -1,191 +0,0 @@ |
||||
package syncing_test |
||||
|
||||
import ( |
||||
"reflect" |
||||
"testing" |
||||
|
||||
bc "github.com/harmony-one/harmony/blockchain" |
||||
"github.com/harmony-one/harmony/crypto/pki" |
||||
"github.com/harmony-one/harmony/p2p" |
||||
"github.com/harmony-one/harmony/services/syncing" |
||||
"github.com/harmony-one/harmony/services/syncing/downloader" |
||||
pb "github.com/harmony-one/harmony/services/syncing/downloader/proto" |
||||
"github.com/stretchr/testify/assert" |
||||
"google.golang.org/grpc" |
||||
) |
||||
|
||||
const ( |
||||
serverPort1 = "9996" |
||||
serverPort2 = "9997" |
||||
serverPort3 = "9998" |
||||
serverIP = "127.0.0.1" |
||||
clientPort = "9999" |
||||
) |
||||
|
||||
var ( |
||||
PriIntOne = 111 |
||||
PriIntTwo = 222 |
||||
PriIntThree = 222 |
||||
TestAddressOne = pki.GetAddressFromInt(PriIntOne) |
||||
TestAddressTwo = pki.GetAddressFromInt(PriIntTwo) |
||||
TestAddressThree = pki.GetAddressFromInt(PriIntThree) |
||||
ShardID = uint32(0) |
||||
ServerPorts = []string{serverPort1, serverPort2, serverPort3} |
||||
) |
||||
|
||||
type FakeNode struct { |
||||
bc *bc.Blockchain |
||||
server *downloader.Server |
||||
ip string |
||||
port string |
||||
grpcServer *grpc.Server |
||||
doneFirstTime bool |
||||
} |
||||
|
||||
// GetBlockHashes used for state download.
|
||||
func (node *FakeNode) GetBlockHashes() [][]byte { |
||||
res := [][]byte{} |
||||
for _, block := range node.bc.Blocks { |
||||
res = append(res, block.Hash[:]) |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// GetBlocks used for state download.
|
||||
func (node *FakeNode) GetBlocks() [][]byte { |
||||
res := [][]byte{} |
||||
for _, block := range node.bc.Blocks { |
||||
res = append(res, block.Serialize()) |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// SetBlockchain is used for testing
|
||||
func (node *FakeNode) Init(ip, port string) { |
||||
addresses := [][20]byte{TestAddressOne, TestAddressTwo} |
||||
node.bc = bc.CreateBlockchainWithMoreBlocks(addresses, ShardID) |
||||
node.ip = ip |
||||
node.port = port |
||||
|
||||
node.server = downloader.NewServer(node) |
||||
} |
||||
|
||||
// SetBlockchain is used for testing
|
||||
func (node *FakeNode) Init2(ip, port string) { |
||||
addresses := [][20]byte{TestAddressOne} |
||||
node.bc = bc.CreateBlockchainWithMoreBlocks(addresses, ShardID) |
||||
node.ip = ip |
||||
node.port = port |
||||
|
||||
node.server = downloader.NewServer(node) |
||||
} |
||||
|
||||
// Start ...
|
||||
func (node *FakeNode) Start() error { |
||||
var err error |
||||
node.grpcServer, err = node.server.Start(node.ip, node.port) |
||||
return err |
||||
} |
||||
|
||||
func (node *FakeNode) addOneMoreBlock() { |
||||
addresses := [][20]byte{TestAddressThree} |
||||
node.bc.Blocks = append(node.bc.Blocks, bc.CreateMoreBlocks(addresses, ShardID)...) |
||||
} |
||||
|
||||
func (node *FakeNode) CalculateResponse(request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) { |
||||
response := &pb.DownloaderResponse{} |
||||
if request.Type == pb.DownloaderRequest_HEADER { |
||||
for _, block := range node.bc.Blocks { |
||||
response.Payload = append(response.Payload, block.Hash[:]) |
||||
} |
||||
if !node.doneFirstTime { |
||||
node.addOneMoreBlock() |
||||
} |
||||
node.doneFirstTime = true |
||||
} else { |
||||
for i := range request.Hashes { |
||||
block := node.bc.FindBlock(request.Hashes[i]) |
||||
response.Payload = append(response.Payload, block.Serialize()) |
||||
} |
||||
} |
||||
return response, nil |
||||
} |
||||
|
||||
func TestCompareSyncPeerConfigByBlockHashes(t *testing.T) { |
||||
a := syncing.CreateTestSyncPeerConfig(nil, [][]byte{{1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6}}) |
||||
b := syncing.CreateTestSyncPeerConfig(nil, [][]byte{{1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6}}) |
||||
assert.Equal(t, syncing.CompareSyncPeerConfigByblockHashes(a, b), 0, "they should be equal") |
||||
c := syncing.CreateTestSyncPeerConfig(nil, [][]byte{{1, 2, 3, 4, 5, 7}, {1, 2, 3, 4, 5, 6}}) |
||||
assert.Equal(t, syncing.CompareSyncPeerConfigByblockHashes(a, c), -1, "a should be less than c") |
||||
d := syncing.CreateTestSyncPeerConfig(nil, [][]byte{{1, 2, 3, 4, 5, 4}, {1, 2, 3, 4, 5, 6}}) |
||||
assert.Equal(t, syncing.CompareSyncPeerConfigByblockHashes(a, d), 1, "a should be greater than c") |
||||
} |
||||
|
||||
func TestSyncing(t *testing.T) { |
||||
fakeNodes := []*FakeNode{&FakeNode{}, &FakeNode{}, &FakeNode{}} |
||||
for i := range fakeNodes { |
||||
fakeNodes[i].Init(serverIP, ServerPorts[i]) |
||||
if err := fakeNodes[i].Start(); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
defer func() { |
||||
for _, fakeNode := range fakeNodes { |
||||
fakeNode.grpcServer.Stop() |
||||
} |
||||
}() |
||||
|
||||
stateSync := &syncing.StateSync{} |
||||
bc := &bc.Blockchain{} |
||||
peers := make([]p2p.Peer, len(fakeNodes)) |
||||
for i := range peers { |
||||
peers[i].IP = fakeNodes[i].ip |
||||
peers[i].Port = fakeNodes[i].port |
||||
} |
||||
|
||||
stateSync.StartStateSync(peers, bc) |
||||
|
||||
for i := range bc.Blocks { |
||||
if !reflect.DeepEqual(bc.Blocks[i], fakeNodes[0].bc.Blocks[i]) { |
||||
t.Error("not equal") |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
func TestSyncingIncludingBadNode(t *testing.T) { |
||||
fakeNodes := []*FakeNode{&FakeNode{}, &FakeNode{}, &FakeNode{}} |
||||
for i := range fakeNodes { |
||||
if i == 2 { |
||||
// Bad node.
|
||||
fakeNodes[i].Init2(serverIP, ServerPorts[i]) |
||||
} else { |
||||
// Good node.
|
||||
fakeNodes[i].Init(serverIP, ServerPorts[i]) |
||||
} |
||||
if err := fakeNodes[i].Start(); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
defer func() { |
||||
for _, fakeNode := range fakeNodes { |
||||
fakeNode.grpcServer.Stop() |
||||
} |
||||
}() |
||||
|
||||
stateSync := &syncing.StateSync{} |
||||
bc := &bc.Blockchain{} |
||||
peers := make([]p2p.Peer, len(fakeNodes)) |
||||
for i := range peers { |
||||
peers[i].IP = fakeNodes[i].ip |
||||
peers[i].Port = fakeNodes[i].port |
||||
} |
||||
|
||||
assert.True(t, stateSync.StartStateSync(peers, bc), "should return true") |
||||
|
||||
for i := range bc.Blocks { |
||||
if !reflect.DeepEqual(bc.Blocks[i], fakeNodes[0].bc.Blocks[i]) { |
||||
t.Error("not equal") |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue