Skip to content

Commit

Permalink
Siliev/block binding fix (#1982)
Browse files Browse the repository at this point in the history
* Disabled block binding.

* Disjoint binding to latest block hash.

* Dump progress.

* Prevention of republish attempts.

---------

Co-authored-by: StefanIliev545 <[email protected]>
  • Loading branch information
StefanIliev545 and StefanIliev545 authored Jul 8, 2024
1 parent add7e32 commit 3b75a25
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
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

0 comments on commit 3b75a25

Please sign in to comment.