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/rate limit #16

Merged
merged 42 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
171dd4e
add DatabaseModule to use redis
Tsuyoshi-Ishikawa Feb 23, 2023
86e209b
add rateLimit check
Tsuyoshi-Ishikawa Feb 26, 2023
a53152e
modify checkAddressList at RateLimit.service
Tsuyoshi-Ishikawa Feb 27, 2023
5be609b
add how to limit tx access
Tsuyoshi-Ishikawa Feb 27, 2023
e2aaa82
modify address_check_logic
Tsuyoshi-Ishikawa Feb 27, 2023
b51b6a6
add rateLimit setting to transactionAllow setting
Tsuyoshi-Ishikawa Feb 28, 2023
56e44f1
randomly remove old_redis_data
Tsuyoshi-Ishikawa Feb 28, 2023
20f9c30
modify getRedisKey
Tsuyoshi-Ishikawa Feb 28, 2023
9abcfc2
add checkRateLimit to checkAllowedTx
Tsuyoshi-Ishikawa Feb 28, 2023
755233c
modify redis setup
Tsuyoshi-Ishikawa Feb 28, 2023
7b98de3
update RateLimit docs
Tsuyoshi-Ishikawa Feb 28, 2023
3f011fa
modify address check logic
Tsuyoshi-Ishikawa Mar 1, 2023
791cc7f
remove error happening when process.env.REDIS_URI is blank
Tsuyoshi-Ishikawa Mar 1, 2023
a1a4ff1
modify checkRateLimit
Tsuyoshi-Ishikawa Mar 1, 2023
783eddf
set isSetRateLimit to config
Tsuyoshi-Ishikawa Mar 1, 2023
bf34478
change checkAllowedTx to getMatchedTxAllowRule
Tsuyoshi-Ishikawa Mar 1, 2023
7a665c7
jsonrpcCheck.service to typeCheck.service
Tsuyoshi-Ishikawa Mar 2, 2023
3691c62
comment out e2e test
Tsuyoshi-Ishikawa Mar 2, 2023
5e7b854
store Buffer data as tx history
Tsuyoshi-Ishikawa Mar 2, 2023
e87d514
modify timestamp range at rateLimit
Tsuyoshi-Ishikawa Mar 2, 2023
375c87b
convert address to lowcase when checking address
Tsuyoshi-Ishikawa Mar 2, 2023
97f4354
add checkAddressList to allowCheck.service
Tsuyoshi-Ishikawa Mar 2, 2023
5aa917e
refactor rateLimit logic
Tsuyoshi-Ishikawa Mar 2, 2023
a801986
remove redis.service and create datastore.service
Tsuyoshi-Ishikawa Mar 2, 2023
10959eb
use redis pipeline when storing tx history
Tsuyoshi-Ishikawa Mar 3, 2023
8cc4e30
modify setTransactionHistory
Tsuyoshi-Ishikawa Mar 3, 2023
00041ae
modify typecheck test
Tsuyoshi-Ishikawa Mar 3, 2023
8bc01f8
modify allowCheck.service test
Tsuyoshi-Ishikawa Mar 3, 2023
b2cc83f
modify proxy.controller test
Tsuyoshi-Ishikawa Mar 3, 2023
6290853
modify proxy.service test
Tsuyoshi-Ishikawa Mar 4, 2023
a1ff16b
modify transaction.service test
Tsuyoshi-Ishikawa Mar 4, 2023
b231e04
modify e2e test module
Tsuyoshi-Ishikawa Mar 4, 2023
53446f1
modify e2e test to pass
Tsuyoshi-Ishikawa Mar 5, 2023
dbb9b8b
disable to open handle keeps Jest from exiting
Tsuyoshi-Ishikawa Mar 5, 2023
7e9c6bc
mock transactionAllowList at e2e_test
Tsuyoshi-Ishikawa Mar 5, 2023
23f566b
modify httpModule mock at e2e_test
Tsuyoshi-Ishikawa Mar 5, 2023
7743bfb
create function to mock config.service get
Tsuyoshi-Ishikawa Mar 5, 2023
4aff8e1
refactor describe at e2e_test
Tsuyoshi-Ishikawa Mar 5, 2023
c653a7b
modify mock at e2e_test
Tsuyoshi-Ishikawa Mar 6, 2023
df0f760
set txAllow check tests to e2e_test
Tsuyoshi-Ishikawa Mar 6, 2023
ab43fd3
create rateLimit.service test
Tsuyoshi-Ishikawa Mar 6, 2023
e34fe83
modify README
Tsuyoshi-Ishikawa Mar 6, 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
147 changes: 82 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,43 +104,74 @@ You can control the from and to of a transaction.

```typescript
// elements contained in the array are allowed to be transacted.
export const getTxAllowList = (): Array<TransactionAllow> => {
return [
{
fromList: ['0xaf395754eB6F542742784cE7702940C60465A46a'],
toList: ['0xaf395754eB6F542742784cE7702940C60465A46a'],
},
{
fromList: ['0xaf395754eB6F542742784cE7702940C60465A46c'],
toList: ['0xaf395754eB6F542742784cE7702940C60465A46c'],
},
];
};
const txAllowList: Array<TransactionAllow> = [
{
fromList: ['0xaf395754eB6F542742784cE7702940C60465A46a'],
toList: ['0xaf395754eB6F542742784cE7702940C60465A46a'],
},
{
fromList: ['0xaf395754eB6F542742784cE7702940C60465A46c'],
toList: ['0xaf395754eB6F542742784cE7702940C60465A46c'],
},
];
```

```typescript
// '*' is wildcard.
export const getTxAllowList = (): Array<TransactionAllow> => {
return [
{
fromList: ['*'],
toList: ['*'],
},
];
}
const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['*'],
},
];
```

```typescript
// ! is denial.

// 0xaf395754eB6F542742784cE7702940C60465A46a are not allowed to be transacted.
export const getTxAllowList = (): Array<TransactionAllow> => {
return [
{
fromList: ['!0xaf395754eB6F542742784cE7702940C60465A46a'],
toList: ['!0xaf395754eB6F542742784cE7702940C60465A46a'],
},
];
};
// But any address other than 0xaf395754eB6F542742784cE7702940C60465A46a are allowed to be transacted.
const txAllowList: Array<TransactionAllow> = [
{
fromList: ['!0xaf395754eB6F542742784cE7702940C60465A46a'],
toList: ['*'],
},
];

// Everyone are not allowed to transact to 0xaf395754eB6F542742784cE7702940C60465A46a.
// everyone are allowed to transact to any address other than 0xaf395754eB6F542742784cE7702940C60465A46a.
const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['!0xaf395754eB6F542742784cE7702940C60465A46a'],
},
];

// Multiple Setting is enabled.
// Everyone are not allowed to transact to 0xaf395754eB6F542742784cE7702940C60465A46a and 0xaf395754eB6F542742784cE7702940C60465A46c.
// everyone are allowed to transact to any address other than 0xaf395754eB6F542742784cE7702940C60465A46a and 0xaf395754eB6F542742784cE7702940C60465A46c.
const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: [
'!0xaf395754eB6F542742784cE7702940C60465A46a',
'!0xaf395754eB6F542742784cE7702940C60465A46c'
],
},
];
```

```typescript
// You can not set setting with address and address_denial.
const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: [
'0xaf395754eB6F542742784cE7702940C60465A46a',
'!0xaf395754eB6F542742784cE7702940C60465A46c'
],
},
];
```

If you want to allow transacting factory and bridge contracts, please set those contract addresses to `to`.
Expand All @@ -153,35 +184,31 @@ L2ERC721Bridge: '0x6200000000000000000000000000000000000001',
```

```typescript
export const getTxAllowList = (): Array<TransactionAllow> => {
return [
{
fromList: [<FROM_YOU_WANT_TO_SET>],
toList: [
'0x4200000000000000000000000000000000000010',
'0x4200000000000000000000000000000000000012',
'0x6200000000000000000000000000000000000001',
],
},
...
];
};
const txAllowList: Array<TransactionAllow> = [
{
fromList: [<FROM_YOU_WANT_TO_SET>],
toList: [
'0x4200000000000000000000000000000000000010',
'0x4200000000000000000000000000000000000012',
'0x6200000000000000000000000000000000000001',
],
},
...
];
```

#### Value
You can control the token value of a transaction.

```typescript
// Only transactions with more than 1000000000000000000unit values are allowed.
export const getTxAllowList = (): Array<TransactionAllow> => {
return [
{
fromList: ['*'],
toList: ['*'],
value: { gt: '1000000000000000000' },
}
];
};
const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['*'],
value: { gt: '1000000000000000000' },
}
];
```

| value's key | Comparison Operation |
Expand All @@ -198,25 +225,15 @@ You can control deployer of a verse.

```typescript
// Only 0xaf395754eB6F542742784cE7702940C60465A46a can deploy
export const getDeployAllowList = (): Array<string> => {
return ['0xaf395754eB6F542742784cE7702940C60465A46a'];
};
const deployAllowList: Array<string> = ['0xaf395754eB6F542742784cE7702940C60465A46a'];

// Everyone can deploy
export const getDeployAllowList = (): Array<string> => {
return ['*'];
};

// 0xaf395754eB6F542742784cE7702940C60465A46c cannot deploy,
// 0xaf395754eB6F542742784cE7702940C60465A46a can deploy
export const getDeployAllowList = (): Array<string> => {
return [
'!0xaf395754eB6F542742784cE7702940C60465A46c',
'0xaf395754eB6F542742784cE7702940C60465A46a',
];
};
const deployAllowList: Array<string> = ['*'];
```

### Transaction access rate limit(Option)
If you set transaction access rate limit, follow [Transaction access rate limit](/docs/RateLimit.md)

### Set allowed header
You can set whether you inherit proxy request's host header on verse request at `src/config/configuration.ts`.
```typescript
Expand Down
169 changes: 169 additions & 0 deletions docs/RateLimit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Transaction access rate limit

## Datastore setting
For setting datastore to store transaction history, you have to set datastore environment variables.

```bash
# In case of redis
RATE_LIMIT_PLUGIN=redis
REDIS_URI=<REDIS_URI> # e.g. redis://localhost:6379/0
```

And please set expire as the period of time to store the transaction.

## Rate limit setting
Using `txAllowList` at `src/config/transactionAllowList.ts`, you can set transaction rate limit.
Please define rateLimit variable and set `txAllowList`.

```typescript
const rateLimitA = {
name: 'wildcard',
interval: 86400,
limit: 1000,
};

const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['*'],
rateLimit: rateLimitA,
},
];
```

| RateLimit key | Description | Required |
| ---- | ---- | ---- |
| name | RateLimit setting name. Must be unique. | O |
| perFrom | Whether the setting is restricted for each from set in fromList or not | |
| perTo | Whether the setting is restricted for each to set in the toList or not | |
| perMethod | Whether the setting is restricted for each contract method or not | |
| interval | Rate limit interval(seconds) | O |
| limit | Number of tx's allowed in the interval | O |

rateLimit can be shared by another txAllowList setting.

```typescript
const rateLimitA = {
name: 'wildcard',
perFrom: true,
perTo: true,
interval: 86400,
limit: 1,
};

const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['0x9809d9d94b0b3380db38b1e1a06047a2964e0041'],
rateLimit: rateLimitA,
},
{
fromList: ['*'],
toList: ['0x40bde52e6b80ae11f34c58c14e1e7fe1f9c834c4'],
rateLimit: rateLimitA,
},
];
```

### Example(limit settings per user)
Each user can perform a transaction to `0x9809d9d94b0b3380db38b1e1a06047a2964e0041` once every 60 seconds.

```typescript
const rateLimitA = {
name: 'wildcard',
perFrom: true,
interval: 60,
limit: 1,
};

const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['0x9809d9d94b0b3380db38b1e1a06047a2964e0041'],
rateLimit: rateLimitA,
},
]
```

### Example(limit settings per all users)
Transaction to `0x9809d9d94b0b3380db38b1e1a06047a2964e0041` can only be executed once every 60 seconds

```typescript
const rateLimitA = {
name: 'wildcard',
interval: 60,
limit: 1,
};

const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['0x9809d9d94b0b3380db38b1e1a06047a2964e0041'],
rateLimit: rateLimitA,
},
];
```

### Example(limit settings per contract)
Transaction to `0x9809d9d94b0b3380db38b1e1a06047a2964e0041` or `0x40bde52e6b80ae11f34c58c14e1e7fe1f9c834c4` can only be executed once every 60 seconds respectively.
```typescript
const rateLimitA = {
name: 'wildcard',
perTo: true,
interval: 60,
limit: 1,
};

const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['0x9809d9d94b0b3380db38b1e1a06047a2964e0041'],
rateLimit: rateLimitA,
},
{
fromList: ['*'],
toList: ['0x40bde52e6b80ae11f34c58c14e1e7fe1f9c834c4'],
rateLimit: rateLimitA,
},
];
```

### Example(limit settings per all contracts)
Both together, Transaction to `0x9809d9d94b0b3380db38b1e1a06047a2964e0041` or `0x40bde52e6b80ae11f34c58c14e1e7fe1f9c834c4` can only be executed once every 60 seconds.

```typescript
const rateLimitA = {
name: 'wildcard',
interval: 60,
limit: 1,
};

const txAllowList: Array<TransactionAllow> = [
{
fromList: ['*'],
toList: ['0x9809d9d94b0b3380db38b1e1a06047a2964e0041'],
rateLimit: rateLimitA,
},
{
fromList: ['*'],
toList: ['0x40bde52e6b80ae11f34c58c14e1e7fe1f9c834c4'],
rateLimit: rateLimitA,
},
];
```

## Set Addresses unlimited tx rate
Addresses set in `getDeployAllowList` and `getUnlimitedTxRateAddresses` are not rate-limited for transactions.

```typescript
const deployAllowList: Array<string> = ['0xaf395754eB6F542742784cE7702940C60465A46c'];

const unlimitedTxRateAddresses: Array<string> = ['0xaf395754eB6F542742784cE7702940C60465A46a'];
```

You can set wildcard
```typescript
const deployAllowList: Array<string> = ['*'];

const unlimitedTxRateAddresses: Array<string> = ['*'];
```
Loading