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

save confirmations sigs into app state #102

Merged
merged 48 commits into from
Dec 20, 2018
Merged
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
5c9d180
starter
hamdiallam Nov 21, 2018
66de47a
testing client with ganache
hamdiallam Nov 23, 2018
48b5e14
Merge commit '1ed2e4f899f687cd0cbc28af87060fa012c4df6b' as 'contracts'
hamdiallam Nov 23, 2018
1ed2e4f
Squashed 'contracts/' content from commit ab4fd0e
hamdiallam Nov 23, 2018
5ae1fc7
contract wrappers and bindings
hamdiallam Nov 24, 2018
afa2634
subscribed to deposits/exists. Some tests
hamdiallam Nov 27, 2018
3ef8f74
added wrappers to the contracts folder
hamdiallam Nov 27, 2018
472844f
started hooking connection to app
colin-axner Dec 3, 2018
528103f
merged develop
hamdiallam Dec 3, 2018
5e12c15
Merge commit '247171637576da849a5a80ad369c91cb8d0e3a05' into rpc
hamdiallam Dec 3, 2018
2471716
Squashed 'contracts/' changes from ab4fd0e..dc28e6a
hamdiallam Dec 3, 2018
ab70ed4
MOAR PROGRESS
AdityaSripal Dec 3, 2018
c7ca091
papusa
colin-axner Dec 3, 2018
a052fd2
gopkg fix
colin-axner Dec 3, 2018
eac6e49
added clean to npm script. temp removed wrappers
hamdiallam Dec 7, 2018
582b8dc
Merge commit '6663c6816e04c161100c39f5d0e0957d36a3535b' into rpc
hamdiallam Dec 7, 2018
6663c68
Squashed 'contracts/' changes from dc28e6a..4fcc49e
hamdiallam Dec 7, 2018
c01227f
updated wrappers
hamdiallam Dec 7, 2018
7563251
updated config for block finality and eth priv key default directory
colin-axner Dec 9, 2018
8cba3b0
Squashed 'contracts/' changes from 4fcc49e..1b58d54
hamdiallam Dec 9, 2018
684b70f
Merge commit '8cba3b021c450320fbb5e5a05199467e54c70a8e' into rpc
hamdiallam Dec 9, 2018
0069dc9
deposits with finality checks
hamdiallam Dec 9, 2018
5ef281d
merge conflicts
AdityaSripal Dec 9, 2018
989e8b0
merge conflicts
AdityaSripal Dec 9, 2018
32fe39c
removed err from HasTxBeenExited
hamdiallam Dec 10, 2018
f8ea391
Merge branch 'colin/connection' of https://github.com/FourthState/pla…
AdityaSripal Dec 10, 2018
55bf4b8
refactors and added tests. Fixed deposit encoding/decoding
hamdiallam Dec 10, 2018
65c58b7
typo
hamdiallam Dec 10, 2018
3058d6b
debugging
AdityaSripal Dec 10, 2018
8678388
merge conflicts
AdityaSripal Dec 10, 2018
829b64a
changed rlp to json
hamdiallam Dec 11, 2018
e16bfbd
save progress
AdityaSripal Dec 11, 2018
975356c
fix bugs and remove print statements
AdityaSripal Dec 12, 2018
4e74968
save confirmations sigs into app state
colin-axner Dec 16, 2018
75ca7a0
added check for fee amount
colin-axner Dec 16, 2018
06f77e4
added confirmation signatures to prove command
colin-axner Dec 17, 2018
2d678b0
ante
colin-axner Dec 18, 2018
04d689c
adjusted names, load latest version
colin-axner Dec 18, 2018
9b559d2
remove prints
colin-axner Dec 18, 2018
195fc58
rebase
colin-axner Dec 20, 2018
b45694a
merge conflicts
colin-axner Dec 20, 2018
c4fb64d
merge conflicts for contracts
colin-axner Dec 20, 2018
62b6f0c
more merge conflicts
colin-axner Dec 20, 2018
95fd9f1
conflict
colin-axner Dec 20, 2018
9fd028c
undo merges that didnt delete
colin-axner Dec 20, 2018
dddede6
last of conflicts
colin-axner Dec 20, 2018
ae8b174
fixed build and test issues
colin-axner Dec 20, 2018
238b602
refactored app tests, works around genesis deposit issue
colin-axner Dec 20, 2018
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
Prev Previous commit
Next Next commit
save confirmations sigs into app state
colin-axner committed Dec 16, 2018
commit 4e749681afaf277dd69778a11fa597df8389e12f
32 changes: 17 additions & 15 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package app

import (
"fmt"
"crypto/ecdsa"
"encoding/binary"
"encoding/json"
"fmt"
auth "github.com/FourthState/plasma-mvp-sidechain/auth"
"github.com/FourthState/plasma-mvp-sidechain/eth"
"github.com/FourthState/plasma-mvp-sidechain/types"
"github.com/FourthState/plasma-mvp-sidechain/x/metadata"
"github.com/FourthState/plasma-mvp-sidechain/utils"
"github.com/FourthState/plasma-mvp-sidechain/x/kvstore"
"github.com/FourthState/plasma-mvp-sidechain/x/utxo"
abci "github.com/tendermint/tendermint/abci/types"
"io"
@@ -43,12 +44,12 @@ type ChildChain struct {
// keys to access the substores
capKeyMainStore *sdk.KVStoreKey

capKeyMetadataStore *sdk.KVStoreKey
capKeyPlasmaStore *sdk.KVStoreKey

// Manage addition and deletion of utxo's
utxoMapper utxo.Mapper

metadataMapper metadata.MetadataMapper
plasmaStore kvstore.KVStore

/* Validator Information */
isValidator bool
@@ -81,12 +82,12 @@ func NewChildChain(logger log.Logger, db dbm.DB, traceStore io.Writer, options .
bapp.SetCommitMultiStoreTracer(traceStore)

var app = &ChildChain{
BaseApp: bapp,
cdc: cdc,
txIndex: 0,
feeAmount: 0,
capKeyMainStore: sdk.NewKVStoreKey("main"),
capKeyMetadataStore: sdk.NewKVStoreKey("metadata"),
BaseApp: bapp,
cdc: cdc,
txIndex: 0,
feeAmount: 0,
capKeyMainStore: sdk.NewKVStoreKey("main"),
capKeyPlasmaStore: sdk.NewKVStoreKey("plasma"),
}

for _, option := range options {
@@ -99,15 +100,15 @@ func NewChildChain(logger log.Logger, db dbm.DB, traceStore io.Writer, options .
cdc,
)

app.metadataMapper = metadata.NewMetadataMapper(
app.capKeyMetadataStore,
app.plasmaStore = kvstore.NewKVStore(
app.capKeyPlasmaStore,
)

app.Router().
AddRoute("spend", utxo.NewSpendHandler(app.utxoMapper, app.nextPosition))

app.MountStoresIAVL(app.capKeyMainStore)
app.MountStoresIAVL(app.capKeyMetadataStore)
app.MountStoresIAVL(app.capKeyPlasmaStore)

app.SetInitChainer(app.initChainer)
app.SetEndBlocker(app.endBlocker)
@@ -127,7 +128,7 @@ func NewChildChain(logger log.Logger, db dbm.DB, traceStore io.Writer, options .
app.ethConnection = plasmaClient

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

err = app.LoadLatestVersion(app.capKeyMainStore)
if err != nil {
@@ -181,9 +182,10 @@ func (app *ChildChain) endBlocker(ctx sdk.Context, req abci.RequestEndBlock) abc

blknumKey := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(blknumKey, uint64(ctx.BlockHeight()))
key := append(utils.RootHashPrefix, blknumKey...)

if ctx.BlockHeader().DataHash != nil {
app.metadataMapper.StoreMetadata(ctx, blknumKey, ctx.BlockHeader().DataHash)
app.plasmaStore.Set(ctx, key, ctx.BlockHeader().DataHash)
}
return abci.ResponseEndBlock{}
}
9 changes: 6 additions & 3 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -251,7 +251,8 @@ func TestSpendTx(t *testing.T) {
ctx := cc.NewContext(false, abci.Header{})
blknumKey := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(blknumKey, uint64(1))
blockhash := cc.metadataMapper.GetMetadata(ctx, blknumKey)
key := append(utils.RootHashPrefix, blknumKey...)
blockhash := cc.plasmaStore.Get(ctx, key)

// Test that spending from a non-deposit/non-genesis UTXO works

@@ -397,7 +398,8 @@ func TestDifferentTxForms(t *testing.T) {
input_utxo := cc.utxoMapper.GetUTXO(ctx, tc.input0.addr.Bytes(), tc.input0.position)
blknumKey := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(blknumKey, uint64(7+uint64(index-1)))
blockhash := cc.metadataMapper.GetMetadata(ctx, blknumKey)
key := append(utils.RootHashPrefix, blknumKey...)
blockhash := cc.plasmaStore.Get(ctx, key)
hash := tmhash.Sum(append(input_utxo.TxHash, blockhash...))

msg.Input0ConfirmSigs = CreateConfirmSig(hash, keys[tc.input0.input_index0], keys[input0_index1], tc.input0.input_index1 != -1)
@@ -504,7 +506,8 @@ func TestMultiTxBlocks(t *testing.T) {

blknumKey := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(blknumKey, uint64(1))
blockhash := cc.metadataMapper.GetMetadata(ctx, blknumKey)
key := append(utils.RootHashPrefix, blknumKey...)
blockhash := cc.plasmaStore.Get(ctx, key)

txBytes, _ := rlp.EncodeToBytes(txs[i])
txHash := tmhash.Sum(txBytes)
55 changes: 43 additions & 12 deletions auth/ante.go
Original file line number Diff line number Diff line change
@@ -6,10 +6,11 @@ import (
"github.com/FourthState/plasma-mvp-sidechain/eth"
types "github.com/FourthState/plasma-mvp-sidechain/types"
utils "github.com/FourthState/plasma-mvp-sidechain/utils"
"github.com/FourthState/plasma-mvp-sidechain/x/metadata"
"github.com/FourthState/plasma-mvp-sidechain/x/kvstore"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto/tmhash"
"reflect"

@@ -18,7 +19,7 @@ import (

// NewAnteHandler returns an AnteHandler that checks signatures,
// confirm signatures, and increments the feeAmount
func NewAnteHandler(utxoMapper utxo.Mapper, metadataMapper metadata.MetadataMapper, feeUpdater utxo.FeeUpdater, plasmaClient *eth.Plasma) sdk.AnteHandler {
func NewAnteHandler(utxoMapper utxo.Mapper, plasmaStore kvstore.KVStore, feeUpdater utxo.FeeUpdater, plasmaClient *eth.Plasma) sdk.AnteHandler {
return func(
ctx sdk.Context, tx sdk.Tx, simulate bool,
) (_ sdk.Context, _ sdk.Result, abort bool) {
@@ -28,13 +29,7 @@ func NewAnteHandler(utxoMapper utxo.Mapper, metadataMapper metadata.MetadataMapp
return ctx, sdk.ErrInternal("tx must be in form of BaseTx").Result(), true
}

// Assert that there are signatures
sigs := baseTx.GetSignatures()
if len(sigs) == 0 {
return ctx,
sdk.ErrUnauthorized("no signers").Result(),
true
}

// Base Tx must have only one msg
msg := baseTx.GetMsgs()[0]
@@ -74,10 +69,12 @@ func NewAnteHandler(utxoMapper utxo.Mapper, metadataMapper metadata.MetadataMapp

// verify the confirmation signature if the input is not a deposit
if position0.DepositNum == 0 && position0.TxIndex != 1<<16-1 {
res = processConfirmSig(ctx, utxoMapper, metadataMapper, position0, addr0, spendMsg.Input0ConfirmSigs)
res = processConfirmSig(ctx, utxoMapper, plasmaStore, position0, addr0, spendMsg.Input0ConfirmSigs)
if !res.IsOK() {
return ctx, res, true
}

setConfirmSigs(ctx, plasmaStore, spendMsg.Blknum0, uint64(spendMsg.Txindex0), spendMsg.Input0ConfirmSigs)
}

// Verify the second input
@@ -106,10 +103,14 @@ func NewAnteHandler(utxoMapper utxo.Mapper, metadataMapper metadata.MetadataMapp
}

if position1.DepositNum == 0 && position1.TxIndex != 1<<16-1 {
res = processConfirmSig(ctx, utxoMapper, metadataMapper, position1, addr1, spendMsg.Input1ConfirmSigs)
res = processConfirmSig(ctx, utxoMapper, plasmaStore, position1, addr1, spendMsg.Input1ConfirmSigs)
if !res.IsOK() {
return ctx, res, true
}

if spendMsg.Blknum0 != spendMsg.Blknum1 && spendMsg.Txindex0 != spendMsg.Txindex1 {
setConfirmSigs(ctx, plasmaStore, spendMsg.Blknum1, uint64(spendMsg.Txindex1), spendMsg.Input1ConfirmSigs)
}
}
}

@@ -139,7 +140,7 @@ func processSig(
}

func processConfirmSig(
ctx sdk.Context, utxoMapper utxo.Mapper, metadataMapper metadata.MetadataMapper,
ctx sdk.Context, utxoMapper utxo.Mapper, plasmaStore kvstore.KVStore,
position types.PlasmaPosition, addr common.Address, sigs [][65]byte) (
res sdk.Result) {

@@ -157,7 +158,8 @@ func processConfirmSig(
// Get the block hash that input was created in
blknumKey := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(blknumKey, input.Position.Get()[0].Uint64())
blockHash := metadataMapper.GetMetadata(ctx, blknumKey)
key := append(utils.RootHashPrefix, blknumKey...)
blockHash := plasmaStore.Get(ctx, key)

// Create confirm signature hash
hash := append(input.TxHash, blockHash...)
@@ -174,6 +176,30 @@ func processConfirmSig(
return sdk.Result{}
}

// Helper function for setting confirmation signatures into app state
// Key is formed as : prefix + {Blknum, TxIndex}
// This prevents double storage of confirmation signatures
// Returns true if successful
func setConfirmSigs(ctx sdk.Context, plasmaStore kvstore.KVStore, blknum, txindex uint64, sigs [][65]byte) bool {
cdc := amino.NewCodec()
pos := [2]uint64{blknum, txindex}

bz, err := cdc.MarshalBinaryBare(pos)
if err != nil {
return false
}

plasmaKey := append(utils.ConfirmSigPrefix, bz...)

var confirmSigs []byte
confirmSigs = append(confirmSigs, sigs[0][:]...)
if len(sigs) == 2 {
confirmSigs = append(confirmSigs, sigs[1][:]...)
}
plasmaStore.Set(ctx, plasmaKey, confirmSigs)
return true
}

// Checks that utxo at the position specified exists, matches the address in the SpendMsg
// and returns the denomination associated with the utxo
func checkUTXO(ctx sdk.Context, plasmaClient *eth.Plasma, mapper utxo.Mapper, position types.PlasmaPosition, addr common.Address) sdk.Result {
@@ -189,6 +215,7 @@ func checkUTXO(ctx sdk.Context, plasmaClient *eth.Plasma, mapper utxo.Mapper, po
if !input.Valid {
return sdk.ErrUnknownRequest(fmt.Sprintf("UTXO trying to be spent, is not valid: %v.", position)).Result()
}
inputAddress = input.Address
}

// Verify that utxo owner equals input address in the transaction
@@ -208,6 +235,10 @@ func DepositExists(nonce uint64, plasmaClient *eth.Plasma) (types.Deposit, bool)
}

func hasTXExited(plasmaClient *eth.Plasma, pos types.PlasmaPosition) sdk.Error {
if plasmaClient == nil {
return nil
}

var positions [4]sdk.Uint
for i, num := range pos.Get() {
positions[i] = num
25 changes: 13 additions & 12 deletions auth/ante_test.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (
"fmt"
types "github.com/FourthState/plasma-mvp-sidechain/types"
utils "github.com/FourthState/plasma-mvp-sidechain/utils"
"github.com/FourthState/plasma-mvp-sidechain/x/metadata"
"github.com/FourthState/plasma-mvp-sidechain/x/kvstore"
"github.com/FourthState/plasma-mvp-sidechain/x/utxo"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
@@ -19,17 +19,17 @@ import (
"testing"
)

func setup() (sdk.Context, utxo.Mapper, metadata.MetadataMapper, utxo.FeeUpdater) {
ms, capKey, metadataCapKey := utxo.SetupMultiStore()
func setup() (sdk.Context, utxo.Mapper, kvstore.KVStore, utxo.FeeUpdater) {
ms, capKey, plasmaCapKey := utxo.SetupMultiStore()

ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger())
cdc := utxo.MakeCodec()
types.RegisterAmino(cdc)

mapper := utxo.NewBaseMapper(capKey, cdc)
metadataMapper := metadata.NewMetadataMapper(metadataCapKey)
plasmaStore := kvstore.NewKVStore(plasmaCapKey)

return ctx, mapper, metadataMapper, feeUpdater
return ctx, mapper, plasmaStore, feeUpdater
}

// should be modified when fees are implemented
@@ -111,7 +111,7 @@ func getInputAddr(addr0, addr1 common.Address, two bool) [][]byte {

// No signatures are provided
func TestNoSigs(t *testing.T) {
ctx, mapper, metadataMapper, feeUpdater := setup()
ctx, mapper, plasmaStore, feeUpdater := setup()

var msg = GenSpendMsg()
var emptysigs [2][65]byte
@@ -123,7 +123,7 @@ func TestNoSigs(t *testing.T) {
mapper.ReceiveUTXO(ctx, utxo1)
mapper.ReceiveUTXO(ctx, utxo2)

handler := NewAnteHandler(mapper, metadataMapper, feeUpdater)
handler := NewAnteHandler(mapper, plasmaStore, feeUpdater, nil)
_, res, abort := handler(ctx, tx, false)

assert.Equal(t, true, abort, "did not abort with no signatures")
@@ -132,7 +132,7 @@ func TestNoSigs(t *testing.T) {

// The wrong amount of signatures are provided
func TestNotEnoughSigs(t *testing.T) {
ctx, mapper, metadataMapper, feeUpdater := setup()
ctx, mapper, plasmaStore, feeUpdater := setup()

var msg = GenSpendMsg()
priv, _ := ethcrypto.GenerateKey()
@@ -148,7 +148,7 @@ func TestNotEnoughSigs(t *testing.T) {
mapper.ReceiveUTXO(ctx, utxo1)
mapper.ReceiveUTXO(ctx, utxo2)

handler := NewAnteHandler(mapper, metadataMapper, feeUpdater)
handler := NewAnteHandler(mapper, plasmaStore, feeUpdater, nil)
_, res, abort := handler(ctx, tx, false)

assert.Equal(t, true, abort, "did not abort with incorrect number of signatures")
@@ -166,7 +166,7 @@ type input struct {

// Tests a different cases.
func TestDifferentCases(t *testing.T) {
ctx, mapper, metadataMapper, feeUpdater := setup()
ctx, mapper, plasmaStore, feeUpdater := setup()

var keys [6]*ecdsa.PrivateKey
var addrs []common.Address
@@ -250,7 +250,7 @@ func TestDifferentCases(t *testing.T) {
owner_index1 := utils.GetIndex(tc.input1.owner_index)
tx := GetTx(msg, keys[tc.input0.owner_index], keys[owner_index1], tc.input1.owner_index != -1)

handler := NewAnteHandler(mapper, metadataMapper, feeUpdater)
handler := NewAnteHandler(mapper, plasmaStore, feeUpdater, nil)
_, res, abort := handler(ctx, tx, false)

assert.Equal(t, true, abort, fmt.Sprintf("did not abort on utxo that does not exist. Case: %d", index))
@@ -270,7 +270,8 @@ func TestDifferentCases(t *testing.T) {

blknumKey := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(blknumKey, tc.input0.position.Get()[0].Uint64())
metadataMapper.StoreMetadata(ctx, blknumKey, blockHash)
key := append(utils.RootHashPrefix, blknumKey...)
plasmaStore.Set(ctx, key, blockHash)

// for ease of testing, txhash is simplified
// app_test tests for correct functionality when setting tx_hash
3 changes: 3 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,9 @@ import (
"reflect"
)

var RootHashPrefix = []byte("root hash")
var ConfirmSigPrefix = []byte("confirmation signatures")

func ZeroAddress(addr common.Address) bool {
return new(big.Int).SetBytes(addr.Bytes()).Sign() == 0
}
30 changes: 30 additions & 0 deletions x/kvstore/mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kvstore

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

type KVStore struct {
contextKey sdk.StoreKey
}

func NewKVStore(contextKey sdk.StoreKey) KVStore {
return KVStore{
contextKey: contextKey,
}
}

func (kvstore KVStore) Set(ctx sdk.Context, key []byte, value []byte) {
store := ctx.KVStore(kvstore.contextKey)
store.Set(key, value)
}

func (kvstore KVStore) Get(ctx sdk.Context, key []byte) []byte {
store := ctx.KVStore(kvstore.contextKey)
return store.Get(key)
}

func (kvstore KVStore) Delete(ctx sdk.Context, key []byte) {
store := ctx.KVStore(kvstore.contextKey)
store.Delete(key)
}
30 changes: 0 additions & 30 deletions x/metadata/mapper.go

This file was deleted.