Skip to content

Commit

Permalink
some more stuff...not complete yet
Browse files Browse the repository at this point in the history
  • Loading branch information
gasteve committed Jul 9, 2013
1 parent 32c690f commit ebddafc
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 105 deletions.
54 changes: 27 additions & 27 deletions Block.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require('classtool');

function spec(b) {
var Util = b.Util || require('../ext/util');
var util = b.util || require('./util/util');
var Debug1 = b.Debug1 || function() {};
var Script = b.Script || require('./script').class();
var Script = b.Script || require('./Script').class();
var Bignum = b.Bignum || require('bignum');
var Put = b.Put || require('bufferput');
var Step = b.Step || require('step');
var Transaction = b.Transaction || require('./transaction');
var TransactionIn = b.TransactionIn || require('./transactionIn');
var TransactionOut = b.TransactionOut || require('./transactionOut');
var Transaction = b.Transaction || require('./transaction').class();
var TransactionIn = Transaction.In;
var TransactionOut = Transaction.Out;
var COINBASE_OP = Transaction.COINBASE_OP;
var VerificationError = b.VerificationError || require('../ext/error').VerificationError;
var VerificationError = b.VerificationError || require('./util/error').VerificationError;
var BlockRules = {
maxTimeOffset: 2 * 60 * 60, // How far block timestamps can be into the future
largestHash: Bignum(2).pow(256)
Expand All @@ -23,16 +23,16 @@ function spec(b) {
data = {};
}
this.hash = data.hash || null;
this.prev_hash = data.prev_hash || Util.NULL_HASH;
this.merkle_root = data.merkle_root || Util.NULL_HASH;
this.prev_hash = data.prev_hash || util.NULL_HASH;
this.merkle_root = data.merkle_root || util.NULL_HASH;
this.timestamp = data.timestamp || 0;
this.bits = data.bits || 0;
this.nonce = data.nonce || 0;
this.version = data.version || 0;
this.height = data.height || 0;
this.size = data.size || 0;
this.active = data.active || false;
this.chainWork = data.chainWork || Util.EMPTY_BUFFER;
this.chainWork = data.chainWork || util.EMPTY_BUFFER;
this.txs = data.txs || [];
};

Expand All @@ -50,7 +50,7 @@ function spec(b) {
Block.prototype.calcHash = function calcHash() {
var header = this.getHeader();

return Util.twoSha256(header);
return util.twoSha256(header);
};

Block.prototype.checkHash = function checkHash() {
Expand All @@ -65,7 +65,7 @@ function spec(b) {
};

Block.prototype.checkProofOfWork = function checkProofOfWork() {
var target = Util.decodeDiffBits(this.bits);
var target = util.decodeDiffBits(this.bits);

// TODO: Create a compare method in node-buffertools that uses the correct
// endian so we don't have to reverse both buffers before comparing.
Expand All @@ -89,7 +89,7 @@ function spec(b) {
* of all possible hashes would mean that 20 "work" is required to meet it.
*/
Block.prototype.getWork = function getWork() {
var target = Util.decodeDiffBits(this.bits, true);
var target = util.decodeDiffBits(this.bits, true);
return BlockRules.largestHash.div(target.add(1));
};

Expand Down Expand Up @@ -141,7 +141,7 @@ function spec(b) {
// This function is a direct translation of CBlock::BuildMerkleTree().

if (txs.length == 0) {
return [Util.NULL_HASH.slice(0)];
return [util.NULL_HASH.slice(0)];
}

// Start by adding all the hashes of the transactions as leaves of the tree.
Expand All @@ -157,7 +157,7 @@ function spec(b) {
var i2 = Math.min(i + 1, size - 1);
var a = tree[j + i];
var b = tree[j + i2];
tree.push(Util.twoSha256(a.concat(b)));
tree.push(util.twoSha256(a.concat(b)));
}
j += size;
}
Expand Down Expand Up @@ -199,7 +199,7 @@ function spec(b) {
};

Block.getBlockValue = function getBlockValue(height) {
var subsidy = Bignum(50).mul(Util.COIN);
var subsidy = Bignum(50).mul(util.COIN);
subsidy = subsidy.div(Bignum(2).pow(Math.floor(height / 210000)));
return subsidy;
};
Expand All @@ -209,7 +209,7 @@ function spec(b) {
};

Block.prototype.toString = function toString() {
return "<Block " + Util.formatHashAlt(this.hash) + " height="+this.height+">";
return "<Block " + util.formatHashAlt(this.hash) + " height="+this.height+">";
};

/**
Expand Down Expand Up @@ -251,7 +251,7 @@ function spec(b) {
var self = this;

var powLimit = blockChain.getMinDiff();
var powLimitTarget = Util.decodeDiffBits(powLimit, true);
var powLimitTarget = util.decodeDiffBits(powLimit, true);

var targetTimespan = blockChain.getTargetTimespan();
var targetSpacing = blockChain.getTargetSpacing();
Expand Down Expand Up @@ -327,7 +327,7 @@ function spec(b) {
actualTimespan = targetTimespan*4;
}

var oldTarget = Util.decodeDiffBits(self.bits, true);
var oldTarget = util.decodeDiffBits(self.bits, true);
var newTarget = oldTarget.mul(actualTimespan).div(targetTimespan);

if (newTarget.cmp(powLimitTarget) > 0) {
Expand All @@ -339,7 +339,7 @@ function spec(b) {
Debug1('Before: '+oldTarget.toBuffer().toString('hex'));
Debug1('After: '+newTarget.toBuffer().toString('hex'));

callback(null, Util.encodeDiffBits(newTarget));
callback(null, util.encodeDiffBits(newTarget));
} catch (err) {
callback(err);
}
Expand Down Expand Up @@ -426,12 +426,12 @@ function spec(b) {
{
var tx = new Transaction();
tx.ins.push(new TransactionIn({
s: Util.EMPTY_BUFFER,
s: util.EMPTY_BUFFER,
q: 0xffffffff,
o: COINBASE_OP
}));
tx.outs.push(new TransactionOut({
v: Util.bigIntToValue(this.getBlockValue()),
v: util.bigIntToValue(this.getBlockValue()),
s: Script.createPubKeyOut(beneficiary).getBuffer()
}));
return tx;
Expand Down Expand Up @@ -518,7 +518,7 @@ function spec(b) {

Block.prototype.solve = function solve(miner, callback) {
var header = this.getHeader();
var target = Util.decodeDiffBits(this.bits);
var target = util.decodeDiffBits(this.bits);
miner.solve(header, target, callback);
};

Expand All @@ -529,10 +529,10 @@ function spec(b) {
function getStandardizedObject(txs)
{
var block = {
hash: Util.formatHashFull(this.getHash()),
hash: util.formatHashFull(this.getHash()),
version: this.version,
prev_block: Util.formatHashFull(this.prev_hash),
mrkl_root: Util.formatHashFull(this.merkle_root),
prev_block: util.formatHashFull(this.prev_hash),
mrkl_root: util.formatHashFull(this.merkle_root),
time: this.timestamp,
bits: this.bits,
nonce: this.nonce,
Expand All @@ -542,13 +542,13 @@ function spec(b) {

if (txs) {
var mrkl_tree = this.getMerkleTree(txs).map(function (buffer) {
return Util.formatHashFull(buffer);
return util.formatHashFull(buffer);
});
block.mrkl_root = mrkl_tree[mrkl_tree.length - 1];

block.n_tx = txs.length;
var totalSize = 80; // Block header
totalSize += Util.getVarIntSize(txs.length); // txn_count
totalSize += util.getVarIntSize(txs.length); // txn_count
txs = txs.map(function (tx) {
tx = tx.getStandardizedObject();
totalSize += tx.size;
Expand Down
22 changes: 11 additions & 11 deletions Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('classtool');

function spec(b) {
var config = b.config || require('./config');
var log = b.log || require('./util/log')(config);
var network = b.network || require('./networks')[config.network];

var MAX_RECEIVE_BUFFER = 10000000;
Expand All @@ -11,13 +12,12 @@ function spec(b) {
var Put = b.Put || require('bufferput');
var Buffers = b.Buffers || require('buffers');
var noop = function() {};
var log = b.log || require('bitpay/log');
var util = b.util || require('./util/util');
var Parser = b.Parser || require('./util/BinaryParser').class();
var util = require('./util/util');
var doubleSha256 = b.doubleSha256 || util.twoSha256;
var nonce = util.generateNonce();

var Block = require('../model/block').class();
var Block = require('./Block').class();

var BIP0031_VERSION = 60000;

Expand Down Expand Up @@ -170,7 +170,7 @@ function spec(b) {
Connection.prototype.sendVersion = function () {
var subversion = '/BitcoinX:0.1/';

var put = Put();
var put = new Put();
put.word32le(PROTOCOL_VERSION); // version
put.word64le(1); // services
put.word64le(Math.round(new Date().getTime()/1000)); // timestamp
Expand All @@ -185,7 +185,7 @@ function spec(b) {
};

Connection.prototype.sendGetBlocks = function (starts, stop) {
var put = Put();
var put = new Put();
put.word32le(this.sendVer);

put.varint(starts.length);
Expand All @@ -208,7 +208,7 @@ function spec(b) {
};

Connection.prototype.sendGetData = function (invs) {
var put = Put();
var put = new Put();
put.varint(invs.length);
for (var i = 0; i < invs.length; i++) {
put.word32le(invs[i].type);
Expand All @@ -218,13 +218,13 @@ function spec(b) {
};

Connection.prototype.sendGetAddr = function (invs) {
var put = Put();
var put = new Put();
this.sendMessage('getaddr', put.buffer());
};

Connection.prototype.sendInv = function(data) {
if(!Array.isArray(data)) data = [data];
var put = Put();
var put = new Put();
put.varint(data.length);
data.forEach(function (value) {
if (value instanceof Block) {
Expand All @@ -240,7 +240,7 @@ function spec(b) {
};

Connection.prototype.sendHeaders = function (headers) {
var put = Put();
var put = new Put();
put.varint(headers.length);
headers.forEach(function (header) {
put.put(header);
Expand All @@ -256,7 +256,7 @@ function spec(b) {
};

Connection.prototype.sendBlock = function (block, txs) {
var put = Put();
var put = new Put();

// Block header
put.put(block.getHeader());
Expand All @@ -283,7 +283,7 @@ function spec(b) {
checksum = new Buffer([]);
}

var message = Put(); // -- HEADER --
var message = new Put(); // -- HEADER --
message.put(magic); // magic bytes
message.put(commandBuf); // command name
message.pad(12 - commandBuf.length); // zero-padded
Expand Down
11 changes: 4 additions & 7 deletions PeerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ require('classtool');

function spec(b) {
var config = b.config || require('./config');
var log = b.log || require('./util/log')(config);
var network = b.network || require('./networks')[config.network];
var Connection = b.Connection || require('./Connection').createClass({config: config});
var Peer = b.Peer || require('./Peer').class();
var noop = function() {};
var log = b.log || {info: noop, warn: noop, err: noop};

GetAdjustedTime = b.GetAdjustedTime || function () {
// TODO: Implement actual adjustment
Expand All @@ -31,16 +31,14 @@ function spec(b) {

PeerManager.Connection = Connection;

PeerManager.prototype.start = function()
{
PeerManager.prototype.start = function() {
this.active = true;
if(!this.timer) {
this.timer = setInterval(this.checkStatus.bind(this), this.interval);
}
};

PeerManager.prototype.stop = function ()
{
PeerManager.prototype.stop = function() {
this.active = false;
if(this.timer) {
clearInterval(this.timer);
Expand All @@ -63,8 +61,7 @@ function spec(b) {
}
};

PeerManager.prototype.checkStatus = function checkStatus()
{
PeerManager.prototype.checkStatus = function checkStatus() {
// Make sure we are connected to all forcePeers
if(this.peers.length) {
var peerIndex = {};
Expand Down
22 changes: 12 additions & 10 deletions Script.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
require('classtool');

function spec(b) {
var config = b.config || require('./config');
var log = b.log || require('./util/log')(config);

var Opcode = require('./opcode').class();

// Make opcodes available as pseudo-constants
for (var i in Opcode.map) {
eval(i + " = " + Opcode.map[i] + ";");
}

var logger = b.logger || require('../ext/logger');
var Util = b.Util || require('../ext/util');
var Parser = b.Parser || require('../ext/binaryParser').class();
var util = b.util || require('./util/util');
var Parser = b.Parser || require('./util/BinaryParser').class();
var Put = b.Put || require('bufferput');

function Script(buffer) {
if(buffer) {
this.buffer = buffer;
} else {
this.buffer = Util.EMPTY_BUFFER;
this.buffer = util.EMPTY_BUFFER;
}
this.chunks = [];
this.parse();
Expand Down Expand Up @@ -84,10 +86,10 @@ function spec(b) {
case 'Address':
return this.chunks[2];
case 'Pubkey':
return Util.sha256ripe160(this.chunks[0]);
return util.sha256ripe160(this.chunks[0]);
default:
logger.scrdbg("Encountered non-standard scriptPubKey");
logger.scrdbg("Strange script was: " + this.toString());
log.debug("Encountered non-standard scriptPubKey");
log.debug("Strange script was: " + this.toString());
return null;
}
};
Expand All @@ -114,8 +116,8 @@ function spec(b) {
case 'Pubkey':
return null;
default:
logger.scrdbg("Encountered non-standard scriptSig");
logger.scrdbg("Strange script was: " + this.toString());
log.debug("Encountered non-standard scriptSig");
log.debug("Strange script was: " + this.toString());
return null;
}
};
Expand Down Expand Up @@ -144,7 +146,7 @@ function spec(b) {
}

if (Buffer.isBuffer(chunk)) {
script += "0x"+Util.formatBuffer(chunk, truncate ? null : 0);
script += "0x"+util.formatBuffer(chunk, truncate ? null : 0);
} else {
script += Opcode.reverseMap[chunk];
}
Expand Down
Loading

0 comments on commit ebddafc

Please sign in to comment.