more serialization.

This commit is contained in:
Christopher Jeffrey 2016-03-06 12:17:33 -08:00
parent 2b1a822823
commit c9d8ae9f4a
6 changed files with 47 additions and 108 deletions

View File

@ -355,9 +355,14 @@ Block.fromRaw = function fromRaw(data, enc, type) {
Block.prototype.toSmall = function toSmall() {
var block = this.abbr();
var buf = new Buffer(block.length + 4 + utils.sizeIntv(this.txs.length) + this.txs.length * 32);
var height = this.height;
var off = 0;
var buf;
buf = new Buffer(
block.length + 4
+ utils.sizeIntv(this.txs.length)
+ this.txs.length * 32);
if (height === -1)
height = 0x7fffffff;

View File

@ -242,7 +242,7 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) {
if (address)
batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY);
batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toRaw());
batch.put('u/t/' + hash + '/' + i, bcoin.coin(tx, i).toExtended());
});
});
@ -312,7 +312,7 @@ BlockDB.prototype.disconnectBlock = function disconnectBlock(hash, callback, bat
batch.put('u/t/'
+ input.prevout.hash
+ '/' + input.prevout.index,
input.output.toRaw());
input.output.toExtended());
});
tx.outputs.forEach(function(output, i) {

View File

@ -160,7 +160,7 @@ Coin.fromJSON = function fromJSON(json) {
};
Coin.prototype.toRaw = function toRaw(enc) {
var data = bcoin.protocol.framer.coin(this, true);
var data = bcoin.protocol.framer.coin(this, false);
if (enc === 'hex')
data = utils.toHex(data);
@ -172,7 +172,7 @@ Coin._fromRaw = function _fromRaw(data, enc) {
if (enc === 'hex')
data = new Buffer(data, 'hex');
data = bcoin.protocol.parser.parseCoin(data, true);
data = bcoin.protocol.parser.parseCoin(data, false);
return data;
};
@ -181,6 +181,28 @@ Coin.fromRaw = function fromRaw(data, enc) {
return new Coin(Coin._fromRaw(data, enc));
};
Coin.prototype.toExtended = function toExtended(enc) {
var data = bcoin.protocol.framer.coin(this, true);
if (enc === 'hex')
data = utils.toHex(data);
return data;
};
Coin._fromExtended = function _fromExtended(data, enc) {
if (enc === 'hex')
data = new Buffer(data, 'hex');
data = bcoin.protocol.parser.parseCoin(data, true);
return data;
};
Coin.fromExtended = function fromExtended(data, enc) {
return new Coin(Coin._fromExtended(data, enc));
};
/**
* Expose
*/

View File

@ -1076,115 +1076,24 @@ MTX.prototype.increaseFee = function increaseFee(unspent, address, fee) {
}
};
MTX.prototype.toCompact = function toCompact(coins) {
return {
type: 'mtx',
block: this.block,
height: this.height,
ts: this.ts,
ps: this.ps,
changeIndex: this.changeIndex,
coins: coins ? this.inputs.map(function(input) {
return input.output ? input.output.toRaw('hex') : null;
}) : null,
tx: utils.toHex(this.render())
};
};
MTX._fromCompact = function _fromCompact(json) {
var raw, data, tx;
assert.equal(json.type, 'mtx');
raw = new Buffer(json.tx, 'hex');
data = bcoin.protocol.parser.parseTX(raw);
data.height = json.height;
data.block = json.block;
data.ts = json.ts;
data.ps = json.ps;
data.changeIndex = json.changeIndex;
if (json.coins) {
json.coins.forEach(function(output, i) {
if (!output)
return;
data.inputs[i].output = bcoin.coin._fromRaw(output, 'hex');
});
}
return data;
};
MTX.fromCompact = function fromCompact(json) {
return new MTX(MTX._fromCompact(json));
};
MTX.prototype.toJSON = function toJSON() {
return {
type: 'mtx',
hash: utils.revHex(this.hash('hex')),
witnessHash: utils.revHex(this.witnessHash('hex')),
height: this.height,
block: this.block ? utils.revHex(this.block) : null,
ts: this.ts,
ps: this.ps,
changeIndex: this.changeIndex,
version: this.version,
inputs: this.inputs.map(function(input) {
return input.toJSON();
}),
outputs: this.outputs.map(function(output) {
return output.toJSON();
}),
locktime: this.locktime
};
};
MTX._fromJSON = function fromJSON(json) {
assert.equal(json.type, 'mtx');
return {
block: json.block ? utils.revHex(json.block) : null,
height: json.height,
ts: json.ts,
ps: json.ps,
changeIndex: json.changeIndex,
version: json.version,
inputs: json.inputs.map(function(input) {
return bcoin.input._fromJSON(input);
}),
outputs: json.outputs.map(function(output) {
return bcoin.output._fromJSON(output);
}),
locktime: json.locktime
};
};
MTX._fromJSON = bcoin.tx._fromJSON;
MTX.fromJSON = function fromJSON(json) {
return new MTX(MTX._fromJSON(json));
};
MTX.prototype.toRaw = function toRaw(enc) {
var data = this.render();
if (enc === 'hex')
data = utils.toHex(data);
return data;
};
MTX._fromRaw = function _fromRaw(data, enc) {
if (enc === 'hex')
data = new Buffer(data, 'hex');
return bcoin.protocol.parser.parseTX(data);
};
MTX._fromRaw = bcoin.tx._fromRaw;
MTX.fromRaw = function fromRaw(data, enc) {
return new MTX(MTX._fromRaw(data, enc));
};
MTX._fromExtended = bcoin.tx._fromExtended;
MTX.fromExtended = function fromExtended(data, enc) {
return new MTX(MTX._fromExtended(data, enc));
};
MTX.fromTX = function fromTX(tx) {
return new MTX(tx);
};

View File

@ -1013,6 +1013,7 @@ TX.prototype.toJSON = function toJSON() {
ts: this.ts,
ps: this.ps,
index: this.index,
changeIndex: this.changeIndex || -1,
version: this.version,
inputs: this.inputs.map(function(input) {
return input.toJSON();
@ -1032,6 +1033,7 @@ TX._fromJSON = function fromJSON(json) {
ts: json.ts,
ps: json.ps,
index: json.index,
changeIndex: json.changeIndex || -1,
version: json.version,
inputs: json.inputs.map(function(input) {
return bcoin.input._fromJSON(input);
@ -1084,6 +1086,7 @@ TX.prototype.toExtended = function toExtended(coins) {
off += utils.writeU32(buf, this.index, off);
off += utils.writeU32(buf, this.ts, off);
off += utils.writeU32(buf, this.ps, off);
// off += utils.writeU32(buf, this.changeIndex || -1, off);
if (coins) {
off += utils.writeIntv(buf, this.inputs.length, off);

View File

@ -463,7 +463,7 @@ TXPool.prototype._add = function add(tx, map, callback, force) {
});
}
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw());
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended());
updated = true;
}
@ -552,7 +552,7 @@ TXPool.prototype._confirm = function _confirm(tx, map, callback) {
coin.height = tx.height;
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw());
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended());
next();
});
@ -679,7 +679,7 @@ TXPool.prototype._remove = function remove(tx, map, callback) {
batch.put(prefix + 'u/t/'
+ input.prevout.hash
+ '/' + input.prevout.index,
input.output.toRaw());
input.output.toExtended());
batch.del(prefix + 'o/' + input.prevout.hash + '/' + input.prevout.index);
});
@ -784,7 +784,7 @@ TXPool.prototype._unconfirm = function unconfirm(tx, map, callback) {
coin.height = tx.height;
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw());
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended());
next();
});