Skip to content

Commit

Permalink
Merge PR#764 from 'pinheadmz/limit-batch'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Sep 23, 2022
2 parents fb5501c + 4e9c7fc commit 6f11212
Show file tree
Hide file tree
Showing 12 changed files with 1,067 additions and 49 deletions.
6 changes: 3 additions & 3 deletions lib/mining/miner.js
Expand Up @@ -596,21 +596,21 @@ class MinerOptions {
if (options.maxOpens != null) {
assert((options.maxOpens >>> 0) === options.maxOpens);
assert(options.maxOpens <= consensus.MAX_BLOCK_OPENS,
'Max sigops must be below MAX_BLOCK_OPENS');
'Max opens must be below MAX_BLOCK_OPENS');
this.maxOpens = options.maxOpens;
}

if (options.maxUpdates != null) {
assert((options.maxUpdates >>> 0) === options.maxUpdates);
assert(options.maxUpdates <= consensus.MAX_BLOCK_UPDATES,
'Max sigops must be below MAX_BLOCK_UPDATES');
'Max updates must be below MAX_BLOCK_UPDATES');
this.maxUpdates = options.maxUpdates;
}

if (options.maxRenewals != null) {
assert((options.maxRenewals >>> 0) === options.maxRenewals);
assert(options.maxRenewals <= consensus.MAX_BLOCK_RENEWALS,
'Max sigops must be below MAX_BLOCK_RENEWALS');
'Max renewals must be below MAX_BLOCK_RENEWALS');
this.maxRenewals = options.maxRenewals;
}

Expand Down
30 changes: 10 additions & 20 deletions lib/primitives/mtx.js
Expand Up @@ -963,18 +963,8 @@ class MTX extends TX {
for (const {prevout} of this.inputs) {
const coin = this.view.getOutput(prevout);

// We're out of luck here.
// Just assume it's a p2pkh.
if (!coin) {
total += 110;
continue;
}

// Previous output script.
const addr = coin.address;

// P2WPKH
if (addr.isPubkeyhash()) {
// No coin, assume pkh
if (!coin || coin.address.isPubkeyhash()) {
let size = 0;
// varint-items-len
size += 1;
Expand All @@ -988,30 +978,30 @@ class MTX extends TX {
continue;
}

// Call out to the custom estimator.
// Call out to the custom witness estimator.
if (estimate) {
const size = await estimate(addr);
let size = await estimate(coin.address);
if (size !== -1) {
// vsize
size = (size + scale - 1) / scale | 0;
total += size;
continue;
}
}

// P2WSH
if (addr.isScripthash()) {
// Unknown script hash, take a wild guess
// and estimate for 2-of-3 multisig.
{
let size = 0;
// varint-items-len
size += 1;
// 2-of-3 multisig input
size += 149;
size += 239;
// vsize
size = (size + scale - 1) / scale | 0;
total += size;
continue;
}

// Unknown.
total += 110;
}

return total;
Expand Down
10 changes: 7 additions & 3 deletions lib/primitives/tx.js
Expand Up @@ -212,9 +212,8 @@ class TX extends bio.Struct {
*/

getWeight() {
const {base, witness} = this.getSizes();
const total = base + witness;
return base * (consensus.WITNESS_SCALE_FACTOR - 1) + total;
const sizes = this.getSizes();
return sizes.getWeight();
}

/**
Expand Down Expand Up @@ -2023,6 +2022,11 @@ class Sizes {
this.base = base;
this.witness = witness;
}

getWeight() {
const total = this.base + this.witness;
return this.base * (consensus.WITNESS_SCALE_FACTOR - 1) + total;
}
}

/*
Expand Down
33 changes: 26 additions & 7 deletions lib/wallet/rpc.js
Expand Up @@ -2489,7 +2489,9 @@ class RPC extends RPCBase {
}

async createBatch(args, help) {
const [actions, options] = this._validateBatch(args, help, 'sendbatch');
const [actions, options] = this._validateBatch(args, help, 'createbatch');
options.paths = true;

const wallet = this.wallet;
const mtx = await wallet.createBatch(actions, options);

Expand All @@ -2499,12 +2501,12 @@ class RPC extends RPCBase {
_validateBatch(args, help, method) {
if (help || args.length < 1 || args.length > 2) {
throw new RPCError(errs.MISC_ERROR,
`${method} ["type", ...args] ( options )`);
`${method} [["type", ...args], ...] ( options )`);
}

const valid = new Validator(args);
const check = valid.array(0);
const options = valid.obj(1);
const options = valid.obj(1, {});
const actions = [];

for (const action of check) {
Expand Down Expand Up @@ -2580,6 +2582,19 @@ class RPC extends RPCBase {
actions.push([type, name, resource]);
break;
}
case 'RENEW': {
assert(
action.length === 0 || action.length === 1,
'RENEW action can only have 1 argument: name'
);
if (action.length === 1) {
const {name} = this._validateRenewal(action);
actions.push([type, name]);
} else {
actions.push([type]);
}
break;
}
case 'TRANSFER': {
assert(
action.length === 2,
Expand All @@ -2591,11 +2606,15 @@ class RPC extends RPCBase {
}
case 'FINALIZE': {
assert(
action.length === 1,
'FINALIZE action requires 1 argument: name'
action.length === 0 || action.length === 1,
'FINALIZE can only have 1 argument: name'
);
const {name} = this._validateFinalize(action);
actions.push([type, name]);
if (action.length === 1) {
const {name} = this._validateFinalize(action);
actions.push([type, name]);
} else {
actions.push([type]);
}
break;
}
case 'CANCEL': {
Expand Down

0 comments on commit 6f11212

Please sign in to comment.