Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split domain management #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,15 @@ class Chain extends AsyncEmitter {
assert(Buffer.isBuffer(hash));
assert((height >>> 0) === height);

// Cannot renew yet.
// Orignal comment: Cannot renew yet.
// This basically allows you to renew domains
// without having a mature enough block hash in the
// first 30 days of blockchain (on mainnet)
// and first 50 blocks on regtest. Considering
// names could not have been bought in that period,
// and the orignal comment, this seems to be a mistake
// and was supposed be return false;

if (height < this.network.names.renewalMaturity)
return true;

Expand Down
49 changes: 18 additions & 31 deletions lib/primitives/mtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,12 +1122,20 @@ class MTX extends TX {
assert(options, 'Options are required.');
assert(options.changeAddress, 'Change address is required.');

// Hack in place to ensure backward compataibility with previous hack
if (this.inputs.length > 0) {
const inputPrevoutKeys = this.inputs.map(input => input.prevout.toKey());
for (const coin of coins) {
const {hash, index} = coin;
const key = Outpoint.toKey(hash, index);
if ( inputPrevoutKeys.some(prevout => prevout.equals(key)) ) {
this.view.addCoin(coin);
}
}
}
// Select necessary coins.
const select = await this.selectCoins(coins, options);

// Make sure we empty the input array.
this.inputs.length = 0;

// Add coins to transaction.
for (const coin of select.chosen)
this.addCoin(coin);
Expand Down Expand Up @@ -1424,6 +1432,7 @@ class CoinSelector {
*/

constructor(tx, options) {
this.parent = tx;
this.tx = tx.clone();
this.coins = [];
this.outputValue = 0;
Expand Down Expand Up @@ -1569,6 +1578,12 @@ class CoinSelector {
for (let i = 0; i < this.tx.inputs.length; i++) {
const {prevout} = this.tx.inputs[i];
this.inputs.set(prevout.toKey(), i);
const coin = this.parent.view.getCoin(prevout);
// If the coin is not in the view, it's value cannot be known,
// therefore it can't be funded correctly.
if(!coin)
throw new Error('Could not resolve input coin value');
this.tx.view.addCoin(coin);
}
}
}
Expand All @@ -1585,7 +1600,6 @@ class CoinSelector {
this.chosen = [];
this.change = 0;
this.fee = CoinSelector.MIN_FEE;
this.tx.inputs.length = 0;

switch (this.selection) {
case 'all':
Expand Down Expand Up @@ -1688,33 +1702,6 @@ class CoinSelector {
*/

fund() {
// Ensure all preferred inputs first.
if (this.inputs.size > 0) {
const coins = [];

for (let i = 0; i < this.inputs.size; i++)
coins.push(null);

for (const coin of this.coins) {
const {hash, index} = coin;
const key = Outpoint.toKey(hash, index);
const i = this.inputs.get(key);

if (i != null) {
coins[i] = coin;
this.inputs.delete(key);
}
}

if (this.inputs.size > 0)
throw new Error('Could not resolve preferred inputs.');

for (const coin of coins) {
this.tx.addCoin(coin);
this.chosen.push(coin);
}
}

if (this.isFull())
return;

Expand Down
25 changes: 12 additions & 13 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const {types} = rules;
const {Mnemonic} = HD;
const {BufferSet} = require('buffer-map');
const Coin = require('../primitives/coin');
const Outpoint = require('../primitives/outpoint');

/*
* Constants
Expand Down Expand Up @@ -1839,7 +1838,7 @@ class Wallet extends EventEmitter {
output.covenant.pushHash(nameHash);
output.covenant.pushU32(height);
output.covenant.pushHash(nonce);
reveal.addOutpoint(Outpoint.fromTX(bid, bidOuputIndex));
reveal.addCoin(bidCoin);
reveal.outputs.push(output);

await this.fill(reveal, { ...options, coins: coins });
Expand Down Expand Up @@ -1926,7 +1925,7 @@ class Wallet extends EventEmitter {
output.covenant.pushU32(ns.height);
output.covenant.pushHash(nonce);

mtx.addOutpoint(prevout);
mtx.addCoin(coin);
mtx.outputs.push(output);
}

Expand Down Expand Up @@ -2051,7 +2050,7 @@ class Wallet extends EventEmitter {
output.covenant.pushU32(ns.height);
output.covenant.pushHash(nonce);

mtx.addOutpoint(prevout);
mtx.addCoin(coin);
mtx.outputs.push(output);
}

Expand Down Expand Up @@ -2176,7 +2175,7 @@ class Wallet extends EventEmitter {
if (coin.height < ns.height)
continue;

mtx.addOutpoint(prevout);
mtx.addCoin(coin);

const output = new Output();
output.address = coin.address;
Expand Down Expand Up @@ -2298,7 +2297,7 @@ class Wallet extends EventEmitter {
if (coin.height < ns.height)
continue;

mtx.addOutpoint(prevout);
mtx.addCoin(coin);

const output = new Output();
output.address = coin.address;
Expand Down Expand Up @@ -2444,7 +2443,7 @@ class Wallet extends EventEmitter {
output.covenant.pushHash(await this.wdb.getRenewalBlock());

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return mtx;
Expand Down Expand Up @@ -2524,7 +2523,7 @@ class Wallet extends EventEmitter {
output.covenant.push(raw);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return mtx;
Expand Down Expand Up @@ -2663,7 +2662,7 @@ class Wallet extends EventEmitter {
output.covenant.pushHash(await this.wdb.getRenewalBlock());

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return mtx;
Expand Down Expand Up @@ -2797,7 +2796,7 @@ class Wallet extends EventEmitter {
output.covenant.push(address.hash);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return mtx;
Expand Down Expand Up @@ -2933,7 +2932,7 @@ class Wallet extends EventEmitter {
output.covenant.push(EMPTY);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return mtx;
Expand Down Expand Up @@ -3077,7 +3076,7 @@ class Wallet extends EventEmitter {
output.covenant.pushHash(await this.wdb.getRenewalBlock());

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return mtx;
Expand Down Expand Up @@ -3208,7 +3207,7 @@ class Wallet extends EventEmitter {
output.covenant.pushU32(ns.height);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return mtx;
Expand Down
7 changes: 5 additions & 2 deletions test/anyone-can-renew-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ describe('Anyone-can-renew address', function() {

aliceReceive = await alice.receiveAddress();
bobReceive = await bob.receiveAddress();

// Skip past first 50 blocks so renewal is possible.
await mineBlocks(50);
});

after(async () => {
Expand Down Expand Up @@ -275,7 +278,7 @@ describe('Anyone-can-renew address', function() {
mtx.output(0).covenant.type = rules.types.RENEW;
mtx.output(0).covenant.pushHash(nameHash);
mtx.output(0).covenant.pushU32(heightBeforeOpen + 1);
mtx.output(0).covenant.pushHash(node.chain.tip.hash);
mtx.output(0).covenant.pushHash(await wdb.getRenewalBlock());

await alice.fund(mtx, {coins: [coin]});
await alice.finalize(mtx, {coins: [coin]});
Expand All @@ -300,7 +303,7 @@ describe('Anyone-can-renew address', function() {
mtx.output(0).covenant.type = rules.types.RENEW;
mtx.output(0).covenant.pushHash(nameHash);
mtx.output(0).covenant.pushU32(heightBeforeOpen + 1);
mtx.output(0).covenant.pushHash(node.chain.tip.hash);
mtx.output(0).covenant.pushHash(await wdb.getRenewalBlock());

await bob.fund(mtx, {coins: [coin]});
await bob.finalize(mtx, {coins: [coin]});
Expand Down
13 changes: 5 additions & 8 deletions test/interactive-swap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,12 @@ describe('Interactive name swap', function() {

// Bob should verify all the data in the MTX to ensure everything is valid,
// but this is the minimum.
const input0 = mtx.input(0).clone(); // copy input with Alice's signature
const coinEntry = await node.chain.db.readCoin(input0.prevout);
const coinEntry = await node.chain.db.readCoin(mtx.input(0).prevout);
assert(coinEntry); // ensures that coin exists and is still unspent

const coin = coinEntry.toCoin(input0.prevout);
const coin = coinEntry.toCoin(mtx.input(0).prevout);
mtx.view.addCoin(coin);

assert(coin.covenant.type === types.TRANSFER);
const addr = new Address({
version: coin.covenant.items[2].readInt8(),
Expand All @@ -226,12 +227,8 @@ describe('Interactive name swap', function() {
const changeAddress = await bob.changeAddress();
const rate = await wdb.estimateFee();
const coins = await bob.getSmartCoins();
// Add the external coin to the coin selector so we don't fail assertions
coins.push(coin);

await mtx.fund(coins, {changeAddress, rate});
// The funding mechanism starts by wiping out existing inputs
// which for us includes Alice's signature. Replace it from our backup.
mtx.inputs[0].inject(input0);

// Rearrange outputs.
// Since we added a change output, the SINGELREVERSE is now broken:
Expand Down
Loading