Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/set block number cache #22

Merged
merged 25 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5119703
return blockNumber cache when metamask requests blockNumber
Tsuyoshi-Ishikawa Mar 20, 2023
eb3b017
modify typecheck.service
Tsuyoshi-Ishikawa Mar 20, 2023
785bb6e
set isUseBlockNumberCache to config.service
Tsuyoshi-Ishikawa Mar 23, 2023
2012170
resolve conflict
Tsuyoshi-Ishikawa Mar 27, 2023
9d0f3ca
set blockNumberCache to redis
Tsuyoshi-Ishikawa Mar 27, 2023
b9e774f
remove cache-manager
Tsuyoshi-Ishikawa Mar 27, 2023
a5e69db
modify proxy.service test
Tsuyoshi-Ishikawa Mar 27, 2023
6d42b64
modify transaction.service test
Tsuyoshi-Ishikawa Mar 27, 2023
fd63c31
mock console.error at test
Tsuyoshi-Ishikawa Mar 28, 2023
334ee60
set blockNumberCache using ip_address and use-agent
Tsuyoshi-Ishikawa Mar 28, 2023
1ad1c4d
update blockNumberCache each time tx is executed
Tsuyoshi-Ishikawa Mar 29, 2023
5c1ad01
enable to set blockNumberCacheExpire from .env
Tsuyoshi-Ishikawa Mar 29, 2023
9d6fc97
set configService_data at constructor at proxy.service
Tsuyoshi-Ishikawa Mar 29, 2023
12449e0
add typeCheck to typeCheck.service
Tsuyoshi-Ishikawa Mar 29, 2023
bf2ab0d
use setex with redis
Tsuyoshi-Ishikawa Mar 30, 2023
41bc4b7
modify typeCheck of sec-ch-ua
Tsuyoshi-Ishikawa Mar 30, 2023
f137361
modify clientIp at datastore.service
Tsuyoshi-Ishikawa Mar 30, 2023
6506599
use nestjs-real-ip
Tsuyoshi-Ishikawa Mar 30, 2023
79d3142
modify proxy.controller test
Tsuyoshi-Ishikawa Mar 31, 2023
d95ab05
mock construct_injected_services at e2e_test
Tsuyoshi-Ishikawa Mar 31, 2023
9815991
modify proxy.service test
Tsuyoshi-Ishikawa Mar 31, 2023
767a109
modify transaction.service test
Tsuyoshi-Ishikawa Mar 31, 2023
7332825
update README
Tsuyoshi-Ishikawa Mar 31, 2023
9a50fae
remove useMocker from test
Tsuyoshi-Ishikawa Mar 31, 2023
096fc0b
enable to set blockNumberCache using Microsoft-Edge
Tsuyoshi-Ishikawa Apr 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export const getTxAllowList = (): Array<TransactionAllow> => {
| lte | txValue <= condition is allowed |

#### Transaction access rate limit(Option)
If you set transaction access rate limit, follow [Transaction access rate limit](https://github.com/oasysgames/verse-proxy/blob/master/docs/RateLimit.md)
If you set transaction access rate limit, follow [Transaction access rate limit](/docs/RateLimit.md)

### Set contract deployer
You can control deployer of a verse at `src/config/transactionAllowList.ts`.
Expand Down Expand Up @@ -300,4 +300,9 @@ VERSE_READ_NODE_URL=[YOUR_VERSE_REPLICA_URL]
### Check Master-Verse-Node
To check the behavior of requests to the Master-Verse-Node, an endpoint named `/master` is provided.

All transactions sent to `/master` are sent to the Master-Verse-Node.
All transactions sent to `/master` are sent to the Master-Verse-Node.

## Reduce Metamask Access
By returning the cache of blockNumber to the metamask, the number of accesses to the metamask can be reduced.
For more detail, check the following doc.
[Reduce Metamask Access](/docs/MetamaskAccess.md)
47 changes: 47 additions & 0 deletions docs/MetamaskAccess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Reduce Metamask Access
By returning the cache of block number to the metamask, the number of accesses to the metamask can be reduced.

This approach is only applicable to browsers built on Chromium (chrome, brave, and Microsoft Edge based on Chromium).

## Context
When the metamask is open in the browser, the metamask checks the block number with `eth_blockNumber` once every 5~6 seconds.

And metamask does the following when block number is updated.
- Execute `eth_getBalance` for all accounts registered in the metamask.
- Check the balance of all ERC20 accounts registered by the current account(ERC20.balanceOf).

Oasys Verse updates the block number each time a transaction is executed.
Therefore, each time a transaction is executed, there is access to update the balance of the metamask as explained above.

## How to Reduce
When there is a request for block number from metamask, the following actions should be taken
- If a block number cache exists, it is returned.
- If the cache of block number does not exist, request `eth_blockNumber` from verse to get the latest block number.

It will prevent a metamask balance update request from occurring each time the transaction updates the block number.

We create block number cache for each user using IP address and user-agent.

## Update block number Cache
The block number cache will be updated in the following cases.

- When `eth_blockNumber` is requested by metamask after the block numberCache has been deleted by the expiration
- The user block number cache is updated when a user executes a transaction by `eth_sendRawTransaction`.

In other words, if there is a change in your ERC20 balance due to a transaction you executed, you can see the change in your token balance immediately after the transaction is executed.

## Concern
Returning a block number cache to the metamask raises the following concerns.

While a block numberCache is being returned, it is impossible to confirm that another account's transaction has changed your account's token balance.

In other words, if the expiration of block numberCache is too long, you can only confirm that your account's token balance has changed once the cache_expire expires.

In addition, when a view function of a contract connected to Metamask (such as ERC20) is executed, the result is cached in Metamask.
If periodic block number checks do not progress the block number, the result of the cache is returned. Therefore, you cannot check the result of the latest view function.

## Setup
It can be enabled by setting block numberCache's expiration from the environment variable.
```bash
BLOCK_NUMBER_CACHE_EXPIRE_SEC=15 # 15 seconds
```
101 changes: 101 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"body-parser": "^1.20.2",
"ethers": "^5.7.1",
"ioredis": "^5.3.1",
"nestjs-real-ip": "^2.2.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
Expand Down
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { CacheModule, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { HttpModule } from '@nestjs/axios';
import { ProxyController } from './controllers';
Expand All @@ -19,6 +19,7 @@ import configuration from './config/configuration';
load: [configuration],
}),
HttpModule,
CacheModule.register(),
],
controllers: [ProxyController],
providers: [
Expand Down
3 changes: 3 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export default () => ({
process.env.VERSE_URL ||
'http://localhost:8545',
verseReadNodeUrl: process.env.VERSE_READ_NODE_URL,
blockNumberCacheExpire: process.env.BLOCK_NUMBER_CACHE_EXPIRE_SEC
? parseInt(process.env.BLOCK_NUMBER_CACHE_EXPIRE_SEC, 10)
: undefined,
datastore: process.env.DATASTORE ?? '',
allowedMethods: [
/^net_version$/,
Expand Down
Loading