Skip to content

Commit 807d52b

Browse files
committed
Build 1.1.5
1 parent 83bf692 commit 807d52b

19 files changed

+240
-82
lines changed

CHANGELOG.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
# Changelog
22

3+
## [1.1.5] - 2020-08-11
4+
5+
## Added
6+
7+
- Self destructed contracts support.
8+
- Tools: updateBalances, checks/updates the balances of all DB addresses.
9+
10+
## Fixed
11+
12+
- Contracts balances
13+
314
## [1.1.4] - 2020-08-06
415

516
### Updated
617

7-
- Db.createCollection to the new mongodb driver behavior
18+
- Db.createCollection to the new mongodb driver behavior.
819
- blocks checker service, delay before start.
9-
- config: disable services in config
20+
- Config: disable services in config.
1021

1122
## [1.1.3] - 2020-08-04
1223

1324
This release requires the regeneration of the DB follow the instructions in [UPDATES.md](UPDATES.md) before starting.
1425

1526
### Fixed
1627

17-
- Event decoding
18-
- Addresses balances
19-
- Token Addresses balances and detection
28+
- Event decoding.
29+
- Addresses balances.
30+
- Token Addresses balances and detection.
2031
- Repetitive indexing of collections at each service start.
21-
- Tokens total supply, see #30
32+
- Tokens total supply, see #30.
2233

2334
## [1.1.2] - 2020-07-17
2435

dist/api/api.config.js

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ if (log && log.dir) {
1313
conf.out_file = `${dir}/${name}-out.log`;
1414
}
1515

16-
1716
const apps = [conf];exports.apps = apps;
1817

1918
console.log(apps);

dist/lib/collections.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@
286286

287287
{
288288
key: { 'action.to': 1 },
289-
name: 'internalTxsToIndex' }] },
289+
name: 'internalTxsToIndex' },
290+
291+
{
292+
key: { type: 1 },
293+
name: 'internalTxTypeIndex' }] },
290294

291295

292296

dist/lib/types.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ const TOTAL_SUPPLY = 21 * 10 ** 6;exports.TOTAL_SUPPLY = TOTAL_SUPPLY;
8484
const fields = {
8585
LAST_BLOCK_MINED: 'lastBlockMined',
8686
DEPLOYED_CODE: 'deployedCode',
87-
CREATED_BY_TX: 'createdByTx' };exports.fields = fields;
87+
CREATED_BY_TX: 'createdByTx',
88+
DESTROYED_BY: 'destroyedByTx' };exports.fields = fields;
8889

8990

9091
const EVMversions = [

dist/services/blocks.config.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.apps = exports.paths = void 0;
22
var _config = _interopRequireDefault(require("../lib/config"));
3-
var _servicesConfig = require("./servicesConfig");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
4-
const { services } = _config.default.blocks;
3+
var _serviceFactory = require("./serviceFactory");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
54

6-
const scripts = Object.entries(_servicesConfig.servicesNames).
7-
filter(([service]) => services[service]).
5+
const scripts = Object.entries(_serviceFactory.enabledServices).
86
map(([service, name]) => name);
97

108
const scriptName = name => `${name}.js`;

dist/services/classes/Address.js

+45-18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Address extends _BcThing.BcThing {
2424
this.name = nName;
2525
this.isNative = !!nName;
2626
this.dbData = undefined;
27+
this.blockCode = undefined;
2728
this.tx = tx;
2829
this.data = createAddressData(this);
2930
this.setBlock(block);
@@ -56,36 +57,46 @@ class Address extends _BcThing.BcThing {
5657

5758
async getCode() {
5859
try {
59-
if (this.isZeroAddress || this.isNative) return null;
60-
let { code } = this.getData();
61-
let { nod3, address, blockNumber } = this;
62-
if (code !== undefined) return code;
63-
code = await nod3.eth.getCode(address, blockNumber);
64-
code = (0, _utils.isNullData)(code) ? null : code;
65-
this.setData({ code });
66-
return code;
60+
if (this.isZeroAddress || this.isNative) {
61+
this.blockCode = null;
62+
}
63+
let { nod3, address, blockNumber, blockCode } = this;
64+
if (blockCode !== undefined) return blockCode;
65+
blockCode = await nod3.eth.getCode(address, blockNumber);
66+
blockCode = (0, _utils.isNullData)(blockCode) ? null : blockCode;
67+
this.blockCode = blockCode;
68+
return blockCode;
6769
} catch (err) {
6870
return Promise.reject(err);
6971
}
7072
}
7173

74+
saveCode() {
75+
let { code } = this.getData();
76+
if (code) return;
77+
let { blockCode } = this;
78+
if (!blockCode) return;
79+
code = blockCode;
80+
let codeStoredAtBlock = this.blockNumber;
81+
this.setData({ code, codeStoredAtBlock });
82+
}
83+
7284
async fetch(forceFetch) {
7385
try {
7486
if (this.fetched && !forceFetch) return this.getData(true);
75-
this.fetched = false;
7687
let dbData = this.isZeroAddress ? {} : await this.getFromDb();
7788
this.setData(dbData);
7889

7990
let { blockNumber } = this;
8091

8192
let storedBlock = this.data.blockNumber || 0;
82-
if (blockNumber >= storedBlock) {
93+
if (storedBlock <= blockNumber) {
8394
let balance = await this.getBalance('latest');
8495
this.setData({ balance, blockNumber });
8596
}
8697

8798
let code = await this.getCode();
88-
// TODO suicide
99+
this.saveCode();
89100
if (code) {
90101
// get contract info
91102
let deployedCode = dbData ? dbData[_types.fields.DEPLOYED_CODE] : undefined;
@@ -101,8 +112,11 @@ class Address extends _BcThing.BcThing {
101112
data[_types.fields.DEPLOYED_CODE] = deployedCode;
102113
this.setData(data);
103114
}
104-
this.makeContract(deployedCode, dbData);
115+
this.makeContract(deployedCode);
105116
let contractData = await this.contract.fetch();
117+
// prevent update this fields from contractData
118+
delete contractData.balance;
119+
delete contractData.blockNumber;
106120
this.setData(contractData);
107121
}
108122
if (this.isNative) this.makeContract();
@@ -187,15 +201,15 @@ class Address extends _BcThing.BcThing {
187201
}
188202

189203
isContract() {
190-
let { code, type } = this.getData();
191-
let { isNative, address } = this;
192-
if (undefined === code && !isNative && !(0, _rskUtils.isZeroAddress)(address)) {
204+
let { address, blockCode } = this;
205+
if (undefined === blockCode) {
193206
throw new Error(`Run getCode first ${address}`);
194207
}
195-
return type === _types.addrTypes.CONTRACT;
208+
return !!blockCode;
196209
}
197210

198-
makeContract(deployedCode, dbData) {
211+
makeContract(deployedCode) {
212+
const dbData = Object.assign({}, this.getData(true));
199213
if (this.contract) return this.contract;
200214
let { address, nod3, initConfig, collections, block } = this;
201215
this.contract = new _Contract.default(address, deployedCode, { dbData, nod3, initConfig, collections, block });
@@ -208,6 +222,12 @@ class Address extends _BcThing.BcThing {
208222
} catch (err) {
209223
return Promise.reject(err);
210224
}
225+
}
226+
227+
suicide(destroyedBy) {
228+
let data = {};
229+
data[_types.fields.DESTROYED_BY] = destroyedBy;
230+
this.setData(data);
211231
}}exports.Address = Address;
212232

213233

@@ -239,13 +259,20 @@ function createAddressData({ address, isNative, name }) {
239259
switch (prop) {
240260
case 'code':
241261
if ((0, _utils.isNullData)(val)) val = null;
242-
if (val) {
262+
if (val && data[_types.fields.DESTROYED_BY] === undefined) {
243263
data.type = _types.addrTypes.CONTRACT;
244264
}
245265
// Fix to support suicide
246266
data.code = val;
247267
break;
248268

269+
case _types.fields.DESTROYED_BY:
270+
if (data[prop] !== undefined) return true;
271+
val = (0, _InternalTx.checkInternalTransactionData)(Object.assign({}, val));
272+
data[prop] = val;
273+
data.type = _types.addrTypes.ADDRESS;
274+
break;
275+
249276
case _types.fields.LAST_BLOCK_MINED:
250277
const lastBlock = data[_types.fields.LAST_BLOCK_MINED] || {};
251278
let number = lastBlock.number || -1;

dist/services/classes/Addresses.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ class Addresses {
88
this.initConfig = initConfig;
99
this.addresses = {};
1010
}
11-
add(address, options = {}) {
11+
createAddress(address, options = {}) {
1212
if (!(0, _utils.isAddress)(address)) throw new Error(`Invalid address ${address}`);
13+
options = options || {};
14+
let { nod3, initConfig, collections } = this;
15+
options = Object.assign(options, { nod3, initConfig, collections });
16+
return new _Address.default(address, options);
17+
}
18+
add(address, options = {}) {
1319
if (!this.addresses[address]) {
14-
options = options || {};
15-
let { nod3, initConfig, collections } = this;
16-
options = Object.assign(options, { nod3, initConfig, collections });
17-
this.addresses[address] = new _Address.default(address, options);
20+
this.addresses[address] = this.createAddress(address, options);
1821
}
1922
return this.addresses[address];
2023
}

dist/services/classes/Block.js

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class Block extends _BcThing.BcThing {
9797
return result;
9898
} catch (err) {
9999
this.log.error('Error inserting events');
100+
this.log.trace(`Events: ${JSON.stringify(events, null, 2)}`);
100101
return Promise.reject(err);
101102
}
102103
}

dist/services/classes/BlockSummary.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class BlockSummary extends _BcThing.BcThing {
4747
let events = [].concat(...txsData.map(d => d.events));
4848
let internalTransactions = [].concat(...txsData.map(d => d.internalTransactions));
4949
let tokenAddresses = [].concat(...txsData.map(d => d.tokenAddresses));
50+
let suicides = [].concat(...txsData.map(d => d.suicides));
5051
let addresses = await Addresses.fetch();
51-
this.setData({ internalTransactions, events, addresses, tokenAddresses });
52+
this.setData({ internalTransactions, events, addresses, tokenAddresses, suicides });
5253
this.fetched = true;
5354
return this.getData();
5455
} catch (err) {

dist/services/classes/InternalTx.js

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.getInternalTxId = getInternalTxId;exports.filterValueAddresses = filterValueAddresses;exports.default = exports.InternalTx = void 0;var _BcThing = require("./BcThing");
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.checkInternatTransactionType = checkInternatTransactionType;exports.getInternalTxId = getInternalTxId;exports.filterValueAddresses = filterValueAddresses;exports.checkInternalTransactionData = checkInternalTransactionData;exports.default = exports.InternalTx = void 0;var _BcThing = require("./BcThing");
22
var _ids = require("../../lib/ids");
3+
var _utils = require("../../lib/utils");
34

4-
const REQUIRED_FIELDS = [
5-
'blockHash',
6-
'blockNumber',
7-
'transactionHash',
8-
'transactionPosition',
9-
'type',
10-
'subtraces',
11-
'traceAddress',
12-
'result',
13-
'action',
14-
'timestamp'];
5+
const ITX_FIELDS = {
6+
blockNumber: null,
7+
transactionHash: _utils.isBlockHash,
8+
blockHash: _utils.isBlockHash,
9+
transactionPosition: null,
10+
type: checkInternatTransactionType,
11+
subtraces: null,
12+
traceAddress: Array.isArray,
13+
result: null,
14+
action: null,
15+
timestamp: null,
16+
_index: null };
1517

1618

1719
class InternalTx extends _BcThing.BcThing {
@@ -21,11 +23,7 @@ class InternalTx extends _BcThing.BcThing {
2123
}
2224

2325
checkData(data) {
24-
if (typeof data !== 'object') throw new Error('Data is not an object');
25-
for (let field of REQUIRED_FIELDS) {
26-
if (!data.hasOwnProperty(field)) throw new Error(`Missing field: ${field}`);
27-
}
28-
return data;
26+
return checkInternalTransactionData(data);
2927
}
3028

3129
setData(data) {
@@ -40,14 +38,24 @@ class InternalTx extends _BcThing.BcThing {
4038
let data = this.getData();
4139
let { action } = data;
4240
let { isAddress } = this;
43-
return Object.entries(action).
44-
filter(a => {
45-
let [name, value] = a;
41+
let addresses = Object.entries(action).
42+
filter(([name, value]) => {
4643
return name !== 'balance' && isAddress(value);
4744
}).map(v => v[1]);
45+
return [...new Set(addresses)];
46+
}
47+
48+
isSuicide() {
49+
let { type, action } = this.getData();
50+
return checkInternatTransactionType(type) === 'suicide' && (0, _utils.isAddress)(action.address);
4851
}}exports.InternalTx = InternalTx;
4952

5053

54+
function checkInternatTransactionType(type) {
55+
if (typeof type !== 'string') throw new Error(`Invalid itx type: ${type}`);
56+
return type;
57+
}
58+
5159
function getInternalTxId({ blockNumber, transactionPosition: transactionIndex, transactionHash: hash, _index: index }) {
5260
return (0, _ids.generateId)({ blockNumber, transactionIndex, hash, index });
5361
}
@@ -59,9 +67,25 @@ function filterValueAddresses(internalTransactions) {
5967
if (!error && parseInt(value) > 0) {
6068
addresses.add(from);
6169
addresses.add(to);
70+
// review suicide and refund address
6271
}
6372
});
6473
return [...addresses];
74+
}
75+
76+
function checkInternalTransactionData(data) {
77+
if (typeof data !== 'object') throw new Error('Data is not an object');
78+
for (let field of Object.keys(ITX_FIELDS)) {
79+
if (!data.hasOwnProperty(field)) throw new Error(`Missing field: ${field}`);
80+
let value = data[field];
81+
let check = ITX_FIELDS[field];
82+
if (typeof check === 'function') {
83+
if (!check(data[field])) {
84+
throw new Error(`Invalid value: ${value} for itx field: ${field}`);
85+
}
86+
}
87+
}
88+
return data;
6589
}var _default =
6690

6791
InternalTx;exports.default = _default;

0 commit comments

Comments
 (0)