Initial commit for bitcore-p2p

This commit is contained in:
Esteban Ordano 2014-12-24 13:30:55 -03:00 committed by Braydon Fuller
parent 4dbfb7eeba
commit 1863a6547b
20 changed files with 391 additions and 40 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
*.sw[a-z]
coverage
node_modules
bitcore-scaffolding.js
bitcore-scaffolding.min.js
lib/errors/index.js
npm-debug.log
bower_components
report
.DS_Store

36
.jsdoc.conf Normal file
View File

@ -0,0 +1,36 @@
{
"tags": {
"allowUnknownTags": true
},
"source": {
"include": ["docs/README.md"],
"exclude": [],
"includePattern": "lib/.+\\.js(doc)?$",
"excludePattern": "(^|\\/|\\\\)_"
},
"plugins": ["plugins/markdown"],
"templates": {
"cleverLinks": false,
"monospaceLinks": false
},
"opts": {
"template": "node_modules/ink-docstrap/template",
"encoding": "utf8",
"destination": "./apiref/",
"recurse": true,
"query": "value",
"private": true,
"lenient": true
},
"templates": {
"systemName": "bitcore",
"copyright": "© 2013-2015, BitPay Inc.",
"navType": "vertical",
"theme": "journal",
"linenums": true,
"collapseSymbols": false,
"inverseNav": false,
"outputSourceFiles": true
}
}

44
.jshintrc Normal file
View File

@ -0,0 +1,44 @@
{
"bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.).
"browser": true, // Standard browser globals e.g. `window`, `document`.
"camelcase": false, // Permit only camelcase for `var` and `object indexes`.
"curly": true, // Require {} for every new block or scope.
"devel": false, // Allow development statements e.g. `console.log();`.
"eqeqeq": true, // Require triple equals i.e. `===`.
"esnext": true, // Allow ES.next specific features such as `const` and `let`.
"freeze": true, // Forbid overwriting prototypes of native objects such as Array, Date and so on.
"immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
"indent": 2, // Specify indentation spacing
"latedef": true, // Prohibit variable use before definition.
"newcap": false, // Require capitalization of all constructor functions e.g. `new F()`.
"noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`.
"node": true, // Enable globals available when code is running inside of the NodeJS runtime environment.
"noempty": true, // Prohibit use of empty blocks.
"nonew": true, // Prohibits the use of constructor functions for side-effects
"quotmark": "single", // Define quotes to string values.
"regexp": true, // Prohibit `.` and `[^...]` in regular expressions.
"smarttabs": false, // Supress warnings about mixed tabs and spaces
"strict": true, // Require `use strict` pragma in every file.
"trailing": true, // Prohibit trailing whitespaces.
"undef": true, // Require all non-global variables be declared before they are used.
"unused": true, // Warn unused variables.
"maxparams": 4, // Maximum number of parameters for a function
"maxstatements": 15, // Maximum number of statements in a function
"maxcomplexity": 6, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity)
"maxdepth": 4, // Maximum depth of nested control structures
"maxlen": 120, // Maximum number of cols in a line
"predef": [ // Extra globals.
"after",
"afterEach",
"before",
"beforeEach",
"define",
"describe",
"exports",
"it",
"module",
"require"
]
}

13
.travis.yml Normal file
View File

@ -0,0 +1,13 @@
language: node_js
node_js:
- '0.10'
before_install:
- npm install -g bower
- npm install -g grunt-cli
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
install:
- bower install
- npm install
after_script:
- gulp coveralls

3
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,3 @@
# Contributing
Please see [CONTRIBUTING.md](https://github.com/bitpay/bitcore/blob/master/CONTRIBUTING.md) on the main bitcore repo.

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2015 BitPay, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -1,4 +1,17 @@
node-libcoin
============
bitcore-p2p
=======
bitcore-p2p adds support for connecting to the bitcoin p2p network for node.
See [the main bitcore repo](https://github.com/bitpay/bitcore) for more information.
## Contributing
See [CONTRIBUTING.md](https://github.com/bitpay/bitcore) on the main bitcore repo for information about how to contribute.
## License
Code released under [the MIT license](https://github.com/bitpay/bitcore/blob/master/LICENSE).
Copyright 2013-2015 BitPay, Inc. Bitcore is a trademark maintained by BitPay, Inc.
Nodejs Bitcoin Library

92
gulpfile.js Normal file
View File

@ -0,0 +1,92 @@
/**
* @file gulpfile.js
*
* Defines tasks that can be run on gulp.
*
* Summary: <ul>
* <li> `test` - runs all the tests on node
* <li> `test:nofail` - internally used for watching (due to bug on gulp-mocha)
* <li> `watch:test` - watch for file changes and run tests
* <li> `lint` - run `jshint`
* <li> `coverage` - run `istanbul` with mocha to generate a report of test coverage
* <li> `coveralls` - updates coveralls info
* <li> `release` - automates release process (only for bitcore maintainers)
* </ul>
*/
'use strict';
var gulp = require('gulp');
var coveralls = require('gulp-coveralls');
var jshint = require('gulp-jshint');
var mocha = require('gulp-mocha');
var shell = require('gulp-shell');
var files = ['lib/**/*.js'];
var tests = ['test/**/*.js'];
var alljs = files.concat(tests);
function ignoreError() {
/* jshint ignore:start */ // using `this` in this context is weird
this.emit('end');
/* jshint ignore:end */
}
var testMocha = function() {
return gulp.src(tests).pipe(new mocha({
reporter: 'spec'
}));
};
/**
* Testing
*/
gulp.task('test', testMocha);
gulp.task('test:nofail', function() {
return testMocha().on('error', ignoreError);
});
/**
* Code quality and documentation
*/
gulp.task('lint', function() {
return gulp.src(alljs)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('plato', shell.task(['plato -d report -r -l .jshintrc -t bitcore lib']));
gulp.task('coverage', shell.task(['node_modules/.bin/./istanbul cover node_modules/.bin/_mocha -- --recursive']));
gulp.task('coveralls', ['coverage'], function() {
gulp.src('coverage/lcov.info').pipe(coveralls());
});
/**
* Watch tasks
*/
gulp.task('watch:test', function() {
// TODO: Only run tests that are linked to file changes by doing
// something smart like reading through the require statements
return gulp.watch(alljs, ['test']);
});
gulp.task('watch:coverage', function() {
// TODO: Only run tests that are linked to file changes by doing
// something smart like reading through the require statements
return gulp.watch(alljs, ['coverage']);
});
gulp.task('watch:lint', function() {
// TODO: Only lint files that are linked to file changes by doing
// something smart like reading through the require statements
return gulp.watch(alljs, ['lint']);
});
/* Default task */
gulp.task('default', ['lint', 'coverage'], function() { });

4
index.js Normal file
View File

@ -0,0 +1,4 @@
var bitcore = require('bitcore-base');
bitcore.P2P = require('./lib');
module.exports = bitcore.P2P;

13
karma.conf.js Normal file
View File

@ -0,0 +1,13 @@
'use strict';
// karma.conf.js
module.exports = function(config) {
config.set({
frameworks: ['mocha'],
browsers: ['Chrome', 'Firefox'],
singleRun: true,
files: [
'browser/tests.js'
]
});
};

View File

@ -1,5 +1,5 @@
/**
* @namespace Transport
* @namespace P2P
*/
module.exports = {
Messages: require('./messages'),

View File

@ -1,6 +1,6 @@
'use strict';
/**
* @namespace Transport.Message
* @namespace P2P.Message
*/
/* jshint curly: false */
@ -8,13 +8,15 @@ var Buffers = require('buffers');
var Put = require('bufferput');
var util = require('util');
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 TransactionModel = require('../transaction');
var bitcore = require('bitcore-base');
var BlockHeaderModel = bitcore.BlockHeader;
var BlockModel = bitcore.Block;
var BufferReader = bitcore.encoding.BufferReader;
var BufferUtil = bitcore.util.buffer;
var Hash = bitcore.crypto.Hash;
var Random = bitcore.crypto.Random;
var TransactionModel = bitcore.Transaction;
var CONNECTION_NONCE = Random.getPseudoRandomBuffer(8);
var PROTOCOL_VERSION = 70000;
@ -22,7 +24,7 @@ var PROTOCOL_VERSION = 70000;
/**
* Static helper for consuming a data buffer until the next message.
*
* @name Transport.Message#parseMessage
* @name P2P.Message#parseMessage
* @param{Network} network - the network object
* @param{Buffer} dataBuffer - the buffer to read from
* @returns{Message|undefined} A message or undefined if there is nothing to read.
@ -60,7 +62,7 @@ module.exports.parseMessage = parseMessage;
/**
* @desc Internal function that discards data until founds the next message.
* @name Transport.Message#discardUntilNextMessage
* @name P2P.Message#discardUntilNextMessage
*/
function discardUntilNextMessage(network, dataBuffer) {
var magicNumber = network.networkMagic;
@ -87,19 +89,19 @@ function discardUntilNextMessage(network, dataBuffer) {
/**
* Abstract Message that knows how to parse and serialize itself.
* Concret subclases should implement {fromBuffer} and {getPayload} methods.
* @name Transport.Message
* @name P2P.Message
*/
function Message() {}
/**
* @value
* @name Transport.Message.COMMANDS
* @name P2P.Message.COMMANDS
*/
Message.COMMANDS = {};
/**
* Look up a message type by command name and instantiate the correct Message
* @name Transport.Message#buildMessage
* @name P2P.Message#buildMessage
*/
Message.buildMessage = function(command, payload) {
try {
@ -162,7 +164,7 @@ module.exports.Message = Message;
/**
* Version Message
*
* @name Transport.Message.Version
* @name P2P.Message.Version
* @param{string} subversion - version of the client
* @param{Buffer} nonce - a random 8 bytes buffer
*/
@ -208,7 +210,7 @@ module.exports.Version = Message.COMMANDS.version = Version;
/**
* Inv Message
*
* @name Transport.Message.Inventory
* @name P2P.Message.Inventory
* @param{Array} inventory - reported elements
*/
function Inventory(inventory) {
@ -247,7 +249,7 @@ module.exports.Inventory = Message.COMMANDS.inv = Inventory;
/**
* Getdata Message
*
* @name Transport.Message.GetData
* @name P2P.Message.GetData
* @param{Array} inventory - requested elements
*/
function GetData(inventory) {
@ -261,7 +263,7 @@ module.exports.GetData = GetData;
/**
* Ping Message
*
* @name Transport.Message.Ping
* @name P2P.Message.Ping
* @param{Buffer} nonce - a random 8 bytes buffer
*/
function Ping(nonce) {
@ -284,7 +286,7 @@ module.exports.Ping = Message.COMMANDS.ping = Ping;
/**
* Pong Message
*
* @name Transport.Message.Pong
* @name P2P.Message.Pong
* @param{Buffer} nonce - a random 8 bytes buffer
*/
function Pong(nonce) {
@ -298,7 +300,7 @@ module.exports.Pong = Message.COMMANDS.pong = Pong;
/**
* Addr Message
*
* @name Transport.Message.Addressess
* @name P2P.Message.Addressess
* @param{Array} addresses - array of know addresses
*/
function Addresses(addresses) {
@ -364,7 +366,7 @@ module.exports.Addresses = Message.COMMANDS.addr = Addresses;
/**
* GetAddr Message
*
* @name Transport.Message.GetAddresses
* @name P2P.Message.GetAddresses
*/
function GetAddresses() {
this.command = 'getaddr';
@ -376,7 +378,7 @@ module.exports.GetAddresses = Message.COMMANDS.getaddr = GetAddresses;
/**
* Verack Message
*
* @name Transport.Message.VerAck
* @name P2P.Message.VerAck
*/
function VerAck() {
this.command = 'verack';
@ -388,7 +390,7 @@ module.exports.VerAck = Message.COMMANDS.verack = VerAck;
/**
* Reject Message
*
* @name Transport.Message.Reject
* @name P2P.Message.Reject
*/
function Reject() {
this.command = 'reject';
@ -402,7 +404,7 @@ module.exports.Reject = Message.COMMANDS.reject = Reject;
/**
* Alert Message
*
* @name Transport.Message.Alert
* @name P2P.Message.Alert
*/
function Alert(payload, signature) {
this.command = 'alert';
@ -434,7 +436,7 @@ module.exports.Alert = Message.COMMANDS.alert = Alert;
/**
* Headers Message
*
* @name Transport.Message.Headers
* @name P2P.Message.Headers
* @param{Array} blockheaders - array of block headers
*/
function Headers(blockheaders) {
@ -473,7 +475,7 @@ module.exports.Headers = Message.COMMANDS.headers = Headers;
/**
* Block Message
*
* @name Transport.Message.Block
* @name P2P.Message.Block
* @param{Block} block
*/
function Block(block) {
@ -496,7 +498,7 @@ module.exports.Block = Message.COMMANDS.block = Block;
/**
* Tx Message
*
* @name Transport.Message.Transaction
* @name P2P.Message.Transaction
* @param{Transaction} transaction
*/
function Transaction(transaction) {
@ -519,7 +521,7 @@ module.exports.Transaction = Message.COMMANDS.tx = Transaction;
/**
* Getblocks Message
*
* @name Transport.Message.GetBlocks
* @name P2P.Message.GetBlocks
* @param{Array} starts - array of buffers with the starting block hashes
* @param{Buffer} [stop] - hash of the last block
*/
@ -570,7 +572,7 @@ module.exports.GetBlocks = Message.COMMANDS.getblocks = GetBlocks;
/**
* Getheaders Message
*
* @name Transport.Message.GetHeaders
* @name P2P.Message.GetHeaders
* @param{Array} starts - array of buffers with the starting block hashes
* @param{Buffer} [stop] - hash of the last block
*/

View File

@ -6,7 +6,8 @@ var Net = require('net');
var Socks5Client = require('socks5-client');
var util = require('util');
var Networks = require('../networks');
var bitcore = require('bitcore-base');
var Networks = bitcore.Networks;
var Messages = require('./messages');
var MAX_RECEIVE_BUFFER = 10000000;

View File

@ -2,8 +2,10 @@
var dns = require('dns');
var EventEmitter = require('events').EventEmitter;
var Networks = require('../networks');
var sha256 = require('../crypto/hash').sha256;
var bitcore = require('bitcore-base');
var Networks = bitcore.Networks;
var sha256 = bitcore.crypto.Hash.sha256;
var Peer = require('./peer');
var util = require('util');

54
package.json Normal file
View File

@ -0,0 +1,54 @@
{
"name": "bitcore-p2p",
"version": "0.8.5",
"description": "Interface to the bitcoin P2P network for bitcore",
"author": "BitPay <dev@bitpay.com>",
"main": "index.js",
"scripts": {
"lint": "gulp lint",
"test": "gulp test",
"coverage": "gulp coverage",
"build": "gulp",
"postinstall": "node ./lib/errors/build.js"
},
"contributors": [
{
"name": "Esteban Ordano",
"email": "eordano@gmail.com"
}
],
"keywords": [
"bitcoin",
"bitcore"
],
"repository": {
"type": "git",
"url": "https://github.com/bitpay/bitcore-p2p.git"
},
"dependencies": {
"bitcore-base": "~0.8.5"
},
"devDependencies": {
"browserify": "~6.3.3",
"chai": "~1.10.0",
"gulp": "^3.8.10",
"gulp-bump": "^0.1.11",
"gulp-coveralls": "^0.1.3",
"gulp-git": "^0.5.5",
"gulp-jshint": "^1.9.0",
"gulp-mocha": "^2.0.0",
"gulp-rename": "^1.2.0",
"gulp-shell": "^0.2.10",
"gulp-uglify": "^1.0.2",
"gulp-util": "=3.0.1",
"istanbul": "^0.3.5",
"karma": "^0.12.28",
"karma-firefox-launcher": "^0.1.3",
"karma-mocha": "^0.1.9",
"mocha": "~2.0.1",
"plato": "^1.3.0",
"run-sequence": "^1.0.2",
"sinon": "^1.12.2"
},
"license": "MIT"
}

22
test/data/messages.json Normal file

File diff suppressed because one or more lines are too long

18
test/index.html Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Mocha</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="../browser/tests.js"></script>
<script>
mocha.run();
</script>
</body>
</html>

View File

@ -4,9 +4,10 @@ var chai = require('chai');
var should = chai.should();
var bitcore = require('../..');
var Data = require('../data/messages');
var Messages = bitcore.transport.Messages;
var bitcore = require('bitcore-base');
var Data = require('./data/messages');
var P2P = require('../');
var Messages = P2P.Messages;
var Networks = bitcore.Networks;
describe('Messages', function() {

1
test/mocha.opts Normal file
View File

@ -0,0 +1 @@
--recursive

View File

@ -8,8 +8,9 @@ var Socks5Client = require('socks5-client');
var should = chai.should();
var expect = chai.expect;
var bitcore = require('../..');
var Peer = bitcore.transport.Peer;
var bitcore = require('bitcore-base');
var P2P = require('../');
var Peer = P2P.Peer;
var Networks = bitcore.Networks;
describe('Peer', function() {