Skip to content

Commit

Permalink
1, Added demos
Browse files Browse the repository at this point in the history
2, Added comments
3, Update package to 1.0.9
  • Loading branch information
xingworld committed Sep 4, 2018
1 parent 26d4755 commit fb2ba8d
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# TrustNote Pow-Miner

## Install
```
$ npm install trustnote-pow-miner
```


## Linux

currently we are working on.
Expand Down
29 changes: 29 additions & 0 deletions demos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Demos for TrustNote Pow-Miner

## Install
```
$ npm install https://github.com/trustnote/trustnote-pow-common.git#dev -save
$ npm install trustnote-pow-miner -save
```


## Get Started

#### start mining
```
$ node pow_start_mining.js
```


#### stop mining
```
$ node pow_stop_mining.js
```



#### difficulty
convert difficult from 256 hex string to uint32
```
$ node pow_difficulty.js
```
22 changes: 22 additions & 0 deletions demos/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "trustnote-pow-miner-demo",
"description": "Demo of TrustNote PoW Miner",
"author": "Trustnote",
"version": "0.1.0",
"license": "MIT",
"repository": {
"url": "https://github.com/trustnote/trustnote-pow-miner.git",
"type": "git"
},
"browser": {
"request": "browser-request",
"secp256k1": "secp256k1/js"
},
"dependencies": {
"bitcore-lib": "^0.13.14",
"bitcore-mnemonic": "~1.0.0",
"json-rpc2": "^1.0.2",
"trustnote-pow-common": "git+https://github.com/trustnote/trustnote-pow-common.git#dev",
"trustnote-pow-miner": ">=1.0.0"
}
}
15 changes: 15 additions & 0 deletions demos/pow_difficulty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const _trustnote_pow_miner = require( 'trustnote-pow-miner' );


let sDifficulty256Hex = "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
let nDifficultyU32 = _trustnote_pow_miner.difficulty256HexToUInt32( sDifficulty256Hex );


console.log( `CONVERT DIFFICULT FROM 256 HEX STRING TO UINT32` );
console.log( `sDifficulty256Hex\t: ${ sDifficulty256Hex }` );
console.log( `nDifficultyU32 \t: ${ nDifficultyU32 }` );





68 changes: 68 additions & 0 deletions demos/pow_start_mining.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const _pow = require( 'trustnote-pow-common/pow.js' );
const _event_bus = require( 'trustnote-pow-common/event_bus.js' );
const _trustnote_pow_miner = require( 'trustnote-pow-miner' );


//
// * @param {number} oInput.currentRoundIndex
// * @param {string} oInput.currentFirstTrustMEBall
// * @param {string} oInput.currentDifficulty
// * @param {string} oInput.currentPubSeed
// * @param {string} oInput.superNodeAuthor
//
let nDifficulty = _trustnote_pow_miner.difficulty256HexToUInt32( "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" );
let oMiningInput = {
roundIndex : 111,
firstTrustMEBall : 'rjywtuZ8A70vgIsZ7L4lBR3gz62Nl3vZr2t7I4lzsMU=',
difficulty : nDifficulty,
publicSeed : 'public key',
superNodeAuthor : 'xing.supernode.trustnote.org',
};


_event_bus.on
(
'pow_mined_gift',
( objSolution ) =>
{
console.log( `############################################################` );
console.log( objSolution );

if ( 'object' === typeof objSolution )
{
console.log( `will check proof of work` );
_pow.checkProofOfWork( oMiningInput, objSolution.hash, objSolution.nonce, function( err, objResult )
{
console.log( `_pow.checkProofOfWork, err : ${ err }, objResult : `, objResult );
if ( null === err &&
'object' === typeof objResult &&
'number' === typeof objResult.code && 0 === objResult.code )
{
console.log( `PoW is correct.` );
}
else
{
console.error( `PoW is invalid : `, err );
}
});
}
else
{
console.error( `invalid objSolution` );
}
}
);
_pow.startMiningWithInputs
(
oMiningInput,
function( err )
{
if ( err )
{
console.log( `failed to start calculation, `, err );
return;
}

console.log( `start calculation successfully.` );
}
);
4 changes: 4 additions & 0 deletions demos/pow_stop_mining.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const _trustnote_pow_miner = require( 'trustnote-pow-miner' );


_trustnote_pow_miner.stopMining();
12 changes: 10 additions & 2 deletions js/miner.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ function isWorkerExists( nPId )
* @param {number} oOptions.maxLoop
* @param {number} oOptions.difficulty
* @param {function} pfnCallback( err, { hashHex : sActualHashHex, nonce : uActualNonce } )
* TIME IS OVER
* ( null, { gameOver : true, hashHex : null, nonce : 0 } );
*/
function checkWorkers( oOptions, pfnCallback )
{
Expand Down Expand Up @@ -204,7 +206,13 @@ function checkWorkers( oOptions, pfnCallback )
* check win
*
* @param sData
* @return {boolean}
* @return {object}
* return an plain object if wins
* {
* hashHex : 'hash string',
* nonce : 000,
* }
* otherwise, return null
*/
function checkWin( sData )
{
Expand Down Expand Up @@ -243,7 +251,7 @@ function checkWin( sData )
* spawn worker
*
* @param {object} oOptions
* @param {function} pfnCallback
* @param {function} pfnCallback( err, { hashHex : string, nonce : number } )
* @return {*}
*/
function spawnWorker( oOptions, pfnCallback )
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trustnote-pow-miner",
"version": "1.0.8",
"version": "1.0.9",
"license": "MIT",
"author": "TrustNote",
"description": "TrustNote miner for Node.js",
Expand All @@ -25,7 +25,9 @@
"bower_components",
"tests",
"c",
"doc"
"doc",
"rsync_dev.sh",
"rsync_pow.sh"
],
"repository": {
"type": "git",
Expand Down

0 comments on commit fb2ba8d

Please sign in to comment.