@@ -14,9 +14,12 @@ const Coin = require('../lib/primitives/coin');
1414const KeyRing = require ( '../lib/primitives/keyring' ) ;
1515const Address = require ( '../lib/primitives/address' ) ;
1616const Outpoint = require ( '../lib/primitives/outpoint' ) ;
17+ const Block = require ( '../lib/primitives/block' ) ;
1718const Script = require ( '../lib/script/script' ) ;
1819const Witness = require ( '../lib/script/witness' ) ;
1920const CoinView = require ( '../lib/coins/coinview' ) ;
21+ const util = require ( '../lib/utils/util' ) ;
22+ const consensus = require ( '../lib/protocol/consensus' ) ;
2023const MemWallet = require ( './util/memwallet' ) ;
2124const ALL = Script . hashType . ALL ;
2225
@@ -63,6 +66,38 @@ function dummyInput(addr, hash) {
6366 return Coin . fromTX ( fund , 0 , - 1 ) ;
6467}
6568
69+ async function dummyBlock ( txs , coinbase = false ) {
70+ if ( coinbase ) {
71+ const cb = new MTX ( ) ;
72+ cb . locktime = chain . height + 1 ;
73+ txs = [ cb , ...txs ] ;
74+ }
75+
76+ const view = new CoinView ( ) ;
77+ for ( const tx of txs ) {
78+ view . addTX ( tx , - 1 ) ;
79+ }
80+
81+ const now = util . now ( ) ;
82+ const time = chain . tip . time <= now ? chain . tip . time + 1 : now ;
83+
84+ const block = new Block ( {
85+ version : 1 ,
86+ prevBlock : Buffer . from ( chain . tip . hash , 'hex' ) ,
87+ merkleRoot : random . randomBytes ( 32 ) ,
88+ witnessRoot : random . randomBytes ( 32 ) ,
89+ treeRoot : random . randomBytes ( 32 ) ,
90+ filterRoot : random . randomBytes ( 32 ) ,
91+ reservedRoot : random . randomBytes ( 32 ) ,
92+ time : time ,
93+ bits : await chain . getTarget ( time , chain . tip ) ,
94+ nonce : Buffer . alloc ( consensus . NONCE_SIZE ) ,
95+ txs : txs
96+ } ) ;
97+
98+ return [ block , view ] ;
99+ }
100+
66101describe ( 'Mempool' , function ( ) {
67102 this . timeout ( 5000 ) ;
68103
@@ -298,6 +333,62 @@ describe('Mempool', function() {
298333 assert ( ! mempool . hasReject ( cachedTX . hash ( ) ) ) ;
299334 } ) ;
300335
336+ it ( 'should remove tx after being included in block' , async ( ) => {
337+ const key = KeyRing . generate ( ) ;
338+ const addr = key . getAddress ( ) ;
339+
340+ const t1 = new MTX ( ) ;
341+ {
342+ t1 . addOutput ( wallet . getAddress ( ) , 50000 ) ;
343+ t1 . addOutput ( wallet . getAddress ( ) , 10000 ) ;
344+
345+ const script = Script . fromPubkeyhash ( key . getHash ( ) ) ;
346+
347+ t1 . addCoin ( dummyInput ( addr , ONE_HASH ) ) ;
348+
349+ const sig = t1 . signature ( 0 , script , 70000 , key . privateKey , ALL ) ;
350+ t1 . inputs [ 0 ] . witness = Witness . fromItems ( [ sig , key . publicKey ] ) ;
351+ await mempool . addTX ( t1 . toTX ( ) ) ;
352+ }
353+
354+ const t2 = new MTX ( ) ;
355+ {
356+ const key = KeyRing . generate ( ) ;
357+ const addr = key . getAddress ( ) ;
358+
359+ t2 . addOutput ( wallet . getAddress ( ) , 50000 ) ;
360+ t2 . addOutput ( wallet . getAddress ( ) , 10000 ) ;
361+
362+ const script = Script . fromPubkeyhash ( key . getHash ( ) ) ;
363+
364+ t2 . addCoin ( dummyInput ( addr , ONE_HASH ) ) ;
365+
366+ const sig = t2 . signature ( 0 , script , 70000 , key . privateKey , ALL ) ;
367+ t2 . inputs [ 0 ] . witness = Witness . fromItems ( [ sig , key . publicKey ] ) ;
368+ await mempool . addTX ( t2 . toTX ( ) ) ;
369+ }
370+
371+ const [ block , view ] = await dummyBlock ( [ t1 ] , true ) ;
372+
373+ {
374+ const entry = await mempool . getEntry ( t1 . hash ( ) ) ;
375+ assert . equal ( entry . txid ( ) , t1 . txid ( ) ) ;
376+ }
377+
378+ await mempool . addBlock ( block , block . txs , view ) ;
379+
380+ {
381+ const entry = await mempool . getEntry ( t1 . hash ( ) ) ;
382+ assert . equal ( entry , undefined ) ;
383+ }
384+
385+ {
386+ const tx = t2 . toTX ( ) ;
387+ const entry = await mempool . getEntry ( tx . hash ( ) ) ;
388+ assert . equal ( entry . txid ( ) , tx . txid ( ) ) ;
389+ }
390+ } ) ;
391+
301392 it ( 'should destroy mempool' , async ( ) => {
302393 await mempool . close ( ) ;
303394 await chain . close ( ) ;
0 commit comments