Browse Source

Add missing serializations

patch-2
Yemel Jardi 10 years ago
parent
commit
2d4b26a6a1
  1. 50
      lib/transport/messages.js

50
lib/transport/messages.js

@ -5,12 +5,13 @@ var buffertools = require('buffertools');
var Put = require('bufferput');
var util = require('util');
var Block = require('../block');
var BlockHeaderModel = require('../blockheader');
var BlockModel = require('../block');
var BufferReader = require('../encoding/bufferreader');
var BufferUtil = require('../util/buffer');
var Hash = require('../crypto/hash');
var Random = require('../crypto/random');
var Transaction = require('../transaction');
var TransactionModel = require('../transaction');
var CONNECTION_NONCE = Random.getPseudoRandomBuffer(8);
var PROTOCOL_VERSION = 70000;
@ -303,7 +304,17 @@ Addresses.prototype.fromBuffer = function(payload) {
};
Addresses.prototype.getPayload = function() {
return BufferUtil.EMPTY_BUFFER; // TODO
var put = new Put();
put.varint(this.addresses.length);
for (var i = 0; i < this.addresses.length; i++) {
put.word32le(this.addresses[i].time);
put.word64le(this.addresses[i].services);
put.put(this.addresses[i].ip);
put.word16be(this.addresses[i].port);
}
return put.buffer();
};
module.exports.Addresses = Message.COMMANDS['addr'] = Addresses;
@ -360,7 +371,14 @@ Alert.prototype.fromBuffer = function(payload) {
};
Alert.prototype.getPayload = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
var put = new Put();
put.varint(this.payload.length);
put.put(this.payload);
put.varint(this.signature.length);
put.put(this.signature);
return put.buffer();
};
module.exports.Alert = Message.COMMANDS['alert'] = Alert;
@ -382,7 +400,7 @@ Headers.prototype.fromBuffer = function(payload) {
this.headers = [];
for (var i = 0; i < count; i++) {
var header = Block().fromBufferReader(parser);
var header = BlockHeaderModel._fromBufferReader(parser);
this.headers.push(header);
}
@ -390,7 +408,15 @@ Headers.prototype.fromBuffer = function(payload) {
};
Headers.prototype.getPayload = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
var put = new Put();
put.varint(this.headers.length);
for (var i = 0; i < this.headers.length; i++) {
var buffer = this.headers[i].toBuffer();
put.put(buffer);
}
return put.buffer();
};
module.exports.Headers = Message.COMMANDS['headers'] = Headers;
@ -407,13 +433,12 @@ function Block(block) {
util.inherits(Block, Message);
Block.prototype.fromBuffer = function(payload) {
var parser = new BufferReader(payload);
this.block = Block().fromBufferReader(parser);
this.block = BlockModel(payload);
return this;
};
Block.prototype.getPayload = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
return this.block.toBuffer();
};
module.exports.Block = Message.COMMANDS['block'] = Block;
@ -430,13 +455,12 @@ function Transaction(transaction) {
util.inherits(Transaction, Message);
Transaction.prototype.fromBuffer = function(payload) {
var parser = new BufferReader(payload);
this.transaction = Transaction().fromBufferReader(parser);
this.transaction = TransactionModel(payload);
return this;
};
Transaction.prototype.getPayload = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
return this.transaction.toBuffer();
};
module.exports.Transaction = Message.COMMANDS['tx'] = Transaction;
@ -474,7 +498,7 @@ GetBlocks.prototype.getPayload = function() {
put.word32le(this.version);
put.varint(this.starts.length);
for (var i = 0; i < starts.length; i++) {
for (var i = 0; i < this.starts.length; i++) {
if (this.starts[i].length != 32) {
throw new Error('Invalid hash length');
}

Loading…
Cancel
Save