The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/blockchain/utxopool_test.go

69 lines
2.1 KiB

package blockchain
import (
"testing"
)
7 years ago
func TestVerifyOneTransactionAndUpdate(t *testing.T) {
bc := CreateBlockchain(TestAddressOne, 0)
utxoPool := CreateUTXOPoolFromGenesisBlock(bc.Blocks[0])
7 years ago
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressThree, 3, 0)
bc.AddNewUserTransfer(utxoPool, PriKeyOne, TestAddressOne, TestAddressTwo, 100, 0)
7 years ago
tx := bc.NewUTXOTransaction(PriKeyOne, TestAddressOne, TestAddressFour, 10, 0)
7 years ago
if tx == nil {
t.Error("failed to create a new transaction.")
}
7 years ago
if err, _ := utxoPool.VerifyOneTransaction(tx, nil); err != nil {
7 years ago
t.Error("failed to verify a valid transaction.")
}
7 years ago
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")
}
}