Skip to content

Commit

Permalink
feat!: change VoucherExecuted event
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Dec 18, 2023
1 parent 3baed5f commit 705230f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 54 deletions.
8 changes: 8 additions & 0 deletions onchain/rollups/.changeset/khaki-ladybugs-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@cartesi/rollups": major
---

Changed `VoucherExecuted` event to have `inputIndex` and `outputIndexWithinInput` as parameters instead of `voucherPosition`.
This change was made due to an internal change that involved transitioning from the homebrew `Bitmask` library to OpenZeppelin's `BitMaps` library.
It is now easier for the off-chain to reason about `VoucherExecuted` events, since they don't have to decode `voucherPosition` into `inputIndex` and `outputIndexWithinInput` anymore.
Off-chain components that listened to `VoucherExecuted` must now listen to the new event instead.
31 changes: 9 additions & 22 deletions onchain/rollups/contracts/dapp/CartesiDApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import {LibProof} from "../library/LibProof.sol";
import {InputRange} from "../common/InputRange.sol";
import {LibInputRange} from "../library/LibInputRange.sol";

import {Bitmask} from "@cartesi/util/contracts/Bitmask.sol";

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ERC721Holder} from "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {BitMaps} from "@openzeppelin/contracts/utils/structs/BitMaps.sol";

/// @title Cartesi DApp
///
Expand Down Expand Up @@ -73,7 +72,7 @@ contract CartesiDApp is
ERC1155Holder,
ReentrancyGuard
{
using Bitmask for mapping(uint256 => uint256);
using BitMaps for BitMaps.BitMap;
using LibOutputValidation for OutputValidityProof;
using LibProof for Proof;
using LibInputRange for InputRange;
Expand All @@ -95,7 +94,7 @@ contract CartesiDApp is
/// @notice The executed voucher bitmask, which keeps track of which vouchers
/// were executed already in order to avoid re-execution.
/// @dev See the `wasVoucherExecuted` function.
mapping(uint256 => uint256) internal voucherBitmask;
mapping(uint256 => BitMaps.BitMap) internal voucherBitmaps;

/// @notice The current consensus contract.
/// @dev See the `getConsensus` and `migrateToConsensus` functions.
Expand Down Expand Up @@ -155,39 +154,27 @@ contract CartesiDApp is
// reverts if proof isn't valid
_proof.validity.validateVoucher(_destination, _payload, epochHash);

uint256 voucherPosition = LibOutputValidation.getBitMaskPosition(
_proof.validity.outputIndexWithinInput,
inputIndex
);
uint256 outputIndexWithinInput = _proof.validity.outputIndexWithinInput;
BitMaps.BitMap storage bitmap = voucherBitmaps[outputIndexWithinInput];

// check if voucher has been executed
if (_wasVoucherExecuted(voucherPosition)) {
if (bitmap.get(inputIndex)) {
revert VoucherReexecutionNotAllowed();
}

// execute voucher
_destination.functionCall(_payload);

// mark it as executed and emit event
voucherBitmask.setBit(voucherPosition, true);
emit VoucherExecuted(voucherPosition);
bitmap.set(inputIndex);
emit VoucherExecuted(inputIndex, outputIndexWithinInput);
}

function wasVoucherExecuted(
uint256 _inputIndex,
uint256 _outputIndexWithinInput
) external view override returns (bool) {
uint256 voucherPosition = LibOutputValidation.getBitMaskPosition(
_outputIndexWithinInput,
_inputIndex
);
return _wasVoucherExecuted(voucherPosition);
}

function _wasVoucherExecuted(
uint256 _voucherPosition
) internal view returns (bool) {
return voucherBitmask.getBit(_voucherPosition);
return voucherBitmaps[_outputIndexWithinInput].get(_inputIndex);
}

function validateNotice(
Expand Down
6 changes: 3 additions & 3 deletions onchain/rollups/contracts/dapp/ICartesiDApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ interface ICartesiDApp is IERC721Receiver, IERC1155Receiver {
event NewConsensus(IConsensus newConsensus);

/// @notice A voucher was executed from the DApp.
/// @param voucherId A number that uniquely identifies the voucher
/// amongst all vouchers emitted by this DApp
event VoucherExecuted(uint256 voucherId);
/// @param inputIndex The index of the input that emitted the voucher
/// @param outputIndexWithinInput The index of the voucher amongst all outputs emitted by the input
event VoucherExecuted(uint256 inputIndex, uint256 outputIndexWithinInput);

// Permissioned functions

Expand Down
14 changes: 0 additions & 14 deletions onchain/rollups/contracts/library/LibOutputValidation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,6 @@ library LibOutputValidation {
);
}

/// @notice Get the position of a voucher on the bit mask.
/// @param voucher The index of voucher from those generated by such input
/// @param input The index of the input in the DApp's input box
/// @return Position of the voucher on the bit mask
function getBitMaskPosition(
uint256 voucher,
uint256 input
) internal pure returns (uint256) {
// voucher * 2 ** 128 + input
// this shouldn't overflow because it is impossible to have > 2**128 vouchers
// and because we are assuming there will be < 2 ** 128 inputs on the input box
return (((voucher << 128) | input));
}

/// @notice Validate input index range and get the input index.
/// @param v The output validity proof
/// @param firstInputIndex The index of the first input of the epoch in the input box
Expand Down
3 changes: 1 addition & 2 deletions onchain/rollups/deploy/02_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
log: true,
};

const { Bitmask, MerkleV2 } = await deployments.all();
const { MerkleV2 } = await deployments.all();

await deployments.deploy("AuthorityFactory", opts);

await deployments.deploy("CartesiDAppFactory", {
...opts,
libraries: {
Bitmask: Bitmask.address,
MerkleV2: MerkleV2.address,
},
});
Expand Down
20 changes: 7 additions & 13 deletions onchain/rollups/test/foundry/dapp/CartesiDApp.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ contract CartesiDAppTest is TestBase {
uint256 immutable transferAmount;
IInputRelay[] inputRelays;

event VoucherExecuted(uint256 voucherPosition);
event VoucherExecuted(uint256 inputIndex, uint256 outputIndexWithinInput);
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
Expand Down Expand Up @@ -242,10 +242,8 @@ contract CartesiDAppTest is TestBase {
// expect event
vm.expectEmit(false, false, false, true, address(dapp));
emit VoucherExecuted(
LibOutputValidation.getBitMaskPosition(
proof.validity.outputIndexWithinInput,
_calculateInputIndex(proof)
)
_calculateInputIndex(proof),
proof.validity.outputIndexWithinInput
);

// perform call
Expand Down Expand Up @@ -415,10 +413,8 @@ contract CartesiDAppTest is TestBase {
// expect event
vm.expectEmit(false, false, false, true, address(dapp));
emit VoucherExecuted(
LibOutputValidation.getBitMaskPosition(
proof.validity.outputIndexWithinInput,
_calculateInputIndex(proof)
)
_calculateInputIndex(proof),
proof.validity.outputIndexWithinInput
);

// perform call
Expand Down Expand Up @@ -534,10 +530,8 @@ contract CartesiDAppTest is TestBase {
// expect event
vm.expectEmit(false, false, false, true, address(dapp));
emit VoucherExecuted(
LibOutputValidation.getBitMaskPosition(
proof.validity.outputIndexWithinInput,
_calculateInputIndex(proof)
)
_calculateInputIndex(proof),
proof.validity.outputIndexWithinInput
);

// perform call
Expand Down

0 comments on commit 705230f

Please sign in to comment.