@ocap/statedb-memory 1.6.3 → 1.6.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/db.js +16 -12
- package/lib/table/account.js +17 -9
- package/lib/table/base.js +44 -16
- package/lib/table/rollup.js +13 -0
- package/lib/table/token.js +13 -0
- package/package.json +12 -10
- package/lib/table/asset.js +0 -10
- package/lib/table/delegation.js +0 -10
- package/lib/table/tx.js +0 -10
package/README.md
CHANGED
package/lib/db.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const { StateDB } = require('@ocap/statedb');
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
2
|
+
const Table = require('./table/base');
|
|
3
|
+
const Account = require('./table/account');
|
|
4
|
+
const Token = require('./table/token');
|
|
5
|
+
const Rollup = require('./table/rollup');
|
|
6
6
|
|
|
7
7
|
const { name, version } = require('../package.json');
|
|
8
8
|
|
|
@@ -13,15 +13,19 @@ class MemoryStateDB extends StateDB {
|
|
|
13
13
|
this.name = name;
|
|
14
14
|
this.version = version;
|
|
15
15
|
|
|
16
|
-
this.account = new
|
|
17
|
-
this.asset = new
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
16
|
+
this.account = new Account('account', 'address');
|
|
17
|
+
this.asset = new Table('asset', 'address');
|
|
18
|
+
this.factory = new Table('factory', 'address');
|
|
19
|
+
this.delegation = new Table('delegation', 'address');
|
|
20
|
+
this.tx = new Table('tx', 'hash');
|
|
21
|
+
this.token = new Token('token', 'address');
|
|
22
|
+
this.chain = new Table('chain', 'address');
|
|
23
|
+
this.stake = new Table('stake', 'address');
|
|
24
|
+
this.rollup = new Rollup('rollup', 'address');
|
|
25
|
+
this.rollupBlock = new Table('rollupBlock', 'hash');
|
|
26
|
+
this.evidence = new Table('evidence', 'hash');
|
|
20
27
|
|
|
21
|
-
this.
|
|
22
|
-
this.asset.onReady(() => this.markReady('asset'));
|
|
23
|
-
this.delegation.onReady(() => this.markReady('delegation'));
|
|
24
|
-
this.tx.onReady(() => this.markReady('tx'));
|
|
28
|
+
this.attachReadyListeners();
|
|
25
29
|
}
|
|
26
30
|
}
|
|
27
31
|
|
package/lib/table/account.js
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
const { ensureChecksumAddress } = require('@ocap/state/lib/states/account');
|
|
2
|
+
const debug = require('debug')(require('../../package.json').name);
|
|
2
3
|
const MemoryStateDB = require('./base');
|
|
3
4
|
|
|
4
5
|
class AccountStateDB extends MemoryStateDB {
|
|
5
|
-
constructor() {
|
|
6
|
-
super();
|
|
7
|
-
this.name = 'account';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
6
|
_get(address, { final = true } = {}) {
|
|
11
|
-
const current = this.
|
|
7
|
+
const current = this.collection.by(this.uniqIndex, ensureChecksumAddress(address)) || null;
|
|
12
8
|
if (current && final && Array.isArray(current.migratedTo) && current.migratedTo.length) {
|
|
13
|
-
return this.
|
|
9
|
+
return this._get(current.migratedTo[0]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return current || null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
_create(id, attrs = {}) {
|
|
16
|
+
if (this._get(id)) {
|
|
17
|
+
throw new Error(`${this.name} already exists: ${id}`);
|
|
14
18
|
}
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
const newDoc = { [this.uniqIndex]: id, ...attrs };
|
|
21
|
+
newDoc[this.uniqIndex] = ensureChecksumAddress(newDoc[this.uniqIndex]);
|
|
22
|
+
this.collection.insert(newDoc);
|
|
23
|
+
debug(`${this.name} create`, { id, ...attrs });
|
|
24
|
+
return this._get(id);
|
|
17
25
|
}
|
|
18
26
|
}
|
|
19
27
|
|
package/lib/table/base.js
CHANGED
|
@@ -1,37 +1,65 @@
|
|
|
1
1
|
/* eslint-disable no-underscore-dangle */
|
|
2
2
|
const { StateDBTable } = require('@ocap/statedb');
|
|
3
|
+
const Lokijs = require('lokijs');
|
|
4
|
+
|
|
5
|
+
const db = new Lokijs('ocap-memory-statedb.db');
|
|
6
|
+
|
|
3
7
|
const debug = require('debug')(require('../../package.json').name);
|
|
4
8
|
|
|
5
9
|
class MemoryTable extends StateDBTable {
|
|
6
|
-
constructor() {
|
|
10
|
+
constructor(name, uniqIndex) {
|
|
7
11
|
super();
|
|
8
|
-
|
|
12
|
+
|
|
13
|
+
this.name = name;
|
|
14
|
+
this.uniqIndex = uniqIndex;
|
|
15
|
+
|
|
16
|
+
this.collection = db.addCollection(name, { unique: [uniqIndex], clone: true });
|
|
9
17
|
this.markReady();
|
|
10
18
|
}
|
|
11
19
|
|
|
12
|
-
_create(
|
|
13
|
-
if (this.
|
|
14
|
-
throw new Error(`${this.name} already exists: ${
|
|
20
|
+
_create(id, attrs = {}) {
|
|
21
|
+
if (this._get(id)) {
|
|
22
|
+
throw new Error(`${this.name} already exists: ${id}`);
|
|
15
23
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
|
|
25
|
+
this.collection.insert({ [this.uniqIndex]: id, ...attrs });
|
|
26
|
+
debug(`${this.name} create`, { id, ...attrs });
|
|
27
|
+
return this._get(id);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_get(id) {
|
|
31
|
+
return id ? this.collection.by(this.uniqIndex, id) : null;
|
|
19
32
|
}
|
|
20
33
|
|
|
21
|
-
|
|
22
|
-
return
|
|
34
|
+
_history() {
|
|
35
|
+
return [];
|
|
23
36
|
}
|
|
24
37
|
|
|
25
|
-
_update(
|
|
26
|
-
|
|
27
|
-
|
|
38
|
+
_update(id, updates) {
|
|
39
|
+
const doc = this.collection.by(this.uniqIndex, id);
|
|
40
|
+
if (!doc) {
|
|
41
|
+
throw new Error(`${this.name} does not exists: ${id}`);
|
|
28
42
|
}
|
|
29
|
-
|
|
30
|
-
|
|
43
|
+
|
|
44
|
+
Object.assign(doc, updates);
|
|
45
|
+
this.collection.update(doc);
|
|
46
|
+
return doc;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
updateOrCreate(exist, state, ctx) {
|
|
50
|
+
if (!state[this.uniqIndex]) {
|
|
51
|
+
throw new Error('Cannot update or create without uniq index');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (exist) {
|
|
55
|
+
return this.update(state[this.uniqIndex], state, ctx);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return this.create(state[this.uniqIndex], state, ctx);
|
|
31
59
|
}
|
|
32
60
|
|
|
33
61
|
_reset() {
|
|
34
|
-
this.
|
|
62
|
+
this.collection.removeWhere({});
|
|
35
63
|
}
|
|
36
64
|
}
|
|
37
65
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const MemoryTable = require('./base');
|
|
2
|
+
|
|
3
|
+
class RollupTable extends MemoryTable {
|
|
4
|
+
async existByToken(token) {
|
|
5
|
+
if (!token) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return this.collection.count({ tokenAddress: token }) > 0;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = RollupTable;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const MemoryTable = require('./base');
|
|
2
|
+
|
|
3
|
+
class TokenTable extends MemoryTable {
|
|
4
|
+
async existBySymbol(symbol) {
|
|
5
|
+
if (!symbol) {
|
|
6
|
+
throw new Error('param symbol is required');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return this.collection.count({ symbol }) > 0;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = TokenTable;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/statedb-memory",
|
|
3
3
|
"description": "OCAP statedb adapter that uses memory as backend statedb, just for test purpose",
|
|
4
|
-
"version": "1.6.
|
|
4
|
+
"version": "1.6.10",
|
|
5
5
|
"author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
|
|
6
6
|
"bugs": {
|
|
7
|
-
"url": "https://github.com/ArcBlock/
|
|
7
|
+
"url": "https://github.com/ArcBlock/asset-chain/issues",
|
|
8
8
|
"email": "shijun@arcblock.io"
|
|
9
9
|
},
|
|
10
10
|
"publishConfig": {
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
"wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"
|
|
15
15
|
],
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"jest": "^
|
|
17
|
+
"jest": "^27.3.1"
|
|
18
18
|
},
|
|
19
|
-
"homepage": "https://github.com/ArcBlock/
|
|
19
|
+
"homepage": "https://github.com/ArcBlock/asset-chain/tree/master/statedb/memory",
|
|
20
20
|
"keywords": [
|
|
21
21
|
"ocap",
|
|
22
22
|
"statedb",
|
|
@@ -29,18 +29,20 @@
|
|
|
29
29
|
],
|
|
30
30
|
"repository": {
|
|
31
31
|
"type": "git",
|
|
32
|
-
"url": "https://github.com/ArcBlock/
|
|
32
|
+
"url": "https://github.com/ArcBlock/asset-chain/tree/master/statedb/memory"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"lint": "eslint tests lib",
|
|
36
36
|
"lint:fix": "eslint --fix tests lib",
|
|
37
|
-
"test": "
|
|
37
|
+
"test": "jest --forceExit --detectOpenHandles",
|
|
38
38
|
"coverage": "npm run test -- --coverage"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "ab272e8db3a15c6571cc7fae7cc3d3e0fdd4bdb1",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@ocap/
|
|
43
|
-
"
|
|
44
|
-
"
|
|
42
|
+
"@ocap/state": "1.6.10",
|
|
43
|
+
"@ocap/statedb": "1.6.10",
|
|
44
|
+
"debug": "^4.3.3",
|
|
45
|
+
"lodash": "^4.17.21",
|
|
46
|
+
"lokijs": "^1.5.11"
|
|
45
47
|
}
|
|
46
48
|
}
|
package/lib/table/asset.js
DELETED
package/lib/table/delegation.js
DELETED