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

Siliev/block binding fix #1982

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 64 additions & 2 deletions contracts/generated/ManagementContract/ManagementContract.go

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions contracts/src/management/ManagementContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ contract ManagementContract is Initializable, OwnableUpgradeable {
MessageBus.IMessageBus public messageBus;
MerkleTreeMessageBus.IMerkleTreeMessageBus public merkleMessageBus;
mapping(bytes32 =>bool) public isWithdrawalSpent;
mapping(bytes32 =>bool) public isBundleSaved;

bytes32 public lastBatchHash;

Expand Down Expand Up @@ -102,6 +103,16 @@ contract ManagementContract is Initializable, OwnableUpgradeable {
}
}

function isBundleAvailable(bytes[] memory crossChainHashes) public view returns (bool) {
bytes32 bundleHash = bytes32(0);

for(uint256 i = 0; i < crossChainHashes.length; i++) {
bundleHash = keccak256(abi.encode(bundleHash, bytes32(crossChainHashes[i])));
}

return isBundleSaved[bundleHash];
}

function addCrossChainMessagesRoot(bytes32 _lastBatchHash, bytes32 blockHash, uint256 blockNum, bytes[] memory crossChainHashes, bytes calldata signature, uint256 rollupNumber, bytes32 forkID) external {
/* if (block.number > blockNum + 255) {
revert("Block binding too old");
Expand All @@ -120,9 +131,14 @@ contract ManagementContract is Initializable, OwnableUpgradeable {

lastBatchHash = _lastBatchHash;

bytes32 bundleHash = bytes32(0);

for(uint256 i = 0; i < crossChainHashes.length; i++) {
merkleMessageBus.addStateRoot(bytes32(crossChainHashes[i]), block.timestamp); //todo: change the activation time.
bundleHash = keccak256(abi.encode(bundleHash, bytes32(crossChainHashes[i])));
}

isBundleSaved[bundleHash] = true;
}

// TODO: ensure challenge period is added on top of block timestamp.
Expand Down
8 changes: 7 additions & 1 deletion go/enclave/nodetype/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ func ExportCrossChainData(ctx context.Context, storage storage.Storage, fromSeqN
return nil, errutil.ErrCrossChainBundleNoBatches
}

blockHash := canonicalBatches[len(canonicalBatches)-1].L1Proof
//todo - siliev - all those fetches need to be atomic
header, err := storage.FetchHeadBatchHeader(ctx)
if err != nil {
return nil, err
}

blockHash := header.L1Proof
batchHash := canonicalBatches[len(canonicalBatches)-1].Hash()

block, err := storage.FetchBlock(ctx, blockHash)
Expand Down
19 changes: 19 additions & 0 deletions go/host/l1/statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ func (c *crossChainStateMachine) PublishNextBundle() error {
return err
}

alreadyPublished, err := c.IsBundleAlreadyPublished(bundle)
if err != nil {
return err
}

if alreadyPublished {
c.currentRollup++
return nil
}

err = c.publisher.PublishCrossChainBundle(bundle, big.NewInt(0).SetUint64(data.Number), data.ForkUID)
if err != nil {
return err
Expand All @@ -136,6 +146,15 @@ func (c *crossChainStateMachine) PublishNextBundle() error {
return nil
}

func (c *crossChainStateMachine) IsBundleAlreadyPublished(bundle *common.ExtCrossChainBundle) (bool, error) {
managementContract, err := ManagementContract.NewManagementContract(*c.mgmtContractLib.GetContractAddr(), c.ethClient.EthClient())
if err != nil {
return false, err
}

return managementContract.IsBundleAvailable(&bind.CallOpts{}, bundle.CrossChainRootHashes)
}

// Synchronize - checks if there are any new rollups or forks and moves the tracking needle to the latest common ancestor.
func (c *crossChainStateMachine) Synchronize() error {
forkUID, _, _, err := c.publisher.GetBundleRangeFromManagementContract(big.NewInt(0).SetUint64(c.latestRollup.Number), c.latestRollup.ForkUID)
Expand Down
Loading