Skip to content

Commit

Permalink
feat(protocol): Remove non-working FoundryRandom and implement a cust…
Browse files Browse the repository at this point in the history
…om one (#13651)

Co-authored-by: Daniel Wang <99078276+dantaik@users.noreply.github.com>
  • Loading branch information
adaki2004 and dantaik committed Apr 26, 2023
1 parent bd800a3 commit eee153c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@
path = packages/protocol/lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = chore/v1.5.1
[submodule "packages/protocol/lib/foundry-random"]
path = packages/protocol/lib/foundry-random
url = https://github.com/joejordan/foundry-random
branch = v1.0.2
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
1 change: 0 additions & 1 deletion packages/protocol/lib/foundry-random
Submodule foundry-random deleted from e39ad2
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
1 change: 0 additions & 1 deletion packages/protocol/remappings.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
forge-std/=lib/forge-std/src/
solmate/=lib/solmate/src/
ds-test/=lib/forge-std/lib/ds-test/src/
foundry-random/=lib/foundry-random/src/
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/
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";
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));
}
}
3 changes: 1 addition & 2 deletions packages/protocol/test/TaikoL1LibTokenomicsMainnet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.18;
// Uncomment if you want to compare fee/vs reward
import {Test} from "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import {FoundryRandom} from "foundry-random/FoundryRandom.sol";
import {AddressManager} from "../contracts/common/AddressManager.sol";
import {TaikoConfig} from "../contracts/L1/TaikoConfig.sol";
import {TaikoData} from "../contracts/L1/TaikoData.sol";
Expand Down Expand Up @@ -33,7 +32,7 @@ contract TaikoL1MainnetMockConfig is TaikoL1 {
}
}

contract TaikoL1LibTokenomicsMainnet is TaikoL1TestBase, FoundryRandom {
contract TaikoL1LibTokenomicsMainnet is TaikoL1TestBase {
// To avoid stack too deep error
// Can play to adjust
uint32 iterationCnt = 5000;
Expand Down

0 comments on commit eee153c

Please sign in to comment.