Skip to content

Commit 90072d7

Browse files
authored
Merge pull request #344 from ainblockchain/release/v0.7.0
Release/v0.7.0
2 parents 5ccc257 + e029769 commit 90072d7

File tree

103 files changed

+11293
-4423
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+11293
-4423
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# XXX: Project Related
2-
blockchain/blockchains
2+
chains
33
logs
44
log1.txt
55
log2.txt
66
load1.txt
77
load2.txt
88
blocks1.txt
99
blocks2.txt
10-
blockchain/shard_*
10+
shard-configs
11+
*_on_prem.sh
1112

1213
# XXX: .dockerignore
1314
.git

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ You can override default port numbering system by setting `PORT` and `P2P_PORT`
3232
Set <NUMBER_OF_SHARDS> to 0 if you only want to run a parent chain, or set it to the specific number of shard chains you want to run in addition to the parent chain.
3333
```
3434
gcloud init
35-
sh deploy_gcp.sh {dev|spring|summer} <YOUR_GCP_USER_NAME> <NUMBER_OF_SHARDS>
35+
sh deploy_blockchain_gcp.sh {dev|spring|summer} <YOUR_GCP_USER_NAME> <NUMBER_OF_SHARDS>
3636
```
3737
- Set up Ubuntu machine (if it's on a new VM)
3838
```
39-
sh setup_ubuntu.sh
39+
sh setup_blockchain_ubuntu.sh
4040
```
4141
- Copy files to a sharable folder & install npm packages
4242
```
@@ -66,7 +66,7 @@ docker run --network="host" -d ainblockchain/tracker-server:latest
6666
```
6767
-->
6868

69-
### Client APIs for development and debugging
69+
### Client API for development and debugging
7070

7171
#### Tracker health check
7272

@@ -92,20 +92,20 @@ yarn install
9292
```
9393
- Run blockchain nodes
9494
```
95-
ACCOUNT_INDEX=0 DEBUG=false STAKE=250 node client/index.js
96-
ACCOUNT_INDEX=1 DEBUG=false STAKE=250 node client/index.js
97-
ACCOUNT_INDEX=2 DEBUG=false STAKE=250 node client/index.js
95+
MIN_NUM_VALIDATORS=3 ACCOUNT_INDEX=0 DEBUG=false STAKE=250 CONSOLE_LOG=true ENABLE_DEV_CLIENT_API=true node client/index.js
96+
MIN_NUM_VALIDATORS=3 ACCOUNT_INDEX=1 DEBUG=false STAKE=250 CONSOLE_LOG=true ENABLE_DEV_CLIENT_API=true node client/index.js
97+
MIN_NUM_VALIDATORS=3 ACCOUNT_INDEX=2 DEBUG=false STAKE=250 CONSOLE_LOG=true ENABLE_DEV_CLIENT_API=true node client/index.js
9898
```
9999
You can override default port numbering system by setting `PORT` and `P2P_PORT` environment variables.
100100
Before starting node jobs, remove existing blockchain files and logs if necessary:
101101
```
102-
rm -rf blockchain/blockchains logger/logs
102+
rm -rf chains logs
103103
```
104104
The default minimum size of the validator whitelist is 3. Change MIN_NUM_VALIDATORS parameter in
105105
the genesis-configs/base/genesis.json to change this value. You may also need to modify the GENESIS_WHITELIST and GENESIS_VALIDATORS accordingly.
106106
The genesis configs directory used is `genesis-configs/base` by default and it can be altered using `GENESIS_CONFIGS_DIR` env variable. For example, afan shard cluster can use the following command line:
107107
```
108-
GENESIS_CONFIGS_DIR=genesis-configs/afan-shard ACCOUNT_INDEX=0 DEBUG=false STAKE=250 node client/index.js
108+
GENESIS_CONFIGS_DIR=genesis-configs/afan-shard MIN_NUM_VALIDATORS=1 ACCOUNT_INDEX=0 DEBUG=false STAKE=250 CONSOLE_LOG=true ENABLE_DEV_CLIENT_API=true node client/index.js
109109
```
110110

111111
### How to run tests
@@ -124,11 +124,11 @@ npm run test_integration
124124
Set <NUMBER_OF_SHARDS> to 0 if you only want to run a parent chain, or set it to the specific number of shard chains you want to run in addition to the parent chain.
125125
```
126126
gcloud init
127-
sh deploy_gcp.sh {dev|spring|summer} <YOUR_GCP_USER_NAME> <NUMBER_OF_SHARDS>
127+
sh deploy_blockchain_gcp.sh {dev|spring|summer} <YOUR_GCP_USER_NAME> <NUMBER_OF_SHARDS>
128128
```
129129
- Set up Ubuntu machine (if it's on a new VM)
130130
```
131-
sh setup_ubuntu.sh
131+
sh setup_blockchain_ubuntu.sh
132132
```
133133
- Copy files to a sharable folder & install npm packages
134134
```
@@ -178,7 +178,7 @@ npm run test_smoke
178178
npm run test_integration
179179
```
180180

181-
### Client APIs for development and debugging
181+
### Client API for development and debugging
182182

183183
#### Node health check
184184

afan_client/index.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11

22
const RequestManager = require('./request_manager');
33
const ProfitManager = require('./profit_manager');
4-
const errors = require('request-promise/errors');
54

65
const INVEST_FEE = 0.1;
7-
const APP_NAME = 'afan';
6+
const APP_PATH = '/apps/afan';
87

98
class AfanClient {
109
constructor(endpoint) {
1110
this.endpoint = endpoint;
1211
}
1312

1413
tx_invest(from, to, value) {
15-
const requestManager = new RequestManager(this.endpoint, APP_NAME);
14+
const requestManager = new RequestManager(this.endpoint, APP_PATH);
1615
requestManager.increaseBalance(from, -value);
1716
requestManager.increaseInvestorBalance(to, from, value);
1817
requestManager.increaseInvestTotal(to, value);
@@ -30,11 +29,8 @@ class AfanClient {
3029
const investorsResponse = await requestManager.getInvestors(to);
3130
investors = investorsResponse.result;
3231
} catch (err) {
33-
if (err instanceof errors.StatusCodeError) {
34-
investors = null;
35-
} else {
36-
throw err
37-
}
32+
console.log(err);
33+
throw err;
3834
}
3935

4036
requestManager.increaseBalance(from, -value);
@@ -43,32 +39,31 @@ class AfanClient {
4339
}
4440

4541
async tx_crushOnPost(from, to, pid, value) {
46-
const requestManager = new RequestManager(this.endpoint, APP_NAME);
42+
const requestManager = new RequestManager(this.endpoint, APP_PATH);
4743
await this.shareProfit(requestManager, from, to, value);
4844
requestManager.increasePostCrushOn(to, pid, from, value);
4945

5046
return requestManager.send();
5147
}
5248

5349
async tx_crushOnReply(from, to, pid, rid, value) {
54-
const requestManager = new RequestManager(this.endpoint, APP_NAME);
50+
const requestManager = new RequestManager(this.endpoint, APP_PATH);
5551
await this.shareProfit(requestManager, from, to, value);
5652
requestManager.increaseReplyCrushOn(pid, rid, from, value);
5753

5854
return requestManager.send();
5955
}
6056

6157
async tx_adpropose(from, to, value, intermed) {
62-
const requestManager = new RequestManager(this.endpoint, APP_NAME);
58+
const requestManager = new RequestManager(this.endpoint, APP_PATH);
6359
try {
6460
const state = await requestManager.getAdState(from, to);
6561
if (state.result && state.result !== 3) {
6662
return {code: -4, message: 'Already proposed'};
6763
}
6864
} catch (err) {
69-
if (!(err instanceof errors.StatusCodeError)) {
70-
throw err
71-
}
65+
console.log(err);
66+
throw err;
7267
}
7368
requestManager.increaseBalance(from, -value);
7469
requestManager.increaseBalance(intermed, value);

afan_client/request_manager.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
1+
const _ = require('lodash');
22
const Ref = require('./ref');
3-
const rp = require('request-promise');
3+
const axios = require('axios');
44

55
class RequestManager {
66
constructor(endpoint, root) {
@@ -10,28 +10,23 @@ class RequestManager {
1010
}
1111

1212
send() {
13-
const options = {
14-
method: 'POST',
15-
uri: this.endpoint + '/set',
16-
body: {op_list: this.updates},
17-
json: true, // Automatically stringifies the body to JSON
18-
};
19-
20-
return rp(options).then(function(parsedBody) {
21-
// POST succeeded
22-
return parsedBody;
23-
}).catch(function(err) {
24-
// POST failed
13+
return axios.post(this.endpoint + '/set', { op_list: this.updates })
14+
.then((resp) => {
15+
return _.get(resp, 'data', null);
16+
}).catch((err) => {
2517
console.log(err);
18+
return null;
2619
});
2720
}
2821

2922
getRef(ref) {
30-
const options = {
31-
uri: this.endpoint + `/get_value?ref=${this.root}/${ref}`,
32-
json: true,
33-
};
34-
return rp(options);
23+
return axios.get(this.endpoint + `/get_value?ref=${this.root}/${ref}`)
24+
.then((resp) => {
25+
return _.get(resp, 'data', null);
26+
}).catch((err) => {
27+
console.log(err);
28+
return null;
29+
});
3530
}
3631

3732
getInvestors(uid) {

blockchain/block-file-patterns.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

blockchain/block-file-util.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const fs = require('fs');
2+
const glob = require('glob');
3+
const path = require('path');
4+
const {compare} = require('natural-orderby');
5+
const zlib = require('zlib');
6+
const {CHAINS_N2B_DIR_NAME, CHAINS_H2N_DIR_NAME} = require('../common/constants');
7+
const ChainUtil = require('../common/chain-util');
8+
const FILE_NAME_SUFFIX = 'json.gz';
9+
const logger = require('../logger')('BLOCK-FILE-UTIL');
10+
11+
class BlockFileUtil {
12+
static getBlockPath(chainPath, blockNumber) {
13+
return path.join(chainPath, CHAINS_N2B_DIR_NAME, this.getBlockFilenameByNumber(blockNumber));
14+
}
15+
16+
static getHashToNumberPath(chainPath, blockHash) {
17+
return path.join(chainPath, CHAINS_H2N_DIR_NAME, blockHash);
18+
}
19+
20+
static getBlockFilenameByNumber(blockNumber) {
21+
return `${blockNumber}.${FILE_NAME_SUFFIX}`;
22+
}
23+
24+
static getBlockFilename(block) {
25+
return this.getBlockFilenameByNumber(block.number);
26+
}
27+
28+
// TODO(csh): Don't use glob?
29+
static getAllBlockPaths(chainPath) {
30+
const allBlockFilesPattern = `${chainPath}/${CHAINS_N2B_DIR_NAME}/*.${FILE_NAME_SUFFIX}`;
31+
return glob.sync(allBlockFilesPattern).sort(compare());
32+
}
33+
34+
static getBlockPaths(chainPath, from, to) {
35+
const blockPaths = [];
36+
for (let number = from; number < to; number++) {
37+
const blockFile = this.getBlockPath(chainPath, number);
38+
if (fs.existsSync(blockFile)) {
39+
blockPaths.push(blockFile);
40+
}
41+
}
42+
return blockPaths;
43+
}
44+
45+
static createBlockchainDir(chainPath) {
46+
const n2bPath = path.join(chainPath, CHAINS_N2B_DIR_NAME);
47+
const h2nPath = path.join(chainPath, CHAINS_H2N_DIR_NAME);
48+
let isBlockEmpty = true;
49+
50+
if (!fs.existsSync(chainPath)) {
51+
fs.mkdirSync(chainPath, {recursive: true});
52+
}
53+
54+
if (!fs.existsSync(n2bPath)) {
55+
fs.mkdirSync(n2bPath);
56+
}
57+
58+
if (!fs.existsSync(h2nPath)) {
59+
fs.mkdirSync(h2nPath);
60+
}
61+
62+
if (fs.readdirSync(n2bPath).length > 0) {
63+
isBlockEmpty = false;
64+
}
65+
return isBlockEmpty;
66+
}
67+
68+
// TODO(csh): Change to asynchronous
69+
static readBlock(blockPath) {
70+
const zippedFs = fs.readFileSync(blockPath);
71+
return JSON.parse(zlib.gunzipSync(zippedFs).toString());
72+
}
73+
74+
static readBlockByNumber(chainPath, blockNumber) {
75+
const blockPath = this.getBlockPath(chainPath, blockNumber);
76+
return this.readBlock(blockPath);
77+
}
78+
79+
// TODO(csh): Change to asynchronous
80+
static writeBlock(chainPath, block) {
81+
const blockPath = this.getBlockPath(chainPath, block.number);
82+
if (!fs.existsSync(blockPath)) {
83+
const compressed = zlib.gzipSync(Buffer.from(JSON.stringify(block)));
84+
fs.writeFileSync(blockPath, compressed);
85+
} else {
86+
logger.debug(`${blockPath} file already exists!`);
87+
}
88+
}
89+
90+
static writeHashToNumber(chainPath, blockHash, blockNumber) {
91+
if (!blockHash || !ChainUtil.isNumber(blockNumber) || blockNumber < 0) {
92+
logger.error(`Invalid writeHashToNumber parameters (${blockHash}, ${blockNumber})`);
93+
return;
94+
}
95+
const hashToNumberPath = this.getHashToNumberPath(chainPath, blockHash);
96+
if (!fs.existsSync(hashToNumberPath)) {
97+
fs.writeFileSync(hashToNumberPath, blockNumber);
98+
} else {
99+
logger.debug(`${hashToNumberPath} file already exists!`);
100+
}
101+
}
102+
103+
static readHashToNumber(chainPath, blockHash) {
104+
const hashToNumberPath = this.getHashToNumberPath(chainPath, blockHash);
105+
return Number(fs.readFileSync(hashToNumberPath).toString());
106+
}
107+
}
108+
109+
module.exports = BlockFileUtil;

0 commit comments

Comments
 (0)