-
Notifications
You must be signed in to change notification settings - Fork 322
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
test: end to end test node & pxe persistence #3911
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cf4a454
fix: close leveldb handle when node stops
alexghr 5de4fef
fix: memory fifo warns when flushing
alexghr 363bcc3
fix: log how many accounts PXE restored
alexghr 3616c4c
test: persistence
alexghr d1c3bfd
test: test node/pxe database persistence
alexghr 00353c8
fix: reject transactions if pxe not running
alexghr 2dc8b54
feat: expose waitForAccountSynch
alexghr cfb7492
wip
alexghr 570a6f8
test: extended e2e persistence test
alexghr 434fed6
fix: make pxe.sendTx more lenient about running
alexghr 6c88858
refactor: ternary over if/else + assignment
alexghr 541e92f
chore: fix formatting
alexghr 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
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
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
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,184 @@ | ||
import { getUnsafeSchnorrAccount, getUnsafeSchnorrWallet } from '@aztec/accounts/single_key'; | ||
import { AccountWallet, waitForAccountSynch } from '@aztec/aztec.js'; | ||
import { CompleteAddress, EthAddress, Fq, Fr } from '@aztec/circuits.js'; | ||
import { DeployL1Contracts } from '@aztec/ethereum'; | ||
import { EasyPrivateTokenContract } from '@aztec/noir-contracts/EasyPrivateToken'; | ||
|
||
import { mkdtemp } from 'fs/promises'; | ||
import { tmpdir } from 'os'; | ||
import { join } from 'path'; | ||
|
||
import { EndToEndContext, setup } from './fixtures/utils.js'; | ||
|
||
describe('Aztec persistence', () => { | ||
/** | ||
* These tests check that the Aztec Node and PXE can be shutdown and restarted without losing data. | ||
* | ||
* There are four scenarios to check: | ||
* 1. Node and PXE are started with an existing databases | ||
* 2. PXE is started with an existing database and connects to a Node with an empty database | ||
* 3. PXE is started with an empty database and connects to a Node with an existing database | ||
* 4. PXE is started with an empty database and connects to a Node with an empty database | ||
* | ||
* All four scenarios use the same L1 state, which is deployed in the `beforeAll` hook. | ||
*/ | ||
|
||
// the test contract and account deploying it | ||
let contractAddress: CompleteAddress; | ||
let ownerPrivateKey: Fq; | ||
let ownerAddress: CompleteAddress; | ||
|
||
// a directory where data will be persisted by components | ||
// passing this through to the Node or PXE will control whether they use persisted data or not | ||
let dataDirectory: string; | ||
|
||
// state that is persisted between tests | ||
let deployL1ContractsValues: DeployL1Contracts; | ||
|
||
let context: EndToEndContext; | ||
|
||
// deploy L1 contracts, start initial node & PXE, deploy test contract & shutdown node and PXE | ||
beforeAll(async () => { | ||
dataDirectory = await mkdtemp(join(tmpdir(), 'aztec-node-')); | ||
|
||
const initialContext = await setup(0, { dataDirectory }, { dataDirectory }); | ||
deployL1ContractsValues = initialContext.deployL1ContractsValues; | ||
|
||
ownerPrivateKey = Fq.random(); | ||
const ownerWallet = await getUnsafeSchnorrAccount(initialContext.pxe, ownerPrivateKey, Fr.ZERO).waitDeploy(); | ||
ownerAddress = ownerWallet.getCompleteAddress(); | ||
|
||
const deployer = EasyPrivateTokenContract.deploy(ownerWallet, 1000n, ownerWallet.getAddress()); | ||
await deployer.simulate({}); | ||
|
||
const contract = await deployer.send().deployed(); | ||
contractAddress = contract.completeAddress; | ||
|
||
await initialContext.teardown(); | ||
}, 100_000); | ||
|
||
describe.each([ | ||
[ | ||
// ie we were shutdown and now starting back up. Initial sync should be ~instant | ||
'when starting Node and PXE with existing databases', | ||
() => setup(0, { dataDirectory, deployL1ContractsValues }, { dataDirectory }), | ||
1000, | ||
], | ||
[ | ||
// ie our PXE was restarted, data kept intact and now connects to a "new" Node. Initial synch will synch from scratch | ||
'when starting a PXE with an existing database, connected to a Node with database synched from scratch', | ||
() => setup(0, { deployL1ContractsValues }, { dataDirectory }), | ||
10_000, | ||
], | ||
])('%s', (_, contextSetup, timeout) => { | ||
let ownerWallet: AccountWallet; | ||
let contract: EasyPrivateTokenContract; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we used the regular TokenContract instead, we should also be able to test that public state is properly restored, in case that's useful to check. |
||
|
||
beforeEach(async () => { | ||
context = await contextSetup(); | ||
ownerWallet = await getUnsafeSchnorrWallet(context.pxe, ownerAddress.address, ownerPrivateKey); | ||
contract = await EasyPrivateTokenContract.at(contractAddress.address, ownerWallet); | ||
}, timeout); | ||
|
||
afterEach(async () => { | ||
await context.teardown(); | ||
}); | ||
|
||
it('correctly restores balances', async () => { | ||
// test for >0 instead of exact value so test isn't dependent on run order | ||
await expect(contract.methods.getBalance(ownerWallet.getAddress()).view()).resolves.toBeGreaterThan(0n); | ||
}); | ||
|
||
it('tracks new notes for the owner', async () => { | ||
const balance = await contract.methods.getBalance(ownerWallet.getAddress()).view(); | ||
await contract.methods.mint(1000n, ownerWallet.getAddress()).send().wait(); | ||
await expect(contract.methods.getBalance(ownerWallet.getAddress()).view()).resolves.toEqual(balance + 1000n); | ||
}); | ||
|
||
it('allows transfers of tokens from owner', async () => { | ||
const otherWallet = await getUnsafeSchnorrAccount(context.pxe, Fq.random(), Fr.ZERO).waitDeploy(); | ||
|
||
const initialOwnerBalance = await contract.methods.getBalance(ownerWallet.getAddress()).view(); | ||
await contract.methods.transfer(500n, ownerWallet.getAddress(), otherWallet.getAddress()).send().wait(); | ||
const [ownerBalance, targetBalance] = await Promise.all([ | ||
contract.methods.getBalance(ownerWallet.getAddress()).view(), | ||
contract.methods.getBalance(otherWallet.getAddress()).view(), | ||
]); | ||
|
||
expect(ownerBalance).toEqual(initialOwnerBalance - 500n); | ||
expect(targetBalance).toEqual(500n); | ||
}); | ||
}); | ||
|
||
describe.each([ | ||
[ | ||
// ie. I'm setting up a new full node, sync from scratch and restore wallets/notes | ||
'when starting the Node and PXE with empty databases', | ||
() => setup(0, { deployL1ContractsValues }, {}), | ||
10_000, | ||
], | ||
[ | ||
// ie. I'm setting up a new PXE, restore wallets/notes from a Node | ||
'when starting a PXE with an empty database connected to a Node with an existing database', | ||
() => setup(0, { dataDirectory, deployL1ContractsValues }, {}), | ||
10_000, | ||
], | ||
])('%s', (_, contextSetup, timeout) => { | ||
beforeEach(async () => { | ||
context = await contextSetup(); | ||
}, timeout); | ||
afterEach(async () => { | ||
await context.teardown(); | ||
}); | ||
|
||
it('pxe does not have the owner account', async () => { | ||
await expect(context.pxe.getRecipient(ownerAddress.address)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('the node has the contract', async () => { | ||
await expect(context.aztecNode.getContractData(contractAddress.address)).resolves.toBeDefined(); | ||
}); | ||
|
||
it('pxe does not know of the deployed contract', async () => { | ||
await context.pxe.registerRecipient(ownerAddress); | ||
|
||
const wallet = await getUnsafeSchnorrAccount(context.pxe, Fq.random(), Fr.ZERO).waitDeploy(); | ||
const contract = await EasyPrivateTokenContract.at(contractAddress.address, wallet); | ||
await expect(contract.methods.getBalance(ownerAddress.address).view()).rejects.toThrowError(/Unknown contract/); | ||
}); | ||
|
||
it("pxe does not have owner's notes", async () => { | ||
await context.pxe.addContracts([ | ||
{ | ||
artifact: EasyPrivateTokenContract.artifact, | ||
completeAddress: contractAddress, | ||
portalContract: EthAddress.ZERO, | ||
}, | ||
]); | ||
await context.pxe.registerRecipient(ownerAddress); | ||
|
||
const wallet = await getUnsafeSchnorrAccount(context.pxe, Fq.random(), Fr.ZERO).waitDeploy(); | ||
const contract = await EasyPrivateTokenContract.at(contractAddress.address, wallet); | ||
await expect(contract.methods.getBalance(ownerAddress.address).view()).resolves.toEqual(0n); | ||
}); | ||
|
||
it('pxe restores notes after registering the owner', async () => { | ||
await context.pxe.addContracts([ | ||
{ | ||
artifact: EasyPrivateTokenContract.artifact, | ||
completeAddress: contractAddress, | ||
portalContract: EthAddress.ZERO, | ||
}, | ||
]); | ||
|
||
await context.pxe.registerAccount(ownerPrivateKey, ownerAddress.partialAddress); | ||
const ownerWallet = await getUnsafeSchnorrAccount(context.pxe, ownerPrivateKey, ownerAddress).getWallet(); | ||
const contract = await EasyPrivateTokenContract.at(contractAddress.address, ownerWallet); | ||
|
||
await waitForAccountSynch(context.pxe, ownerAddress, { interval: 1, timeout: 10 }); | ||
|
||
// check that notes total more than 0 so that this test isn't dependent on run order | ||
await expect(contract.methods.getBalance(ownerAddress.address).view()).resolves.toBeGreaterThan(0n); | ||
}); | ||
}); | ||
}); |
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.
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.
In the context of a jest test, old references to nodes/pxe stay and they need to clean up all of the resources that they use. LevelDB can only have one reader/one writer per db instance