Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
WIP More contract work, c2t
Browse files Browse the repository at this point in the history
  • Loading branch information
jacque006 committed Sep 14, 2021
1 parent 2ba835b commit 25bf336
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 17 deletions.
8 changes: 8 additions & 0 deletions contracts/Create2Transfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract Create2Transfer {
* */
function processCreate2TransferCommit(
bytes32 stateRoot,
bytes32[] memory stateHashes,
uint256 maxTxSize,
uint256 feeReceiver,
bytes memory txs,
Expand All @@ -32,6 +33,11 @@ contract Create2Transfer {
return (stateRoot, Types.Result.BadCompression);
uint256 size = txs.create2TransferSize();
if (size > maxTxSize) return (stateRoot, Types.Result.TooManyTx);
// TODO This modulo check could be wrong if hashes is 2x, 3x etc tx size. Rethink.
require(
stateHashes.length % size == 1,
"stateHashes size mismatch with txs"
);

uint256 fees = 0;
// tokenID should be the same for all states in this commit
Expand All @@ -42,6 +48,7 @@ contract Create2Transfer {
_tx = txs.create2TransferDecode(i);
(stateRoot, result) = Transition.processCreate2Transfer(
stateRoot,
stateHashes[i],
_tx,
tokenID,
proofs[i * 2],
Expand All @@ -53,6 +60,7 @@ contract Create2Transfer {
}
(stateRoot, result) = Transition.processReceiver(
stateRoot,
stateHashes[size + 1],
feeReceiver,
tokenID,
fees,
Expand Down
10 changes: 8 additions & 2 deletions contracts/Transfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ contract Transfer {
* */
function processTransferCommit(
bytes32 stateRoot,
bytes32[2][] memory stateHashes,
uint256 maxTxSize,
uint256 feeReceiver,
bytes memory txs,
Expand All @@ -34,6 +35,10 @@ contract Transfer {

uint256 size = txs.transferSize();
if (size > maxTxSize) return (stateRoot, Types.Result.TooManyTx);
require(
stateHashes.length % size == size + 1,
"stateHashes size mismatch with txs"
);

uint256 fees = 0;
// tokenID should be the same for all states in this commit
Expand All @@ -44,8 +49,8 @@ contract Transfer {
_tx = txs.transferDecode(i);
(stateRoot, result) = Transition.processTransfer(
stateRoot,
stateHashFrom,
stateHashTo,
stateHashes[i * 2][0], // stateHashFrom
stateHashes[i * 2 + 1][1], // stateHashTo
_tx,
tokenID,
proofs[i * 2],
Expand All @@ -57,6 +62,7 @@ contract Transfer {
}
(stateRoot, result) = Transition.processReceiver(
stateRoot,
stateHashes[size * 2][0],
feeReceiver,
tokenID,
fees,
Expand Down
2 changes: 2 additions & 0 deletions contracts/client/FrontendCreate2Transfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ contract FrontendCreate2Transfer {

function process(
bytes32 stateRoot,
bytes32 stateHashFrom,
bytes memory encodedTx,
uint256 tokenID,
Types.StateMerkleProof memory from,
Expand All @@ -180,6 +181,7 @@ contract FrontendCreate2Transfer {
return
Transition.processCreate2Transfer(
stateRoot,
stateHashFrom,
_tx,
tokenID,
from,
Expand Down
13 changes: 12 additions & 1 deletion contracts/client/FrontendTransfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ contract FrontendTransfer {

function process(
bytes32 stateRoot,
bytes32 stateHashFrom,
bytes32 stateHashTo,
bytes memory encodedTx,
uint256 tokenID,
Types.StateMerkleProof memory from,
Expand All @@ -146,7 +148,16 @@ contract FrontendTransfer {
offchainTx.amount,
offchainTx.fee
);
return Transition.processTransfer(stateRoot, _tx, tokenID, from, to);
return
Transition.processTransfer(
stateRoot,
stateHashFrom,
stateHashTo,
_tx,
tokenID,
from,
to
);
}

function checkSignature(
Expand Down
30 changes: 22 additions & 8 deletions contracts/libs/Transition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ library Transition {

function processMassMigration(
bytes32 stateRoot,
bytes32 stateHashFrom,
Tx.MassMigration memory _tx,
uint256 tokenID,
Types.StateMerkleProof memory from
Expand All @@ -60,6 +61,7 @@ library Transition {
{
(newRoot, result) = processSender(
stateRoot,
stateHashFrom,
_tx.fromIndex,
tokenID,
_tx.amount,
Expand All @@ -74,13 +76,15 @@ library Transition {

function processCreate2Transfer(
bytes32 stateRoot,
bytes32 stateHashFrom,
Tx.Create2Transfer memory _tx,
uint256 tokenID,
Types.StateMerkleProof memory from,
Types.StateMerkleProof memory to
) internal pure returns (bytes32 newRoot, Types.Result result) {
(newRoot, result) = processSender(
stateRoot,
stateHashFrom,
_tx.fromIndex,
tokenID,
_tx.amount,
Expand Down Expand Up @@ -136,12 +140,17 @@ library Transition {
stateHash,
senderStateIndex,
proof.witness
)
, "Transition: Sender does not exist");
),
"Transition: Sender does not exist"
);
// check if sender is empty
if (stateHash == Types.ZERO_BYTES32) return Types.Result.BadFromIndex;
if (stateHash == Types.ZERO_BYTES32)
return (bytes32(0), Types.Result.BadFromIndex);
// sender is non-empty, the disputer now has to justify the preimage of the state
require(stateHash == keccak256(proof.state.encode()), "stateHash mismatch");
require(
stateHash == keccak256(proof.state.encode()),
"stateHash mismatch"
);

(Types.UserState memory newSender, Types.Result result) =
validateAndApplySender(tokenID, amount, fee, proof.state);
Expand Down Expand Up @@ -169,12 +178,17 @@ library Transition {
stateHash,
receiverStateIndex,
proof.witness
)
, "Transition: receiver does not exist");
),
"Transition: receiver does not exist"
);
// check if receiver is empty
if (stateHash == Types.ZERO_BYTES32) return Types.Result.BadFromIndex;
if (stateHash == Types.ZERO_BYTES32)
return (bytes32(0), Types.Result.BadFromIndex);
// receiver is non-empty, the disputer now has to justify the preimage of the state
require(stateHash == keccak256(proof.state.encode()), "stateHash mismatch");
require(
stateHash == keccak256(proof.state.encode()),
"stateHash mismatch"
);

(Types.UserState memory newReceiver, Types.Result result) =
validateAndApplyReceiver(tokenID, amount, proof.state);
Expand Down
15 changes: 10 additions & 5 deletions contracts/rollup/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ contract Rollup is BatchManager, EIP712, IEIP712 {
string public constant DOMAIN_NAME = "Hubble";
string public constant DOMAIN_VERSION = "1";

/**
/**
* @dev If this is not being externally consumed,
* it can be removed and refs replaced with Types.ZERO_BYTES32
*/
bytes32 public constant ZERO_BYTES32 = Types.ZERO_BYTES32;
bytes32 public immutable ZERO_BYTES32;

// External contracts
BLSAccountRegistry public immutable accountRegistry;
Expand Down Expand Up @@ -72,6 +72,8 @@ contract Rollup is BatchManager, EIP712, IEIP712 {
)
EIP712(DOMAIN_NAME, DOMAIN_VERSION)
{
ZERO_BYTES32 = Types.ZERO_BYTES32;

accountRegistry = _accountRegistry;
transfer = _transfer;
massMigration = _massMigration;
Expand Down Expand Up @@ -369,8 +371,8 @@ contract Rollup is BatchManager, EIP712, IEIP712 {
uint256 batchID,
Types.CommitmentInclusionProof memory previous,
Types.TransferCommitmentInclusionProof memory target,
Types.StateMerkleProof[] memory proofs

Types.StateMerkleProof[] memory proofs,
bytes32[2][] memory stateHashes
)
public
isDisputable(batchID)
Expand All @@ -384,6 +386,7 @@ contract Rollup is BatchManager, EIP712, IEIP712 {
(bytes32 processedStateRoot, Types.Result result) =
transfer.processTransferCommit(
previous.commitment.stateRoot,
stateHashes,
paramMaxTxsPerCommit,
target.commitment.body.feeReceiver,
target.commitment.body.txs,
Expand Down Expand Up @@ -429,7 +432,8 @@ contract Rollup is BatchManager, EIP712, IEIP712 {
uint256 batchID,
Types.CommitmentInclusionProof memory previous,
Types.TransferCommitmentInclusionProof memory target,
Types.StateMerkleProof[] memory proofs
Types.StateMerkleProof[] memory proofs,
bytes32[] memory stateHashes
)
public
isDisputable(batchID)
Expand All @@ -443,6 +447,7 @@ contract Rollup is BatchManager, EIP712, IEIP712 {
(bytes32 processedStateRoot, Types.Result result) =
create2Transfer.processCreate2TransferCommit(
previous.commitment.stateRoot,
stateHashes,
paramMaxTxsPerCommit,
target.commitment.body.feeReceiver,
target.commitment.body.txs,
Expand Down
4 changes: 4 additions & 0 deletions contracts/test/TestCreate2Transfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract TestCreate2Transfer is Create2Transfer {

function testProcessCreate2Transfer(
bytes32 _balanceRoot,
bytes32 stateHashFrom,
Tx.Create2Transfer memory _tx,
uint256 tokenID,
Types.StateMerkleProof memory from,
Expand All @@ -39,6 +40,7 @@ contract TestCreate2Transfer is Create2Transfer {
return
Transition.processCreate2Transfer(
_balanceRoot,
stateHashFrom,
_tx,
tokenID,
from,
Expand All @@ -48,6 +50,7 @@ contract TestCreate2Transfer is Create2Transfer {

function testProcessCreate2TransferCommit(
bytes32 stateRoot,
bytes32[] memory stateHashes,
uint256 maxTxSize,
uint256 feeReceiver,
bytes memory txs,
Expand All @@ -57,6 +60,7 @@ contract TestCreate2Transfer is Create2Transfer {
uint256 operationCost = gasleft();
(newRoot, ) = processCreate2TransferCommit(
stateRoot,
stateHashes,
maxTxSize,
feeReceiver,
txs,
Expand Down
15 changes: 14 additions & 1 deletion contracts/test/TestTransfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,28 @@ contract TestTransfer is Transfer {

function testProcessTransfer(
bytes32 _balanceRoot,
bytes32 stateHashFrom,
bytes32 stateHashTo,
Tx.Transfer memory _tx,
uint256 tokenID,
Types.StateMerkleProof memory from,
Types.StateMerkleProof memory to
) public pure returns (bytes32, Types.Result) {
return Transition.processTransfer(_balanceRoot, _tx, tokenID, from, to);
return
Transition.processTransfer(
_balanceRoot,
stateHashFrom,
stateHashTo,
_tx,
tokenID,
from,
to
);
}

function testProcessTransferCommit(
bytes32 stateRoot,
bytes32[2][] memory stateHashes,
uint256 maxTxSize,
uint256 feeReceiver,
bytes memory txs,
Expand All @@ -50,6 +62,7 @@ contract TestTransfer is Transfer {
uint256 operationCost = gasleft();
(newRoot, ) = processTransferCommit(
stateRoot,
stateHashes,
maxTxSize,
feeReceiver,
txs,
Expand Down

0 comments on commit 25bf336

Please sign in to comment.