more serialization.
This commit is contained in:
parent
2b1a822823
commit
c9d8ae9f4a
@ -355,9 +355,14 @@ Block.fromRaw = function fromRaw(data, enc, type) {
|
|||||||
|
|
||||||
Block.prototype.toSmall = function toSmall() {
|
Block.prototype.toSmall = function toSmall() {
|
||||||
var block = this.abbr();
|
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 height = this.height;
|
||||||
var off = 0;
|
var off = 0;
|
||||||
|
var buf;
|
||||||
|
|
||||||
|
buf = new Buffer(
|
||||||
|
block.length + 4
|
||||||
|
+ utils.sizeIntv(this.txs.length)
|
||||||
|
+ this.txs.length * 32);
|
||||||
|
|
||||||
if (height === -1)
|
if (height === -1)
|
||||||
height = 0x7fffffff;
|
height = 0x7fffffff;
|
||||||
|
|||||||
@ -242,7 +242,7 @@ BlockDB.prototype.connectBlock = function connectBlock(block, callback, batch) {
|
|||||||
if (address)
|
if (address)
|
||||||
batch.put('u/a/' + address + '/' + hash + '/' + i, DUMMY);
|
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/'
|
batch.put('u/t/'
|
||||||
+ input.prevout.hash
|
+ input.prevout.hash
|
||||||
+ '/' + input.prevout.index,
|
+ '/' + input.prevout.index,
|
||||||
input.output.toRaw());
|
input.output.toExtended());
|
||||||
});
|
});
|
||||||
|
|
||||||
tx.outputs.forEach(function(output, i) {
|
tx.outputs.forEach(function(output, i) {
|
||||||
|
|||||||
@ -160,7 +160,7 @@ Coin.fromJSON = function fromJSON(json) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Coin.prototype.toRaw = function toRaw(enc) {
|
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')
|
if (enc === 'hex')
|
||||||
data = utils.toHex(data);
|
data = utils.toHex(data);
|
||||||
@ -172,7 +172,7 @@ Coin._fromRaw = function _fromRaw(data, enc) {
|
|||||||
if (enc === 'hex')
|
if (enc === 'hex')
|
||||||
data = new Buffer(data, 'hex');
|
data = new Buffer(data, 'hex');
|
||||||
|
|
||||||
data = bcoin.protocol.parser.parseCoin(data, true);
|
data = bcoin.protocol.parser.parseCoin(data, false);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
@ -181,6 +181,28 @@ Coin.fromRaw = function fromRaw(data, enc) {
|
|||||||
return new Coin(Coin._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
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
107
lib/bcoin/mtx.js
107
lib/bcoin/mtx.js
@ -1076,115 +1076,24 @@ MTX.prototype.increaseFee = function increaseFee(unspent, address, fee) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
MTX.prototype.toCompact = function toCompact(coins) {
|
MTX._fromJSON = bcoin.tx._fromJSON;
|
||||||
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 = function fromJSON(json) {
|
MTX.fromJSON = function fromJSON(json) {
|
||||||
return new MTX(MTX._fromJSON(json));
|
return new MTX(MTX._fromJSON(json));
|
||||||
};
|
};
|
||||||
|
|
||||||
MTX.prototype.toRaw = function toRaw(enc) {
|
MTX._fromRaw = bcoin.tx._fromRaw;
|
||||||
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 = function fromRaw(data, enc) {
|
MTX.fromRaw = function fromRaw(data, enc) {
|
||||||
return new MTX(MTX._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) {
|
MTX.fromTX = function fromTX(tx) {
|
||||||
return new MTX(tx);
|
return new MTX(tx);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1013,6 +1013,7 @@ TX.prototype.toJSON = function toJSON() {
|
|||||||
ts: this.ts,
|
ts: this.ts,
|
||||||
ps: this.ps,
|
ps: this.ps,
|
||||||
index: this.index,
|
index: this.index,
|
||||||
|
changeIndex: this.changeIndex || -1,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
inputs: this.inputs.map(function(input) {
|
inputs: this.inputs.map(function(input) {
|
||||||
return input.toJSON();
|
return input.toJSON();
|
||||||
@ -1032,6 +1033,7 @@ TX._fromJSON = function fromJSON(json) {
|
|||||||
ts: json.ts,
|
ts: json.ts,
|
||||||
ps: json.ps,
|
ps: json.ps,
|
||||||
index: json.index,
|
index: json.index,
|
||||||
|
changeIndex: json.changeIndex || -1,
|
||||||
version: json.version,
|
version: json.version,
|
||||||
inputs: json.inputs.map(function(input) {
|
inputs: json.inputs.map(function(input) {
|
||||||
return bcoin.input._fromJSON(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.index, off);
|
||||||
off += utils.writeU32(buf, this.ts, off);
|
off += utils.writeU32(buf, this.ts, off);
|
||||||
off += utils.writeU32(buf, this.ps, off);
|
off += utils.writeU32(buf, this.ps, off);
|
||||||
|
// off += utils.writeU32(buf, this.changeIndex || -1, off);
|
||||||
|
|
||||||
if (coins) {
|
if (coins) {
|
||||||
off += utils.writeIntv(buf, this.inputs.length, off);
|
off += utils.writeIntv(buf, this.inputs.length, off);
|
||||||
|
|||||||
@ -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;
|
updated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -552,7 +552,7 @@ TXPool.prototype._confirm = function _confirm(tx, map, callback) {
|
|||||||
|
|
||||||
coin.height = tx.height;
|
coin.height = tx.height;
|
||||||
|
|
||||||
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw());
|
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended());
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
@ -679,7 +679,7 @@ TXPool.prototype._remove = function remove(tx, map, callback) {
|
|||||||
batch.put(prefix + 'u/t/'
|
batch.put(prefix + 'u/t/'
|
||||||
+ input.prevout.hash
|
+ input.prevout.hash
|
||||||
+ '/' + input.prevout.index,
|
+ '/' + input.prevout.index,
|
||||||
input.output.toRaw());
|
input.output.toExtended());
|
||||||
|
|
||||||
batch.del(prefix + 'o/' + input.prevout.hash + '/' + input.prevout.index);
|
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;
|
coin.height = tx.height;
|
||||||
|
|
||||||
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toRaw());
|
batch.put(prefix + 'u/t/' + hash + '/' + i, coin.toExtended());
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user