55
66const assert = require ( 'bsert' ) ;
77const random = require ( 'bcrypto/lib/random' ) ;
8+ const Network = require ( '../lib/protocol/network' ) ;
89const MempoolEntry = require ( '../lib/mempool/mempoolentry' ) ;
910const Mempool = require ( '../lib/mempool/mempool' ) ;
1011const WorkerPool = require ( '../lib/workers/workerpool' ) ;
@@ -36,13 +37,14 @@ const ownership = require('../lib/covenants/ownership');
3637const ONE_HASH = Buffer . alloc ( 32 , 0x00 ) ;
3738ONE_HASH [ 0 ] = 0x01 ;
3839
40+ const network = Network . get ( 'regtest' ) ;
3941const workers = new WorkerPool ( {
4042 enabled : true
4143} ) ;
4244
4345const chain = new Chain ( {
44- network : 'regtest' ,
4546 memory : true ,
47+ network,
4648 workers
4749} ) ;
4850
@@ -52,7 +54,7 @@ const mempool = new Mempool({
5254 workers
5355} ) ;
5456
55- const wallet = new MemWallet ( ) ;
57+ const wallet = new MemWallet ( { network } ) ;
5658
5759let cachedTX = null ;
5860
@@ -225,6 +227,62 @@ describe('Mempool', function() {
225227 } ) ) ;
226228 } ) ;
227229
230+ it ( 'should get spent coins and reflect in coinview' , async ( ) => {
231+ const wallet = new MemWallet ( { network } ) ;
232+ const addr = wallet . getAddress ( ) ;
233+
234+ const dummyCoin = dummyInput ( addr , random . randomBytes ( 32 ) ) ;
235+
236+ const mtx1 = new MTX ( ) ;
237+ mtx1 . addOutput ( wallet . getAddress ( ) , 50000 ) ;
238+ mtx1 . addCoin ( dummyCoin ) ;
239+
240+ wallet . sign ( mtx1 ) ;
241+
242+ const tx1 = mtx1 . toTX ( ) ;
243+ const coin1 = Coin . fromTX ( tx1 , 0 , - 1 ) ;
244+
245+ const mtx2 = new MTX ( ) ;
246+ mtx2 . addOutput ( wallet . getAddress ( ) , 10000 ) ;
247+ mtx2 . addOutput ( wallet . getAddress ( ) , 30000 ) ; // 10k fee
248+ mtx2 . addCoin ( coin1 ) ;
249+
250+ wallet . sign ( mtx2 ) ;
251+
252+ const tx2 = mtx2 . toTX ( ) ;
253+
254+ await mempool . addTX ( tx1 ) ;
255+
256+ {
257+ const view = await mempool . getCoinView ( tx2 ) ;
258+ const sview = await mempool . getSpentView ( tx2 ) ;
259+ assert ( view . hasEntry ( coin1 ) ) ;
260+ assert ( sview . hasEntry ( coin1 ) ) ;
261+ assert . strictEqual ( mempool . hasCoin ( coin1 . hash , coin1 . index ) , true ) ;
262+ assert . strictEqual ( mempool . isSpent ( coin1 . hash , coin1 . index ) , false ) ;
263+ }
264+
265+ await mempool . addTX ( tx2 ) ;
266+
267+ {
268+ const view = await mempool . getCoinView ( tx1 ) ;
269+ const sview = await mempool . getSpentView ( tx1 ) ;
270+ assert ( ! view . hasEntry ( dummyCoin ) ) ;
271+ assert ( sview . hasEntry ( dummyCoin ) ) ;
272+ assert . strictEqual ( mempool . hasCoin ( coin1 . hash , coin1 . index ) , false ) ;
273+ assert . strictEqual ( mempool . isSpent ( coin1 . hash , coin1 . index ) , true ) ;
274+ }
275+
276+ {
277+ const view = await mempool . getCoinView ( tx2 ) ;
278+ const sview = await mempool . getSpentView ( tx2 ) ;
279+ assert ( ! view . hasEntry ( coin1 ) ) ;
280+ assert ( sview . hasEntry ( coin1 ) ) ;
281+ assert . strictEqual ( mempool . hasCoin ( coin1 . hash , coin1 . index ) , false ) ;
282+ assert . strictEqual ( mempool . isSpent ( coin1 . hash , coin1 . index ) , true ) ;
283+ }
284+ } ) ;
285+
228286 it ( 'should handle locktime' , async ( ) => {
229287 const key = KeyRing . generate ( ) ;
230288 const addr = key . getAddress ( ) ;
@@ -416,7 +474,7 @@ describe('Mempool', function() {
416474 const chain = new Chain ( {
417475 memory : true ,
418476 workers,
419- network : 'regtest'
477+ network
420478 } ) ;
421479
422480 const mempool = new Mempool ( {
@@ -425,6 +483,8 @@ describe('Mempool', function() {
425483 memory : true
426484 } ) ;
427485
486+ const wallet = new MemWallet ( { network } ) ;
487+
428488 const COINBASE_MATURITY = mempool . network . coinbaseMaturity ;
429489 const TREE_INTERVAL = mempool . network . names . treeInterval ;
430490 mempool . network . names . auctionStart = 0 ;
@@ -444,9 +504,7 @@ describe('Mempool', function() {
444504 // Number of coins available in
445505 // chaincoins (100k satoshi per coin).
446506 const N = 100 ;
447- const chaincoins = new MemWallet ( {
448- network : 'regtest'
449- } ) ;
507+ const chaincoins = new MemWallet ( { network } ) ;
450508
451509 chain . on ( 'block' , ( block , entry ) => {
452510 chaincoins . addBlock ( entry , block . txs ) ;
@@ -1180,7 +1238,7 @@ describe('Mempool', function() {
11801238 const chain = new Chain ( {
11811239 memory : true ,
11821240 workers,
1183- network : 'regtest'
1241+ network
11841242 } ) ;
11851243
11861244 const mempool = new Mempool ( {
@@ -1206,8 +1264,8 @@ describe('Mempool', function() {
12061264 // Number of coins available in
12071265 // chaincoins (100k satoshi per coin).
12081266 const N = 100 ;
1209- const chaincoins = new MemWallet ( ) ;
1210- const wallet = new MemWallet ( ) ;
1267+ const chaincoins = new MemWallet ( { network } ) ;
1268+ const wallet = new MemWallet ( { network } ) ;
12111269
12121270 async function getMockBlock ( chain , txs = [ ] , cb = true ) {
12131271 if ( cb ) {
0 commit comments