@txfence/redis 0.1.0
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 +12 -0
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +138 -0
- package/package.json +31 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# @txfence/redis
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Initial public release — policy engine, intent execution, formal verification, adversarial stress testing, cryptographic provenance chains, temporal rules, and MEV protection across EVM, Solana, and Cosmos
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies
|
|
12
|
+
- @txfence/core@0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aditya Chauhan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @txfence/redis
|
|
2
|
+
|
|
3
|
+
Redis-backed `CapLockProvider` for [txfence](https://github.com/AdityaChauhanX07/txfence). Drop-in replacement for `createMemoryCapLockProvider` when multiple agents need to coordinate against the same spending caps across processes or machines.
|
|
4
|
+
|
|
5
|
+
## Why Redis?
|
|
6
|
+
|
|
7
|
+
`createMemoryCapLockProvider` from `@txfence/core` is in-process. If two agents each have their own process and both spend against the same cap, the in-memory provider cannot see across processes — race conditions are possible.
|
|
8
|
+
|
|
9
|
+
`@txfence/redis` uses atomic Lua scripts to make `acquire / commit / release` safe across any number of concurrent agents.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @txfence/redis @txfence/core ioredis
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import Redis from 'ioredis'
|
|
21
|
+
import { createRedisCapLockProvider } from '@txfence/redis'
|
|
22
|
+
import { createAgent } from '@txfence/core'
|
|
23
|
+
|
|
24
|
+
const redis = new Redis(process.env.REDIS_URL!)
|
|
25
|
+
|
|
26
|
+
const capLockProvider = createRedisCapLockProvider(redis, {
|
|
27
|
+
caps: [
|
|
28
|
+
{
|
|
29
|
+
capId: 'treasury-main',
|
|
30
|
+
absoluteCap: { maxAmount: 500_000n, token: 'USDC' },
|
|
31
|
+
rollingWindow: { windowMs: 3_600_000, maxAmount: 25_000n, token: 'USDC' },
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
keyPrefix: 'txfence:caps',
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const agent = createAgent(
|
|
38
|
+
config, adapters, rpcUrls, executor,
|
|
39
|
+
capLockProvider,
|
|
40
|
+
)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Atomicity
|
|
44
|
+
|
|
45
|
+
Every operation that mutates a cap (`acquire`, `commit`, `release`) runs as a single Lua script inside Redis. The script holds the lock for the duration of the check-and-mutate, so two agents racing for the last 100 USDC of headroom cannot both succeed.
|
|
46
|
+
|
|
47
|
+
## Connection lifecycle
|
|
48
|
+
|
|
49
|
+
`@txfence/redis` does not create or close the `Redis` client. Inject your own:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const redis = new Redis(process.env.REDIS_URL!)
|
|
53
|
+
|
|
54
|
+
// ... use the provider ...
|
|
55
|
+
|
|
56
|
+
await redis.quit()
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Full project README: https://github.com/AdityaChauhanX07/txfence
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// src/scripts.ts
|
|
2
|
+
var ACQUIRE_SCRIPT = `
|
|
3
|
+
local absoluteTotal = tonumber(redis.call('GET', KEYS[1])) or 0
|
|
4
|
+
local pendingTotal = tonumber(redis.call('GET', KEYS[2])) or 0
|
|
5
|
+
local amount = tonumber(ARGV[1])
|
|
6
|
+
local absoluteCapMax = tonumber(ARGV[2])
|
|
7
|
+
local rollingWindowMax = tonumber(ARGV[3])
|
|
8
|
+
local rollingWindowMs = tonumber(ARGV[4])
|
|
9
|
+
local currentTime = tonumber(ARGV[6])
|
|
10
|
+
|
|
11
|
+
if absoluteCapMax > 0 then
|
|
12
|
+
if absoluteTotal + pendingTotal + amount > absoluteCapMax then
|
|
13
|
+
return 'absolute_cap_exceeded'
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
if rollingWindowMax > 0 then
|
|
18
|
+
local windowStart = currentTime - rollingWindowMs
|
|
19
|
+
redis.call('ZREMRANGEBYSCORE', KEYS[4], '-inf', windowStart - 1)
|
|
20
|
+
local members = redis.call('ZRANGEBYSCORE', KEYS[4], windowStart, '+inf')
|
|
21
|
+
local windowSum = 0
|
|
22
|
+
for _, member in ipairs(members) do
|
|
23
|
+
local sep = string.find(member, ':', 1, true)
|
|
24
|
+
if sep then
|
|
25
|
+
windowSum = windowSum + (tonumber(string.sub(member, sep + 1)) or 0)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
if windowSum + pendingTotal + amount > rollingWindowMax then
|
|
29
|
+
return 'rolling_window_exceeded'
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
redis.call('INCRBY', KEYS[2], ARGV[1])
|
|
34
|
+
redis.call('SET', KEYS[3], ARGV[1], 'EX', 300)
|
|
35
|
+
return 'granted'
|
|
36
|
+
`;
|
|
37
|
+
var COMMIT_SCRIPT = `
|
|
38
|
+
local pendingTotal = tonumber(redis.call('GET', KEYS[2])) or 0
|
|
39
|
+
local amount = tonumber(ARGV[1])
|
|
40
|
+
local currentTime = ARGV[3]
|
|
41
|
+
|
|
42
|
+
if pendingTotal > amount then
|
|
43
|
+
redis.call('DECRBY', KEYS[2], ARGV[1])
|
|
44
|
+
else
|
|
45
|
+
redis.call('SET', KEYS[2], 0)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
redis.call('INCRBY', KEYS[1], ARGV[1])
|
|
49
|
+
redis.call('ZADD', KEYS[4], currentTime, currentTime .. ':' .. ARGV[1])
|
|
50
|
+
redis.call('DEL', KEYS[3])
|
|
51
|
+
return 'ok'
|
|
52
|
+
`;
|
|
53
|
+
var RELEASE_SCRIPT = `
|
|
54
|
+
local pendingTotal = tonumber(redis.call('GET', KEYS[1])) or 0
|
|
55
|
+
local amount = tonumber(ARGV[1])
|
|
56
|
+
|
|
57
|
+
if pendingTotal > amount then
|
|
58
|
+
redis.call('DECRBY', KEYS[1], ARGV[1])
|
|
59
|
+
else
|
|
60
|
+
redis.call('SET', KEYS[1], 0)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
redis.call('DEL', KEYS[2])
|
|
64
|
+
return 'ok'
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
// src/provider.ts
|
|
68
|
+
function absoluteKey(capId) {
|
|
69
|
+
return `txfence:cap:${capId}:absolute`;
|
|
70
|
+
}
|
|
71
|
+
function pendingKey(capId) {
|
|
72
|
+
return `txfence:cap:${capId}:pending`;
|
|
73
|
+
}
|
|
74
|
+
function lockKey(capId, lockId) {
|
|
75
|
+
return `txfence:cap:${capId}:lock:${lockId}`;
|
|
76
|
+
}
|
|
77
|
+
function windowKey(capId) {
|
|
78
|
+
return `txfence:cap:${capId}:window`;
|
|
79
|
+
}
|
|
80
|
+
function generateLockId() {
|
|
81
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
82
|
+
}
|
|
83
|
+
function createRedisCapLockProvider(redis, configs) {
|
|
84
|
+
return {
|
|
85
|
+
async acquire(capId, amount, _token) {
|
|
86
|
+
const config = configs.find((c) => c.capId === capId);
|
|
87
|
+
if (config === void 0) throw new Error("unknown capId: " + capId);
|
|
88
|
+
const lockId = generateLockId();
|
|
89
|
+
const absoluteCapMax = (config.absoluteCap?.maxAmount ?? 0n).toString();
|
|
90
|
+
const rollingWindowMax = (config.rollingWindow?.maxAmount ?? 0n).toString();
|
|
91
|
+
const rollingWindowMs = (config.rollingWindow?.windowMs ?? 0).toString();
|
|
92
|
+
const result = await redis.eval(
|
|
93
|
+
ACQUIRE_SCRIPT,
|
|
94
|
+
4,
|
|
95
|
+
absoluteKey(capId),
|
|
96
|
+
pendingKey(capId),
|
|
97
|
+
lockKey(capId, lockId),
|
|
98
|
+
windowKey(capId),
|
|
99
|
+
amount.toString(),
|
|
100
|
+
absoluteCapMax,
|
|
101
|
+
rollingWindowMax,
|
|
102
|
+
rollingWindowMs,
|
|
103
|
+
lockId,
|
|
104
|
+
Date.now().toString()
|
|
105
|
+
);
|
|
106
|
+
const resultStr = String(result);
|
|
107
|
+
if (resultStr === "granted") return { granted: true, lockId };
|
|
108
|
+
if (resultStr === "absolute_cap_exceeded") return { granted: false, reason: "absolute_cap_exceeded" };
|
|
109
|
+
if (resultStr === "rolling_window_exceeded") return { granted: false, reason: "rolling_window_exceeded" };
|
|
110
|
+
throw new Error("unexpected result from Redis acquire script: " + resultStr);
|
|
111
|
+
},
|
|
112
|
+
async release(capId, lockId, amount) {
|
|
113
|
+
await redis.eval(
|
|
114
|
+
RELEASE_SCRIPT,
|
|
115
|
+
2,
|
|
116
|
+
pendingKey(capId),
|
|
117
|
+
lockKey(capId, lockId),
|
|
118
|
+
amount.toString()
|
|
119
|
+
);
|
|
120
|
+
},
|
|
121
|
+
async commit(capId, lockId, amount) {
|
|
122
|
+
await redis.eval(
|
|
123
|
+
COMMIT_SCRIPT,
|
|
124
|
+
4,
|
|
125
|
+
absoluteKey(capId),
|
|
126
|
+
pendingKey(capId),
|
|
127
|
+
lockKey(capId, lockId),
|
|
128
|
+
windowKey(capId),
|
|
129
|
+
amount.toString(),
|
|
130
|
+
lockId,
|
|
131
|
+
Date.now().toString()
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export {
|
|
137
|
+
createRedisCapLockProvider
|
|
138
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@txfence/redis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"CHANGELOG.md"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"ioredis": "^5.3.2",
|
|
20
|
+
"@txfence/core": "0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"vitest": "^4.1.5"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup",
|
|
27
|
+
"clean": "rm -rf dist",
|
|
28
|
+
"test": "vitest run --passWithNoTests",
|
|
29
|
+
"test:watch": "vitest"
|
|
30
|
+
}
|
|
31
|
+
}
|