Skip to content

Commit

Permalink
Merge pull request #50 from FourthState/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AdityaSripal authored Jul 15, 2018
2 parents 88d44a1 + fbf1901 commit 6be0ef4
Show file tree
Hide file tree
Showing 38 changed files with 2,310 additions and 556 deletions.
389 changes: 348 additions & 41 deletions Gopkg.lock

Large diffs are not rendered by default.

28 changes: 12 additions & 16 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,31 @@

[[constraint]]
name = "github.com/cosmos/cosmos-sdk"
version = "0.17.2"
version = "0.21.0"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"

[[constraint]]
name = "github.com/tendermint/abci"
version = "0.10.3"

[[constraint]]
name = "github.com/tendermint/go-amino"
version = "0.9.9"

[[constraint]]
name = "github.com/tendermint/go-crypto"
version = "0.6.2"

[[constraint]]
name = "github.com/tendermint/tmlibs"
version = "0.8.3"
version = "0.10.1"

[[constraint]]
name = "github.com/ethereum/go-ethereum"
version = "1.8.8"

[[override]]
name = "github.com/tendermint/go-wire"
version = "0.7.3"
name = "github.com/tendermint/tendermint"
version = "=0.22.2"

[[override]]
name = "github.com/tendermint/iavl"
version = "=v0.9.2"

[[override]]
name = "github.com/golang/protobuf"
version = "=1.1.0"

[prune]
go-tests = true
Expand Down
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,45 @@ Branch | Tests | Coverage
develop | [![Build Status](https://travis-ci.org/FourthState/plasma-mvp-sidechain.svg?branch=develop)](https://travis-ci.org/FourthState/plasma-mvp-sidechain) | [![codecov](https://codecov.io/gh/FourthState/plasma-mvp-sidechain/branch/develop/graph/badge.svg)](https://codecov.io/gh/FourthState/plasma-mvp-sidechain)
master | [![Build Status](https://travis-ci.org/FourthState/plasma-mvp-sidechain.svg?branch=master)](https://travis-ci.org/FourthState/plasma-mvp-sidechain) | [![codecov](https://codecov.io/gh/FourthState/plasma-mvp-sidechain/branch/master/graph/badge.svg)](https://codecov.io/gh/FourthState/plasma-mvp-sidechain)

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

We have decided to modify our implementation by removing confirmation signatures as described in the [More Viable Plasma](https://ethresear.ch/t/more-viable-plasma/2160) construction.

**Note**: This sidechain is being constructed to be compatible with our [rootchain contract](https://github.com/FourthState/plasma-mvp-rootchain/master)

## Overview
As a layer 2 scaling solution, Plasma has two major components: verification and computation. Verification is handled by the rootchain contract which resolves any disputes and distributes funds accordingly.

Computation is handled off chain by a sidechain. This sidechain levarges the Cosmos SDK to create a scalable and flexible blockchain, that can maintain it's security through reporting merkle roots to the root chain. We will be using [Tendermint](https://github.com/tendermint/tendermint) for consensus on this blockchain.
Computation is handled off chain by a sidechain. This sidechain leverages the Cosmos SDK to create a scalable and flexible blockchain, that can maintain it's security through reporting merkle roots to the root chain. We will be using [Tendermint](https://github.com/tendermint/tendermint) for consensus on this blockchain.

We are using a UTXO model for this blockchain. This allows us to do secure and compact proofs when interacting with the rootchain contract.

## Starting a sidechain

In order to run a sidechain with tendermint consensus and a client to form transaction, a plasma node and light client will need to be initialized.

**Note**: The following assumes you have [golang](https://golang.org/) properly setup and all dependecies have already been installed. See [Contribution Guidelines](https://github.com/FourthState/plasma-mvp-sidechain/blob/master/CONTRIBUTING.md) for more information.

Plasma Node:

- Navigate to `client/plasmad/` directory
- Run `go install` via command line

The plasma node (plasmad) is now installed and can be called from any directory with `plasmad`

Run `plasmad init` via command line to start an instance of a plasma node with a connection to a tendermint validator.

Run `plasmad start` via command line to begin running the plasma node. You should see empty blocks being proposed and committed.

Plasma Light Client:

- Navigate to `client/plasmacli/` directory
- Run `go install` via command line

Use `plasmacli` to run any of the commands for this light client

The light client uses the Ethereum keystore to create and store passphrase encrypted keys in `$HOME/.plasmacli/keys/`

### Plasma Architecture
See our [research repository](https://github.com/FourthState/plasma-research) for architectural explanations of our Plasma implementation.

Expand Down
87 changes: 64 additions & 23 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package app

import (
auth "github.com/FourthState/plasma-mvp-sidechain/auth"
"encoding/json"
auth "github.com/FourthState/plasma-mvp-sidechain/auth"
plasmaDB "github.com/FourthState/plasma-mvp-sidechain/db"
types "github.com/FourthState/plasma-mvp-sidechain/types"
"github.com/FourthState/plasma-mvp-sidechain/types"
abci "github.com/tendermint/tendermint/abci/types"
"io"

bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types"
rlp "github.com/ethereum/go-ethereum/rlp"
"github.com/tendermint/go-amino"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
//rlp "github.com/ethereum/go-ethereum/rlp"
crypto "github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
)

const (
Expand All @@ -35,19 +38,20 @@ type ChildChain struct {

// Manage addition and deletion of utxo's
utxoMapper types.UTXOMapper

txHash []byte
}

func NewChildChain(logger log.Logger, db dbm.DB) *ChildChain {
func NewChildChain(logger log.Logger, db dbm.DB, traceStore io.Writer) *ChildChain {
cdc := MakeCodec()

bapp := bam.NewBaseApp(appName, cdc, logger, db)
bapp.SetCommitMultiStoreTracer(traceStore)

var app = &ChildChain{
BaseApp: bam.NewBaseApp(appName, cdc, logger, db),
BaseApp: bapp,
cdc: cdc,
txIndex: new(uint16),
feeAmount: new(uint64),
capKeyMainStore: sdk.NewKVStoreKey("main"),
txHash: nil,
}

// define the utxoMapper
Expand All @@ -59,13 +63,16 @@ func NewChildChain(logger log.Logger, db dbm.DB) *ChildChain {
// UTXOKeeper to adjust spending and recieving of utxo's
UTXOKeeper := plasmaDB.NewUTXOKeeper(app.utxoMapper)
app.Router().
AddRoute("txs", auth.NewHandler(UTXOKeeper, app.txIndex))
AddRoute("spend", auth.NewHandler(UTXOKeeper, app.txIndex))

// set the BaseApp txDecoder to use txDecoder with RLP
app.SetTxDecoder(app.txDecoder)

app.MountStoresIAVL(app.capKeyMainStore)

app.SetInitChainer(app.initChainer)
app.SetEndBlocker(app.endBlocker)

// NOTE: type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
app.SetAnteHandler(auth.NewAnteHandler(app.utxoMapper, app.txIndex, app.feeAmount))

Expand All @@ -77,26 +84,60 @@ func NewChildChain(logger log.Logger, db dbm.DB) *ChildChain {
return app
}

func (app *ChildChain) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
stateJSON := req.AppStateBytes
// TODO is this now the whole genesis file?

var genesisState GenesisState
err := app.cdc.UnmarshalJSON(stateJSON, &genesisState)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}

// load the accounts
for _, gutxo := range genesisState.UTXOs {
utxo := ToUTXO(gutxo)
app.utxoMapper.AddUTXO(ctx, utxo)
}

// load the initial stake information
return abci.ResponseInitChain{}
}

func (app *ChildChain) endBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
// reset txIndex and fee
*app.txIndex = 0
*app.feeAmount = 0

return abci.ResponseEndBlock{}
}

// RLP decodes the txBytes to a BaseTx
func (app *ChildChain) txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) {
// TODO: implement method with RLP
var tx = types.BaseTx{}
// BaseTx is struct for Msg wrapped with authentication data
err := app.cdc.UnmarshalBinary(txBytes, &tx)

err := rlp.DecodeBytes(txBytes, &tx)
if err != nil {
return nil, sdk.ErrTxDecode("")
return nil, sdk.ErrTxDecode(err.Error())
}
return tx, nil
}

func (app *ChildChain) endBlocker(ctx sdk.Context, req abci.RequestEndBlock) {
header := ctx.BlockHeader()
app.txHash = header.GetDataHash()
}

func MakeCodec() *amino.Codec {
cdc := amino.NewCodec()
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
cdc.RegisterConcrete(PlasmaGenTx{}, "app/PlasmaGenTx", nil)
types.RegisterAmino(cdc)
crypto.RegisterAmino(cdc)
return cdc
}

func (app *ChildChain) ExportAppStateJSON() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
// TODO: Implement
// Currently non-functional, just enough to compile
tx := types.BaseTx{}
appState, err = app.cdc.MarshalJSONIndent(tx, "", "\t")
validators = []tmtypes.GenesisValidator{}
return appState, validators, err
}
Loading

0 comments on commit 6be0ef4

Please sign in to comment.