Browse Source

Connection: parse TXs, blocks via their object parsers.

patch-2
Jeff Garzik 12 years ago
parent
commit
420773ac39
  1. 76
      Connection.js

76
Connection.js

@ -13,6 +13,8 @@ function spec(b) {
var Buffers = b.Buffers || require('buffers'); var Buffers = b.Buffers || require('buffers');
require('./Buffers.monkey').patch(Buffers); require('./Buffers.monkey').patch(Buffers);
var noop = function() {}; var noop = function() {};
var Block = require('./Block').class();
var Transaction = require('./Transaction').class();
var util = b.util || require('./util/util'); var util = b.util || require('./util/util');
var Parser = b.Parser || require('./util/BinaryParser').class(); var Parser = b.Parser || require('./util/BinaryParser').class();
var doubleSha256 = b.doubleSha256 || util.twoSha256; var doubleSha256 = b.doubleSha256 || util.twoSha256;
@ -431,31 +433,32 @@ function spec(b) {
break; break;
case 'block': case 'block':
data.version = parser.word32le(); var block = new Block();
data.prev_hash = parser.buffer(32); block.parse(parser);
data.merkle_root = parser.buffer(32);
data.timestamp = parser.word32le();
data.bits = parser.word32le();
data.nonce = parser.word32le();
var txCount = parser.varInt(); data.block = block;
data.version = block.version;
data.prev_hash = block.prev_hash;
data.merkle_root = block.merkle_root;
data.timestamp = block.timestamp;
data.bits = block.bits;
data.nonce = block.nonce;
data.txs = []; data.txs = block.txs;
for (i = 0; i < txCount; i++) {
data.txs.push(Connection.parseTx(parser));
}
data.size = payload.length; data.size = payload.length;
break; break;
case 'tx': case 'tx':
var txData = Connection.parseTx(parser); var tx = new Transaction();
tx.parse(parser);
return { return {
command: command, command: command,
version: txData.version, version: tx.version,
lock_time: txData.lock_time, lock_time: tx.lock_time,
ins: txData.ins, ins: tx.ins,
outs: txData.outs outs: tx.outs,
tx: tx,
}; };
case 'getblocks': case 'getblocks':
@ -521,47 +524,6 @@ function spec(b) {
return data; return data;
}; };
Connection.parseTx = function (parser) {
if (Buffer.isBuffer(parser)) {
parser = new Parser(parser);
}
var data = {}, i, sLen, startPos = parser.pos;
data.version = parser.word32le();
var txinCount = parser.varInt();
data.ins = [];
for (j = 0; j < txinCount; j++) {
var txin = {};
txin.o = parser.buffer(36); // outpoint
sLen = parser.varInt(); // script_len
txin.s = parser.buffer(sLen); // script
txin.q = parser.word32le(); // sequence
data.ins.push(txin);
}
var txoutCount = parser.varInt();
data.outs = [];
for (j = 0; j < txoutCount; j++) {
var txout = {};
txout.v = parser.buffer(8); // value
sLen = parser.varInt(); // script_len
txout.s = parser.buffer(sLen); // script
data.outs.push(txout);
}
data.lock_time = parser.word32le();
var endPos = parser.pos;
data.buffer = parser.subject.slice(startPos, endPos);
return data;
};
return Connection; return Connection;
}; };
module.defineClass(spec); module.defineClass(spec);

Loading…
Cancel
Save