segwit things

This commit is contained in:
Christopher Jeffrey 2016-02-27 05:51:48 -08:00
parent 77c9348824
commit bd868cda7a
6 changed files with 33 additions and 5 deletions

View File

@ -49,6 +49,9 @@ Block.prototype.render = function render() {
return bcoin.protocol.framer.block(this);
}
if (this.hasWitness())
return bcoin.protocol.framer.block(this);
this._raw = bcoin.protocol.framer.block(this);
this._size = this._raw.length;
this._witness = false;
@ -63,6 +66,9 @@ Block.prototype.renderWitness = function renderWitness() {
return bcoin.protocol.framer.witnessBlock(this);
}
if (!this.hasWitness())
return bcoin.protocol.framer.witnessBlock(this);
this._raw = bcoin.protocol.framer.witnessBlock(this);
this._size = this._raw.length;
this._witness = true;
@ -80,6 +86,14 @@ Block.prototype.getCost = function getCost() {
return this._cost;
};
Block.prototype.hasWitness = function hasWitness() {
for (var i = 0; i < this.txs.length; i++) {
if (this.txs[i].hasWitness())
return true;
}
return false;
};
Block.prototype.getSigopsCost = function getSigopCost(scriptHash) {
var cost = 0;
var i;
@ -128,7 +142,7 @@ Block.prototype.getCommitmentHash = function getCommitmentHash() {
};
Block.prototype.__defineGetter__('commitmentHash', function() {
var coinbase, i, commitment, witnessNonce, commitmentHash;
var coinbase, i, commitment, commitmentHash;
if (this._commitmentHash)
return this._commitmentHash;

View File

@ -348,7 +348,7 @@ Chain.prototype._preload = function _preload(callback) {
blocks.forEach(function(data) {
var entry = bcoin.chainblock.fromRaw(self, height, data);
var block = bcoin.block(entry, 'headers');
var block = bcoin.headers(entry);
var start;
// Do some paranoid checks.
@ -364,7 +364,7 @@ Chain.prototype._preload = function _preload(callback) {
// Verify the block headers. We don't want to
// trust an external centralized source completely.
if (!block.verify()) {
if (!block.verifyHeaders()) {
start = Math.max(0, height - 2);
stream.destroy();
self.resetHeightAsync(start, function(e) {
@ -655,6 +655,12 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
var i, j, input, hash;
var sigops = 0;
utils.print(height);
utils.print(block.commitmentHash ? utils.revHex(block.commitmentHash) : null);
utils.print(utils.revHex(block.getCommitmentHash() || '00'));
utils.print(block.txs[0]);
utils.print(block.txs[1]);
if (err)
return callback(err);

View File

@ -586,7 +586,7 @@ Framer._block = function _block(block, witness) {
var i, tx, p;
for (i = 0; i < block.txs.length; i++) {
tx = witness
tx = witness && block.txs[i].hasWitness()
? Framer.witnessTX(block.txs[i])
: Framer.tx(block.txs[i]);
txs.push(tx);

View File

@ -461,8 +461,12 @@ script.verifyProgram = function verifyProgram(witness, output, tx, i, flags) {
}
}
utils.print(script.format(stack));
res = script.execute(redeem, stack, tx, i, flags);
utils.print(script.format(redeem));
utils.print(script.format(stack));
// Verify the script did not fail as well as the stack values
if (!res || stack.length === 0 || !script.bool(stack.pop())) {
throw new Error('script failed');

View File

@ -120,6 +120,10 @@ TX.prototype.witnessHash = function witnessHash(enc) {
: new Buffer(constants.zeroHash);
}
// if (!this._witness)
if (!this.hasWitness())
return this.hash(enc);
if (!this._whash)
this._whash = utils.dsha256(this.renderWitness());

View File

@ -44,7 +44,7 @@ function Wallet(options) {
options.master = bcoin.hd.fromSeed();
this.options = options;
this.db = options.db || new bcoin.walletdb({ type: 'file' });
this.db = options.db || new bcoin.walletdb();
this.addresses = [];
this.master = options.master || null;
this.addressMap = options.addressMap || {};