@@ -22,6 +22,7 @@ const Witness = require('../lib/script/witness');
2222const CoinView = require ( '../lib/coins/coinview' ) ;
2323const util = require ( '../lib/utils/util' ) ;
2424const consensus = require ( '../lib/protocol/consensus' ) ;
25+ const policy = require ( '../lib/protocol/policy' ) ;
2526const MemWallet = require ( './util/memwallet' ) ;
2627const ALL = Script . hashType . ALL ;
2728const common = require ( '../lib/blockchain/common' ) ;
@@ -63,7 +64,7 @@ const wallet = new MemWallet({ network });
6364
6465let cachedTX = null ;
6566
66- function dummyInput ( addr , hash ) {
67+ function dummyInput ( addr , hash , value = 70000 ) {
6768 const coin = new Coin ( ) ;
6869 coin . height = 0 ;
6970 coin . value = 0 ;
@@ -73,7 +74,7 @@ function dummyInput(addr, hash) {
7374
7475 const fund = new MTX ( ) ;
7576 fund . addCoin ( coin ) ;
76- fund . addOutput ( addr , 70000 ) ;
77+ fund . addOutput ( addr , value ) ;
7778
7879 const [ tx , view ] = fund . commit ( ) ;
7980
@@ -465,6 +466,85 @@ describe('Mempool', function() {
465466 }
466467 } ) ;
467468
469+ it ( 'should reject absurd fee' , async ( ) => {
470+ const wallet = new MemWallet ( { network } ) ;
471+ const addr = wallet . getAddress ( ) ;
472+ const funds = 10000e6 ;
473+
474+ const mtx = new MTX ( ) ;
475+ mtx . addCoin (
476+ dummyInput (
477+ addr ,
478+ random . randomBytes ( 32 ) ,
479+ funds
480+ )
481+ ) ;
482+ mtx . addOutput ( wallet . getAddress ( ) , 0 ) ; // temp
483+ wallet . sign ( mtx ) ;
484+
485+ const vsize = mtx . getVirtualSize ( ) ;
486+ const minFee = ( vsize / 1000 ) * network . minRelay ;
487+ const absurdFee = minFee * policy . ABSURD_FEE_FACTOR ;
488+
489+ // Revise with exactly absurd fee
490+ mtx . outputs [ 0 ] . value = funds - absurdFee - 1 ;
491+ mtx . inputs [ 0 ] . witness . items . length = 0 ;
492+ wallet . sign ( mtx ) ;
493+ const tx1 = mtx . toTX ( ) ;
494+
495+ await assert . rejects (
496+ mempool . addTX ( tx1 ) ,
497+ { message : / a b s u r d l y - h i g h - f e e / }
498+ ) ;
499+
500+ // Revise again with just under absurd fee
501+ mtx . outputs [ 0 ] . value = funds - absurdFee ;
502+ mtx . inputs [ 0 ] . witness . items . length = 0 ;
503+ wallet . sign ( mtx ) ;
504+ const tx2 = mtx . toTX ( ) ;
505+
506+ await mempool . addTX ( tx2 ) ;
507+ } ) ;
508+
509+ it ( 'should reject too-low fee' , async ( ) => {
510+ const wallet = new MemWallet ( { network } ) ;
511+ const addr = wallet . getAddress ( ) ;
512+ const funds = 10000e6 ;
513+
514+ const mtx = new MTX ( ) ;
515+ mtx . addCoin (
516+ dummyInput (
517+ addr ,
518+ random . randomBytes ( 32 ) ,
519+ funds
520+ )
521+ ) ;
522+ mtx . addOutput ( wallet . getAddress ( ) , 0 ) ; // temp
523+ wallet . sign ( mtx ) ;
524+
525+ const vsize = mtx . getVirtualSize ( ) ;
526+ const minFee = ( vsize / 1000 ) * network . minRelay ;
527+
528+ // Revise with just under minFee
529+ mtx . outputs [ 0 ] . value = funds - minFee + 1 ;
530+ mtx . inputs [ 0 ] . witness . items . length = 0 ;
531+ wallet . sign ( mtx ) ;
532+ const tx1 = mtx . toTX ( ) ;
533+
534+ await assert . rejects (
535+ mempool . addTX ( tx1 ) ,
536+ { message : / i n s u f f i c i e n t p r i o r i t y / }
537+ ) ;
538+
539+ // Revise again with exactly minFee
540+ mtx . outputs [ 0 ] . value = funds - minFee ;
541+ mtx . inputs [ 0 ] . witness . items . length = 0 ;
542+ wallet . sign ( mtx ) ;
543+ const tx2 = mtx . toTX ( ) ;
544+
545+ await mempool . addTX ( tx2 ) ;
546+ } ) ;
547+
468548 it ( 'should destroy mempool' , async ( ) => {
469549 await mempool . close ( ) ;
470550 await chain . close ( ) ;
0 commit comments