Skip to content

Commit

Permalink
feat(protocol): make gasUsed of L2 blocks available on L1 (#13440)
Browse files Browse the repository at this point in the history
Co-authored-by: David <david@taiko.xyz>
  • Loading branch information
dantaik and davidtaikocha authored Mar 24, 2023
1 parent 6138b1f commit 4ddbd9b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ library TaikoData {
bytes32 blockHash;
bytes32 signalRoot;
address prover;
uint64 gasUsed;
}

struct ForkChoice {
Expand Down
9 changes: 5 additions & 4 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,25 @@ library LibProving {
false
);

bytes32[9] memory inputs;
bytes32[10] memory inputs;
inputs[0] = bytes32(uint256(uint160(l1SignalService)));
inputs[1] = bytes32(uint256(uint160(l2SignalService)));
inputs[2] = bytes32(uint256(uint160(taikoL2)));
inputs[3] = evidence.parentHash;
inputs[4] = evidence.blockHash;
inputs[5] = evidence.signalRoot;
inputs[6] = bytes32(uint256(uint160(evidence.prover)));
inputs[7] = blk.metaHash;
inputs[7] = bytes32(uint256(evidence.gasUsed)); // TODO(daniel): document this
inputs[8] = blk.metaHash;

// Circuits shall use this value to check anchor gas limit.
// Note that this value is not necessary and can be hard-coded
// in to the circuit code, but if we upgrade the protocol
// and the gas limit changes, then having it here may be handy.
inputs[8] = bytes32(config.anchorTxGasLimit);
inputs[9] = bytes32(config.anchorTxGasLimit);

assembly {
instance := keccak256(inputs, mul(32, 9))
instance := keccak256(inputs, mul(32, 10))
}
}

Expand Down
15 changes: 10 additions & 5 deletions packages/protocol/test2/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ contract TaikoL1Test is TaikoL1TestBase {
_depositTaikoToken(Carol, 1E6, 100);

bytes32 parentHash = GENESIS_BLOCK_HASH;
uint64 gasUsed = 1000000;

for (
uint256 blockId = 1;
Expand All @@ -92,7 +93,7 @@ contract TaikoL1Test is TaikoL1TestBase {

bytes32 blockHash = bytes32(1E10 + blockId);
bytes32 signalRoot = bytes32(1E9 + blockId);
proveBlock(Bob, meta, parentHash, blockHash, signalRoot);
proveBlock(Bob, gasUsed, meta, parentHash, blockHash, signalRoot);
verifyBlock(Carol, 1);
parentHash = blockHash;
}
Expand All @@ -105,6 +106,7 @@ contract TaikoL1Test is TaikoL1TestBase {
_depositTaikoToken(Alice, 1000, 1000);

bytes32 parentHash = GENESIS_BLOCK_HASH;
uint64 gasUsed = 1000000;

for (uint256 blockId = 1; blockId <= 2; blockId++) {
printVariables("before propose");
Expand All @@ -113,7 +115,7 @@ contract TaikoL1Test is TaikoL1TestBase {

bytes32 blockHash = bytes32(1E10 + blockId);
bytes32 signalRoot = bytes32(1E9 + blockId);
proveBlock(Alice, meta, parentHash, blockHash, signalRoot);
proveBlock(Alice, gasUsed, meta, parentHash, blockHash, signalRoot);
verifyBlock(Alice, 2);
parentHash = blockHash;
}
Expand All @@ -125,6 +127,7 @@ contract TaikoL1Test is TaikoL1TestBase {
_depositTaikoToken(Alice, 1E6, 100);

bytes32 parentHash = GENESIS_BLOCK_HASH;
uint64 gasUsed = 1000000;

for (
uint256 blockId = 1;
Expand All @@ -137,7 +140,7 @@ contract TaikoL1Test is TaikoL1TestBase {

bytes32 blockHash = bytes32(1E10 + blockId);
bytes32 signalRoot = bytes32(1E9 + blockId);
proveBlock(Alice, meta, parentHash, blockHash, signalRoot);
proveBlock(Alice, gasUsed, meta, parentHash, blockHash, signalRoot);
parentHash = blockHash;
}
verifyBlock(Alice, conf.maxNumProposedBlocks - 2);
Expand All @@ -153,6 +156,7 @@ contract TaikoL1Test is TaikoL1TestBase {
_depositTaikoToken(Carol, 1E6, 100);

bytes32 parentHash = GENESIS_BLOCK_HASH;
uint64 gasUsed = 1000000;

for (
uint256 blockId = 1;
Expand All @@ -165,7 +169,7 @@ contract TaikoL1Test is TaikoL1TestBase {

bytes32 blockHash = bytes32(1E10 + blockId);
bytes32 signalRoot = bytes32(1E9 + blockId);
proveBlock(Bob, meta, parentHash, blockHash, signalRoot);
proveBlock(Bob, gasUsed, meta, parentHash, blockHash, signalRoot);
verifyBlock(Carol, 1);
mine(blockId);
parentHash = blockHash;
Expand All @@ -181,6 +185,7 @@ contract TaikoL1Test is TaikoL1TestBase {
_depositTaikoToken(Carol, 1E6, 100);

bytes32 parentHash = GENESIS_BLOCK_HASH;
uint64 gasUsed = 1000000;

uint256 total = conf.maxNumProposedBlocks * 10;

Expand All @@ -191,7 +196,7 @@ contract TaikoL1Test is TaikoL1TestBase {

bytes32 blockHash = bytes32(1E10 + blockId);
bytes32 signalRoot = bytes32(1E9 + blockId);
proveBlock(Bob, meta, parentHash, blockHash, signalRoot);
proveBlock(Bob, gasUsed, meta, parentHash, blockHash, signalRoot);
verifyBlock(Carol, 1);
mine(total + 1 - blockId);
parentHash = blockHash;
Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/test2/TaikoL1TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ abstract contract TaikoL1TestBase is Test {

function proveBlock(
address prover,
uint64 gasUsed,
TaikoData.BlockMetadata memory meta,
bytes32 parentHash,
bytes32 blockHash,
Expand All @@ -126,7 +127,8 @@ abstract contract TaikoL1TestBase is Test {
parentHash: parentHash,
blockHash: blockHash,
signalRoot: signalRoot,
prover: prover
prover: prover,
gasUsed: gasUsed
});

vm.prank(prover, prover);
Expand Down

0 comments on commit 4ddbd9b

Please sign in to comment.