@@ -82,6 +82,36 @@ describe('Blockchain class', function() {
8282 } ) ;
8383 } ) ;
8484
85+ describe ( 'minePendingTransactions' , function ( ) {
86+ // It should not be possible for a user to create multiple pending
87+ // transactions for a total amount higher than his balance.
88+ // In this test we start with this situation:
89+ // - Wallet "walletAddr" -> 80 coins (100 mining reward - 2 test tx)
90+ // - Wallet "wallet2" -> 0 coins
91+ it ( 'should not allow pending transactions to go below zero' , function ( ) {
92+ const blockchain = createBlockchainWithTx ( ) ;
93+ const walletAddr = signingKey . getPublic ( 'hex' ) ;
94+
95+ // Verify that the wallets have the correct balance
96+ assert . strict . equal ( blockchain . getBalanceOfAddress ( 'wallet2' ) , 0 ) ;
97+ assert . strict . equal ( blockchain . getBalanceOfAddress ( walletAddr ) , 80 ) ;
98+
99+ // Create a transaction for 80 coins (from walletAddr -> "wallet2")
100+ blockchain . addTransaction ( createSignedTx ( 80 ) ) ;
101+
102+ // Try tro create another transaction for which we don't have the balance.
103+ // Blockchain should refuse this!
104+ assert . throws ( ( ) => { blockchain . addTransaction ( createSignedTx ( 80 ) ) ; } , Error ) ;
105+
106+ // Mine transactions, send rewards to another address
107+ blockchain . minePendingTransactions ( 1 ) ;
108+
109+ // Verify that the first transaction did go through.
110+ assert . strict . equal ( blockchain . getBalanceOfAddress ( walletAddr ) , 0 ) ;
111+ assert . strict . equal ( blockchain . getBalanceOfAddress ( 'wallet2' ) , 80 ) ;
112+ } ) ;
113+ } ) ;
114+
85115 describe ( 'helper functions' , function ( ) {
86116 it ( 'should correctly set first block to genesis block' , function ( ) {
87117 assert . strict . deepEqual ( blockchain . chain [ 0 ] , blockchain . createGenesisBlock ( ) ) ;
0 commit comments