Skip to content

Commit

Permalink
Hamdi/confsigs deposits (#62)
Browse files Browse the repository at this point in the history
1.) Deposits entirely represented on the rootchain and exited/finalized separately from transactions
2.) Removed BytesUtil
3.) txBytes include deposit nonce
4.) Confirm signatures are presents and tests provide good coverage. More will be added to cover require branches.
5.) General refactors in the contract and libraries

fixes #57
fixes #36
  • Loading branch information
hamdiallam authored and legengliu committed Sep 12, 2018
1 parent 42867f7 commit b000573
Show file tree
Hide file tree
Showing 22 changed files with 3,839 additions and 9,078 deletions.
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ Otherwise:
Install dependencies with:
``npm install``

**Note**: requires Solidity 0.4.18 and Truffle 4.1.0
**Note**: requires Solidity 0.4.24 and Truffle 4.1.13

Make sure the tests pass:
1. Start ganache-cli: ``ganache-cli -m=plasma_mvp``
2. Deploy contract: ``truffle migrate``
3. Run tests: ``truffle test``
2. Run tests: ``truffle test``

Create a branch that is named off the feature you are trying to implement. See these [guidelines](https://nvie.com/posts/a-successful-git-branching-model/)

Expand Down
36 changes: 15 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

We're implementing [Minimum Viable Plasma](https://ethresear.ch/t/minimal-viable-plasma/426)

This is the Rootchain contract repository for Blockchain @ Berkeley's Plasma team. This repository was originally forked from Omisego's [MVP](https://github.com/omisego/plasma-mvp), but has been significantly changed since.

**Note**: Our current implementation assumes the child chain uses Proof of Authority, but we plan to allow for multiple validators in the near future.
This is the Rootchain contract repository for Blockchain @ Berkeley's Plasma team.

## Overview
Plasma is a layer 2 scaling solution which conducts transaction processing off chain and allows for only merkle roots of each block to be reported to a root chain. This allows for users to benefit from off chain scaling while still relying on decentralized security.
Expand All @@ -18,30 +16,26 @@ The root contract of a Plasma child chain represents an intermediary who can res
## Root Contract Details
A transaction is encoded in the following form:

``[Blknum1, TxIndex1, Oindex1, Amount1, ConfirmSig1,``

``Blknum2, TxIndex2, Oindex2, Amount2, ConfirmSig2,``

``NewOwner, Denom1, NewOwner, Denom2, Fee]``


``submitBlock``: Validator(s) submits merkle root of the current block

``deposit``: Users will deposit onto the child chain by calling ``deposit()``. Deposits are stored into a priority queue and processed upon the next call of ``submitBlock()``. **Note**: If a validator decides to never call submitBlock again, users with pending deposits will still be able to withdraw their deposit

``startExit``: When a user decides to exit for any reason, they will call ``startExit()`` and their pending exit will be added to a priority queue that has a 1 week challenge period. This function requires a bond.

``challengeExit``: If any users notice an invalid withdrawal attempt, they may challenge this exit by providing the relevant information. If their challenge is successful, they will be awarded the bond associated with the exit attempt.
```
[Blknum1, TxIndex1, Oindex1, DepositNonce1, Amount1, ConfirmSig1
``finalizeExits``: This function will finalize any pending exits that have been in the priority queue for longer than a week. If the exit attempt has not been invalidated by a successful challenge then it will be eligible for withdrawal.
Blknum2, TxIndex2, Oindex2, DepositNonce2, Amount2, ConfirmSig2
``withdraw``: Allows users to withdraw any balance that avaliable after the successful processing of an exit.
NewOwner, Denom1, NewOwner, Denom2, Fee]
```

### Documentation

See our [documentation](https://github.com/FourthState/plasma-mvp-rootchain/blob/master/docs/rootchainFunctions.md)
See our [documentation](https://github.com/FourthState/plasma-mvp-rootchain/blob/master/docs/rootchainFunctions.md) for a more detailed description of the smart contract functions.

### Testing
1. ``git clone https://github.com/fourthstate/plasma-mvp-rootchain``
2. ``cd plasma-mvp-rootchain``
3. ``npm install``
4. ``npm install -g truffle ganache-cli`` // if not installed already
5. ``ganache-cli`` // run as a background process
5. ``npm test``

### Contributing

See our [contribution guidelines](https://github.com/FourthState/plasma-mvp-rootchain/blob/master/CONTRIBUTING.md)
See our [contribution guidelines](https://github.com/FourthState/plasma-mvp-rootchain/blob/master/CONTRIBUTING.md). Join our [Discord Server](https://discord.gg/yxZ29kR).
45 changes: 0 additions & 45 deletions contracts/libraries/ByteUtils.sol

This file was deleted.

106 changes: 53 additions & 53 deletions contracts/libraries/PriorityQueue.sol
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
pragma solidity ^0.4.24;

// external modules
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";

/**
* @title PriorityQueue
* @dev A priority queue implementation
*/

contract PriorityQueue is Ownable {
// This queue implementation expects the `heapList` to always have
// a zero'th element. This is for indexing reasons.
// For example: An empty `heapList` can be the array, [0].
library PriorityQueue {
using SafeMath for uint256;

/*
* Storage
*/
uint256[] heapList;
uint256 public currentSize;

constructor()
function insert(uint256[] storage heapList, uint256 k)
public
{
heapList = [0];
currentSize = 0;
heapList.push(k);
uint size = currentSize(heapList);
if (size > 1) {
percUp(heapList, size);
}
}

function insert(uint256 k)
function getMin(uint256[] storage heapList)
public
onlyOwner
view
returns (uint256)
{
heapList.push(k);
currentSize = currentSize.add(1);
percUp(currentSize);
require(currentSize(heapList) > 0, "empty queue");
return heapList[1];
}

function minChild(uint256 i)
function delMin(uint256[] storage heapList)
public
returns (uint256)
{
uint size = currentSize(heapList);
require(size > 0, "empty queue");

uint256 retVal = heapList[1];

heapList[1] = heapList[size];
delete heapList[size];
heapList.length = size; // not `size - 1` due to the unused first element
size = size.sub(1);

if (size > 1) {
percDown(heapList, 1);
}

return retVal;
}

function minChild(uint256[] storage heapList, uint256 i)
private
view
returns (uint256)
{
if (i.mul(2).add(1) > currentSize) {
uint size = currentSize(heapList);
if (i.mul(2).add(1) > size) {
return i.mul(2);
} else {
if (heapList[i.mul(2)] < heapList[i.mul(2).add(1)]) {
Expand All @@ -49,32 +66,7 @@ contract PriorityQueue is Ownable {
}
}

function getMin()
public
view
returns (uint256)
{
return heapList[1];
}

function delMin()
public
onlyOwner
returns (uint256)
{
require(currentSize > 0);
uint256 retVal = heapList[1];
heapList[1] = heapList[currentSize];
delete heapList[currentSize];
currentSize = currentSize.sub(1);
if (currentSize > 1) {
percDown(1);
}
heapList.length = heapList.length.sub(1);
return retVal;
}

function percUp(uint256 i)
function percUp(uint256[] storage heapList, uint256 i)
private
{
uint256 j = i;
Expand All @@ -86,18 +78,26 @@ contract PriorityQueue is Ownable {
if (i != j) heapList[i] = newVal;
}

function percDown(uint256 i)
function percDown(uint256[] storage heapList, uint256 i)
private
{
uint256 j = i;
uint256 newVal = heapList[i];
uint256 mc = minChild(i);
while (mc <= currentSize && newVal > heapList[mc]) {
uint256 mc = minChild(heapList, i);
uint256 size = currentSize(heapList);
while (mc <= size && newVal > heapList[mc]) {
heapList[i] = heapList[mc];
i = mc;
mc = minChild(i);
mc = minChild(heapList, i);
}
if (i != j) heapList[i] = newVal;
}

function currentSize(uint256[] storage heapList)
internal
view
returns (uint256)
{
return heapList.length.sub(1);
}
}
15 changes: 15 additions & 0 deletions contracts/libraries/PriorityQueue_Test.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity ^0.4.24;

import "./PriorityQueue.sol";

// Purpose of this contract is to forward calls to the library for testing
contract PriorityQueue_Test {
using PriorityQueue for uint256[];

uint256[] heapList = [0];

function insert(uint256 k) public { heapList.insert(k); }
function getMin() public view returns (uint256) { return heapList.getMin(); }
function delMin() public { heapList.delMin(); }
function currentSize() public view returns (uint256) { return heapList.currentSize(); }
}
Loading

0 comments on commit b000573

Please sign in to comment.