Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat: add test for failed transaction
Browse files Browse the repository at this point in the history
Signed-off-by: Jawad Tariq <[email protected]>
  • Loading branch information
JDawg287 committed Aug 17, 2023
1 parent 95a4aa7 commit e0b1a66
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion contracts/interfaces/IToposMessaging.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface IToposMessaging {
error CertNotPresent();
error InvalidMerkleProof();
error InvalidSubnetId();
error InvalidTransaction();
error InvalidTransactionStatus();
error InvalidToposCore();
error InvalidToposCoreAddress();
error TransactionAlreadyExecuted();
Expand Down
2 changes: 1 addition & 1 deletion contracts/topos-core/ToposMessaging.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract ToposMessaging is IToposMessaging, EternalStorage {
bytes32[][] memory logsTopics,
bytes[] memory logsData
) = _decodeReceipt(receiptRaw);
if (status != 1) revert InvalidTransaction();
if (status != 1) revert InvalidTransactionStatus();

// the last event (CrossSubnetMessageSent) is always emitted by the topos core contract
uint256 logIndex = logsAddress.length - 1;
Expand Down
28 changes: 28 additions & 0 deletions test/topos-core/ToposMessaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,34 @@ describe('ToposMessaging', () => {
)
})

it('reverts if the transaction status is invalid', async () => {
const { toposCore, erc20Messaging } = await loadFixture(
deployERC20MessagingFixture
)
await toposCore.setNetworkSubnetId(cc.SOURCE_SUBNET_ID_2)
const certificate = testUtils.encodeCertParam(
cc.PREV_CERT_ID_0,
cc.SOURCE_SUBNET_ID_1,
cc.STATE_ROOT_MAX,
txc.INVALID_STATUS_TRANSACTION.receiptRoot,
[cc.SOURCE_SUBNET_ID_2],
cc.VERIFIER,
cc.CERT_ID_1,
cc.DUMMY_STARK_PROOF,
cc.DUMMY_SIGNATURE
)
await toposCore.pushCertificate(certificate, cc.CERT_POS_1)
await expect(
erc20Messaging.execute(
txc.INVALID_STATUS_TRANSACTION.proofBlob,
txc.INVALID_STATUS_TRANSACTION.receiptRoot
)
).to.be.revertedWithCustomError(
erc20Messaging,
'InvalidTransactionStatus'
)
})

it('reverts if the token is not deployed yet', async () => {
const { toposCore, erc20Messaging } = await loadFixture(
deployERC20MessagingFixture
Expand Down
5 changes: 5 additions & 0 deletions test/topos-core/shared/constants/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ export const ZERO_ADDRESS_TRANSACTION: TransactionData = new TransactionData(
'0xf902170180f90212f9020f822080b90209f9020601828555b9010004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000001000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000008080000000000000000000f8fdf8fb94cf7ed3acca5a467e9e704c703e8d87f634fb0fc9f842a0fc206599322fdabaf4a42cd93b837734408b6a1ea6117cb9040c87d728a67d54a00000000000000000000000000000000000000000000000000000000000000002b8a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032',
'0xbe3ae823d0139b5fa7e339cf0735d9f3f8f25e86cf71627c3e98fe3e12414d20'
)

export const INVALID_STATUS_TRANSACTION: TransactionData = new TransactionData(
'0xf901190180f90114f90111822080b9010bf9010880827d98b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0',
'0x8619f1112d159672b84da1e919bfbbcd4935bd897a5b1274bd2a03dae431fa02'
)
15 changes: 6 additions & 9 deletions test/topos-core/shared/utils/mpt_proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function getReceiptMptProof(
])

const receiptsRoot = rawBlock.receiptsRoot
const trie = await createTrie(block)
const trie = await createTrie(block, provider)
const trieRoot = trie.root()
if ('0x' + trieRoot.toString('hex') !== receiptsRoot) {
throw new Error(
Expand All @@ -39,18 +39,15 @@ export async function getReceiptMptProof(
return { proofBlob, receiptsRoot }
}

async function createTrie(block: BlockWithTransactions) {
async function createTrie(
block: BlockWithTransactions,
provider: ethers.providers.JsonRpcProvider
) {
const trie = new Trie()
await Promise.all(
block.transactions.map(async (tx, index) => {
const { /*type,*/ cumulativeGasUsed, logs, logsBloom, status } =
await tx.wait()
// console.log('Tx type: ', type)
// console.log('status: ', status)
// console.log('cumulativeGasUsed: ', cumulativeGasUsed)
// console.log('logsBloom: ', logsBloom)
// console.log('logs: ', logs)

await provider.getTransactionReceipt(tx.hash)
return trie.put(
Buffer.from(RLP.encode(index)),
Buffer.from(
Expand Down

0 comments on commit e0b1a66

Please sign in to comment.