-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(protocol): support delayed forced inclusion of txs (#18883)
Co-authored-by: dantaik <dantaik@users.noreply.github.com> Co-authored-by: xiaodino <ruby@taiko.xyz> Co-authored-by: xiaodino <xiaodino@users.noreply.github.com> Co-authored-by: David <david@taiko.xyz> Co-authored-by: davidtaikocha <davidtaikocha@users.noreply.github.com> Co-authored-by: Anshu Jalan <anshujalan206@gmail.com> Co-authored-by: AnshuJalan <AnshuJalan@users.noreply.github.com> Co-authored-by: davidtaikocha <104078303+davidtaikocha@users.noreply.github.com> Co-authored-by: smtmfft <99081233+smtmfft@users.noreply.github.com> Co-authored-by: smtmfft <smtmfft@users.noreply.github.com> Co-authored-by: maskpp <maskpp266@gmail.com> Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com> Co-authored-by: Gavin Yu <gavin@taiko.xyz> Co-authored-by: YoGhurt111 <23009382+YoGhurt111@users.noreply.github.com> Co-authored-by: Jeffery Walsh <cyberhorsey@gmail.com>
- Loading branch information
1 parent
cdeadc0
commit a244be2
Showing
21 changed files
with
820 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/protocol/contracts/layer1/based/IProposeBatch.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "./ITaikoInbox.sol"; | ||
|
||
/// @title IProposeBatch | ||
/// @notice This interface defines the proposeBatch function that is also part of the ITaikoInbox | ||
/// interface. | ||
/// @custom:security-contact security@taiko.xyz | ||
interface IProposeBatch { | ||
/// @notice Proposes a batch of blocks. | ||
/// @param _params ABI-encoded parameters. | ||
/// @param _txList The transaction list in calldata. If the txList is empty, blob will be used | ||
/// for data availability. | ||
/// @return info_ The info of the proposed batch. | ||
/// @return meta_ The mmetadata of the proposed batch. | ||
function proposeBatch( | ||
bytes calldata _params, | ||
bytes calldata _txList | ||
) | ||
external | ||
returns (ITaikoInbox.BatchInfo memory info_, ITaikoInbox.BatchMetadata memory meta_); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/protocol/contracts/layer1/forced-inclusion/ForcedInclusionStore.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "src/shared/common/EssentialContract.sol"; | ||
import "src/shared/libs/LibMath.sol"; | ||
import "src/shared/libs/LibAddress.sol"; | ||
import "src/shared/libs/LibStrings.sol"; | ||
import "src/layer1/based/ITaikoInbox.sol"; | ||
import "./IForcedInclusionStore.sol"; | ||
|
||
/// @title ForcedInclusionStore | ||
/// @dev A contract for storing and managing forced inclusion requests. Forced inclusions allow | ||
/// users to pay a fee to ensure their transactions are included in a block. The contract maintains | ||
/// a FIFO queue of inclusion requests. | ||
/// @custom:security-contact | ||
contract ForcedInclusionStore is EssentialContract, IForcedInclusionStore { | ||
using LibAddress for address; | ||
using LibMath for uint256; | ||
|
||
uint8 public immutable inclusionDelay; // measured in the number of batches | ||
uint64 public immutable feeInGwei; | ||
|
||
mapping(uint256 id => ForcedInclusion inclusion) public queue; // slot 1 | ||
uint64 public head; // slot 2 | ||
uint64 public tail; | ||
uint64 public lastProcessedAtBatchId; | ||
uint64 private __reserved1; | ||
|
||
uint256[48] private __gap; | ||
|
||
constructor( | ||
address _resolver, | ||
uint8 _inclusionDelay, | ||
uint64 _feeInGwei | ||
) | ||
EssentialContract(_resolver) | ||
{ | ||
require(_inclusionDelay != 0, InvalidParams()); | ||
require(_feeInGwei != 0, InvalidParams()); | ||
|
||
inclusionDelay = _inclusionDelay; | ||
feeInGwei = _feeInGwei; | ||
} | ||
|
||
function init(address _owner) external initializer { | ||
__Essential_init(_owner); | ||
} | ||
|
||
function storeForcedInclusion( | ||
uint8 blobIndex, | ||
uint32 blobByteOffset, | ||
uint32 blobByteSize | ||
) | ||
external | ||
payable | ||
nonReentrant | ||
{ | ||
bytes32 blobHash = _blobHash(blobIndex); | ||
require(blobHash != bytes32(0), BlobNotFound()); | ||
require(msg.value == feeInGwei * 1 gwei, IncorrectFee()); | ||
|
||
ForcedInclusion memory inclusion = ForcedInclusion({ | ||
blobHash: blobHash, | ||
feeInGwei: uint64(msg.value / 1 gwei), | ||
createdAtBatchId: _nextBatchId(), | ||
blobByteOffset: blobByteOffset, | ||
blobByteSize: blobByteSize | ||
}); | ||
|
||
queue[tail++] = inclusion; | ||
|
||
emit ForcedInclusionStored(inclusion); | ||
} | ||
|
||
function consumeOldestForcedInclusion(address _feeRecipient) | ||
external | ||
onlyFromNamed(LibStrings.B_TAIKO_WRAPPER) | ||
nonReentrant | ||
returns (ForcedInclusion memory inclusion_) | ||
{ | ||
// we only need to check the first one, since it will be the oldest. | ||
ForcedInclusion storage inclusion = queue[head]; | ||
require(inclusion.createdAtBatchId != 0, NoForcedInclusionFound()); | ||
|
||
inclusion_ = inclusion; | ||
|
||
lastProcessedAtBatchId = _nextBatchId(); | ||
|
||
unchecked { | ||
delete queue[head++]; | ||
_feeRecipient.sendEtherAndVerify(inclusion_.feeInGwei * 1 gwei); | ||
} | ||
emit ForcedInclusionConsumed(inclusion_); | ||
} | ||
|
||
function getForcedInclusion(uint256 index) external view returns (ForcedInclusion memory) { | ||
require(index >= head, InvalidIndex()); | ||
require(index < tail, InvalidIndex()); | ||
return queue[index]; | ||
} | ||
|
||
function getOldestForcedInclusionDeadline() public view returns (uint256) { | ||
if (head == tail) return type(uint256).max; | ||
|
||
ForcedInclusion storage inclusion = queue[head]; | ||
if (inclusion.createdAtBatchId == 0) return type(uint256).max; | ||
|
||
unchecked { | ||
return uint256(lastProcessedAtBatchId).max(inclusion.createdAtBatchId) + inclusionDelay; | ||
} | ||
} | ||
|
||
function isOldestForcedInclusionDue() external view returns (bool) { | ||
uint256 deadline = getOldestForcedInclusionDeadline(); | ||
return deadline != type(uint256).max && _nextBatchId() >= deadline; | ||
} | ||
|
||
// @dev Override this function for easier testing blobs | ||
function _blobHash(uint8 blobIndex) internal view virtual returns (bytes32) { | ||
return blobhash(blobIndex); | ||
} | ||
|
||
function _nextBatchId() private view returns (uint64) { | ||
return ITaikoInbox(resolve(LibStrings.B_TAIKO, false)).getStats2().numBatches; | ||
} | ||
} |
Oops, something went wrong.