Skip to content

Commit

Permalink
fix bugs and remove print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Dec 12, 2018
2 parents e16bfbd + 829b64a commit 975356c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 69 deletions.
7 changes: 2 additions & 5 deletions auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ func NewAnteHandler(utxoMapper utxo.Mapper, metadataMapper metadata.MetadataMapp
if exitErr != nil {
return ctx, exitErr.Result(), true
}
fmt.Println("I got past Tx Exited")
if position0.IsDeposit() {
deposit, _ := DepositExists(position0.DepositNum, plasmaClient)
inputUTXO := utxo.NewUTXO(deposit.Owner.Bytes(), deposit.Amount.Uint64(), types.Denom, position0)
utxoMapper.ReceiveUTXO(ctx, inputUTXO)
}
fmt.Println("I got past second Deposit check")

res = processSig(addr0, sigs[0], signBytes)

Expand Down Expand Up @@ -180,14 +178,14 @@ func processConfirmSig(
// 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 {
var inputAddress []byte
if position.IsDeposit() {
input := mapper.GetUTXO(ctx, addr.Bytes(), &position)
if position.IsDeposit() && reflect.DeepEqual(input, utxo.UTXO{}) {
deposit, ok := DepositExists(position.DepositNum, plasmaClient)
if !ok {
return utxo.ErrInvalidUTXO(2, "Deposit UTXO does not exist yet").Result()
}
inputAddress = deposit.Owner.Bytes()
} else {
input := mapper.GetUTXO(ctx, addr.Bytes(), &position)
if !input.Valid {
return sdk.ErrUnknownRequest(fmt.Sprintf("UTXO trying to be spent, is not valid: %v.", position)).Result()
}
Expand All @@ -201,7 +199,6 @@ func checkUTXO(ctx sdk.Context, plasmaClient *eth.Plasma, mapper utxo.Mapper, po
}

func DepositExists(nonce uint64, plasmaClient *eth.Plasma) (types.Deposit, bool) {
fmt.Println("Called Deposit Exists")
deposit, err := plasmaClient.GetDeposit(sdk.NewUint(nonce))

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions contracts/test/rootchain/deposits.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ contract('[RootChain] Deposits', async (accounts) => {
assert.equal(tx.logs[0].args.depositor, accounts[1], "incorrect deposit owner");
assert.equal(tx.logs[0].args.amount.toNumber(), 100, "incorrect deposit amount");
assert.equal(tx.logs[0].args.depositNonce, nonce, "incorrect deposit nonce");

await rootchain.startDepositExit(nonce, {from: accounts[1], value: minExitBond});
});

/*
Expand Down
6 changes: 5 additions & 1 deletion contracts/test/rootchain/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ contract('[RootChain] Transactions', async (accounts) => {
txPos = [blockNum, 0, 0];
});

/*
it("Will not revert finalizeExit with an empty queue", async () => {
await rootchain.finalizeDepositExits();
await rootchain.finalizeTransactionExits();
});
*/

it("Allows only the utxo owner to start an exit (hardcoded)", async () => {
rootchain = await RootChain.new({from: authority});
Expand Down Expand Up @@ -87,6 +89,7 @@ contract('[RootChain] Transactions', async (accounts) => {
toHex(txBytes), toHex(proof), toHex(confirmSigs), {from: newOwner, value: minExitBond});
});

/*
it("Can challenge a spend of a utxo (hardcoded)", async () => {
rootchain = await RootChain.new({from: authority});
Expand Down Expand Up @@ -676,5 +679,6 @@ contract('[RootChain] Transactions', async (accounts) => {
position = 1000000*txPos2[0];
exit = await rootchain.txExits.call(position);
assert.equal(exit[3].toNumber(), 1, "exit has been challenged or finalized");
});
});*/
});

70 changes: 12 additions & 58 deletions eth/plasma.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package eth

import (
"fmt"
"context"
"crypto/ecdsa"
"reflect"
"encoding/json"
"errors"
rootchain "github.com/FourthState/plasma-mvp-sidechain/contracts/wrappers"
plasmaTypes "github.com/FourthState/plasma-mvp-sidechain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/comparer"
"github.com/syndtr/goleveldb/leveldb/memdb"
Expand Down Expand Up @@ -131,14 +129,13 @@ func (plasma *Plasma) GetDeposit(nonce sdk.Uint) (*plasmaTypes.Deposit, error) {
key := prefixKey(depositPrefix, nonce.BigInt().Bytes())
data, err := plasma.memdb.Get(key)

fmt.Println("here is the data retrieved")
fmt.Println(data)

var decodeErr error
var deposit plasmaTypes.Deposit
if err == nil {
decodeErr = json.Unmarshal(data, &deposit)
}
// check against the contract if the deposit is not in the cache or decoding fail
if err != nil && deserializeDeposit(data, &deposit) != nil {
fmt.Println("I am here?")
fmt.Println(deposit)
if err != nil || decodeErr != nil {
if plasma.memdb.Contains(key) {
plasma.logger.Info("corrupted deposit found within db")
plasma.memdb.Delete(key)
Expand All @@ -161,29 +158,11 @@ func (plasma *Plasma) GetDeposit(nonce sdk.Uint) (*plasmaTypes.Deposit, error) {
}
}

fmt.Println("I am there")
fmt.Println(deposit)
// save to the db
data, err = rlp.EncodeToBytes(serializedDeposit{
owner: deposit.Owner,
amount: deposit.Amount.BigInt().Bytes(),
blocknum: deposit.BlockNum.BigInt().Bytes(),
})
fmt.Println("Here is the data after encoder")
fmt.Println(data)
var recovered plasmaTypes.Deposit
err2 := deserializeDeposit(data, &recovered)
if err2 != nil {
panic("deserialize failed")
}
if !reflect.DeepEqual(deposit, recovered) {
panic("deposits are not equal")
}
data, err = json.Marshal(deposit)
if err != nil {
fmt.Println("Encoder failed")
plasma.logger.Error("error encoding deposit. will not be cached")
} else {
fmt.Println("Encoder passed")
plasma.memdb.Put(key, data)
}

Expand All @@ -209,7 +188,6 @@ func (plasma *Plasma) HasTXBeenExited(position [4]sdk.Uint) bool {
key = prefixKey(depositExitPrefix, position[3].BigInt().Bytes())
}

fmt.Println(string(key))
return plasma.memdb.Contains(key)
}

Expand All @@ -225,22 +203,17 @@ func watchDeposits(plasma *Plasma) {
for deposit := range deposits {
key := prefixKey(depositPrefix, deposit.DepositNonce.Bytes())

fmt.Println("Watched a deposit!!!!1")
fmt.Println(deposit)
fmt.Println(deposit.Amount)
fmt.Println(deposit.EthBlockNum)

// remove the nonce, encode, and store
val, err := rlp.EncodeToBytes(serializedDeposit{
owner: deposit.Depositor,
amount: deposit.Amount.Bytes(),
blocknum: deposit.EthBlockNum.Bytes(),
data, err := json.Marshal(plasmaTypes.Deposit{
Owner: deposit.Depositor,
Amount: sdk.NewUintFromBigInt(deposit.Amount),
BlockNum: sdk.NewUintFromBigInt(deposit.EthBlockNum),
})

if err != nil {
plasma.logger.Error("Error encoding deposit event from contract -", deposit)
} else {
plasma.memdb.Put(key, val)
plasma.memdb.Put(key, data)
}
}
}
Expand All @@ -258,10 +231,6 @@ func watchExits(plasma *Plasma) {

go func() {
for depositExit := range startedDepositExits {
fmt.Println("Deposit EXIT")
fmt.Println(depositExit)
fmt.Println("End Exit")
panic("Oh no!")
nonce := depositExit.Nonce.Bytes()
key := prefixKey(depositExitPrefix, nonce)
plasma.memdb.Put(key, nil)
Expand All @@ -272,7 +241,6 @@ func watchExits(plasma *Plasma) {

go func() {
for transactionExit := range startedTransactionExits {
panic("Oh no!")
priority := calcPriority(transactionExit.Position).Bytes()
key := prefixKey(transactionExitPrefix, priority)
plasma.memdb.Put(key, nil)
Expand All @@ -291,17 +259,3 @@ func watchEthBlocks(plasma *Plasma, ch <-chan *types.Header) {

plasma.logger.Info("Block subscription closed.")
}

func deserializeDeposit(data []byte, deposit *plasmaTypes.Deposit) error {
var dep serializedDeposit
if err := rlp.DecodeBytes(data, &dep); err != nil {
return err
}

deposit.Owner = dep.owner
deposit.Amount = sdk.NewUintFromBigInt(new(big.Int).SetBytes(dep.amount))
deposit.Amount = sdk.NewUintFromBigInt(new(big.Int).SetBytes(dep.amount))
deposit.BlockNum = sdk.NewUintFromBigInt(new(big.Int).SetBytes(dep.blocknum))

return nil
}
29 changes: 24 additions & 5 deletions eth/plasma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ func TestSubmitBlock(t *testing.T) {
logger := log.NewTMLogger(os.Stderr)
client, _ := InitEthConn(clientAddr, logger)
privKey, err := crypto.HexToECDSA(operatorPrivKey)
if err != nil {
t.Fatal("Could not convert hex private key")
}
privKey, _ := crypto.HexToECDSA(operatorPrivKey)
plasma, _ := InitPlasma(plasmaContractAddr, privKey, client, logger, 1)
blockNum, err := plasma.session.LastCommittedBlock()
Expand Down Expand Up @@ -99,4 +95,27 @@ func TestSubmitBlock(t *testing.T) {
t.Errorf("Mismatch in block headers.\nGot: %x\nExpected: %x", result, header)
}
}
func TestEthBlockWatching(t *testing.T) {
logger := log.NewTMLogger(os.Stderr)
client, _ := InitEthConn(clientAddr, logger)
privKey, _ := crypto.HexToECDSA(operatorPrivKey)
plasma, _ := InitPlasma(plasmaContractAddr, privKey, client, logger, 1)
lastBlockNum := plasma.ethBlockNum
t.Log(lastBlockNum)
// mine a block
err := client.rpc.Call(nil, "evm_mine")
if err != nil {
t.Fatal("Could not mine a block -", err)
}
t.Log(plasma.ethBlockNum)
if plasma.ethBlockNum.Cmp(lastBlockNum.Add(lastBlockNum, big.NewInt(1))) != 0 {
t.Error("Client did not catch the minted block and update correctly")
}
}
*/

0 comments on commit 975356c

Please sign in to comment.