-
Notifications
You must be signed in to change notification settings - Fork 265
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
feat: epoch cache, do not attest if not in committee or from current proposer #10327
Merged
+899
−143
Merged
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
e2d7829
feat: delete attestations older than a slot
Maddiaa0 67ea59c
fmt
Maddiaa0 b95bcef
test: attestation pool pruning
Maddiaa0 f6ac466
fmt
Maddiaa0 e67eaef
feat: epoch cache, do not attest if not in committee or from current …
Maddiaa0 0265e47
cleanup
Maddiaa0 6a795ea
test: validator client tests
Maddiaa0 50eaa4c
fmt
Maddiaa0 98b118a
fmt + test
Maddiaa0 6507c02
tmp
Maddiaa0 97f35c3
fix: next and current slot
Maddiaa0 7003114
fmt
Maddiaa0 452e781
Merge branch 'master' into md/epoch-cache--
Maddiaa0 8c8df0c
post merge fix
Maddiaa0 e2b43fb
fix: ordering
Maddiaa0 e0976bb
fix: ordering
Maddiaa0 da87b55
fix
Maddiaa0 5fc9900
fix: linter and test
just-mitch 17353cf
Merge branch 'master' into md/epoch-cache--
just-mitch bcbcccd
fix: merge conflicts
just-mitch 1309414
fix: function ordering and p2p tests
just-mitch 6aba551
fix: reex test - sending same proposal twice in one slot
Maddiaa0 82da16b
fix: for a slot ci runner, decrease the time jump length
Maddiaa0 13ec8e4
fix: epoch cache flake
Maddiaa0 676b7ed
fix: in native testnet, deploy validators at genesis
Maddiaa0 ca99658
fix: run interleaved
Maddiaa0 e955e8d
fix: earthfile
Maddiaa0 dd88dbf
Merge branch 'master' into md/epoch-cache--
Maddiaa0 38f4b15
fix: unused import
just-mitch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,4 +102,4 @@ | |
"engines": { | ||
"node": ">=18" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 15 additions & 1 deletion
16
...ct/archiver/src/archiver/epoch_helpers.ts → .../circuit-types/src/epoch-helpers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,4 +99,4 @@ | |
] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('@aztec/foundation/eslint'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Epoch Cache | ||
|
||
Stores the current validator set. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
{ | ||
"name": "@aztec/epoch-cache", | ||
"version": "0.1.0", | ||
"type": "module", | ||
"exports": { | ||
".": "./dest/index.js", | ||
"./test": "./dest/test/index.js", | ||
"./contracts": "./dest/contracts/index.js" | ||
}, | ||
"typedocOptions": { | ||
"entryPoints": [ | ||
"./src/index.ts" | ||
], | ||
"name": "Epoch Cache", | ||
"tsconfig": "./tsconfig.json" | ||
}, | ||
"scripts": { | ||
"build": "yarn clean && tsc -b", | ||
"build:dev": "tsc -b --watch", | ||
"clean": "rm -rf ./dest .tsbuildinfo", | ||
"formatting": "run -T prettier --check ./src && run -T eslint ./src", | ||
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src", | ||
"start:dev": "tsc-watch -p tsconfig.json --onSuccess 'yarn start'", | ||
"start": "node ./dest/index.js", | ||
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests" | ||
}, | ||
"inherits": [ | ||
"../package.common.json" | ||
], | ||
"dependencies": { | ||
"@aztec/circuit-types": "workspace:*", | ||
"@aztec/ethereum": "workspace:*", | ||
"@aztec/foundation": "workspace:^", | ||
"@aztec/l1-artifacts": "workspace:^", | ||
"@viem/anvil": "^0.0.10", | ||
"dotenv": "^16.0.3", | ||
"get-port": "^7.1.0", | ||
"tslib": "^2.4.0", | ||
"viem": "^2.7.15", | ||
"zod": "^3.23.8" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.5.0", | ||
"@types/jest": "^29.5.0", | ||
"@types/node": "^18.14.6", | ||
"jest": "^29.5.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
}, | ||
"files": [ | ||
"dest", | ||
"src", | ||
"!*.test.*" | ||
], | ||
"types": "./dest/index.d.ts", | ||
"jest": { | ||
"moduleNameMapper": { | ||
"^(\\.{1,2}/.*)\\.[cm]?js$": "$1" | ||
}, | ||
"testRegex": "./src/.*\\.test\\.(js|mjs|ts)$", | ||
"rootDir": "./src", | ||
"transform": { | ||
"^.+\\.tsx?$": [ | ||
"@swc/jest", | ||
{ | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"decorators": true | ||
}, | ||
"transform": { | ||
"decoratorVersion": "2022-03" | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
"extensionsToTreatAsEsm": [ | ||
".ts" | ||
], | ||
"reporters": [ | ||
[ | ||
"default", | ||
{ | ||
"summaryThreshold": 9999 | ||
} | ||
] | ||
] | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// import { EpochCache } from "./epoch_cache.js"; | ||
|
||
describe('EpochCache', () => { | ||
it('Should return the current validator', () => { | ||
expect(true).toBe(true); | ||
// const epochCache = await EpochCache.create(EthAddress.random()); | ||
// const currentValidator = await epochCache.getCurrentValidator(); | ||
// expect(currentValidator).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { | ||
EmptyL1RollupConstants, | ||
type L1RollupConstants, | ||
getEpochNumberAtTimestamp, | ||
getSlotAtTimestamp, | ||
} from '@aztec/circuit-types'; | ||
import { | ||
RollupContract, | ||
createEthereumChain, | ||
getL1ContractsConfigEnvVars, | ||
getL1ReaderConfigFromEnv, | ||
} from '@aztec/ethereum'; | ||
import { EthAddress } from '@aztec/foundation/eth-address'; | ||
|
||
import { createPublicClient, http } from 'viem'; | ||
|
||
type EpochAndSlot = { | ||
epoch: bigint; | ||
slot: bigint; | ||
ts: bigint; | ||
}; | ||
|
||
export class EpochCache { | ||
private validators: Map<EthAddress, boolean>; | ||
private currentEpoch: bigint; | ||
|
||
constructor( | ||
private rollup: RollupContract, | ||
private readonly l1constants: L1RollupConstants = EmptyL1RollupConstants, | ||
) { | ||
this.validators = new Map<EthAddress, boolean>(); | ||
|
||
this.currentEpoch = getEpochNumberAtTimestamp(BigInt(Math.floor(Date.now() / 1000)), this.l1constants); | ||
} | ||
|
||
// TODO: cleanup and merge rollup getters with l1 createAndSync in the archiver | ||
static async create(rollupAddress: EthAddress) { | ||
const l1ReaderConfig = getL1ReaderConfigFromEnv(); | ||
const l1constants = getL1ContractsConfigEnvVars(); | ||
|
||
const chain = createEthereumChain(l1ReaderConfig.l1RpcUrl, l1ReaderConfig.l1ChainId); | ||
const publicClient = createPublicClient({ | ||
chain: chain.chainInfo, | ||
transport: http(chain.rpcUrl), | ||
pollingInterval: l1ReaderConfig.viemPollingIntervalMS, | ||
}); | ||
|
||
const rollup = new RollupContract(publicClient, rollupAddress.toString()); | ||
const [l1StartBlock, l1GenesisTime] = await Promise.all([ | ||
rollup.getL1StartBlock(), | ||
rollup.getL1GenesisTime(), | ||
] as const); | ||
|
||
const l1RollupConstants: L1RollupConstants = { | ||
l1StartBlock, | ||
l1GenesisTime, | ||
slotDuration: l1constants.aztecSlotDuration, | ||
epochDuration: l1constants.aztecEpochDuration, | ||
ethereumSlotDuration: l1constants.ethereumSlotDuration, | ||
}; | ||
|
||
return new EpochCache(rollup, l1RollupConstants); | ||
} | ||
|
||
getEpochAndSlotNow(): EpochAndSlot { | ||
const now = BigInt(Math.floor(Date.now() / 1000)); | ||
return this.getEpochAndSlotAtTimestamp(now); | ||
} | ||
|
||
getEpochAndSlotAtTimestamp(ts: bigint): EpochAndSlot { | ||
return { | ||
epoch: getEpochNumberAtTimestamp(ts, this.l1constants), | ||
slot: getSlotAtTimestamp(ts, this.l1constants), | ||
ts, | ||
}; | ||
} | ||
|
||
async getValidatorSet(): Promise<EthAddress[]> { | ||
// If the current epoch has changed, then we need to make a request to update the validator set | ||
const { epoch: currentEpoch, ts } = this.getEpochAndSlotNow(); | ||
|
||
if (currentEpoch !== this.currentEpoch) { | ||
this.currentEpoch = currentEpoch; | ||
const validatorSet = await this.rollup.getCommitteeAt(ts); | ||
this.validators = new Map(validatorSet.map((v: `0x${string}`) => [EthAddress.fromString(v), true])); | ||
} | ||
|
||
return Array.from(this.validators.keys()); | ||
} | ||
|
||
async getCurrentValidator(): Promise<EthAddress> { | ||
// Validators are sorted by their index in the committee, and getValidatorSet will cache | ||
// TODO: should we get the timestamp from the underlying l1 node? | ||
const { slot: currentSlot } = this.getEpochAndSlotNow(); | ||
const validators = await this.getValidatorSet(); | ||
|
||
return validators[Number(currentSlot) % validators.length]; | ||
} | ||
|
||
async isInCommittee(validator: EthAddress): Promise<boolean> { | ||
const validators = await this.getValidatorSet(); | ||
return validators.includes(validator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './epoch_cache.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"extends": "..", | ||
"compilerOptions": { | ||
"outDir": "dest", | ||
"rootDir": "src", | ||
"tsBuildInfoFile": ".tsbuildinfo" | ||
}, | ||
"include": ["src"], | ||
"references": [ | ||
{ | ||
"path": "../circuit-types" | ||
}, | ||
{ | ||
"path": "../ethereum" | ||
}, | ||
{ | ||
"path": "../foundation" | ||
}, | ||
{ | ||
"path": "../l1-artifacts" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit worried that clock drift will cause issues in this setup, but do not want to poll the smart contract for everything, what do we think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. We could have it reset at a preconfigured interval? e.g. once per epoch sync with the rollup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #10435