|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
|
|
|
|
|
// MaxNumberOfTransactions is the max number of transaction per a block.
|
|
|
|
MaxNumberOfTransactions = 100 |
|
|
|
MaxNumberOfTransactions = 100 |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -16,9 +17,9 @@ type UTXOPool struct { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// VerifyTransactions verifies if a list of transactions valid.
|
|
|
|
// VerifyTransactions verifies if a list of transactions valid.
|
|
|
|
func (utxopool *UTXOPool) VerifyTransactions(transactions []*Transaction) bool { |
|
|
|
func (utxoPool *UTXOPool) VerifyTransactions(transactions []*Transaction) bool { |
|
|
|
spentTXOs := make(map[string]map[string]map[int]bool) |
|
|
|
spentTXOs := make(map[string]map[string]map[int]bool) |
|
|
|
if utxopool != nil { |
|
|
|
if utxoPool != nil { |
|
|
|
for _, tx := range transactions { |
|
|
|
for _, tx := range transactions { |
|
|
|
inTotal := 0 |
|
|
|
inTotal := 0 |
|
|
|
// Calculate the sum of TxInput
|
|
|
|
// Calculate the sum of TxInput
|
|
|
@ -41,7 +42,7 @@ func (utxopool *UTXOPool) VerifyTransactions(transactions []*Transaction) bool { |
|
|
|
spentTXOs[in.Address][inTxID][index] = true |
|
|
|
spentTXOs[in.Address][inTxID][index] = true |
|
|
|
|
|
|
|
|
|
|
|
// Sum the balance up to the inTotal.
|
|
|
|
// Sum the balance up to the inTotal.
|
|
|
|
if val, ok := utxopool.utxo[in.Address][inTxID][index]; ok { |
|
|
|
if val, ok := utxoPool.utxo[in.Address][inTxID][index]; ok { |
|
|
|
inTotal += val |
|
|
|
inTotal += val |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return false |
|
|
|
return false |
|
|
@ -61,8 +62,8 @@ func (utxopool *UTXOPool) VerifyTransactions(transactions []*Transaction) bool { |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// VerifyOneTransactionAndUpdate verifies if a list of transactions valid.
|
|
|
|
// VerifyOneTransaction verifies if a list of transactions valid.
|
|
|
|
func (utxopool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { |
|
|
|
func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { |
|
|
|
spentTXOs := make(map[string]map[string]map[int]bool) |
|
|
|
spentTXOs := make(map[string]map[string]map[int]bool) |
|
|
|
txID := hex.EncodeToString(tx.ID) |
|
|
|
txID := hex.EncodeToString(tx.ID) |
|
|
|
inTotal := 0 |
|
|
|
inTotal := 0 |
|
|
@ -71,7 +72,7 @@ func (utxopool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { |
|
|
|
inTxID := hex.EncodeToString(in.TxID) |
|
|
|
inTxID := hex.EncodeToString(in.TxID) |
|
|
|
index := in.TxOutputIndex |
|
|
|
index := in.TxOutputIndex |
|
|
|
// Check if the transaction with the addres is spent or not.
|
|
|
|
// Check if the transaction with the addres is spent or not.
|
|
|
|
if val, ok := utxopool.utxo[in.Address][inTxID][index]; ok { |
|
|
|
if val, ok := utxoPool.utxo[in.Address][inTxID][index]; ok { |
|
|
|
if spentTXOs[in.Address][inTxID][index] { |
|
|
|
if spentTXOs[in.Address][inTxID][index] { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
@ -92,7 +93,7 @@ func (utxopool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { |
|
|
|
outTotal := 0 |
|
|
|
outTotal := 0 |
|
|
|
// Calculate the sum of TxOutput
|
|
|
|
// Calculate the sum of TxOutput
|
|
|
|
for index, out := range tx.TxOutput { |
|
|
|
for index, out := range tx.TxOutput { |
|
|
|
if val, ok := spentTXOs[out.Address][txID][index]; ok { |
|
|
|
if _, ok := spentTXOs[out.Address][txID][index]; ok { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
outTotal += out.Value |
|
|
|
outTotal += out.Value |
|
|
@ -103,69 +104,72 @@ func (utxopool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (utxopool *UTXOPool) Update(tx* Transaction) { |
|
|
|
// UpdateOneTransaction updates utxoPool in respect to the new Transaction.
|
|
|
|
if utxopool != nil { |
|
|
|
func (utxoPool *UTXOPool) UpdateOneTransaction(tx *Transaction) { |
|
|
|
|
|
|
|
if utxoPool != nil { |
|
|
|
txID := hex.EncodeToString(tx.ID) |
|
|
|
txID := hex.EncodeToString(tx.ID) |
|
|
|
|
|
|
|
|
|
|
|
// Remove
|
|
|
|
// Remove
|
|
|
|
for _, in := range tx.TxInput { |
|
|
|
for _, in := range tx.TxInput { |
|
|
|
inTxID := hex.EncodeToString(in.TxID) |
|
|
|
inTxID := hex.EncodeToString(in.TxID) |
|
|
|
delete(utxopool.utxo[in.Address][inTxID], in.TxOutputIndex) |
|
|
|
delete(utxoPool.utxo[in.Address][inTxID], in.TxOutputIndex) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Update
|
|
|
|
// Update
|
|
|
|
for index, out := range tx.TxOutput { |
|
|
|
for index, out := range tx.TxOutput { |
|
|
|
if _, ok := utxopool.utxo[out.Address]; !ok { |
|
|
|
if _, ok := utxoPool.utxo[out.Address]; !ok { |
|
|
|
utxopool.utxo[out.Address] = make(map[string]map[int]int) |
|
|
|
utxoPool.utxo[out.Address] = make(map[string]map[int]int) |
|
|
|
utxopool.utxo[out.Address][txID] = make(map[int]int) |
|
|
|
utxoPool.utxo[out.Address][txID] = make(map[int]int) |
|
|
|
} |
|
|
|
} |
|
|
|
if _, ok := utxopool.utxo[out.Address][txID]; !ok { |
|
|
|
if _, ok := utxoPool.utxo[out.Address][txID]; !ok { |
|
|
|
utxopool.utxo[out.Address][txID] = make(map[int]int) |
|
|
|
utxoPool.utxo[out.Address][txID] = make(map[int]int) |
|
|
|
} |
|
|
|
} |
|
|
|
utxopool.utxo[out.Address][txID][index] = out.Value |
|
|
|
utxoPool.utxo[out.Address][txID][index] = out.Value |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (utxopool *UTXOPool) VerifyOneTransactionAndUpdate(tx *Transaction) bool { |
|
|
|
// VerifyOneTransactionAndUpdate verifies and update a valid transaction.
|
|
|
|
if utxopool.VerifyOneTransaction(tx) { |
|
|
|
// Return false if the transaction is not valid.
|
|
|
|
utxopool.Update(tx) |
|
|
|
func (utxoPool *UTXOPool) VerifyOneTransactionAndUpdate(tx *Transaction) bool { |
|
|
|
|
|
|
|
if utxoPool.VerifyOneTransaction(tx) { |
|
|
|
|
|
|
|
utxoPool.UpdateOneTransaction(tx) |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
retur false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// VerifyAndUpdate verifies a list of transactions and update utxoPool.
|
|
|
|
// VerifyAndUpdate verifies a list of transactions and update utxoPool.
|
|
|
|
func (utxopool *UTXOPool) VerifyAndUpdate(transactions []*Transaction) bool { |
|
|
|
func (utxoPool *UTXOPool) VerifyAndUpdate(transactions []*Transaction) bool { |
|
|
|
if utxopool.VerifyTransactions(transactions) { |
|
|
|
if utxoPool.VerifyTransactions(transactions) { |
|
|
|
utxopool.Update(transactions) |
|
|
|
utxoPool.Update(transactions) |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Update utxo balances with a list of new transactions.
|
|
|
|
// Update utxo balances with a list of new transactions.
|
|
|
|
func (utxopool *UTXOPool) Update(transactions []*Transaction) { |
|
|
|
func (utxoPool *UTXOPool) Update(transactions []*Transaction) { |
|
|
|
if utxopool != nil { |
|
|
|
if utxoPool != nil { |
|
|
|
for _, tx := range transactions { |
|
|
|
for _, tx := range transactions { |
|
|
|
curTxID := hex.EncodeToString(tx.ID) |
|
|
|
curTxID := hex.EncodeToString(tx.ID) |
|
|
|
|
|
|
|
|
|
|
|
// Remove
|
|
|
|
// Remove
|
|
|
|
for _, in := range tx.TxInput { |
|
|
|
for _, in := range tx.TxInput { |
|
|
|
inTxID := hex.EncodeToString(in.TxID) |
|
|
|
inTxID := hex.EncodeToString(in.TxID) |
|
|
|
delete(utxopool.utxo[in.Address][inTxID], in.TxOutputIndex) |
|
|
|
delete(utxoPool.utxo[in.Address][inTxID], in.TxOutputIndex) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Update
|
|
|
|
// Update
|
|
|
|
for index, out := range tx.TxOutput { |
|
|
|
for index, out := range tx.TxOutput { |
|
|
|
if _, ok := utxopool.utxo[out.Address]; !ok { |
|
|
|
if _, ok := utxoPool.utxo[out.Address]; !ok { |
|
|
|
utxopool.utxo[out.Address] = make(map[string]map[int]int) |
|
|
|
utxoPool.utxo[out.Address] = make(map[string]map[int]int) |
|
|
|
utxopool.utxo[out.Address][curTxID] = make(map[int]int) |
|
|
|
utxoPool.utxo[out.Address][curTxID] = make(map[int]int) |
|
|
|
} |
|
|
|
} |
|
|
|
if _, ok := utxopool.utxo[out.Address][curTxID]; !ok { |
|
|
|
if _, ok := utxoPool.utxo[out.Address][curTxID]; !ok { |
|
|
|
utxopool.utxo[out.Address][curTxID] = make(map[int]int) |
|
|
|
utxoPool.utxo[out.Address][curTxID] = make(map[int]int) |
|
|
|
} |
|
|
|
} |
|
|
|
utxopool.utxo[out.Address][curTxID][index] = out.Value |
|
|
|
utxoPool.utxo[out.Address][curTxID][index] = out.Value |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -191,13 +195,14 @@ func CreateUTXOPoolFromGenesisBlockChain(bc *Blockchain) *UTXOPool { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SelectTransactionsForNewBlock returns a list of index of valid transactions for the new block.
|
|
|
|
// SelectTransactionsForNewBlock returns a list of index of valid transactions for the new block.
|
|
|
|
func (utxoPool *UTXOPool) SelectTransactionsForNewBlock(transactions []*Transaction) (selected, unselected []*Transaction) { |
|
|
|
func (utxoPool *UTXOPool) SelectTransactionsForNewBlock(transactions []*Transaction) ([]*Transaction, []*Transaction) { |
|
|
|
selected, unselected = []*Transaction{}, []*Transaction{} |
|
|
|
selected, unselected := []*Transaction{}, []*Transaction{} |
|
|
|
for id, tx := range transactions { |
|
|
|
for _, tx := range transactions { |
|
|
|
if len(selected) < MaxNumberOfTransactions && utxoPool.VerifyOneTransactionAndUpdate(tx) { |
|
|
|
if len(selected) < MaxNumberOfTransactions && utxoPool.VerifyOneTransactionAndUpdate(tx) { |
|
|
|
append(selected, tx) |
|
|
|
selected = append(selected, tx) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
append(unselected, tx) |
|
|
|
unselected = append(unselected, tx) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return selected, unselected |
|
|
|
} |
|
|
|
} |
|
|
|