Skip to content
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: add getNumberOfInputsBeforeCurrentBlock #339

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions contracts/inputs/IInputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ interface IInputBox {
address appContract
) external view returns (uint256);

/// @notice Get the number of inputs sent to an application
/// before the current block. This can be useful for deriving
/// input ranges from block ranges.
/// @param appContract The application contract address
function getNumberOfInputsBeforeCurrentBlock(
address appContract
) external view returns (uint256);

/// @notice Get the hash of an input in an application's input box.
/// @param appContract The application contract address
/// @param index The input index
Expand Down
31 changes: 30 additions & 1 deletion contracts/inputs/InputBox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ contract InputBox is IInputBox {
/// @notice Mapping of application contract addresses to arrays of input hashes.
mapping(address => bytes32[]) private _inputBoxes;

/// @notice Snapshot of number of inputs
struct Snapshot {
uint256 blockNumber;
uint256 numberOfInputsBeforeBlock;
}

/// @notice Mapping of application contract addresses to snapshot.
mapping(address => Snapshot) private _snapshots;

/// @inheritdoc IInputBox
function addInput(
address appContract,
Expand All @@ -20,6 +29,13 @@ contract InputBox is IInputBox {

uint256 index = inputBox.length;

// take snapshot if first input of block
Snapshot storage snapshot = _snapshots[appContract];
if (snapshot.blockNumber < block.number) {
snapshot.blockNumber = block.number;
snapshot.numberOfInputsBeforeBlock = index;
}

bytes memory input = abi.encodeCall(
Inputs.EvmAdvance,
(
Expand Down Expand Up @@ -54,10 +70,23 @@ contract InputBox is IInputBox {
/// @inheritdoc IInputBox
function getNumberOfInputs(
address appContract
) external view override returns (uint256) {
) public view override returns (uint256) {
return _inputBoxes[appContract].length;
}

/// @inheritdoc IInputBox
function getNumberOfInputsBeforeCurrentBlock(
address appContract
) external view returns (uint256) {
Snapshot storage snapshot = _snapshots[appContract];
if (snapshot.blockNumber == block.number) {
return snapshot.numberOfInputsBeforeBlock;
} else {
// snapshot.blockNumber < block.number
return getNumberOfInputs(appContract);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for using getNumberOfInputs rather than snapshots here :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because _snapshots only holds the index of the first input of the block.
If we are not in the same block in which the last snapshot was taken, then we have to return the total number of inputs up until now.
Let me illustrate:

at first, snapshot = (block=0, #inputs=0)
then an input is added on block 7, snapshot = (block=7, #inputs=1)
another input is added on block 7, snapshot = (block=7, #inputs=1) [the same]
then an input is added on block 8, snapshot = (block=8, #inputs=3)
another input is added on block 8, snapshot = (block=8, #inputs=3) [the same]
now, we're on block 42
if we used the snapshot, it would return 3,
but before the current block (42), there are 4 inputs already
so we need to use the current number of inputs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Yeah it's clear. Thanks for the illustration!!

}
}

/// @inheritdoc IInputBox
function getInputHash(
address appContract,
Expand Down
14 changes: 14 additions & 0 deletions test/inputs/InputBox.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract InputBoxTest is Test {

function testNoInputs(address appContract) public view {
assertEq(_inputBox.getNumberOfInputs(appContract), 0);
assertEq(_inputBox.getNumberOfInputsBeforeCurrentBlock(appContract), 0);
}

function testAddLargeInput() public {
Expand Down Expand Up @@ -105,6 +106,19 @@ contract InputBoxTest is Test {
}
}

function testNumberOfInputsBeforeCurrentBlock() external {
address appContract = vm.addr(1);
for (uint256 j; j < 3; ++j) {
uint256 n = _inputBox.getNumberOfInputs(appContract);
for (uint256 i; i < 2; ++i) {
_inputBox.addInput(appContract, new bytes(0));
// prettier-ignore
assertEq(_inputBox.getNumberOfInputsBeforeCurrentBlock(appContract), n);
}
vm.roll(vm.getBlockNumber() + 1);
}
}

function _prevrandao(uint256 blockNumber) internal pure returns (uint256) {
return uint256(keccak256(abi.encode("prevrandao", blockNumber)));
}
Expand Down
Loading