@ocap/statedb-memory 1.28.9 → 1.29.1
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 -2
- package/esm/db.d.mts +31 -0
- package/esm/db.mjs +101 -0
- package/esm/index.d.mts +2 -0
- package/esm/index.mjs +3 -0
- package/esm/package.mjs +69 -0
- package/esm/table/account.d.mts +17 -0
- package/esm/table/account.mjs +27 -0
- package/esm/table/balance.d.mts +23 -0
- package/esm/table/balance.mjs +43 -0
- package/esm/table/base.d.mts +37 -0
- package/esm/table/base.mjs +94 -0
- package/esm/table/rollup.d.mts +14 -0
- package/esm/table/rollup.mjs +20 -0
- package/esm/table/token.d.mts +14 -0
- package/esm/table/token.mjs +17 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/db.cjs +104 -0
- package/lib/db.d.cts +31 -0
- package/lib/index.cjs +4 -0
- package/lib/index.d.cts +2 -0
- package/lib/package.cjs +81 -0
- package/lib/table/account.cjs +29 -0
- package/lib/table/account.d.cts +17 -0
- package/lib/table/balance.cjs +45 -0
- package/lib/table/balance.d.cts +23 -0
- package/lib/table/base.cjs +98 -0
- package/lib/table/base.d.cts +37 -0
- package/lib/table/rollup.cjs +21 -0
- package/lib/table/rollup.d.cts +14 -0
- package/lib/table/token.cjs +18 -0
- package/lib/table/token.d.cts +14 -0
- package/package.json +35 -9
- package/lib/db.js +0 -60
- package/lib/index.js +0 -1
- package/lib/table/account.js +0 -20
- package/lib/table/balance.js +0 -41
- package/lib/table/base.js +0 -135
- package/lib/table/rollup.js +0 -13
- package/lib/table/token.js +0 -13
package/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# [**@ocap/statedb-memory**](https://github.com/arcblock/blockchain)
|
|
2
2
|
|
|
3
|
-
[](https://github.com/prettier/prettier)
|
|
4
3
|
|
|
5
4
|
> OCAP Adapter that stores data to the disk through [nedb](https://npmjs.org/package/nedb)
|
|
6
5
|
|
|
@@ -15,7 +14,7 @@ bun install @ocap/statedb-memory
|
|
|
15
14
|
## Usage
|
|
16
15
|
|
|
17
16
|
```js
|
|
18
|
-
const MemoryStateDB = require('@ocap/statedb-memory');
|
|
17
|
+
const MemoryStateDB = require('@ocap/statedb-memory').default;
|
|
19
18
|
|
|
20
19
|
const statedb = new MemoryStateDB();
|
|
21
20
|
```
|
package/esm/db.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { StateDB } from "@ocap/statedb";
|
|
2
|
+
import Lokijs from "lokijs";
|
|
3
|
+
import { IAccountState, IAssetFactoryState, IAssetState, IBalanceTable, IChainState, IDelegateState, IEvidenceState, IRollupBlock, IRollupTable, IStakeState, IStateTable, ITokenFactoryState, ITokenTable, ITxState } from "@ocap/types";
|
|
4
|
+
|
|
5
|
+
//#region src/db.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 内存 StateDB 实现
|
|
9
|
+
* 使用 LokiJS 作为内存存储引擎
|
|
10
|
+
*/
|
|
11
|
+
declare class MemoryStateDB extends StateDB {
|
|
12
|
+
name: string;
|
|
13
|
+
version: string;
|
|
14
|
+
db: Lokijs;
|
|
15
|
+
balance: IBalanceTable;
|
|
16
|
+
account: IStateTable<IAccountState>;
|
|
17
|
+
factory: IStateTable<IAssetFactoryState>;
|
|
18
|
+
stake: IStateTable<IStakeState>;
|
|
19
|
+
asset: IStateTable<IAssetState>;
|
|
20
|
+
delegation: IStateTable<IDelegateState>;
|
|
21
|
+
tx: IStateTable<ITxState>;
|
|
22
|
+
token: ITokenTable;
|
|
23
|
+
chain: IStateTable<IChainState>;
|
|
24
|
+
rollup: IRollupTable;
|
|
25
|
+
rollupBlock: IStateTable<IRollupBlock>;
|
|
26
|
+
evidence: IStateTable<IEvidenceState>;
|
|
27
|
+
tokenFactory: IStateTable<ITokenFactoryState>;
|
|
28
|
+
constructor();
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { MemoryStateDB as default };
|
package/esm/db.mjs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import package_default from "./package.mjs";
|
|
2
|
+
import base_default from "./table/base.mjs";
|
|
3
|
+
import account_default from "./table/account.mjs";
|
|
4
|
+
import balance_default from "./table/balance.mjs";
|
|
5
|
+
import rollup_default from "./table/rollup.mjs";
|
|
6
|
+
import token_default from "./table/token.mjs";
|
|
7
|
+
import { StateDB } from "@ocap/statedb";
|
|
8
|
+
import Lokijs from "lokijs";
|
|
9
|
+
|
|
10
|
+
//#region src/db.ts
|
|
11
|
+
const { name, version } = package_default;
|
|
12
|
+
let instanceCounter = 0;
|
|
13
|
+
/**
|
|
14
|
+
* 内存 StateDB 实现
|
|
15
|
+
* 使用 LokiJS 作为内存存储引擎
|
|
16
|
+
*/
|
|
17
|
+
var MemoryStateDB = class extends StateDB {
|
|
18
|
+
constructor() {
|
|
19
|
+
super();
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.version = version;
|
|
22
|
+
instanceCounter += 1;
|
|
23
|
+
this.db = new Lokijs(`ocap-memory-statedb-${instanceCounter}.db`);
|
|
24
|
+
this.balance = new balance_default({
|
|
25
|
+
name: "balance",
|
|
26
|
+
uniqIndex: ["address", "tokenAddress"],
|
|
27
|
+
db: this.db
|
|
28
|
+
});
|
|
29
|
+
this.account = new account_default({
|
|
30
|
+
name: "account",
|
|
31
|
+
uniqIndex: "address",
|
|
32
|
+
balanceTable: this.balance,
|
|
33
|
+
syncBalance: true,
|
|
34
|
+
db: this.db
|
|
35
|
+
});
|
|
36
|
+
this.factory = new base_default({
|
|
37
|
+
name: "factory",
|
|
38
|
+
uniqIndex: "address",
|
|
39
|
+
syncBalance: true,
|
|
40
|
+
balanceTable: this.balance,
|
|
41
|
+
db: this.db
|
|
42
|
+
});
|
|
43
|
+
this.stake = new base_default({
|
|
44
|
+
name: "stake",
|
|
45
|
+
uniqIndex: "address",
|
|
46
|
+
syncBalance: true,
|
|
47
|
+
balanceTable: this.balance,
|
|
48
|
+
db: this.db
|
|
49
|
+
});
|
|
50
|
+
this.asset = new base_default({
|
|
51
|
+
name: "asset",
|
|
52
|
+
uniqIndex: "address",
|
|
53
|
+
db: this.db
|
|
54
|
+
});
|
|
55
|
+
this.delegation = new base_default({
|
|
56
|
+
name: "delegation",
|
|
57
|
+
uniqIndex: "address",
|
|
58
|
+
db: this.db
|
|
59
|
+
});
|
|
60
|
+
this.tx = new base_default({
|
|
61
|
+
name: "tx",
|
|
62
|
+
uniqIndex: "hash",
|
|
63
|
+
db: this.db
|
|
64
|
+
});
|
|
65
|
+
this.token = new token_default({
|
|
66
|
+
name: "token",
|
|
67
|
+
uniqIndex: "address",
|
|
68
|
+
db: this.db
|
|
69
|
+
});
|
|
70
|
+
this.chain = new base_default({
|
|
71
|
+
name: "chain",
|
|
72
|
+
uniqIndex: "address",
|
|
73
|
+
db: this.db
|
|
74
|
+
});
|
|
75
|
+
this.rollup = new rollup_default({
|
|
76
|
+
name: "rollup",
|
|
77
|
+
uniqIndex: "address",
|
|
78
|
+
db: this.db
|
|
79
|
+
});
|
|
80
|
+
this.rollupBlock = new base_default({
|
|
81
|
+
name: "rollupBlock",
|
|
82
|
+
uniqIndex: "hash",
|
|
83
|
+
db: this.db
|
|
84
|
+
});
|
|
85
|
+
this.evidence = new base_default({
|
|
86
|
+
name: "evidence",
|
|
87
|
+
uniqIndex: "hash",
|
|
88
|
+
db: this.db
|
|
89
|
+
});
|
|
90
|
+
this.tokenFactory = new base_default({
|
|
91
|
+
name: "tokenFactory",
|
|
92
|
+
uniqIndex: "address",
|
|
93
|
+
db: this.db
|
|
94
|
+
});
|
|
95
|
+
this.attachReadyListeners();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var db_default = MemoryStateDB;
|
|
99
|
+
|
|
100
|
+
//#endregion
|
|
101
|
+
export { db_default as default };
|
package/esm/index.d.mts
ADDED
package/esm/index.mjs
ADDED
package/esm/package.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//#region package.json
|
|
2
|
+
var name = "@ocap/statedb-memory";
|
|
3
|
+
var package_default = {
|
|
4
|
+
name,
|
|
5
|
+
description: "OCAP statedb adapter that uses memory as backend statedb, just for test purpose",
|
|
6
|
+
version: "1.20.2",
|
|
7
|
+
author: "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
|
|
8
|
+
bugs: {
|
|
9
|
+
"url": "https://github.com/ArcBlock/blockchain/issues",
|
|
10
|
+
"email": "shijun@arcblock.io"
|
|
11
|
+
},
|
|
12
|
+
publishConfig: { "access": "public" },
|
|
13
|
+
contributors: ["wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"],
|
|
14
|
+
devDependencies: { "@types/lokijs": "^1.5.14" },
|
|
15
|
+
homepage: "https://github.com/ArcBlock/blockchain/tree/master/statedb/memory",
|
|
16
|
+
keywords: [
|
|
17
|
+
"ocap",
|
|
18
|
+
"statedb",
|
|
19
|
+
"memory"
|
|
20
|
+
],
|
|
21
|
+
license: "Apache-2.0",
|
|
22
|
+
type: "module",
|
|
23
|
+
main: "./lib/index.cjs",
|
|
24
|
+
module: "./esm/index.mjs",
|
|
25
|
+
types: "./esm/index.d.mts",
|
|
26
|
+
exports: {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./esm/index.d.mts",
|
|
29
|
+
"import": "./esm/index.mjs",
|
|
30
|
+
"default": "./lib/index.cjs"
|
|
31
|
+
},
|
|
32
|
+
"./lib/*.js": {
|
|
33
|
+
"types": "./esm/*.d.mts",
|
|
34
|
+
"import": "./esm/*.mjs",
|
|
35
|
+
"default": "./lib/*.cjs"
|
|
36
|
+
},
|
|
37
|
+
"./lib/*": {
|
|
38
|
+
"types": "./esm/*.d.mts",
|
|
39
|
+
"import": "./esm/*.mjs",
|
|
40
|
+
"default": "./lib/*.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
files: ["lib", "esm"],
|
|
44
|
+
repository: {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/ArcBlock/blockchain/tree/master/statedb/memory"
|
|
47
|
+
},
|
|
48
|
+
scripts: {
|
|
49
|
+
"build": "tsdown",
|
|
50
|
+
"prebuild": "rm -rf lib esm",
|
|
51
|
+
"lint": "biome check",
|
|
52
|
+
"lint:fix": "biome check --write",
|
|
53
|
+
"test": "bun test",
|
|
54
|
+
"coverage": "npm run test -- --coverage"
|
|
55
|
+
},
|
|
56
|
+
gitHead: "87990c8b5e215107fc587c1ced0d6b3e2cd2483e",
|
|
57
|
+
dependencies: {
|
|
58
|
+
"@ocap/state": "workspace:*",
|
|
59
|
+
"@ocap/statedb": "workspace:*",
|
|
60
|
+
"@ocap/types": "workspace:*",
|
|
61
|
+
"@ocap/util": "workspace:*",
|
|
62
|
+
"debug": "^4.4.3",
|
|
63
|
+
"lodash": "^4.17.23",
|
|
64
|
+
"lokijs": "^1.5.12"
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { package_default as default, name };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import MemoryTable from "./base.mjs";
|
|
2
|
+
import { IAccountState, IOperationContext } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/account.d.ts
|
|
5
|
+
interface AccountOperationContext extends IOperationContext {
|
|
6
|
+
traceMigration?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Account 表
|
|
10
|
+
* 扩展基础表,增加账户迁移追踪功能
|
|
11
|
+
*/
|
|
12
|
+
declare class AccountStateDB extends MemoryTable<IAccountState> {
|
|
13
|
+
_get(address: string, context?: AccountOperationContext): Promise<IAccountState | null>;
|
|
14
|
+
_create(key: string, attrs?: Partial<IAccountState>, context?: IOperationContext): Promise<IAccountState>;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { AccountStateDB as default };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
import { ensureChecksumAddress } from "@ocap/state/lib/states/account";
|
|
3
|
+
|
|
4
|
+
//#region src/table/account.ts
|
|
5
|
+
/**
|
|
6
|
+
* Account 表
|
|
7
|
+
* 扩展基础表,增加账户迁移追踪功能
|
|
8
|
+
*/
|
|
9
|
+
var AccountStateDB = class extends base_default {
|
|
10
|
+
async _get(address, context = {}) {
|
|
11
|
+
const { traceMigration = true, ...restContext } = context;
|
|
12
|
+
const current = await super._get(ensureChecksumAddress(address), restContext);
|
|
13
|
+
if (current && traceMigration && Array.isArray(current.migratedTo) && current.migratedTo.length) return this._get(current.migratedTo[0], context);
|
|
14
|
+
return current;
|
|
15
|
+
}
|
|
16
|
+
_create(key, attrs = {}, context) {
|
|
17
|
+
const address = ensureChecksumAddress(key);
|
|
18
|
+
return super._create(address, {
|
|
19
|
+
...attrs,
|
|
20
|
+
[this.uniqIndex]: address
|
|
21
|
+
}, context);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var account_default = AccountStateDB;
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { account_default as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import MemoryTable from "./base.mjs";
|
|
2
|
+
import { IBalanceState, IOperationContext } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/balance.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Balance 表
|
|
8
|
+
* 扩展基础表,增加余额管理功能
|
|
9
|
+
*/
|
|
10
|
+
declare class BalanceTable extends MemoryTable<IBalanceState> {
|
|
11
|
+
getBalance(address: string, _context?: IOperationContext): Promise<Record<string, string>>;
|
|
12
|
+
updateBalance({
|
|
13
|
+
address,
|
|
14
|
+
tokens,
|
|
15
|
+
context
|
|
16
|
+
}: {
|
|
17
|
+
address: string;
|
|
18
|
+
tokens: Record<string, string>;
|
|
19
|
+
context?: unknown;
|
|
20
|
+
}, ctx?: IOperationContext): Promise<Record<string, string>>;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { BalanceTable as default };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
import { ensureChecksumAddress } from "@ocap/state/lib/states/account";
|
|
3
|
+
|
|
4
|
+
//#region src/table/balance.ts
|
|
5
|
+
/**
|
|
6
|
+
* Balance 表
|
|
7
|
+
* 扩展基础表,增加余额管理功能
|
|
8
|
+
*/
|
|
9
|
+
var BalanceTable = class extends base_default {
|
|
10
|
+
async getBalance(address, _context) {
|
|
11
|
+
return (await this.collection.find({ address: ensureChecksumAddress(address) }) || []).reduce((acc, token) => {
|
|
12
|
+
acc[token.tokenAddress] = token.balance;
|
|
13
|
+
return acc;
|
|
14
|
+
}, {});
|
|
15
|
+
}
|
|
16
|
+
async updateBalance({ address, tokens, context = {} }, ctx) {
|
|
17
|
+
if (!Object.keys(tokens).length) return {};
|
|
18
|
+
const tokenBalances = await this.getBalance(address);
|
|
19
|
+
const updatedTokens = Object.keys(tokens).filter((token) => tokenBalances[token] && tokenBalances[token] !== "0" || tokens[token] && tokens[token] !== "0").filter((token) => tokenBalances[token] !== tokens[token]);
|
|
20
|
+
await Promise.all(updatedTokens.map(async (token) => {
|
|
21
|
+
const key = {
|
|
22
|
+
address,
|
|
23
|
+
tokenAddress: token
|
|
24
|
+
};
|
|
25
|
+
if (tokenBalances[token]) await this.update(key, {
|
|
26
|
+
...key,
|
|
27
|
+
balance: tokens[token],
|
|
28
|
+
context
|
|
29
|
+
}, ctx);
|
|
30
|
+
else await this.create(key, {
|
|
31
|
+
...key,
|
|
32
|
+
balance: tokens[token],
|
|
33
|
+
context
|
|
34
|
+
}, ctx);
|
|
35
|
+
tokenBalances[token] = tokens[token];
|
|
36
|
+
}));
|
|
37
|
+
return tokens;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var balance_default = BalanceTable;
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
export { balance_default as default };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { StateDBTable } from "@ocap/statedb";
|
|
2
|
+
import Lokijs from "lokijs";
|
|
3
|
+
import { IBalanceTable, IOperationContext } from "@ocap/types";
|
|
4
|
+
|
|
5
|
+
//#region src/table/base.d.ts
|
|
6
|
+
interface TableOptions {
|
|
7
|
+
name: string;
|
|
8
|
+
uniqIndex: string | string[];
|
|
9
|
+
syncBalance?: boolean;
|
|
10
|
+
balanceTable?: IBalanceTable;
|
|
11
|
+
db: Lokijs;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 内存表基类
|
|
15
|
+
* 使用 LokiJS Collection 作为底层存储
|
|
16
|
+
*/
|
|
17
|
+
declare class MemoryTable<T = unknown> extends StateDBTable<T> {
|
|
18
|
+
name: string;
|
|
19
|
+
balanceTable?: IBalanceTable;
|
|
20
|
+
syncBalance: boolean;
|
|
21
|
+
collection: Collection<LokiObj & Record<string, unknown>>;
|
|
22
|
+
constructor({
|
|
23
|
+
name,
|
|
24
|
+
uniqIndex,
|
|
25
|
+
balanceTable,
|
|
26
|
+
syncBalance,
|
|
27
|
+
db
|
|
28
|
+
}: TableOptions);
|
|
29
|
+
_create(key: string | Record<string, unknown>, attrs?: Partial<T>, _context?: IOperationContext): Promise<T>;
|
|
30
|
+
_get(key: string | Record<string, unknown>, _context?: IOperationContext): Promise<T | null>;
|
|
31
|
+
_history(_key?: string, _context?: IOperationContext): T[];
|
|
32
|
+
_update(key: string | Record<string, unknown>, updates: Partial<T>, _context?: IOperationContext): Promise<T>;
|
|
33
|
+
updateOrCreate(exist: unknown, state: Partial<T>, ctx?: IOperationContext): Promise<T>;
|
|
34
|
+
_reset(_context?: IOperationContext): void;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { MemoryTable as default };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { name } from "../package.mjs";
|
|
2
|
+
import { StateDBTable } from "@ocap/statedb";
|
|
3
|
+
import Debug from "debug";
|
|
4
|
+
import omit from "lodash/omit.js";
|
|
5
|
+
|
|
6
|
+
//#region src/table/base.ts
|
|
7
|
+
const debug = Debug(name);
|
|
8
|
+
/**
|
|
9
|
+
* 内存表基类
|
|
10
|
+
* 使用 LokiJS Collection 作为底层存储
|
|
11
|
+
*/
|
|
12
|
+
var MemoryTable = class MemoryTable extends StateDBTable {
|
|
13
|
+
constructor({ name: name$1, uniqIndex, balanceTable, syncBalance = false, db }) {
|
|
14
|
+
super(uniqIndex);
|
|
15
|
+
this.name = name$1;
|
|
16
|
+
this.balanceTable = balanceTable;
|
|
17
|
+
this.syncBalance = syncBalance;
|
|
18
|
+
if (this.syncBalance && !this.balanceTable) throw new Error("balanceTable is required when syncBalance is true");
|
|
19
|
+
if (!db) throw new Error("db is required for MemoryTable");
|
|
20
|
+
this.collection = db.addCollection(name$1, {
|
|
21
|
+
unique: [this.primaryKey],
|
|
22
|
+
clone: true
|
|
23
|
+
});
|
|
24
|
+
this.markReady();
|
|
25
|
+
}
|
|
26
|
+
async _create(key, attrs = {}, _context) {
|
|
27
|
+
const id = this.generatePrimaryKey(key);
|
|
28
|
+
if (await MemoryTable.prototype._get.call(this, id)) throw new Error(`${this.name} already exists: ${key}`);
|
|
29
|
+
debug(`insert ${this.name}`, attrs);
|
|
30
|
+
const insertAttrs = { ...attrs };
|
|
31
|
+
if (this.syncBalance) delete insertAttrs.tokens;
|
|
32
|
+
const result = await this.collection.insert({
|
|
33
|
+
[this.primaryKey]: id,
|
|
34
|
+
...insertAttrs
|
|
35
|
+
});
|
|
36
|
+
if (this.syncBalance && attrs.tokens) {
|
|
37
|
+
debug(`update balance for ${this.name}`, attrs);
|
|
38
|
+
result.tokens = await this.balanceTable.updateBalance({
|
|
39
|
+
address: attrs.address,
|
|
40
|
+
tokens: attrs.tokens,
|
|
41
|
+
context: attrs.context
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
async _get(key, _context) {
|
|
47
|
+
if (!key) return null;
|
|
48
|
+
const id = this.generatePrimaryKey(key);
|
|
49
|
+
const result = await this.collection.by(this.primaryKey, id) ?? null;
|
|
50
|
+
if (result && this.syncBalance) {
|
|
51
|
+
const balance = await this.balanceTable.getBalance(result.address);
|
|
52
|
+
result.tokens = {
|
|
53
|
+
...result.tokens || {},
|
|
54
|
+
...balance
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
_history(_key, _context) {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
async _update(key, updates, _context) {
|
|
63
|
+
const id = this.generatePrimaryKey(key);
|
|
64
|
+
const doc = await MemoryTable.prototype._get.call(this, id);
|
|
65
|
+
if (!doc) throw new Error(`${this.name} does not exists: ${key}`);
|
|
66
|
+
Object.assign(doc, updates);
|
|
67
|
+
const updateAttrs = { ...doc };
|
|
68
|
+
if (this.syncBalance) delete updateAttrs.tokens;
|
|
69
|
+
await this.collection.update(updateAttrs);
|
|
70
|
+
if (this.syncBalance && doc.tokens) {
|
|
71
|
+
debug(`update balance for ${this.name}`, doc);
|
|
72
|
+
doc.tokens = await this.balanceTable.updateBalance({
|
|
73
|
+
address: doc.address,
|
|
74
|
+
tokens: doc.tokens,
|
|
75
|
+
context: doc.context
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return doc;
|
|
79
|
+
}
|
|
80
|
+
updateOrCreate(exist, state, ctx) {
|
|
81
|
+
const id = this.generatePrimaryKey(state);
|
|
82
|
+
const attrs = omit(state, this.uniqIndex ?? []);
|
|
83
|
+
if (!id) throw new Error("Cannot update or create without uniq index");
|
|
84
|
+
if (exist) return this.update(id, attrs, ctx);
|
|
85
|
+
return this.create(id, attrs, ctx);
|
|
86
|
+
}
|
|
87
|
+
_reset(_context) {
|
|
88
|
+
this.collection.removeWhere({});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
var base_default = MemoryTable;
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
export { base_default as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import MemoryTable from "./base.mjs";
|
|
2
|
+
import { IRollupState } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/rollup.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Rollup 表
|
|
8
|
+
* 扩展基础表,增加按代币查询功能
|
|
9
|
+
*/
|
|
10
|
+
declare class RollupTable extends MemoryTable<IRollupState> {
|
|
11
|
+
existByToken(token?: string): Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { RollupTable as default };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/table/rollup.ts
|
|
4
|
+
/**
|
|
5
|
+
* Rollup 表
|
|
6
|
+
* 扩展基础表,增加按代币查询功能
|
|
7
|
+
*/
|
|
8
|
+
var RollupTable = class extends base_default {
|
|
9
|
+
async existByToken(token) {
|
|
10
|
+
if (!token) return false;
|
|
11
|
+
return this.collection.count({
|
|
12
|
+
tokenAddress: token,
|
|
13
|
+
paused: false
|
|
14
|
+
}) > 0;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var rollup_default = RollupTable;
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { rollup_default as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import MemoryTable from "./base.mjs";
|
|
2
|
+
import { ITokenState } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/token.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Token 表
|
|
8
|
+
* 扩展基础表,增加按符号查询功能
|
|
9
|
+
*/
|
|
10
|
+
declare class TokenTable extends MemoryTable<ITokenState> {
|
|
11
|
+
existBySymbol(symbol: string): Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { TokenTable as default };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/table/token.ts
|
|
4
|
+
/**
|
|
5
|
+
* Token 表
|
|
6
|
+
* 扩展基础表,增加按符号查询功能
|
|
7
|
+
*/
|
|
8
|
+
var TokenTable = class extends base_default {
|
|
9
|
+
async existBySymbol(symbol) {
|
|
10
|
+
if (!symbol) throw new Error("param symbol is required");
|
|
11
|
+
return this.collection.count({ symbol }) > 0;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var token_default = TokenTable;
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { token_default as default };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/lib/db.cjs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
3
|
+
const require_package = require('./package.cjs');
|
|
4
|
+
const require_table_base = require('./table/base.cjs');
|
|
5
|
+
const require_table_account = require('./table/account.cjs');
|
|
6
|
+
const require_table_balance = require('./table/balance.cjs');
|
|
7
|
+
const require_table_rollup = require('./table/rollup.cjs');
|
|
8
|
+
const require_table_token = require('./table/token.cjs');
|
|
9
|
+
let _ocap_statedb = require("@ocap/statedb");
|
|
10
|
+
let lokijs = require("lokijs");
|
|
11
|
+
lokijs = require_rolldown_runtime.__toESM(lokijs);
|
|
12
|
+
|
|
13
|
+
//#region src/db.ts
|
|
14
|
+
const { name, version } = require_package.default;
|
|
15
|
+
let instanceCounter = 0;
|
|
16
|
+
/**
|
|
17
|
+
* 内存 StateDB 实现
|
|
18
|
+
* 使用 LokiJS 作为内存存储引擎
|
|
19
|
+
*/
|
|
20
|
+
var MemoryStateDB = class extends _ocap_statedb.StateDB {
|
|
21
|
+
constructor() {
|
|
22
|
+
super();
|
|
23
|
+
this.name = name;
|
|
24
|
+
this.version = version;
|
|
25
|
+
instanceCounter += 1;
|
|
26
|
+
this.db = new lokijs.default(`ocap-memory-statedb-${instanceCounter}.db`);
|
|
27
|
+
this.balance = new require_table_balance.default({
|
|
28
|
+
name: "balance",
|
|
29
|
+
uniqIndex: ["address", "tokenAddress"],
|
|
30
|
+
db: this.db
|
|
31
|
+
});
|
|
32
|
+
this.account = new require_table_account.default({
|
|
33
|
+
name: "account",
|
|
34
|
+
uniqIndex: "address",
|
|
35
|
+
balanceTable: this.balance,
|
|
36
|
+
syncBalance: true,
|
|
37
|
+
db: this.db
|
|
38
|
+
});
|
|
39
|
+
this.factory = new require_table_base.default({
|
|
40
|
+
name: "factory",
|
|
41
|
+
uniqIndex: "address",
|
|
42
|
+
syncBalance: true,
|
|
43
|
+
balanceTable: this.balance,
|
|
44
|
+
db: this.db
|
|
45
|
+
});
|
|
46
|
+
this.stake = new require_table_base.default({
|
|
47
|
+
name: "stake",
|
|
48
|
+
uniqIndex: "address",
|
|
49
|
+
syncBalance: true,
|
|
50
|
+
balanceTable: this.balance,
|
|
51
|
+
db: this.db
|
|
52
|
+
});
|
|
53
|
+
this.asset = new require_table_base.default({
|
|
54
|
+
name: "asset",
|
|
55
|
+
uniqIndex: "address",
|
|
56
|
+
db: this.db
|
|
57
|
+
});
|
|
58
|
+
this.delegation = new require_table_base.default({
|
|
59
|
+
name: "delegation",
|
|
60
|
+
uniqIndex: "address",
|
|
61
|
+
db: this.db
|
|
62
|
+
});
|
|
63
|
+
this.tx = new require_table_base.default({
|
|
64
|
+
name: "tx",
|
|
65
|
+
uniqIndex: "hash",
|
|
66
|
+
db: this.db
|
|
67
|
+
});
|
|
68
|
+
this.token = new require_table_token.default({
|
|
69
|
+
name: "token",
|
|
70
|
+
uniqIndex: "address",
|
|
71
|
+
db: this.db
|
|
72
|
+
});
|
|
73
|
+
this.chain = new require_table_base.default({
|
|
74
|
+
name: "chain",
|
|
75
|
+
uniqIndex: "address",
|
|
76
|
+
db: this.db
|
|
77
|
+
});
|
|
78
|
+
this.rollup = new require_table_rollup.default({
|
|
79
|
+
name: "rollup",
|
|
80
|
+
uniqIndex: "address",
|
|
81
|
+
db: this.db
|
|
82
|
+
});
|
|
83
|
+
this.rollupBlock = new require_table_base.default({
|
|
84
|
+
name: "rollupBlock",
|
|
85
|
+
uniqIndex: "hash",
|
|
86
|
+
db: this.db
|
|
87
|
+
});
|
|
88
|
+
this.evidence = new require_table_base.default({
|
|
89
|
+
name: "evidence",
|
|
90
|
+
uniqIndex: "hash",
|
|
91
|
+
db: this.db
|
|
92
|
+
});
|
|
93
|
+
this.tokenFactory = new require_table_base.default({
|
|
94
|
+
name: "tokenFactory",
|
|
95
|
+
uniqIndex: "address",
|
|
96
|
+
db: this.db
|
|
97
|
+
});
|
|
98
|
+
this.attachReadyListeners();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
var db_default = MemoryStateDB;
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
exports.default = db_default;
|