Skip to content

Commit

Permalink
Upload first code
Browse files Browse the repository at this point in the history
  • Loading branch information
huangliangk committed May 12, 2017
1 parent e5cf89b commit 97e920d
Show file tree
Hide file tree
Showing 376 changed files with 126,957 additions and 1 deletion.
93 changes: 92 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,92 @@
#acchain
# Pylink



## System Dependency

- nodejs v6.3.1+
- npm 3.10.3+ (not cnpm)
- sqlite v3.8.2+
- g++
- libssl

## Install node_modules

```
npm install
```

## Run

```
node app.js
```

## Usage

```
node app.js --help
Usage: app [options]
Options:
-h, --help output usage information
-V, --version output the version number
-c, --config <path> Config file path
-p, --port <port> Listening port number
-a, --address <ip> Listening host name or ip
-b, --blockchain <path> Blockchain db path
-g, --genesisblock <path> Genesisblock path
-x, --peers [peers...] Peers list
-l, --log <level> Log level
-d, --daemon Run asch node as daemon
--reindex Reindex blockchain
--base <dir> Base directory
```

## Front end (wallet ui)

```
cd public
npm install -g
npm start serve
```

Then you can open ```localhost:8008``` in you browser

## Default localnet genesis account

```
{
"keypair": {
"publicKey": "8065a105c785a08757727fded3a06f8f312e73ad40f1f3502e0232ea42e67efd",
"privateKey": "a64af28537545301f66579604628b55c7a7a102752bbd8f0b0d152f9754e78d58065a105c785a08757727fded3a06f8f312e73ad40f1f3502e0232ea42e67efd"
},
"address": "14762548536863074694",
"secret": "someone manual strong movie roof episode eight spatial brown soldier soup motor"
}
```

## Releated projects

- [asch_docs](https://github.com/sqfasd/asch_docs)
- [asch-frontend](https://github.com/sqfasd/asch-frontend)
- [asch-cli](https://github.com/sqfasd/asch-cli)
- [asch-js](https://github.com/sqfasd/asch-js)
- [asch-sandbox](https://github.com/sqfasd/asch-sandbox)
- [asch-dice-game-dapp](https://github.com/sqfasd/asch-dice-game-dapp)

## License

The MIT License (MIT)

Copyright (c) 2017 PUER BANK BLOCKCHAIN</br>
Copyright (c) 2016 Asch</br>
Copyright (c) 2015 Crypti

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.
240 changes: 240 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
var assert = require('assert');
var crypto = require('crypto');
var program = require('commander');
var path = require('path');
var fs = require('fs');
var async = require('async');
var Logger = require('./src/logger');
var init = require('./src/init');

function verifyGenesisBlock(scope, block) {
try {
var payloadHash = crypto.createHash('sha256');
var payloadLength = 0;

for (var i = 0; i < block.transactions.length; ++i) {
var trs = block.transactions[i];
var bytes = scope.base.transaction.getBytes(trs);
payloadLength += bytes.length;
payloadHash.update(bytes);
}
var id = scope.base.block.getId(block);
assert.equal(payloadLength, block.payloadLength, 'Unexpected payloadLength');
assert.equal(payloadHash.digest().toString('hex'), block.payloadHash, 'Unexpected payloadHash');
assert.equal(id, block.id, 'Unexpected block id');
// assert.equal(id, '11839820784468442760', 'Block id is incorrect');
} catch (e) {
assert(false, 'Failed to verify genesis block: ' + e);
}
}

function main() {
process.stdin.resume();

var version = '1.0.0';
program
.version(version)
.option('-c, --config <path>', 'Config file path')
.option('-p, --port <port>', 'Listening port number')
.option('-a, --address <ip>', 'Listening host name or ip')
.option('-b, --blockchain <path>', 'Blockchain db path')
.option('-g, --genesisblock <path>', 'Genesisblock path')
.option('-x, --peers [peers...]', 'Peers list')
.option('-l, --log <level>', 'Log level')
.option('-d, --daemon', 'Run asch node as daemon')
.option('-e, --execute <path>', 'exe')
.option('--reindex', 'Reindex blockchain')
.option('--base <dir>', 'Base directory')
.parse(process.argv);

var baseDir = program.base || './';

var pidFile = path.join(baseDir, 'acchain.pid');
if (fs.existsSync(pidFile)) {
console.log('Failed: asch server already started');
return;
}

var appConfigFile = path.join(baseDir, 'config.json');
if (program.config) {
appConfigFile = path.resolve(process.cwd(), program.config);
}
var appConfig = JSON.parse(fs.readFileSync(appConfigFile, 'utf8'));

if (!appConfig.dapp.masterpassword) {
var randomstring = require("randomstring");
appConfig.dapp.masterpassword = randomstring.generate({
length: 12,
readable: true,
charset: 'alphanumeric'
});
fs.writeFileSync(appConfigFile, JSON.stringify(appConfig, null, 2), "utf8");
}

appConfig.version = version;
appConfig.baseDir = baseDir;
appConfig.buildVersion = 'development';
appConfig.netVersion = process.env.NET_VERSION || 'localnet';
appConfig.publicDir = path.join(baseDir, 'public', 'dist');

global.Config = appConfig;

var genesisblockFile = path.join(baseDir, 'genesisBlock.json');
if (program.genesisblock) {
genesisblockFile = path.resolve(process.cwd(), program.genesisblock);
}
var genesisblock = JSON.parse(fs.readFileSync(genesisblockFile, 'utf8'));

if (program.port) {
appConfig.port = program.port;
}

if (program.address) {
appConfig.address = program.address;
}

if (program.peers) {
if (typeof program.peers === 'string') {
appConfig.peers.list = program.peers.split(',').map(function (peer) {
peer = peer.split(":");
return {
ip: peer.shift(),
port: peer.shift() || appConfig.port
};
});
} else {
appConfig.peers.list = [];
}
}

if (appConfig.netVersion === 'mainnet') {
var seeds = [
757137132,
1815983436,
759980934,
759980683,
1807690192,
1758431015,
1760474482,
1760474149,
759110497,
757134616
];
var ip = require('ip');
for (var i = 0; i < seeds.length; ++i) {
appConfig.peers.list.push({ ip: ip.fromLong(seeds[i]), port: 80 });
}
}

if (program.log) {
appConfig.logLevel = program.log;
}

var protoFile = path.join(baseDir, 'proto', 'index.proto');
if (!fs.existsSync(protoFile)) {
console.log('Failed: proto file not exists!');
return;
}

if (program.daemon) {
console.log('Asch server started as daemon ...');
require('daemon')();
fs.writeFileSync(pidFile, process.pid, 'utf8');
}

var logger = new Logger({
filename: path.join(baseDir, 'logs', 'debug.log'),
echo: program.deamon ? null : appConfig.logLevel,
errorLevel: appConfig.logLevel
});

var options = {
dbFile: program.blockchain || path.join(baseDir, 'blockchain.db'),
appConfig: appConfig,
genesisblock: genesisblock,
logger: logger,
protoFile: protoFile
};

if (program.reindex) {
appConfig.loading.verifyOnLoading = true;
}

global.featureSwitch = {}

init(options, function (err, scope) {
if (err) {
scope.logger.fatal(err);
if (fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile);
}
process.exit(1);
return;
}
verifyGenesisBlock(scope, scope.genesisblock.block);

if (program.execute) {
// only for debug use
// require(path.resolve(program.execute))(scope);
}

scope.bus.message('bind', scope.modules);
global.modules = scope.modules

global.featureSwitch.enableLongId = true
global.featureSwitch.enableUIA = true
global.featureSwitch.fixVoteNewAddressIssue = true

scope.logger.info('Modules ready and launched');
if (!scope.config.publicIp) {
scope.logger.warn('Failed to get public ip, block forging MAY not work!');
}

process.once('cleanup', function () {
scope.logger.info('Cleaning up...');
async.eachSeries(scope.modules, function (module, cb) {
if (typeof (module.cleanup) == 'function') {
module.cleanup(cb);
} else {
setImmediate(cb);
}
}, function (err) {
if (err) {
scope.logger.error('Error while cleaning up', err);
} else {
scope.logger.info('Cleaned up successfully');
}
if (fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile);
}
process.exit(1);
});
});

process.once('SIGTERM', function () {
process.emit('cleanup');
})

process.once('exit', function () {
scope.logger.info('process exited');
});

process.once('SIGINT', function () {
process.emit('cleanup');
});

process.on('uncaughtException', function (err) {
// handle the error safely
scope.logger.fatal('uncaughtException', { message: err.message, stack: err.stack });
process.emit('cleanup');
});

if (typeof gc !== 'undefined') {
setInterval(function () {
gc();
}, 60000);
}
});
}

main();
Loading

0 comments on commit 97e920d

Please sign in to comment.