From a7ab25a078f9ecb23b55b2ec5cb5d97e0573ce5f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 5 Mar 2017 17:29:20 -0800 Subject: [PATCH] policy: add some safety measures --- lib/protocol/policy.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/protocol/policy.js b/lib/protocol/policy.js index b850c934..8c708697 100644 --- a/lib/protocol/policy.js +++ b/lib/protocol/policy.js @@ -11,6 +11,7 @@ * @module protocol/policy */ +var assert = require('assert'); var consensus = require('./consensus'); /** @@ -221,6 +222,12 @@ exports.getMinFee = function getMinFee(size, rate) { if (rate == null) rate = exports.MIN_RELAY; + assert(size >= 0); + assert(rate >= 0); + + if (size === 0) + return 0; + fee = Math.floor(rate * size / 1000); if (fee === 0 && rate > 0) @@ -244,6 +251,12 @@ exports.getRoundFee = function getRoundFee(size, rate) { if (rate == null) rate = exports.MIN_RELAY; + assert(size >= 0); + assert(rate >= 0); + + if (size === 0) + return 0; + fee = rate * Math.ceil(size / 1000); if (fee === 0 && rate > 0) @@ -260,5 +273,11 @@ exports.getRoundFee = function getRoundFee(size, rate) { */ exports.getRate = function getRate(size, fee) { + assert(size >= 0); + assert(fee >= 0); + + if (size === 0) + return 0; + return Math.floor(fee * 1000 / size); };