@metamask-previews/eth-json-rpc-middleware 21.0.0-preview-c96ff8f → 21.0.0-preview-6b15e01
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/CHANGELOG.md +0 -3
- package/dist/block-cache.cjs +27 -15
- package/dist/block-cache.cjs.map +1 -1
- package/dist/block-cache.d.cts +3 -5
- package/dist/block-cache.d.cts.map +1 -1
- package/dist/block-cache.d.mts +3 -5
- package/dist/block-cache.d.mts.map +1 -1
- package/dist/block-cache.mjs +27 -15
- package/dist/block-cache.mjs.map +1 -1
- package/dist/block-ref-rewrite.cjs +8 -12
- package/dist/block-ref-rewrite.cjs.map +1 -1
- package/dist/block-ref-rewrite.d.cts +3 -3
- package/dist/block-ref-rewrite.d.cts.map +1 -1
- package/dist/block-ref-rewrite.d.mts +3 -3
- package/dist/block-ref-rewrite.d.mts.map +1 -1
- package/dist/block-ref-rewrite.mjs +8 -12
- package/dist/block-ref-rewrite.mjs.map +1 -1
- package/dist/block-ref.cjs +10 -17
- package/dist/block-ref.cjs.map +1 -1
- package/dist/block-ref.d.cts +3 -12
- package/dist/block-ref.d.cts.map +1 -1
- package/dist/block-ref.d.mts +3 -12
- package/dist/block-ref.d.mts.map +1 -1
- package/dist/block-ref.mjs +10 -17
- package/dist/block-ref.mjs.map +1 -1
- package/dist/block-tracker-inspector.cjs +25 -28
- package/dist/block-tracker-inspector.cjs.map +1 -1
- package/dist/block-tracker-inspector.d.cts +3 -3
- package/dist/block-tracker-inspector.d.cts.map +1 -1
- package/dist/block-tracker-inspector.d.mts +3 -3
- package/dist/block-tracker-inspector.d.mts.map +1 -1
- package/dist/block-tracker-inspector.mjs +25 -28
- package/dist/block-tracker-inspector.mjs.map +1 -1
- package/dist/fetch.cjs +12 -8
- package/dist/fetch.cjs.map +1 -1
- package/dist/fetch.d.cts +3 -5
- package/dist/fetch.d.cts.map +1 -1
- package/dist/fetch.d.mts +3 -5
- package/dist/fetch.d.mts.map +1 -1
- package/dist/fetch.mjs +12 -8
- package/dist/fetch.mjs.map +1 -1
- package/dist/inflight-cache.cjs +45 -61
- package/dist/inflight-cache.cjs.map +1 -1
- package/dist/inflight-cache.d.cts +3 -12
- package/dist/inflight-cache.d.cts.map +1 -1
- package/dist/inflight-cache.d.mts +3 -12
- package/dist/inflight-cache.d.mts.map +1 -1
- package/dist/inflight-cache.mjs +45 -61
- package/dist/inflight-cache.mjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.cjs +9 -11
- package/dist/methods/wallet-request-execution-permissions.cjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.cts +3 -5
- package/dist/methods/wallet-request-execution-permissions.d.cts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.mts +3 -5
- package/dist/methods/wallet-request-execution-permissions.d.mts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.mjs +7 -9
- package/dist/methods/wallet-request-execution-permissions.mjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.cjs +11 -14
- package/dist/methods/wallet-revoke-execution-permission.cjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.cts +5 -10
- package/dist/methods/wallet-revoke-execution-permission.d.cts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.mts +5 -10
- package/dist/methods/wallet-revoke-execution-permission.d.mts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.mjs +8 -11
- package/dist/methods/wallet-revoke-execution-permission.mjs.map +1 -1
- package/dist/providerAsMiddleware.cjs +1 -5
- package/dist/providerAsMiddleware.cjs.map +1 -1
- package/dist/providerAsMiddleware.d.cts +3 -5
- package/dist/providerAsMiddleware.d.cts.map +1 -1
- package/dist/providerAsMiddleware.d.mts +3 -5
- package/dist/providerAsMiddleware.d.mts.map +1 -1
- package/dist/providerAsMiddleware.mjs +0 -3
- package/dist/providerAsMiddleware.mjs.map +1 -1
- package/dist/retryOnEmpty.cjs +11 -8
- package/dist/retryOnEmpty.cjs.map +1 -1
- package/dist/retryOnEmpty.d.cts +3 -3
- package/dist/retryOnEmpty.d.cts.map +1 -1
- package/dist/retryOnEmpty.d.mts +3 -3
- package/dist/retryOnEmpty.d.mts.map +1 -1
- package/dist/retryOnEmpty.mjs +11 -8
- package/dist/retryOnEmpty.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +5 -0
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +5 -0
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/validation.cjs +2 -2
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.cts +3 -6
- package/dist/utils/validation.d.cts.map +1 -1
- package/dist/utils/validation.d.mts +3 -6
- package/dist/utils/validation.d.mts.map +1 -1
- package/dist/utils/validation.mjs +2 -2
- package/dist/utils/validation.mjs.map +1 -1
- package/dist/wallet.cjs +92 -87
- package/dist/wallet.cjs.map +1 -1
- package/dist/wallet.d.cts +5 -8
- package/dist/wallet.d.cts.map +1 -1
- package/dist/wallet.d.mts +5 -8
- package/dist/wallet.d.mts.map +1 -1
- package/dist/wallet.mjs +93 -88
- package/dist/wallet.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,9 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
-
- **BREAKING:** Migrate to `JsonRpcEngineV2` ([#7065](https://github.com/MetaMask/core/pull/7065))
|
|
13
|
-
- Migrates all middleware from `JsonRpcEngine` to `JsonRpcEngineV2`.
|
|
14
|
-
- To continue using this package with the legacy `JsonRpcEngine`, use the `asLegacyMiddleware` backwards compatibility function.
|
|
15
12
|
- **BREAKING:** Use `InternalProvider` instead of `SafeEventEmitterProvider` ([#6796](https://github.com/MetaMask/core/pull/6796))
|
|
16
13
|
- Wherever a `SafeEventEmitterProvider` was expected, an `InternalProvider` is now expected instead.
|
|
17
14
|
- **BREAKING:** Stop retrying `undefined` results for methods that include a block tag parameter ([#7001](https://github.com/MetaMask/core/pull/7001))
|
package/dist/block-cache.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createBlockCacheMiddleware = void 0;
|
|
4
|
+
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
4
5
|
const logging_utils_1 = require("./logging-utils.cjs");
|
|
5
6
|
const cache_1 = require("./utils/cache.cjs");
|
|
6
7
|
const log = (0, logging_utils_1.createModuleLogger)(logging_utils_1.projectLogger, 'block-cache');
|
|
@@ -86,9 +87,11 @@ class BlockCacheStrategy {
|
|
|
86
87
|
}
|
|
87
88
|
}
|
|
88
89
|
function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
90
|
+
// validate options
|
|
89
91
|
if (!blockTracker) {
|
|
90
92
|
throw new Error('createBlockCacheMiddleware - No PollingBlockTracker specified');
|
|
91
93
|
}
|
|
94
|
+
// create caching strategies
|
|
92
95
|
const blockCache = new BlockCacheStrategy();
|
|
93
96
|
const strategies = {
|
|
94
97
|
[cache_1.CacheStrategy.Permanent]: blockCache,
|
|
@@ -96,23 +99,28 @@ function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
96
99
|
[cache_1.CacheStrategy.Fork]: blockCache,
|
|
97
100
|
[cache_1.CacheStrategy.Never]: undefined,
|
|
98
101
|
};
|
|
99
|
-
return async (
|
|
100
|
-
if
|
|
102
|
+
return (0, json_rpc_engine_1.createAsyncMiddleware)(async (req, res, next) => {
|
|
103
|
+
// allow cach to be skipped if so specified
|
|
104
|
+
if (req.skipCache) {
|
|
101
105
|
return next();
|
|
102
106
|
}
|
|
103
|
-
|
|
107
|
+
// check type and matching strategy
|
|
108
|
+
const type = (0, cache_1.cacheTypeForMethod)(req.method);
|
|
104
109
|
const strategy = strategies[type];
|
|
110
|
+
// If there's no strategy in place, pass it down the chain.
|
|
105
111
|
if (!strategy) {
|
|
106
112
|
return next();
|
|
107
113
|
}
|
|
108
|
-
|
|
114
|
+
// If the strategy can't cache this request, ignore it.
|
|
115
|
+
if (!strategy.canCacheRequest(req)) {
|
|
109
116
|
return next();
|
|
110
117
|
}
|
|
111
|
-
|
|
118
|
+
// get block reference (number or keyword)
|
|
119
|
+
const requestBlockTag = (0, cache_1.blockTagForRequest)(req);
|
|
112
120
|
const blockTag = requestBlockTag && typeof requestBlockTag === 'string'
|
|
113
121
|
? requestBlockTag
|
|
114
122
|
: 'latest';
|
|
115
|
-
log('blockTag = %o, req = %o', blockTag,
|
|
123
|
+
log('blockTag = %o, req = %o', blockTag, req);
|
|
116
124
|
// get exact block number
|
|
117
125
|
let requestedBlockNumber;
|
|
118
126
|
if (blockTag === 'earliest') {
|
|
@@ -120,6 +128,7 @@ function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
120
128
|
requestedBlockNumber = '0x00';
|
|
121
129
|
}
|
|
122
130
|
else if (blockTag === 'latest') {
|
|
131
|
+
// fetch latest block number
|
|
123
132
|
log('Fetching latest block number to determine cache key');
|
|
124
133
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
125
134
|
// clear all cache before latest block
|
|
@@ -128,26 +137,29 @@ function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
128
137
|
requestedBlockNumber = latestBlockNumber;
|
|
129
138
|
}
|
|
130
139
|
else {
|
|
131
|
-
//
|
|
140
|
+
// We have a hex number
|
|
132
141
|
requestedBlockNumber = blockTag;
|
|
133
142
|
}
|
|
134
143
|
// end on a hit, continue on a miss
|
|
135
|
-
const cacheResult = await strategy.get(
|
|
144
|
+
const cacheResult = await strategy.get(req, requestedBlockNumber);
|
|
136
145
|
if (cacheResult === undefined) {
|
|
137
146
|
// cache miss
|
|
138
147
|
// wait for other middleware to handle request
|
|
139
148
|
log('No cache stored under block number %o, carrying request forward', requestedBlockNumber);
|
|
140
|
-
|
|
149
|
+
await next();
|
|
141
150
|
// add result to cache
|
|
142
151
|
// it's safe to cast res.result as Block, due to runtime type checks
|
|
143
152
|
// performed when strategy.set is called
|
|
144
|
-
log('Populating cache with',
|
|
145
|
-
await strategy.set(
|
|
146
|
-
return result;
|
|
153
|
+
log('Populating cache with', res);
|
|
154
|
+
await strategy.set(req, requestedBlockNumber, res.result);
|
|
147
155
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
156
|
+
else {
|
|
157
|
+
// fill in result from cache
|
|
158
|
+
log('Cache hit, reusing cache result stored under block number %o', requestedBlockNumber);
|
|
159
|
+
res.result = cacheResult;
|
|
160
|
+
}
|
|
161
|
+
return undefined;
|
|
162
|
+
});
|
|
151
163
|
}
|
|
152
164
|
exports.createBlockCacheMiddleware = createBlockCacheMiddleware;
|
|
153
165
|
//# sourceMappingURL=block-cache.cjs.map
|
package/dist/block-cache.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.cjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;AAOA,uDAAoE;AAOpE,6CAMuB;AAEvB,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAc,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMpE,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;IAClB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,IAAI,UAAU,GAAe,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACrD,+BAA+B;QAC/B,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,QAAQ,GAAe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oB,GAAG,QAAQ,CAAC;SACjC;QAED,mCAAmC;QACnC,MAAM,WAAW,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;QACtE,IAAI,WAAW,KAAK,SAAS,EAAE;YAC7B,aAAa;YACb,8CAA8C;YAC9C,GAAG,CACD,iEAAiE,EACjE,oBAAoB,CACrB,CAAC;YACF,MAAM,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;YAE5B,sBAAsB;YACtB,oEAAoE;YACpE,wCAAwC;YACxC,GAAG,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;YACrC,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAe,CAAC,CAAC;YACnE,OAAO,MAAM,CAAC;SACf;QACD,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAzFD,gEAyFC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n private cache: Cache;\n\n constructor() {\n this.cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.cache[num]);\n }\n}\n\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"block-cache.cjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":";;;AACA,+DAAkE;AAGlE,uDAAoE;AASpE,6CAMuB;AAEvB,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAG,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMzD,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;IAClB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,IAAI,UAAU,GAAe,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACrD,+BAA+B;QAC/B,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,QAAQ,GAAe,EAAE,CAAC;YAChC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC;YACnC,UAAU,GAAG,QAAQ,CAAC;SACvB;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,GAAG,CACP,OAAuB,EACvB,oBAA4B;QAE5B,qBAAqB;QACrB,MAAM,UAAU,GAAe,IAAI,CAAC,aAAa,CAAC,oBAAoB,CAAC,CAAC;QACxE,gCAAgC;QAChC,MAAM,UAAU,GAAkB,IAAA,iCAAyB,EAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAC3E,OAAO,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACzD,CAAC;I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,CAAC;YAClC,MAAM,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,oBAAoB,EAAE,GAAG,CAAC,MAAe,CAAC,CAAC;SACpE;aAAM;YACL,4BAA4B;YAC5B,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;YACF,GAAG,CAAC,MAAM,GAAG,WAAW,CAAC;SAC1B;QACD,OAAO,SAAS,CAAC;IACnB,CAAC,CACF,CAAC;AACJ,CAAC;AApGD,gEAoGC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n JsonRpcCacheMiddleware,\n JsonRpcRequestToCache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n private cache: Cache;\n\n constructor() {\n this.cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result as any)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.cache[num]);\n }\n}\n\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcCacheMiddleware<\n JsonRpcParams,\n Json\n> {\n // validate options\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n // create caching strategies\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return createAsyncMiddleware(\n async (req: JsonRpcRequestToCache<JsonRpcParams>, res, next) => {\n // allow cach to be skipped if so specified\n if (req.skipCache) {\n return next();\n }\n // check type and matching strategy\n const type = cacheTypeForMethod(req.method);\n const strategy = strategies[type];\n // If there's no strategy in place, pass it down the chain.\n if (!strategy) {\n return next();\n }\n\n // If the strategy can't cache this request, ignore it.\n if (!strategy.canCacheRequest(req)) {\n return next();\n }\n\n // get block reference (number or keyword)\n const requestBlockTag = blockTagForRequest(req);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, req);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n // fetch latest block number\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // We have a hex number\n requestedBlockNumber = blockTag;\n }\n // end on a hit, continue on a miss\n const cacheResult: Block | undefined = await strategy.get(\n req,\n requestedBlockNumber,\n );\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', res);\n await strategy.set(req, requestedBlockNumber, res.result as Block);\n } else {\n // fill in result from cache\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n res.result = cacheResult;\n }\n return undefined;\n },\n );\n}\n"]}
|
package/dist/block-cache.d.cts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
3
|
+
import type { JsonRpcCacheMiddleware } from "./types.cjs";
|
|
4
4
|
type BlockCacheMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
7
|
-
export declare function createBlockCacheMiddleware({ blockTracker, }?: BlockCacheMiddlewareOptions):
|
|
8
|
-
skipCache?: boolean;
|
|
9
|
-
}>>;
|
|
7
|
+
export declare function createBlockCacheMiddleware({ blockTracker, }?: BlockCacheMiddlewareOptions): JsonRpcCacheMiddleware<JsonRpcParams, Json>;
|
|
10
8
|
export {};
|
|
11
9
|
//# sourceMappingURL=block-cache.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.d.cts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;
|
|
1
|
+
{"version":3,"file":"block-cache.d.cts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AAEvE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAkB,wBAAwB;AAG3E,OAAO,KAAK,EAKV,sBAAsB,EAEvB,oBAAgB;AAajB,KAAK,2BAA2B,GAAG;IACjC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AA2GF,wBAAgB,0BAA0B,CAAC,EACzC,YAAY,GACb,GAAE,2BAAgC,GAAG,sBAAsB,CAC1D,aAAa,EACb,IAAI,CACL,CA+FA"}
|
package/dist/block-cache.d.mts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
3
|
+
import type { JsonRpcCacheMiddleware } from "./types.mjs";
|
|
4
4
|
type BlockCacheMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
7
|
-
export declare function createBlockCacheMiddleware({ blockTracker, }?: BlockCacheMiddlewareOptions):
|
|
8
|
-
skipCache?: boolean;
|
|
9
|
-
}>>;
|
|
7
|
+
export declare function createBlockCacheMiddleware({ blockTracker, }?: BlockCacheMiddlewareOptions): JsonRpcCacheMiddleware<JsonRpcParams, Json>;
|
|
10
8
|
export {};
|
|
11
9
|
//# sourceMappingURL=block-cache.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.d.mts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;
|
|
1
|
+
{"version":3,"file":"block-cache.d.mts","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AAEvE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAkB,wBAAwB;AAG3E,OAAO,KAAK,EAKV,sBAAsB,EAEvB,oBAAgB;AAajB,KAAK,2BAA2B,GAAG;IACjC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AA2GF,wBAAgB,0BAA0B,CAAC,EACzC,YAAY,GACb,GAAE,2BAAgC,GAAG,sBAAsB,CAC1D,aAAa,EACb,IAAI,CACL,CA+FA"}
|
package/dist/block-cache.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createAsyncMiddleware } from "@metamask/json-rpc-engine";
|
|
1
2
|
import { projectLogger, createModuleLogger } from "./logging-utils.mjs";
|
|
2
3
|
import { cacheIdentifierForRequest, blockTagForRequest, cacheTypeForMethod, canCache, CacheStrategy } from "./utils/cache.mjs";
|
|
3
4
|
const log = createModuleLogger(projectLogger, 'block-cache');
|
|
@@ -83,9 +84,11 @@ class BlockCacheStrategy {
|
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
86
|
export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
87
|
+
// validate options
|
|
86
88
|
if (!blockTracker) {
|
|
87
89
|
throw new Error('createBlockCacheMiddleware - No PollingBlockTracker specified');
|
|
88
90
|
}
|
|
91
|
+
// create caching strategies
|
|
89
92
|
const blockCache = new BlockCacheStrategy();
|
|
90
93
|
const strategies = {
|
|
91
94
|
[CacheStrategy.Permanent]: blockCache,
|
|
@@ -93,23 +96,28 @@ export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
93
96
|
[CacheStrategy.Fork]: blockCache,
|
|
94
97
|
[CacheStrategy.Never]: undefined,
|
|
95
98
|
};
|
|
96
|
-
return async (
|
|
97
|
-
if
|
|
99
|
+
return createAsyncMiddleware(async (req, res, next) => {
|
|
100
|
+
// allow cach to be skipped if so specified
|
|
101
|
+
if (req.skipCache) {
|
|
98
102
|
return next();
|
|
99
103
|
}
|
|
100
|
-
|
|
104
|
+
// check type and matching strategy
|
|
105
|
+
const type = cacheTypeForMethod(req.method);
|
|
101
106
|
const strategy = strategies[type];
|
|
107
|
+
// If there's no strategy in place, pass it down the chain.
|
|
102
108
|
if (!strategy) {
|
|
103
109
|
return next();
|
|
104
110
|
}
|
|
105
|
-
|
|
111
|
+
// If the strategy can't cache this request, ignore it.
|
|
112
|
+
if (!strategy.canCacheRequest(req)) {
|
|
106
113
|
return next();
|
|
107
114
|
}
|
|
108
|
-
|
|
115
|
+
// get block reference (number or keyword)
|
|
116
|
+
const requestBlockTag = blockTagForRequest(req);
|
|
109
117
|
const blockTag = requestBlockTag && typeof requestBlockTag === 'string'
|
|
110
118
|
? requestBlockTag
|
|
111
119
|
: 'latest';
|
|
112
|
-
log('blockTag = %o, req = %o', blockTag,
|
|
120
|
+
log('blockTag = %o, req = %o', blockTag, req);
|
|
113
121
|
// get exact block number
|
|
114
122
|
let requestedBlockNumber;
|
|
115
123
|
if (blockTag === 'earliest') {
|
|
@@ -117,6 +125,7 @@ export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
117
125
|
requestedBlockNumber = '0x00';
|
|
118
126
|
}
|
|
119
127
|
else if (blockTag === 'latest') {
|
|
128
|
+
// fetch latest block number
|
|
120
129
|
log('Fetching latest block number to determine cache key');
|
|
121
130
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
122
131
|
// clear all cache before latest block
|
|
@@ -125,25 +134,28 @@ export function createBlockCacheMiddleware({ blockTracker, } = {}) {
|
|
|
125
134
|
requestedBlockNumber = latestBlockNumber;
|
|
126
135
|
}
|
|
127
136
|
else {
|
|
128
|
-
//
|
|
137
|
+
// We have a hex number
|
|
129
138
|
requestedBlockNumber = blockTag;
|
|
130
139
|
}
|
|
131
140
|
// end on a hit, continue on a miss
|
|
132
|
-
const cacheResult = await strategy.get(
|
|
141
|
+
const cacheResult = await strategy.get(req, requestedBlockNumber);
|
|
133
142
|
if (cacheResult === undefined) {
|
|
134
143
|
// cache miss
|
|
135
144
|
// wait for other middleware to handle request
|
|
136
145
|
log('No cache stored under block number %o, carrying request forward', requestedBlockNumber);
|
|
137
|
-
|
|
146
|
+
await next();
|
|
138
147
|
// add result to cache
|
|
139
148
|
// it's safe to cast res.result as Block, due to runtime type checks
|
|
140
149
|
// performed when strategy.set is called
|
|
141
|
-
log('Populating cache with',
|
|
142
|
-
await strategy.set(
|
|
143
|
-
return result;
|
|
150
|
+
log('Populating cache with', res);
|
|
151
|
+
await strategy.set(req, requestedBlockNumber, res.result);
|
|
144
152
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
153
|
+
else {
|
|
154
|
+
// fill in result from cache
|
|
155
|
+
log('Cache hit, reusing cache result stored under block number %o', requestedBlockNumber);
|
|
156
|
+
res.result = cacheResult;
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
});
|
|
148
160
|
}
|
|
149
161
|
//# sourceMappingURL=block-cache.mjs.map
|
package/dist/block-cache.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-cache.mjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAOpE,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,aAAa,EACd,0BAAsB;AAEvB,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC;AAC7D,0EAA0E;AAC1E,MAAM,WAAW,GAAc,CAAC,SAAS,EAAE,IAAI,EAAE,iBAAiB,CAAC,CAAC;AAMpE,EAAE;AACF,mBAAmB;AACnB,EAAE;AAEF,MAAM,kBAAkB;IAGtB;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;IAClB,CAAC;IAED,aAAa,CAAC,cAAsB;QAClC,MAAM,WAAW,GAAW,MAAM,CAAC,QAAQ,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;QAChE,IAAI,UAAU,GAAe,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACrD,+BAA+B;QAC/B,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,QAAQ,GAAe,EAAE,CAAC;YAChC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,QAAQ,CAAC;YACnC,UAAU,GAAG,QAAQ,CAAC;SACvB;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,GAAG,CACP,OAAuB,EACvB,oBAA4B;QAE5B,qBAAqB;QACrB,MAAM,UAAU,GAAe,IAAI,CAAC,aAAa,CAAC,oBAAoB,CAAC,CAAC;QACxE,gCAAgC;QAChC,MAAM,UAAU,GAAkB,yBAAyB,CAAC,OAAO,E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C,CAAC;YACrC,MAAM,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAe,CAAC,CAAC;YACnE,OAAO,MAAM,CAAC;SACf;QACD,GAAG,CACD,8DAA8D,EAC9D,oBAAoB,CACrB,CAAC;QACF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues: unknown[] = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n private cache: Cache;\n\n constructor() {\n this.cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.cache[num]);\n }\n}\n\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache?: boolean }>\n> {\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return async ({ request, next, context }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const type = cacheTypeForMethod(request.method);\n const strategy = strategies[type];\n if (!strategy) {\n return next();\n }\n\n if (!strategy.canCacheRequest(request)) {\n return next();\n }\n\n const requestBlockTag = blockTagForRequest(request);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, request);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // we have a hex number\n requestedBlockNumber = blockTag;\n }\n\n // end on a hit, continue on a miss\n const cacheResult = await strategy.get(request, requestedBlockNumber);\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n const result = await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', result);\n await strategy.set(request, requestedBlockNumber, result as Block);\n return result;\n }\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n return cacheResult;\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"block-cache.mjs","sourceRoot":"","sources":["../src/block-cache.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,kCAAkC;AAGlE,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AASpE,OAAO,EACL,yBAAyB,EACzB,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,aAAa,EACd,0BAAsB;AAEvB,MAAM,GAAG,GAAG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ockTracker } from '@metamask/eth-block-tracker';\nimport { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type {\n Block,\n BlockCache,\n // eslint-disable-next-line @typescript-eslint/no-shadow\n Cache,\n JsonRpcCacheMiddleware,\n JsonRpcRequestToCache,\n} from './types';\nimport {\n cacheIdentifierForRequest,\n blockTagForRequest,\n cacheTypeForMethod,\n canCache,\n CacheStrategy,\n} from './utils/cache';\n\nconst log = createModuleLogger(projectLogger, 'block-cache');\n// `<nil>` comes from https://github.com/ethereum/go-ethereum/issues/16925\nconst emptyValues = [undefined, null, '\\u003cnil\\u003e'];\n\ntype BlockCacheMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n//\n// Cache Strategies\n//\n\nclass BlockCacheStrategy {\n private cache: Cache;\n\n constructor() {\n this.cache = {};\n }\n\n getBlockCache(blockNumberHex: string): BlockCache {\n const blockNumber: number = Number.parseInt(blockNumberHex, 16);\n let blockCache: BlockCache = this.cache[blockNumber];\n // create new cache if necesary\n if (!blockCache) {\n const newCache: BlockCache = {};\n this.cache[blockNumber] = newCache;\n blockCache = newCache;\n }\n return blockCache;\n }\n\n async get(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n ): Promise<Block | undefined> {\n // lookup block cache\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n // lookup payload in block cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n return identifier ? blockCache[identifier] : undefined;\n }\n\n async set(\n request: JsonRpcRequest,\n requestedBlockNumber: string,\n result: Block,\n ): Promise<void> {\n // check if we can cached this result\n const canCacheResult: boolean = this.canCacheResult(request, result);\n if (!canCacheResult) {\n return;\n }\n\n // set the value in the cache\n const identifier: string | null = cacheIdentifierForRequest(request, true);\n if (!identifier) {\n return;\n }\n const blockCache: BlockCache = this.getBlockCache(requestedBlockNumber);\n blockCache[identifier] = result;\n }\n\n canCacheRequest(request: JsonRpcRequest): boolean {\n // check request method\n if (!canCache(request.method)) {\n return false;\n }\n // check blockTag\n const blockTag = blockTagForRequest(request);\n\n if (blockTag === 'pending') {\n return false;\n }\n // can be cached\n return true;\n }\n\n canCacheResult(request: JsonRpcRequest, result: Block): boolean {\n // never cache empty values (e.g. undefined)\n if (emptyValues.includes(result as any)) {\n return false;\n }\n\n // check if transactions have block reference before caching\n if (\n request.method &&\n ['eth_getTransactionByHash', 'eth_getTransactionReceipt'].includes(\n request.method,\n )\n ) {\n if (\n !result?.blockHash ||\n result.blockHash ===\n '0x0000000000000000000000000000000000000000000000000000000000000000'\n ) {\n return false;\n }\n }\n // otherwise true\n return true;\n }\n\n // removes all block caches with block number lower than `oldBlockHex`\n clearBefore(oldBlockHex: string): void {\n const oldBlockNumber: number = Number.parseInt(oldBlockHex, 16);\n // clear old caches\n Object.keys(this.cache)\n .map(Number)\n .filter((num) => num < oldBlockNumber)\n .forEach((num) => delete this.cache[num]);\n }\n}\n\nexport function createBlockCacheMiddleware({\n blockTracker,\n}: BlockCacheMiddlewareOptions = {}): JsonRpcCacheMiddleware<\n JsonRpcParams,\n Json\n> {\n // validate options\n if (!blockTracker) {\n throw new Error(\n 'createBlockCacheMiddleware - No PollingBlockTracker specified',\n );\n }\n\n // create caching strategies\n const blockCache: BlockCacheStrategy = new BlockCacheStrategy();\n const strategies: Record<CacheStrategy, BlockCacheStrategy | undefined> = {\n [CacheStrategy.Permanent]: blockCache,\n [CacheStrategy.Block]: blockCache,\n [CacheStrategy.Fork]: blockCache,\n [CacheStrategy.Never]: undefined,\n };\n\n return createAsyncMiddleware(\n async (req: JsonRpcRequestToCache<JsonRpcParams>, res, next) => {\n // allow cach to be skipped if so specified\n if (req.skipCache) {\n return next();\n }\n // check type and matching strategy\n const type = cacheTypeForMethod(req.method);\n const strategy = strategies[type];\n // If there's no strategy in place, pass it down the chain.\n if (!strategy) {\n return next();\n }\n\n // If the strategy can't cache this request, ignore it.\n if (!strategy.canCacheRequest(req)) {\n return next();\n }\n\n // get block reference (number or keyword)\n const requestBlockTag = blockTagForRequest(req);\n const blockTag =\n requestBlockTag && typeof requestBlockTag === 'string'\n ? requestBlockTag\n : 'latest';\n\n log('blockTag = %o, req = %o', blockTag, req);\n\n // get exact block number\n let requestedBlockNumber: string;\n if (blockTag === 'earliest') {\n // this just exists for symmetry with \"latest\"\n requestedBlockNumber = '0x00';\n } else if (blockTag === 'latest') {\n // fetch latest block number\n log('Fetching latest block number to determine cache key');\n const latestBlockNumber = await blockTracker.getLatestBlock();\n // clear all cache before latest block\n log(\n 'Clearing values stored under block numbers before %o',\n latestBlockNumber,\n );\n blockCache.clearBefore(latestBlockNumber);\n requestedBlockNumber = latestBlockNumber;\n } else {\n // We have a hex number\n requestedBlockNumber = blockTag;\n }\n // end on a hit, continue on a miss\n const cacheResult: Block | undefined = await strategy.get(\n req,\n requestedBlockNumber,\n );\n if (cacheResult === undefined) {\n // cache miss\n // wait for other middleware to handle request\n log(\n 'No cache stored under block number %o, carrying request forward',\n requestedBlockNumber,\n );\n await next();\n\n // add result to cache\n // it's safe to cast res.result as Block, due to runtime type checks\n // performed when strategy.set is called\n log('Populating cache with', res);\n await strategy.set(req, requestedBlockNumber, res.result as Block);\n } else {\n // fill in result from cache\n log(\n 'Cache hit, reusing cache result stored under block number %o',\n requestedBlockNumber,\n );\n res.result = cacheResult;\n }\n return undefined;\n },\n );\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createBlockRefRewriteMiddleware = void 0;
|
|
4
|
+
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
4
5
|
const cache_1 = require("./utils/cache.cjs");
|
|
5
6
|
/**
|
|
6
7
|
* Creates a middleware that rewrites "latest" block references to the known
|
|
@@ -14,13 +15,13 @@ function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
14
15
|
if (!blockTracker) {
|
|
15
16
|
throw Error('BlockRefRewriteMiddleware - mandatory "blockTracker" option is missing.');
|
|
16
17
|
}
|
|
17
|
-
return async (
|
|
18
|
-
const blockRefIndex = (0, cache_1.blockTagParamIndex)(
|
|
18
|
+
return (0, json_rpc_engine_1.createAsyncMiddleware)(async (req, _res, next) => {
|
|
19
|
+
const blockRefIndex = (0, cache_1.blockTagParamIndex)(req.method);
|
|
19
20
|
if (blockRefIndex === undefined) {
|
|
20
21
|
return next();
|
|
21
22
|
}
|
|
22
|
-
const blockRef = Array.isArray(
|
|
23
|
-
?
|
|
23
|
+
const blockRef = Array.isArray(req.params) && req.params[blockRefIndex]
|
|
24
|
+
? req.params[blockRefIndex]
|
|
24
25
|
: // omitted blockRef implies "latest"
|
|
25
26
|
'latest';
|
|
26
27
|
if (blockRef !== 'latest') {
|
|
@@ -28,16 +29,11 @@ function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
28
29
|
}
|
|
29
30
|
// rewrite blockRef to block-tracker's block number
|
|
30
31
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
31
|
-
if (Array.isArray(
|
|
32
|
-
|
|
33
|
-
params[blockRefIndex] = latestBlockNumber;
|
|
34
|
-
return next({
|
|
35
|
-
...request,
|
|
36
|
-
params,
|
|
37
|
-
});
|
|
32
|
+
if (Array.isArray(req.params)) {
|
|
33
|
+
req.params[blockRefIndex] = latestBlockNumber;
|
|
38
34
|
}
|
|
39
35
|
return next();
|
|
40
|
-
};
|
|
36
|
+
});
|
|
41
37
|
}
|
|
42
38
|
exports.createBlockRefRewriteMiddleware = createBlockRefRewriteMiddleware;
|
|
43
39
|
//# sourceMappingURL=block-ref-rewrite.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.cjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.cjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":";;;AAEA,+DAAkE;AAGlE,6CAAmD;AAMnD;;;;;;;GAOG;AACH,SAAgB,+BAA+B,CAAC,EAC9C,YAAY,MACwB,EAAE;IAItC,IAAI,CAAC,YAAY,EAAE;QACjB,MAAM,KAAK,CACT,yEAAyE,CAC1E,CAAC;KACH;IAED,OAAO,IAAA,uCAAqB,EAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;QACrD,MAAM,aAAa,GAAuB,IAAA,0BAAkB,EAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACzE,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,OAAO,IAAI,EAAE,CAAC;SACf;QAED,MAAM,QAAQ,GACZ,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC;YACpD,CAAC,CAAE,GAAG,CAAC,MAAM,CAAC,aAAa,CAAY;YACvC,CAAC,CAAC,oCAAoC;gBACpC,QAAQ,CAAC;QAEf,IAAI,QAAQ,KAAK,QAAQ,EAAE;YACzB,OAAO,IAAI,EAAE,CAAC;SACf;QAED,mDAAmD;QACnD,MAAM,iBAAiB,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;QAC9D,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;YAC7B,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,iBAAiB,CAAC;SAC/C;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC;AAnCD,0EAmCC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport { blockTagParamIndex } from './utils/cache';\n\ntype BlockRefRewriteMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n/**\n * Creates a middleware that rewrites \"latest\" block references to the known\n * latest block number from a block tracker.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createBlockRefRewriteMiddleware({\n blockTracker,\n}: BlockRefRewriteMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcParams,\n Json\n> {\n if (!blockTracker) {\n throw Error(\n 'BlockRefRewriteMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return createAsyncMiddleware(async (req, _res, next) => {\n const blockRefIndex: number | undefined = blockTagParamIndex(req.method);\n if (blockRefIndex === undefined) {\n return next();\n }\n\n const blockRef: string | undefined =\n Array.isArray(req.params) && req.params[blockRefIndex]\n ? (req.params[blockRefIndex] as string)\n : // omitted blockRef implies \"latest\"\n 'latest';\n\n if (blockRef !== 'latest') {\n return next();\n }\n\n // rewrite blockRef to block-tracker's block number\n const latestBlockNumber = await blockTracker.getLatestBlock();\n if (Array.isArray(req.params)) {\n req.params[blockRefIndex] = latestBlockNumber;\n }\n return next();\n });\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine
|
|
3
|
-
import type { Json,
|
|
2
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
3
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
4
4
|
type BlockRefRewriteMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
@@ -12,6 +12,6 @@ type BlockRefRewriteMiddlewareOptions = {
|
|
|
12
12
|
* @param options.blockTracker - The block tracker to use.
|
|
13
13
|
* @returns The middleware.
|
|
14
14
|
*/
|
|
15
|
-
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<
|
|
15
|
+
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
16
16
|
export {};
|
|
17
17
|
//# sourceMappingURL=block-ref-rewrite.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.d.cts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.d.cts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AAEnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAI3D,KAAK,gCAAgC,GAAG;IACtC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAAC,EAC9C,YAAY,GACb,GAAE,gCAAqC,GAAG,iBAAiB,CAC1D,aAAa,EACb,IAAI,CACL,CA8BA"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PollingBlockTracker } from "@metamask/eth-block-tracker";
|
|
2
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine
|
|
3
|
-
import type { Json,
|
|
2
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
3
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
4
4
|
type BlockRefRewriteMiddlewareOptions = {
|
|
5
5
|
blockTracker?: PollingBlockTracker;
|
|
6
6
|
};
|
|
@@ -12,6 +12,6 @@ type BlockRefRewriteMiddlewareOptions = {
|
|
|
12
12
|
* @param options.blockTracker - The block tracker to use.
|
|
13
13
|
* @returns The middleware.
|
|
14
14
|
*/
|
|
15
|
-
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<
|
|
15
|
+
export declare function createBlockRefRewriteMiddleware({ blockTracker, }?: BlockRefRewriteMiddlewareOptions): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
16
16
|
export {};
|
|
17
17
|
//# sourceMappingURL=block-ref-rewrite.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.d.mts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.d.mts","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAoC;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AAEnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAI3D,KAAK,gCAAgC,GAAG;IACtC,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAAC,EAC9C,YAAY,GACb,GAAE,gCAAqC,GAAG,iBAAiB,CAC1D,aAAa,EACb,IAAI,CACL,CA8BA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createAsyncMiddleware } from "@metamask/json-rpc-engine";
|
|
1
2
|
import { blockTagParamIndex } from "./utils/cache.mjs";
|
|
2
3
|
/**
|
|
3
4
|
* Creates a middleware that rewrites "latest" block references to the known
|
|
@@ -11,13 +12,13 @@ export function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
11
12
|
if (!blockTracker) {
|
|
12
13
|
throw Error('BlockRefRewriteMiddleware - mandatory "blockTracker" option is missing.');
|
|
13
14
|
}
|
|
14
|
-
return async (
|
|
15
|
-
const blockRefIndex = blockTagParamIndex(
|
|
15
|
+
return createAsyncMiddleware(async (req, _res, next) => {
|
|
16
|
+
const blockRefIndex = blockTagParamIndex(req.method);
|
|
16
17
|
if (blockRefIndex === undefined) {
|
|
17
18
|
return next();
|
|
18
19
|
}
|
|
19
|
-
const blockRef = Array.isArray(
|
|
20
|
-
?
|
|
20
|
+
const blockRef = Array.isArray(req.params) && req.params[blockRefIndex]
|
|
21
|
+
? req.params[blockRefIndex]
|
|
21
22
|
: // omitted blockRef implies "latest"
|
|
22
23
|
'latest';
|
|
23
24
|
if (blockRef !== 'latest') {
|
|
@@ -25,15 +26,10 @@ export function createBlockRefRewriteMiddleware({ blockTracker, } = {}) {
|
|
|
25
26
|
}
|
|
26
27
|
// rewrite blockRef to block-tracker's block number
|
|
27
28
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
28
|
-
if (Array.isArray(
|
|
29
|
-
|
|
30
|
-
params[blockRefIndex] = latestBlockNumber;
|
|
31
|
-
return next({
|
|
32
|
-
...request,
|
|
33
|
-
params,
|
|
34
|
-
});
|
|
29
|
+
if (Array.isArray(req.params)) {
|
|
30
|
+
req.params[blockRefIndex] = latestBlockNumber;
|
|
35
31
|
}
|
|
36
32
|
return next();
|
|
37
|
-
};
|
|
33
|
+
});
|
|
38
34
|
}
|
|
39
35
|
//# sourceMappingURL=block-ref-rewrite.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block-ref-rewrite.mjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"block-ref-rewrite.mjs","sourceRoot":"","sources":["../src/block-ref-rewrite.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,kCAAkC;AAGlE,OAAO,EAAE,kBAAkB,EAAE,0BAAsB;AAMnD;;;;;;;GAOG;AACH,MAAM,UAAU,+BAA+B,CAAC,EAC9C,YAAY,MACwB,EAAE;IAItC,IAAI,CAAC,YAAY,EAAE;QACjB,MAAM,KAAK,CACT,yEAAyE,CAC1E,CAAC;KACH;IAED,OAAO,qBAAqB,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;QACrD,MAAM,aAAa,GAAuB,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACzE,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,OAAO,IAAI,EAAE,CAAC;SACf;QAED,MAAM,QAAQ,GACZ,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC;YACpD,CAAC,CAAE,GAAG,CAAC,MAAM,CAAC,aAAa,CAAY;YACvC,CAAC,CAAC,oCAAoC;gBACpC,QAAQ,CAAC;QAEf,IAAI,QAAQ,KAAK,QAAQ,EAAE;YACzB,OAAO,IAAI,EAAE,CAAC;SACf;QAED,mDAAmD;QACnD,MAAM,iBAAiB,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;QAC9D,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;YAC7B,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,iBAAiB,CAAC;SAC/C;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type { Json, JsonRpcParams } from '@metamask/utils';\n\nimport { blockTagParamIndex } from './utils/cache';\n\ntype BlockRefRewriteMiddlewareOptions = {\n blockTracker?: PollingBlockTracker;\n};\n\n/**\n * Creates a middleware that rewrites \"latest\" block references to the known\n * latest block number from a block tracker.\n *\n * @param options - The options for the middleware.\n * @param options.blockTracker - The block tracker to use.\n * @returns The middleware.\n */\nexport function createBlockRefRewriteMiddleware({\n blockTracker,\n}: BlockRefRewriteMiddlewareOptions = {}): JsonRpcMiddleware<\n JsonRpcParams,\n Json\n> {\n if (!blockTracker) {\n throw Error(\n 'BlockRefRewriteMiddleware - mandatory \"blockTracker\" option is missing.',\n );\n }\n\n return createAsyncMiddleware(async (req, _res, next) => {\n const blockRefIndex: number | undefined = blockTagParamIndex(req.method);\n if (blockRefIndex === undefined) {\n return next();\n }\n\n const blockRef: string | undefined =\n Array.isArray(req.params) && req.params[blockRefIndex]\n ? (req.params[blockRefIndex] as string)\n : // omitted blockRef implies \"latest\"\n 'latest';\n\n if (blockRef !== 'latest') {\n return next();\n }\n\n // rewrite blockRef to block-tracker's block number\n const latestBlockNumber = await blockTracker.getLatestBlock();\n if (Array.isArray(req.params)) {\n req.params[blockRefIndex] = latestBlockNumber;\n }\n return next();\n });\n}\n"]}
|
package/dist/block-ref.cjs
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createBlockRefMiddleware = void 0;
|
|
4
|
-
const
|
|
4
|
+
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
5
|
+
const full_1 = require("klona/full");
|
|
5
6
|
const logging_utils_1 = require("./logging-utils.cjs");
|
|
6
7
|
const cache_1 = require("./utils/cache.cjs");
|
|
7
8
|
const log = (0, logging_utils_1.createModuleLogger)(logging_utils_1.projectLogger, 'block-ref');
|
|
8
|
-
/**
|
|
9
|
-
* Creates a middleware that rewrites "latest" block references to the known
|
|
10
|
-
* latest block number from a block tracker.
|
|
11
|
-
*
|
|
12
|
-
* @param options - The options for the middleware.
|
|
13
|
-
* @param options.provider - The provider to use.
|
|
14
|
-
* @param options.blockTracker - The block tracker to use.
|
|
15
|
-
* @returns The middleware.
|
|
16
|
-
*/
|
|
17
9
|
function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
18
10
|
if (!provider) {
|
|
19
11
|
throw Error('BlockRefMiddleware - mandatory "provider" option is missing.');
|
|
@@ -21,14 +13,14 @@ function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
|
21
13
|
if (!blockTracker) {
|
|
22
14
|
throw Error('BlockRefMiddleware - mandatory "blockTracker" option is missing.');
|
|
23
15
|
}
|
|
24
|
-
return async (
|
|
25
|
-
const blockRefIndex = (0, cache_1.blockTagParamIndex)(
|
|
16
|
+
return (0, json_rpc_engine_1.createAsyncMiddleware)(async (req, res, next) => {
|
|
17
|
+
const blockRefIndex = (0, cache_1.blockTagParamIndex)(req.method);
|
|
26
18
|
// skip if method does not include blockRef
|
|
27
19
|
if (blockRefIndex === undefined) {
|
|
28
20
|
return next();
|
|
29
21
|
}
|
|
30
|
-
const blockRef = Array.isArray(
|
|
31
|
-
? (
|
|
22
|
+
const blockRef = Array.isArray(req.params)
|
|
23
|
+
? (req.params[blockRefIndex] ?? 'latest')
|
|
32
24
|
: 'latest';
|
|
33
25
|
// skip if not "latest"
|
|
34
26
|
if (blockRef !== 'latest') {
|
|
@@ -39,15 +31,16 @@ function createBlockRefMiddleware({ provider, blockTracker, } = {}) {
|
|
|
39
31
|
const latestBlockNumber = await blockTracker.getLatestBlock();
|
|
40
32
|
log(`blockRef is "latest", setting param ${blockRefIndex} to latest block ${latestBlockNumber}`);
|
|
41
33
|
// create child request with specific block-ref
|
|
42
|
-
const childRequest = (0,
|
|
34
|
+
const childRequest = (0, full_1.klona)(req);
|
|
43
35
|
if (Array.isArray(childRequest.params)) {
|
|
44
36
|
childRequest.params[blockRefIndex] = latestBlockNumber;
|
|
45
37
|
}
|
|
46
38
|
// perform child request
|
|
47
39
|
log('Performing another request %o', childRequest);
|
|
48
40
|
// copy child result onto original response
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
res.result = await provider.request(childRequest);
|
|
42
|
+
return undefined;
|
|
43
|
+
});
|
|
51
44
|
}
|
|
52
45
|
exports.createBlockRefMiddleware = createBlockRefMiddleware;
|
|
53
46
|
//# sourceMappingURL=block-ref.cjs.map
|