Skip to content

Commit

Permalink
flushing messages after each transaction in StarknetValidator l1-l2 m…
Browse files Browse the repository at this point in the history
…essaging tests (#403)
  • Loading branch information
chris-de-leon-cll authored Apr 4, 2024
1 parent 15e04d9 commit f3d6647
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
59 changes: 35 additions & 24 deletions contracts/test/emergency/StarknetValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,32 +608,43 @@ describe('StarknetValidator', () => {
)

// Simulate L1 transmit + validate
const messages = new Array<l1l2messaging.FlushedMessages>()
const newGasEstimate = 1
const receipts = await waitForTransactions([
// Add access
() => starknetValidator.connect(deployer).addAccess(eoaValidator.address),

// By default the gas config is 0, we need to change it or we will submit a 0 fee
() =>
starknetValidator
.connect(deployer)
.setGasConfig(newGasEstimate, mockGasPriceFeed.address, 100),
const receipts = await waitForTransactions(
[
// Add access
() => starknetValidator.connect(deployer).addAccess(eoaValidator.address),

// Validate
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 1),
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 1),
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 127), // incorrect value
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 0), // final status
])

// Simulate the L1 - L2 comms
const resp = await l1l2messaging.flush()
const msgFromL1 = resp.messages_to_l2
expect(msgFromL1).to.have.a.lengthOf(4)
expect(resp.messages_to_l1).to.be.empty
// By default the gas config is 0, we need to change it or we will submit a 0 fee
() =>
starknetValidator
.connect(deployer)
.setGasConfig(newGasEstimate, mockGasPriceFeed.address, 100),

// Validate
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 1),
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 1),
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 127), // incorrect value
() => starknetValidator.connect(eoaValidator).validate(0, 0, 1, 0), // final status
],
async () => {
// Simulate the L1 - L2 comms
const resp = await l1l2messaging.flush()
if (resp.messages_to_l2.length !== 0) {
expect(resp.messages_to_l1).to.be.empty

const msgFromL1 = resp.messages_to_l2
expect(msgFromL1).to.have.a.lengthOf(1)
expect(msgFromL1[0].l1_contract_address).to.hexEqual(starknetValidator.address)
expect(msgFromL1[0].l2_contract_address).to.hexEqual(l2Contract.address)

messages.push(resp)
}
},
)

expect(msgFromL1[0].l1_contract_address).to.hexEqual(starknetValidator.address)
expect(msgFromL1[0].l2_contract_address).to.hexEqual(l2Contract.address)
// Makes sure the correct number of messages were transmitted
expect(messages.length).to.eq(4)

// Assert L2 effects
const result = await l2Contract.latest_round_data()
Expand All @@ -643,7 +654,7 @@ describe('StarknetValidator', () => {
JSON.stringify(
{
latestRoundData: result,
flushResponse: resp,
flushResponse: messages,
txReceipts: receipts,
},
(_, value) => (typeof value === 'bigint' ? value.toString() : value),
Expand Down
2 changes: 2 additions & 0 deletions contracts/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ export const waitForTransaction = async (

export const waitForTransactions = async (
txs: (() => ReturnType<typeof ethers.provider.sendTransaction>)[],
cb?: () => void | Promise<void>,
) => {
const results = new Array<Awaited<ReturnType<typeof waitForTransaction>>>()
for (const tx of txs) {
results.push(await waitForTransaction(tx))
cb != null && (await cb())
}
return results
}
Expand Down

0 comments on commit f3d6647

Please sign in to comment.