@ocap/statedb-fs 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 CHANGED
@@ -1,4 +1,4 @@
1
- # [**@ocap/statedb-fs**](https://github.com/arcblock/ocap-js)
1
+ # [**@ocap/statedb-fs**](https://github.com/arcblock/asset-chain)
2
2
 
3
3
  [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
4
4
 
package/lib/db.js CHANGED
@@ -1,9 +1,9 @@
1
1
  const { StateDB } = require('@ocap/statedb');
2
2
 
3
- const AccountStateDB = require('./table/account');
4
- const AssetStateDB = require('./table/asset');
5
- const DelegationStateDB = require('./table/delegation');
6
- const TxStateDB = require('./table/tx');
3
+ const Table = require('./table/base');
4
+ const Account = require('./table/account');
5
+ const Token = require('./table/token');
6
+ const Rollup = require('./table/rollup');
7
7
 
8
8
  const { name, version } = require('../package.json');
9
9
 
@@ -14,15 +14,19 @@ class FsStateDB extends StateDB {
14
14
  this.name = name;
15
15
  this.version = version;
16
16
 
17
- this.account = new AccountStateDB('account', dataDir, 'address');
18
- this.asset = new AssetStateDB('asset', dataDir, 'address');
19
- this.delegation = new DelegationStateDB('delegation', dataDir, 'address');
20
- this.tx = new TxStateDB('tx', dataDir, 'hash');
17
+ this.account = new Account('account', dataDir, 'address');
18
+ this.asset = new Table('asset', dataDir, 'address');
19
+ this.factory = new Table('factory', dataDir, 'address');
20
+ this.delegation = new Table('delegation', dataDir, 'address');
21
+ this.tx = new Table('tx', dataDir, 'hash');
22
+ this.token = new Token('token', dataDir, 'address');
23
+ this.chain = new Table('chain', dataDir, 'address');
24
+ this.stake = new Table('stake', dataDir, 'address');
25
+ this.rollup = new Rollup('rollup', dataDir, 'address');
26
+ this.rollupBlock = new Table('rollupBlock', dataDir, 'hash');
27
+ this.evidence = new Table('evidence', dataDir, 'hash');
21
28
 
22
- this.account.onReady(() => this.markReady('account'));
23
- this.asset.onReady(() => this.markReady('asset'));
24
- this.delegation.onReady(() => this.markReady('delegation'));
25
- this.tx.onReady(() => this.markReady('tx'));
29
+ this.attachReadyListeners();
26
30
  }
27
31
  }
28
32
 
@@ -1,15 +1,30 @@
1
- /* eslint-disable no-underscore-dangle */
1
+ const { ensureChecksumAddress } = require('@ocap/state/lib/states/account');
2
+ const debug = require('debug')(require('../../package.json').name);
2
3
  const FsTable = require('./base');
3
4
 
4
5
  class AccountTable extends FsTable {
5
6
  async _get(address, { final = true } = {}) {
6
- const current = await this.collection.by(this.uniqIndex, address);
7
+ const current = await this.collection.by(this.uniqIndex, ensureChecksumAddress(address));
7
8
  if (current && final && Array.isArray(current.migratedTo) && current.migratedTo.length) {
8
9
  return this.collection.by(this.uniqIndex, current.migratedTo[0]);
9
10
  }
10
11
 
11
12
  return current;
12
13
  }
14
+
15
+ async _create(key, attrs) {
16
+ const doc = await this._get(key);
17
+ if (doc) {
18
+ throw new Error(`${this.name} already exists: ${key}`);
19
+ }
20
+
21
+ debug(`insert ${this.name}`, attrs);
22
+ const newDoc = { [this.uniqIndex]: key, ...attrs };
23
+ newDoc[this.uniqIndex] = ensureChecksumAddress(newDoc[this.uniqIndex]);
24
+ const result = await this.collection.insert(newDoc);
25
+
26
+ return result;
27
+ }
13
28
  }
14
29
 
15
30
  module.exports = AccountTable;
package/lib/table/base.js CHANGED
@@ -39,15 +39,21 @@ class FsTable extends StateDBTable {
39
39
  throw new Error(`${this.name} already exists: ${key}`);
40
40
  }
41
41
  debug(`insert ${this.name}`, attrs);
42
- return this.collection.insert({ [this.uniqIndex]: key, ...attrs });
42
+ const result = await this.collection.insert({ [this.uniqIndex]: key, ...attrs });
43
+
44
+ return result;
43
45
  }
44
46
 
45
47
  _get(key) {
46
- return this.collection.by(this.uniqIndex, key);
48
+ return key ? this.collection.by(this.uniqIndex, key) : null;
49
+ }
50
+
51
+ _history() {
52
+ return [];
47
53
  }
48
54
 
49
55
  async _update(key, updates) {
50
- const doc = await this._get(key);
56
+ const doc = await this.collection.by(this.uniqIndex, key);
51
57
  if (!doc) {
52
58
  throw new Error(`${this.name} does not exists: ${key}`);
53
59
  }
@@ -55,9 +61,22 @@ class FsTable extends StateDBTable {
55
61
  debug(`update ${this.name}`, { key, updates });
56
62
  Object.assign(doc, updates);
57
63
  await this.collection.update(doc);
64
+
58
65
  return doc;
59
66
  }
60
67
 
68
+ updateOrCreate(exist, state, ctx) {
69
+ if (!state[this.uniqIndex]) {
70
+ throw new Error('Cannot update or create without uniq index');
71
+ }
72
+
73
+ if (exist) {
74
+ return this.update(state[this.uniqIndex], state, ctx);
75
+ }
76
+
77
+ return this.create(state[this.uniqIndex], state, ctx);
78
+ }
79
+
61
80
  _reset() {
62
81
  this.collection.removeWhere({});
63
82
  }
@@ -0,0 +1,13 @@
1
+ const FsTable = require('./base');
2
+
3
+ class RollupTable extends FsTable {
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 FsTable = require('./base');
2
+
3
+ class TokenTable extends FsTable {
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-fs",
3
3
  "description": "OCAP statedb adapter that uses fs as backend",
4
- "version": "1.6.3",
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/ocap-js/issues",
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": "^26.6.3"
17
+ "jest": "^27.3.1"
18
18
  },
19
- "homepage": "https://github.com/ArcBlock/ocap-js/tree/master/statedb/fs",
19
+ "homepage": "https://github.com/ArcBlock/asset-chain/tree/master/statedb/fs",
20
20
  "keywords": [
21
21
  "ocap",
22
22
  "statedb",
@@ -29,18 +29,19 @@
29
29
  ],
30
30
  "repository": {
31
31
  "type": "git",
32
- "url": "https://github.com/ArcBlock/ocap-js/tree/master/statedb/fs"
32
+ "url": "https://github.com/ArcBlock/asset-chain/tree/master/statedb/fs"
33
33
  },
34
34
  "scripts": {
35
35
  "lint": "eslint tests lib",
36
36
  "lint:fix": "eslint --fix tests lib",
37
- "test": "node tools/jest.js",
37
+ "test": "jest --forceExit --detectOpenHandles",
38
38
  "coverage": "npm run test -- --coverage & exit 0"
39
39
  },
40
- "gitHead": "5246168dbbc268a3e6f95c3992d9d343647ca25b",
40
+ "gitHead": "ab272e8db3a15c6571cc7fae7cc3d3e0fdd4bdb1",
41
41
  "dependencies": {
42
- "@ocap/statedb": "^1.6.3",
43
- "debug": "^4.3.1",
42
+ "@ocap/state": "1.6.10",
43
+ "@ocap/statedb": "1.6.10",
44
+ "debug": "^4.3.3",
44
45
  "lodash": "^4.17.21",
45
46
  "lokijs": "^1.5.11"
46
47
  }
@@ -1,5 +0,0 @@
1
- const FsTable = require('./base');
2
-
3
- class AssetTable extends FsTable {}
4
-
5
- module.exports = AssetTable;
@@ -1,5 +0,0 @@
1
- const FsTable = require('./base');
2
-
3
- class DelegationTable extends FsTable {}
4
-
5
- module.exports = DelegationTable;
package/lib/table/tx.js DELETED
@@ -1,5 +0,0 @@
1
- const FsTable = require('./base');
2
-
3
- class TxTable extends FsTable {}
4
-
5
- module.exports = TxTable;