Skip to content

Commit

Permalink
refactor(protocol): remove gasUsed and parentGasUsed (#14582)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik authored Aug 25, 2023
1 parent 1c807c8 commit 3357dad
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 260 deletions.
9 changes: 1 addition & 8 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ library TaikoData {
bytes32 signalRoot;
bytes32 graffiti;
address prover;
uint32 parentGasUsed;
uint32 gasUsed;
bytes proofs;
}

Expand All @@ -137,7 +135,6 @@ library TaikoData {
bytes32 signalRoot;
address prover;
uint64 provenAt;
uint32 gasUsed;
}

/// @dev Struct containing data required for verifying a block.
Expand Down Expand Up @@ -192,11 +189,7 @@ library TaikoData {
// Ring buffer for proposed blocks and a some recent verified blocks.
mapping(uint64 blockId_mode_blockRingBufferSize => Block) blocks;
mapping(
uint64 blockId
=> mapping(
bytes32 parentHash
=> mapping(uint32 parentGasUsed => uint16 forkChoiceId)
)
uint64 blockId => mapping(bytes32 parentHash => uint16 forkChoiceId)
) forkChoiceIds;
mapping(bytes32 txListHash => TxListInfo) txListInfo;
mapping(uint256 depositId_mode_ethDepositRingBufferSize => uint256)
Expand Down
4 changes: 1 addition & 3 deletions packages/protocol/contracts/L1/TaikoEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ abstract contract TaikoEvents {
/// @param blockHash The hash of the proven block.
/// @param signalRoot The signal root of the proven block.
/// @param prover The address of the prover who submitted the proof.
/// @param parentGasUsed The gas used in the parent block.
event BlockProven(
uint256 indexed blockId,
bytes32 parentHash,
bytes32 blockHash,
bytes32 signalRoot,
address prover,
uint32 parentGasUsed
address prover
);

/// @dev Emitted when a block is verified.
Expand Down
7 changes: 2 additions & 5 deletions packages/protocol/contracts/L1/TaikoL1Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,10 @@ abstract contract TaikoL1Base is
/// @notice Gets the fork choice for a specific block.
/// @param blockId Index of the block.
/// @param parentHash Parent hash of the block.
/// @param parentGasUsed Gas used by the parent block.
/// @return ForkChoice data of the block.
function getForkChoice(
uint64 blockId,
bytes32 parentHash,
uint32 parentGasUsed
bytes32 parentHash
)
public
view
Expand All @@ -230,8 +228,7 @@ abstract contract TaikoL1Base is
state: state,
config: getConfig(),
blockId: blockId,
parentHash: parentHash,
parentGasUsed: parentGasUsed
parentHash: parentHash
});
}

Expand Down
29 changes: 9 additions & 20 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ library LibProving {
bytes32 parentHash,
bytes32 blockHash,
bytes32 signalRoot,
address prover,
uint32 parentGasUsed
address prover
);

error L1_ALREADY_PROVEN();
Expand All @@ -48,7 +47,7 @@ library LibProving {
evidence.prover == address(0) || evidence.parentHash == 0
|| evidence.blockHash == 0
|| evidence.blockHash == evidence.parentHash
|| evidence.signalRoot == 0 || evidence.gasUsed == 0
|| evidence.signalRoot == 0
) revert L1_INVALID_EVIDENCE();

TaikoData.SlotB memory b = state.slotB;
Expand Down Expand Up @@ -84,9 +83,8 @@ library LibProving {
}

TaikoData.ForkChoice storage fc;
uint16 fcId = LibUtils.getForkChoiceId(
state, blk, blockId, evidence.parentHash, evidence.parentGasUsed
);
uint16 fcId =
LibUtils.getForkChoiceId(state, blk, blockId, evidence.parentHash);

if (fcId == 0) {
fcId = blk.nextForkChoiceId;
Expand All @@ -99,12 +97,9 @@ library LibProving {

if (fcId == 1) {
// We only write the key when fcId is 1.
fc.key = LibUtils.keyForForkChoice(
evidence.parentHash, evidence.parentGasUsed
);
fc.key = evidence.parentHash;
} else {
state.forkChoiceIds[blockId][evidence.parentHash][evidence
.parentGasUsed] = fcId;
state.forkChoiceIds[blockId][evidence.parentHash] = fcId;
}
} else if (evidence.prover == LibUtils.ORACLE_PROVER) {
// This is the branch the oracle prover is trying to overwrite
Expand All @@ -114,7 +109,6 @@ library LibProving {
if (
fc.blockHash == evidence.blockHash
&& fc.signalRoot == evidence.signalRoot
&& fc.gasUsed == evidence.gasUsed
) revert L1_SAME_PROOF();
} else {
revert L1_ALREADY_PROVEN();
Expand All @@ -124,7 +118,6 @@ library LibProving {
fc.signalRoot = evidence.signalRoot;
fc.prover = evidence.prover;
fc.provenAt = uint64(block.timestamp);
fc.gasUsed = evidence.gasUsed;

IProofVerifier(resolver.resolve("proof_verifier", false)).verifyProofs(
blockId, evidence.proofs, getInstance(evidence)
Expand All @@ -135,17 +128,15 @@ library LibProving {
parentHash: evidence.parentHash,
blockHash: evidence.blockHash,
signalRoot: evidence.signalRoot,
prover: evidence.prover,
parentGasUsed: evidence.parentGasUsed
prover: evidence.prover
});
}

function getForkChoice(
TaikoData.State storage state,
TaikoData.Config memory config,
uint64 blockId,
bytes32 parentHash,
uint32 parentGasUsed
bytes32 parentHash
)
internal
view
Expand All @@ -160,9 +151,7 @@ library LibProving {
state.blocks[blockId % config.blockRingBufferSize];
if (blk.blockId != blockId) revert L1_BLOCK_ID_MISMATCH();

uint16 fcId = LibUtils.getForkChoiceId(
state, blk, blockId, parentHash, parentGasUsed
);
uint16 fcId = LibUtils.getForkChoiceId(state, blk, blockId, parentHash);
if (fcId == 0) revert L1_FORK_CHOICE_NOT_FOUND();

fc = blk.forkChoices[fcId];
Expand Down
27 changes: 3 additions & 24 deletions packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,16 @@ library LibUtils {
TaikoData.State storage state,
TaikoData.Block storage blk,
uint64 blockId,
bytes32 parentHash,
uint32 parentGasUsed
bytes32 parentHash
)
internal
view
returns (uint16 fcId)
{
if (
blk.forkChoices[1].key
== keyForForkChoice(parentHash, parentGasUsed)
) {
if (blk.forkChoices[1].key == parentHash) {
fcId = 1;
} else {
fcId = state.forkChoiceIds[blockId][parentHash][parentGasUsed];
fcId = state.forkChoiceIds[blockId][parentHash];
}

if (fcId >= blk.nextForkChoiceId) {
Expand Down Expand Up @@ -102,23 +98,6 @@ library LibUtils {
}
}

function keyForForkChoice(
bytes32 parentHash,
uint32 parentGasUsed
)
internal
pure
returns (bytes32 key)
{
assembly {
let ptr := mload(0x40)
mstore(ptr, parentGasUsed)
mstore(add(ptr, 32), parentHash)
key := keccak256(add(ptr, 28), 36)
mstore(0x40, add(ptr, 64))
}
}

function getVerifierName(uint16 id) internal pure returns (bytes32) {
return bytes32(uint256(0x1000000) + id);
}
Expand Down
6 changes: 1 addition & 5 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ library LibVerifying {
if (fcId == 0) revert L1_UNEXPECTED_FORK_CHOICE_ID();

bytes32 blockHash = blk.forkChoices[fcId].blockHash;
uint32 gasUsed = blk.forkChoices[fcId].gasUsed;

bytes32 signalRoot;
TaikoData.ForkChoice memory fc;
Expand All @@ -122,9 +121,7 @@ library LibVerifying {
blk = state.blocks[blockId % config.blockRingBufferSize];
if (blk.blockId != blockId) revert L1_BLOCK_ID_MISMATCH();

fcId = LibUtils.getForkChoiceId(
state, blk, blockId, blockHash, gasUsed
);
fcId = LibUtils.getForkChoiceId(state, blk, blockId, blockHash);
if (fcId == 0) break;

fc = blk.forkChoices[fcId];
Expand All @@ -139,7 +136,6 @@ library LibVerifying {
}

blockHash = fc.blockHash;
gasUsed = fc.gasUsed;
signalRoot = fc.signalRoot;
blk.verifiedForkChoiceId = fcId;

Expand Down
44 changes: 5 additions & 39 deletions packages/protocol/test/L1/TaikoL1.sim.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ contract TaikoL1_b is TaikoL1 {
config.proofRegularCooldown = 5 minutes;
config.proofOracleCooldown = 3 minutes;
config.skipProverAssignmentVerificaiton = true;
config.proofBond = 1 ether;
config.proofBond = 1e18; // 1 Taiko token
config.proposerRewardPerSecond = 1e15; // 0.001 Taiko token
}
}

Expand Down Expand Up @@ -83,8 +84,6 @@ contract TaikoL1Simulation is TaikoL1TestBase {
bytes32[] parentHashes = new bytes32[](blocksToSimulate);
bytes32[] blockHashes = new bytes32[](blocksToSimulate);
bytes32[] signalRoots = new bytes32[](blocksToSimulate);
uint32[] parentGasUsed = new uint32[](blocksToSimulate);
uint32[] gasUsed = new uint32[](blocksToSimulate);
uint32[] gasLimits = new uint32[](blocksToSimulate);

function deployTaikoL1() internal override returns (TaikoL1 taikoL1) {
Expand Down Expand Up @@ -221,21 +220,12 @@ contract TaikoL1Simulation is TaikoL1TestBase {
salt = uint256(keccak256(abi.encodePacked(gasLimit, salt)));

if (proposedIndex == 0) {
parentGasUsed[proposedIndex] = 0;
parentHashes[proposedIndex] = GENESIS_BLOCK_HASH;
} else {
parentGasUsed[proposedIndex] = gasUsed[proposedIndex - 1];
parentHashes[proposedIndex] = blockHashes[proposedIndex - 1];
}

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

uint24 txListSize = uint24(
pickRandomNumber(
Expand Down Expand Up @@ -287,8 +277,6 @@ contract TaikoL1Simulation is TaikoL1TestBase {
Bob,
metas[blockId],
parentHashes[blockId],
parentGasUsed[blockId],
gasUsed[blockId],
blockHashes[blockId],
signalRoots[blockId]
);
Expand Down Expand Up @@ -450,21 +438,12 @@ contract TaikoL1Simulation is TaikoL1TestBase {
salt = uint256(keccak256(abi.encodePacked(gasLimit, salt)));

if (proposedIndex == 0) {
parentGasUsed[proposedIndex] = 0;
parentHashes[proposedIndex] = GENESIS_BLOCK_HASH;
} else {
parentGasUsed[proposedIndex] = gasUsed[proposedIndex - 1];
parentHashes[proposedIndex] = blockHashes[proposedIndex - 1];
}

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

uint24 txListSize = uint24(
pickRandomNumber(
Expand Down Expand Up @@ -516,8 +495,6 @@ contract TaikoL1Simulation is TaikoL1TestBase {
Bob,
metas[blockId],
parentHashes[blockId],
parentGasUsed[blockId],
gasUsed[blockId],
blockHashes[blockId],
signalRoots[blockId]
);
Expand Down Expand Up @@ -684,21 +661,12 @@ contract TaikoL1Simulation is TaikoL1TestBase {
salt = uint256(keccak256(abi.encodePacked(gasLimit, salt)));

if (proposedIndex == 0) {
parentGasUsed[proposedIndex] = 0;
parentHashes[proposedIndex] = GENESIS_BLOCK_HASH;
} else {
parentGasUsed[proposedIndex] = gasUsed[proposedIndex - 1];
parentHashes[proposedIndex] = blockHashes[proposedIndex - 1];
}

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

uint24 txListSize = uint24(
pickRandomNumber(
Expand Down Expand Up @@ -750,8 +718,6 @@ contract TaikoL1Simulation is TaikoL1TestBase {
Bob,
metas[blockId],
parentHashes[blockId],
parentGasUsed[blockId],
gasUsed[blockId],
blockHashes[blockId],
signalRoots[blockId]
);
Expand Down
Loading

0 comments on commit 3357dad

Please sign in to comment.