refactor tx.
This commit is contained in:
parent
f714bbf2c0
commit
ae230cf294
@ -63,6 +63,7 @@ function MTX(options) {
|
|||||||
options = {};
|
options = {};
|
||||||
|
|
||||||
this.version = options.version || 1;
|
this.version = options.version || 1;
|
||||||
|
this.flag = options.flag || 1;
|
||||||
this.inputs = [];
|
this.inputs = [];
|
||||||
this.outputs = [];
|
this.outputs = [];
|
||||||
this.locktime = 0;
|
this.locktime = 0;
|
||||||
|
|||||||
@ -56,6 +56,7 @@ function TX(data) {
|
|||||||
|
|
||||||
assert(data, 'TX data is required.');
|
assert(data, 'TX data is required.');
|
||||||
assert(typeof data.version === 'number');
|
assert(typeof data.version === 'number');
|
||||||
|
assert(typeof data.flag === 'number');
|
||||||
assert(Array.isArray(data.inputs));
|
assert(Array.isArray(data.inputs));
|
||||||
assert(Array.isArray(data.outputs));
|
assert(Array.isArray(data.outputs));
|
||||||
assert(typeof data.locktime === 'number');
|
assert(typeof data.locktime === 'number');
|
||||||
@ -1429,7 +1430,8 @@ TX.prototype.getModifiedSize = function getModifiedSize(size) {
|
|||||||
* @param {Number?} size - Size to calculate priority
|
* @param {Number?} size - Size to calculate priority
|
||||||
* based on. If not present, modified size will be
|
* based on. If not present, modified size will be
|
||||||
* calculated and used.
|
* calculated and used.
|
||||||
* @returns {Number} priority
|
* @returns {Object} data - Object containing
|
||||||
|
* `priority` and `value`.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TX.prototype.getPriority = function getPriority(height, size) {
|
TX.prototype.getPriority = function getPriority(height, size) {
|
||||||
@ -1485,7 +1487,7 @@ TX.prototype.getPriority = function getPriority(height, size) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TX.prototype.isFree = function isFree(height, size) {
|
TX.prototype.isFree = function isFree(height, size) {
|
||||||
var priority;
|
var data;
|
||||||
|
|
||||||
if (height == null) {
|
if (height == null) {
|
||||||
height = this.height;
|
height = this.height;
|
||||||
@ -1493,9 +1495,9 @@ TX.prototype.isFree = function isFree(height, size) {
|
|||||||
height = bcoin.network.get().height + 1;
|
height = bcoin.network.get().height + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
priority = this.getPriority(height, size).priority;
|
data = this.getPriority(height, size);
|
||||||
|
|
||||||
return priority > constants.tx.FREE_THRESHOLD;
|
return data.priority > constants.tx.FREE_THRESHOLD;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1528,38 +1530,6 @@ TX.prototype.getRate = function getRate(size) {
|
|||||||
return TX.getRate(size, this.getFee());
|
return TX.getRate(size, this.getFee());
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate minimum fee based on rate and size.
|
|
||||||
* @param {Number?} size
|
|
||||||
* @param {Rate?} rate - Rate of satoshi per kB.
|
|
||||||
* @returns {Amount} fee
|
|
||||||
*/
|
|
||||||
|
|
||||||
TX.getMinFee = function getMinFee(size, rate) {
|
|
||||||
var fee;
|
|
||||||
|
|
||||||
if (rate == null)
|
|
||||||
rate = constants.tx.MIN_RELAY;
|
|
||||||
|
|
||||||
fee = Math.floor(rate * size / 1000);
|
|
||||||
|
|
||||||
if (fee === 0 && rate > 0)
|
|
||||||
fee = rate;
|
|
||||||
|
|
||||||
return fee;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate a fee rate based on size and fees.
|
|
||||||
* @param {Number} size
|
|
||||||
* @param {Amount} fee
|
|
||||||
* @returns {Rate}
|
|
||||||
*/
|
|
||||||
|
|
||||||
TX.getRate = function(size, fee) {
|
|
||||||
return Math.floor(fee * 1000 / size);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the minimum fee in order for the transaction
|
* Calculate the minimum fee in order for the transaction
|
||||||
* to be relayable, but _round to the nearest kilobyte
|
* to be relayable, but _round to the nearest kilobyte
|
||||||
@ -1730,6 +1700,38 @@ TX.prototype.__defineGetter__('wtxid', function() {
|
|||||||
return this.rwhash;
|
return this.rwhash;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate minimum fee based on rate and size.
|
||||||
|
* @param {Number?} size
|
||||||
|
* @param {Rate?} rate - Rate of satoshi per kB.
|
||||||
|
* @returns {Amount} fee
|
||||||
|
*/
|
||||||
|
|
||||||
|
TX.getMinFee = function getMinFee(size, rate) {
|
||||||
|
var fee;
|
||||||
|
|
||||||
|
if (rate == null)
|
||||||
|
rate = constants.tx.MIN_RELAY;
|
||||||
|
|
||||||
|
fee = Math.floor(rate * size / 1000);
|
||||||
|
|
||||||
|
if (fee === 0 && rate > 0)
|
||||||
|
fee = rate;
|
||||||
|
|
||||||
|
return fee;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate a fee rate based on size and fees.
|
||||||
|
* @param {Number} size
|
||||||
|
* @param {Amount} fee
|
||||||
|
* @returns {Rate}
|
||||||
|
*/
|
||||||
|
|
||||||
|
TX.getRate = function(size, fee) {
|
||||||
|
return Math.floor(fee * 1000 / size);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inspect the transaction and return a more
|
* Inspect the transaction and return a more
|
||||||
* user-friendly representation of the data.
|
* user-friendly representation of the data.
|
||||||
@ -1739,8 +1741,8 @@ TX.prototype.__defineGetter__('wtxid', function() {
|
|||||||
TX.prototype.inspect = function inspect() {
|
TX.prototype.inspect = function inspect() {
|
||||||
return {
|
return {
|
||||||
type: 'tx',
|
type: 'tx',
|
||||||
hash: utils.revHex(this.hash('hex')),
|
hash: this.rhash,
|
||||||
witnessHash: utils.revHex(this.witnessHash('hex')),
|
witnessHash: this.rwhash,
|
||||||
size: this.getSize(),
|
size: this.getSize(),
|
||||||
virtualSize: this.maxSize(),
|
virtualSize: this.maxSize(),
|
||||||
height: this.height,
|
height: this.height,
|
||||||
@ -1757,6 +1759,7 @@ TX.prototype.inspect = function inspect() {
|
|||||||
index: this.index,
|
index: this.index,
|
||||||
changeIndex: this.changeIndex || -1,
|
changeIndex: this.changeIndex || -1,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
|
flag: this.flag,
|
||||||
inputs: this.inputs,
|
inputs: this.inputs,
|
||||||
outputs: this.outputs,
|
outputs: this.outputs,
|
||||||
locktime: this.locktime
|
locktime: this.locktime
|
||||||
@ -1783,6 +1786,7 @@ TX.prototype.toJSON = function toJSON() {
|
|||||||
index: this.index,
|
index: this.index,
|
||||||
changeIndex: this.changeIndex || -1,
|
changeIndex: this.changeIndex || -1,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
|
flag: this.flag,
|
||||||
inputs: this.inputs.map(function(input) {
|
inputs: this.inputs.map(function(input) {
|
||||||
return input.toJSON();
|
return input.toJSON();
|
||||||
}),
|
}),
|
||||||
@ -1811,6 +1815,7 @@ TX.parseJSON = function fromJSON(json) {
|
|||||||
index: json.index,
|
index: json.index,
|
||||||
changeIndex: json.changeIndex || -1,
|
changeIndex: json.changeIndex || -1,
|
||||||
version: json.version,
|
version: json.version,
|
||||||
|
flag: json.flag,
|
||||||
inputs: json.inputs.map(function(input) {
|
inputs: json.inputs.map(function(input) {
|
||||||
return bcoin.input.parseJSON(input);
|
return bcoin.input.parseJSON(input);
|
||||||
}),
|
}),
|
||||||
|
|||||||
@ -30,6 +30,7 @@ function createGenesisBlock(options) {
|
|||||||
|
|
||||||
tx = {
|
tx = {
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [{
|
inputs: [{
|
||||||
prevout: {
|
prevout: {
|
||||||
hash: constants.NULL_HASH,
|
hash: constants.NULL_HASH,
|
||||||
|
|||||||
@ -297,6 +297,7 @@ describe('Script', function() {
|
|||||||
it('should handle script test' + suffix + ': ' + comments, function() {
|
it('should handle script test' + suffix + ': ' + comments, function() {
|
||||||
var coin = bcoin.tx({
|
var coin = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [{
|
inputs: [{
|
||||||
prevout: {
|
prevout: {
|
||||||
hash: constants.NULL_HASH,
|
hash: constants.NULL_HASH,
|
||||||
@ -315,6 +316,7 @@ describe('Script', function() {
|
|||||||
});
|
});
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [{
|
inputs: [{
|
||||||
prevout: {
|
prevout: {
|
||||||
hash: coin.hash('hex'),
|
hash: coin.hash('hex'),
|
||||||
|
|||||||
@ -306,6 +306,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >51 bit coin values', function() {
|
it('should fail on >51 bit coin values', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY + 1)],
|
inputs: [createInput(constants.MAX_MONEY + 1)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -320,6 +321,7 @@ describe('TX', function() {
|
|||||||
it('should handle 51 bit coin values', function() {
|
it('should handle 51 bit coin values', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY)],
|
inputs: [createInput(constants.MAX_MONEY)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -334,6 +336,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >51 bit output values', function() {
|
it('should fail on >51 bit output values', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY)],
|
inputs: [createInput(constants.MAX_MONEY)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -348,6 +351,7 @@ describe('TX', function() {
|
|||||||
it('should handle 51 bit output values', function() {
|
it('should handle 51 bit output values', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY)],
|
inputs: [createInput(constants.MAX_MONEY)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -362,6 +366,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >51 bit fees', function() {
|
it('should fail on >51 bit fees', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY + 1)],
|
inputs: [createInput(constants.MAX_MONEY + 1)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -376,6 +381,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >51 bit values from multiple', function() {
|
it('should fail on >51 bit values from multiple', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [
|
inputs: [
|
||||||
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
||||||
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
||||||
@ -394,6 +400,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >51 bit output values from multiple', function() {
|
it('should fail on >51 bit output values from multiple', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY)],
|
inputs: [createInput(constants.MAX_MONEY)],
|
||||||
outputs: [
|
outputs: [
|
||||||
{
|
{
|
||||||
@ -418,6 +425,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >51 bit fees from multiple', function() {
|
it('should fail on >51 bit fees from multiple', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [
|
inputs: [
|
||||||
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
||||||
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
createInput(Math.floor(constants.MAX_MONEY / 2)),
|
||||||
@ -439,6 +447,7 @@ describe('TX', function() {
|
|||||||
for (var i = 0; i < 3; i++) {
|
for (var i = 0; i < 3; i++) {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [
|
inputs: [
|
||||||
createInput(Math.floor(constants.MAX_MONEY / 2))
|
createInput(Math.floor(constants.MAX_MONEY / 2))
|
||||||
],
|
],
|
||||||
@ -456,6 +465,7 @@ describe('TX', function() {
|
|||||||
it('should fail to parse >53 bit values', function() {
|
it('should fail to parse >53 bit values', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [
|
inputs: [
|
||||||
createInput(Math.floor(constants.MAX_MONEY / 2))
|
createInput(Math.floor(constants.MAX_MONEY / 2))
|
||||||
],
|
],
|
||||||
@ -483,6 +493,7 @@ describe('TX', function() {
|
|||||||
it('should fail on 53 bit coin values', function() {
|
it('should fail on 53 bit coin values', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(utils.MAX_SAFE_INTEGER)],
|
inputs: [createInput(utils.MAX_SAFE_INTEGER)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -497,6 +508,7 @@ describe('TX', function() {
|
|||||||
it('should fail on 53 bit output values', function() {
|
it('should fail on 53 bit output values', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY)],
|
inputs: [createInput(constants.MAX_MONEY)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -511,6 +523,7 @@ describe('TX', function() {
|
|||||||
it('should fail on 53 bit fees', function() {
|
it('should fail on 53 bit fees', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(utils.MAX_SAFE_INTEGER)],
|
inputs: [createInput(utils.MAX_SAFE_INTEGER)],
|
||||||
outputs: [{
|
outputs: [{
|
||||||
script: [],
|
script: [],
|
||||||
@ -526,6 +539,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >53 bit values from multiple', function() {
|
it('should fail on >53 bit values from multiple', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [
|
inputs: [
|
||||||
createInput(MAX),
|
createInput(MAX),
|
||||||
createInput(MAX),
|
createInput(MAX),
|
||||||
@ -544,6 +558,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >53 bit output values from multiple', function() {
|
it('should fail on >53 bit output values from multiple', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [createInput(constants.MAX_MONEY)],
|
inputs: [createInput(constants.MAX_MONEY)],
|
||||||
outputs: [
|
outputs: [
|
||||||
{
|
{
|
||||||
@ -568,6 +583,7 @@ describe('TX', function() {
|
|||||||
it('should fail on >53 bit fees from multiple', function() {
|
it('should fail on >53 bit fees from multiple', function() {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [
|
inputs: [
|
||||||
createInput(MAX),
|
createInput(MAX),
|
||||||
createInput(MAX),
|
createInput(MAX),
|
||||||
@ -589,6 +605,7 @@ describe('TX', function() {
|
|||||||
for (var i = 0; i < 3; i++) {
|
for (var i = 0; i < 3; i++) {
|
||||||
var tx = bcoin.tx({
|
var tx = bcoin.tx({
|
||||||
version: 1,
|
version: 1,
|
||||||
|
flag: 1,
|
||||||
inputs: [
|
inputs: [
|
||||||
createInput(MAX)
|
createInput(MAX)
|
||||||
],
|
],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user