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(protocol): Remove non-working FoundryRandom and implement a custom one #13651

Merged
merged 6 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/protocol/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ optimizer = true
optimizer_runs = 200
ffi = true
gas_limit = '18446744073709551615'
memory_limit = 1073741824

# Do not change the block_gas_limit value, TaikoL2.t.sol depends on it.
block_gas_limit = 30000000 #30M
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test": "forge test -vvv --gas-report --fuzz-seed $(date +%s) --match-path test/**/*.t.sol",
"snapshot": "forge snapshot --match-path test/**/*.t.sol",
"test:coverage": "forge coverage --report lcov",
"test:sim": "forge test -vvv --gas-report --fuzz-seed $(date +%s) --match-path test/**/*.sim.sol --block-gas-limit 30000000000 --memory-limit 1073741824",
"test:sim": "forge test -vvv --gas-report --fuzz-seed $(date +%s) --match-path test/**/*.sim.sol --block-gas-limit 30000000000",
"generate:genesis": "ts-node ./utils/generate_genesis/main.ts",
"test:genesis": "./test/genesis/generate_genesis.test.sh",
"deploy:foundry": "./script/download_solc.sh && ./script/test_deploy_on_l1.sh",
Expand Down
89 changes: 65 additions & 24 deletions packages/protocol/test/TaikoL1.sim.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity ^0.8.18;

import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import {FoundryRandom} from "foundry-random/FoundryRandom.sol";
Copy link
Contributor

Choose a reason for hiding this comment

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

please also remove it from .gitmodules

Copy link
Contributor

Choose a reason for hiding this comment

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

Screenshot 2023-04-26 at 13 27 44

import {TaikoConfig} from "../contracts/L1/TaikoConfig.sol";
import {TaikoData} from "../contracts/L1/TaikoData.sol";
import {TaikoL1} from "../contracts/L1/TaikoL1.sol";
Expand Down Expand Up @@ -37,7 +36,10 @@ contract Verifier {
}
}

contract TaikoL1Simulation is TaikoL1TestBase, FoundryRandom {
contract TaikoL1Simulation is TaikoL1TestBase {
// Initial salt for semi-random generation
uint256 salt = 2195684615613153;

function deployTaikoL1() internal override returns (TaikoL1 taikoL1) {
taikoL1 = new TaikoL1_b();
}
Expand Down Expand Up @@ -73,35 +75,73 @@ contract TaikoL1Simulation is TaikoL1TestBase, FoundryRandom {
uint256 avgBlockTime = 10 seconds;

for (uint256 blockId = 1; blockId < blocksToSimulate; blockId++) {
time += randomNumber(avgBlockTime * 2);
uint256 newRandomWithoutSalt = uint256(
keccak256(abi.encodePacked(time, msg.sender, block.timestamp))
);

time += pickRandomNumber(
newRandomWithoutSalt,
avgBlockTime,
(avgBlockTime * 2 - avgBlockTime + 1)
);
//Regenerate salt every time used at pickRandomNumber
salt = uint256(keccak256(abi.encodePacked(time, salt)));

while ((time / 12) * 12 > block.timestamp) {
vm.warp(block.timestamp + 12);
vm.roll(block.number + 1);
}

uint32 gasLimit = uint32(randomNumber(100E3, 30E6)); // 100K to 30M
uint32 gasUsed = uint32(randomNumber(gasLimit / 2, gasLimit));
uint24 txListSize = uint24(randomNumber(1, conf.maxBytesPerTxList));
bytes32 blockHash = bytes32(randomNumber(type(uint256).max));
bytes32 signalRoot = bytes32(randomNumber(type(uint256).max));
uint32 gasLimit = uint32(
pickRandomNumber(
newRandomWithoutSalt,
100E3,
(3000000 - 100000 + 1)
)
); // 100K to 30M
salt = uint256(keccak256(abi.encodePacked(gasLimit, salt)));

uint32 gasUsed = uint32(
pickRandomNumber(
newRandomWithoutSalt,
(gasLimit / 2),
((gasLimit / 2) + 1)
)
);
salt = uint256(keccak256(abi.encodePacked(gasUsed, salt)));

uint24 txListSize = uint24(
pickRandomNumber(
newRandomWithoutSalt,
1,
conf.maxBytesPerTxList
) //Actually (conf.maxBytesPerTxList-1)+1 but that's the same
);
salt = uint256(keccak256(abi.encodePacked(txListSize, salt)));

bytes32 blockHash = bytes32(
pickRandomNumber(newRandomWithoutSalt, 0, type(uint256).max)
);
salt = uint256(keccak256(abi.encodePacked(blockHash, salt)));

bytes32 signalRoot = bytes32(
pickRandomNumber(newRandomWithoutSalt, 0, type(uint256).max)
);
salt = uint256(keccak256(abi.encodePacked(signalRoot, salt)));

TaikoData.BlockMetadata memory meta = proposeBlock(
Alice,
gasLimit,
txListSize
);

// Here we need to have some time elapsed between propose and prove
// Realistically lets make it somewhere 160-240 sec, it is realistic
// for a testnet. Created this function because randomNumber seems to
// be non-working properly.
uint8 proveTimeCnt = pickRandomProveTime(
uint256(
keccak256(
abi.encodePacked(time, msg.sender, block.timestamp)
)
)
);
// for a testnet.
uint256 proveTimeCnt = pickRandomNumber(newRandomWithoutSalt, 8, 5);

salt = uint256(keccak256(abi.encodePacked(proveTimeCnt, salt)));
//console2.log("salt:", salt);

mine(proveTimeCnt);

Expand Down Expand Up @@ -156,12 +196,13 @@ contract TaikoL1Simulation is TaikoL1TestBase, FoundryRandom {
console2.log(str);
}

function pickRandomProveTime(
uint256 randomNum
) internal pure returns (uint8) {
// Result shall be between 8-12 (inclusive)
// so that it will result in a 160-240s proof time
// while the proof time target is 200s
return uint8(8 + (randomNum % 5));
// Semi-random number generator
function pickRandomNumber(
uint256 randomNum,
uint256 lowerLimit,
uint256 diffBtwLowerAndUpperLimit
) internal view returns (uint256) {
randomNum = uint256(keccak256(abi.encodePacked(randomNum, salt)));
return (lowerLimit + (randomNum % diffBtwLowerAndUpperLimit));
}
}