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

parent and child insurance stubs #272

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions src/insurance/Child.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {AddressAliasHelper} from "../libraries/AddressAliasHelper.sol";

// assumes parent and child chain uses ETH for fees
// EVERY FUNCTION MUST NEVER REVERT WHEN CALLED BY PARENT IN SEQUENCE, OTHERWISE QUEUE IS STUCK
contract Child {
address public immutable parentChainAddr;
address public immutable sequencerAddr;
uint256 public sequenceNumber;
uint256 public amtSatisfied;

constructor(address _parentChainAddr, address _sequencerAddr) {
parentChainAddr = _parentChainAddr;
sequencerAddr = _sequencerAddr;
}

modifier onlyInSequenceFromParent(uint256 seqNum) {
require(seqNum == sequenceNumber, "invalid sequence number");
require(msg.sender == AddressAliasHelper.applyL1ToL2Alias(parentChainAddr), "only parent chain contract can call");
sequenceNumber++;
_;
}

function deposit(uint256 seqNum) public payable onlyInSequenceFromParent(seqNum) {
// no need to do anything here, just receiving ETH
}

function withdraw(uint256 seqNum, uint256 amount) public onlyInSequenceFromParent(seqNum) {
amount = amount < address(this).balance ? amount : address(this).balance;
(bool success,) = payable(sequencerAddr).call{value: amount}("");
require(success, "withdraw failed");
}

// If (blocknum, blockhash) is in the history of Chain X, then add amount to S. Otherwise pay out amount to beneficiaryAddr.
// will revert if arb block num < blockNum
function commit(uint256 seqNum, address beneficiary, uint256 amount, uint256 blockNum, bytes32 blockHash) external payable onlyInSequenceFromParent(seqNum) {
// revert if arb block num < blockNum
// pay msg.value to sequencer
// check if blockHash is part of history
// if yes: increment amtSatisfied (S)
// if no: pay out amount to beneficiary. do not revert on failure, because a contract could DoS
}
}
51 changes: 51 additions & 0 deletions src/insurance/Parent.sol
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the assumption that the signedCommitment in buy also encodes the values being committed to alongside the commitment + signature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, just pushed up a commit that includes those. the entire struct SequencerCommitment is signed over

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

// assumes parent and child chain uses ETH for fees
contract Parent {
struct SequencerCommitment {
uint256 blockNum;
bytes32 blockHash;
uint256 pricePerEthWad;
}

address public immutable sequencer;

// D
uint256 public depositedAmount;

Check failure

Code scanning / Slither

Uninitialized state variables High

// I - insurance sold
uint256 public insuranceSold;

// sequence number sigma
uint256 public sequenceNumber;

constructor(address _sequencer) {
sequencer = _sequencer;
}

modifier onlySequencer() {
require(msg.sender == sequencer, "only sequencer can call");
_;
}

function deposit() public payable onlySequencer {
// increment depositedAmount by value
// create a retryable to hit the child contract deposit function
// increment seqNum
}

function withdraw(uint256 amount) public onlySequencer {
// decrement depositedAmount by amount
// create a retryable to hit the child contract withdraw function
// increment seqNum
}

function buy(uint256 amount, uint256 minSatisfied, address beneficiary, bytes memory signedCommitment) public payable {
// verify the signed commitment
// require depositAmount - insuranceSold + minSatisfied >= amount
// require msg.value == price*amount
// increment insuranceSold by amount
// create a retryable to hit the child contract settle function, sending msg.value
// increment seqNum
}
}
Loading