github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/transactionpool_test.go (about) 1 package modules 2 3 import ( 4 "testing" 5 6 "github.com/NebulousLabs/Sia/encoding" 7 "github.com/NebulousLabs/Sia/types" 8 ) 9 10 // TestConsensusConflict checks that the consensus conflict type is correctly 11 // assembling consensus conflict errors. 12 func TestConsensusConflict(t *testing.T) { 13 t.Parallel() 14 15 ncc := NewConsensusConflict("problem") 16 if ncc.Error() != "consensus conflict: problem" { 17 t.Error("wrong error message being reported in a consensus conflict") 18 } 19 20 err := func() error { 21 return ncc 22 }() 23 if err.Error() != "consensus conflict: problem" { 24 t.Error("wrong error message being reported in a consensus conflict") 25 } 26 if _, ok := err.(ConsensusConflict); !ok { 27 t.Error("error is not maintaining consensus conflict type") 28 } 29 } 30 31 // TestCalculateFee checks that the CalculateFee function is correctly tallying 32 // the number of fees in a transaction set. 33 func TestCalculateFee(t *testing.T) { 34 t.Parallel() 35 36 // Try calculating the fees on a nil transaction set. 37 if CalculateFee(nil).Cmp(types.ZeroCurrency) != 0 { 38 t.Error("CalculateFee is incorrectly handling nil input") 39 } 40 // Try a single transaction with no fees. 41 txnSet := []types.Transaction{{}} 42 if CalculateFee(txnSet).Cmp(types.ZeroCurrency) != 0 { 43 t.Error("CalculateFee is not correctly calculating the fees on an empty transaction set") 44 } 45 // Try a non-empty transaction. 46 txnSet = []types.Transaction{{ 47 SiacoinOutputs: []types.SiacoinOutput{{ 48 Value: types.NewCurrency64(253e9), 49 }}, 50 }} 51 if CalculateFee(txnSet).Cmp(types.ZeroCurrency) != 0 { 52 t.Error("CalculateFee is not correctly calculating the fees on a non-empty transaction set") 53 } 54 55 // Try a transaction set with a single miner fee. 56 baseFee := types.NewCurrency64(12e3) 57 txnSet = []types.Transaction{{ 58 MinerFees: []types.Currency{ 59 baseFee, 60 }, 61 }} 62 setLen := uint64(len(encoding.Marshal(txnSet))) 63 expectedFee := baseFee.Div64(setLen) 64 if CalculateFee(txnSet).Cmp(expectedFee) != 0 { 65 t.Error("CalculateFee doesn't seem to be calculating the correct transaction fee") 66 } 67 // Try the transaction set when there is more data. 68 txnSet[0].FileContracts = append(txnSet[0].FileContracts, types.FileContract{ 69 FileSize: 10e3, 70 }) 71 newSetLen := uint64(len(encoding.Marshal(txnSet))) 72 if newSetLen <= setLen { 73 t.Fatal("transaction set did not grow after adding a file contract") 74 } 75 newExpectedFee := baseFee.Div64(newSetLen) 76 if newExpectedFee.Cmp(expectedFee) >= 0 { 77 t.Error("the new expected fee should go down as the txn size increases") 78 } 79 if CalculateFee(txnSet).Cmp(newExpectedFee) != 0 { 80 t.Error("the new expected fee does not match the new actual fee") 81 } 82 83 // Try a transaction set with multiple transactions and multiple fees per 84 // transaction. 85 fee1 := types.NewCurrency64(1e6) 86 fee2 := types.NewCurrency64(2e6) 87 fee3 := types.NewCurrency64(3e6) 88 fee4 := types.NewCurrency64(4e6) 89 txnSet = []types.Transaction{ 90 { 91 MinerFees: []types.Currency{ 92 fee1, 93 fee2, 94 }, 95 }, 96 { 97 MinerFees: []types.Currency{ 98 fee3, 99 fee4, 100 }, 101 }, 102 } 103 currencyLen := types.NewCurrency64(uint64(len(encoding.Marshal(txnSet)))) 104 multiExpectedFee := fee1.Add(fee2).Add(fee3).Add(fee4).Div(currencyLen) 105 if CalculateFee(txnSet).Cmp(multiExpectedFee) != 0 { 106 t.Error("got the wrong fee for a multi transaction set") 107 } 108 }