Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Commit

Permalink
removed all app-specific fields and made accounts and txs generic
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhavchellani committed Jul 4, 2020
1 parent 2a40ae6 commit 044aa2e
Show file tree
Hide file tree
Showing 21 changed files with 3,889 additions and 531 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ dep:
contracts:
abigen --abi=contracts/rollup/rollup.abi --pkg=rollup --out=contracts/rollup/rollup.go
abigen --abi=contracts/logger/logger.abi --pkg=logger --out=contracts/logger/logger.go
abigen --abi=contracts/rolluputils/rolluputils.abi --pkg=rolluputils --out=contracts/rolluputils/rolluputils.go
abigen --abi=contracts/fraudproof/fraudproof.abi --pkg=fraudproof --out=contracts/fraudproof/fraudproof.go

clean:
rm -rf build
Expand Down
97 changes: 79 additions & 18 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,43 @@ func (a *Aggregator) pickBatch() {
// ProcessTx fetches all the data required to validate tx from smart contact
// and calls the proccess tx function to return the updated balance root and accounts
func (a *Aggregator) ProcessTx(txs []core.Tx) error {
rootAcc, err := a.DB.GetRoot()
if err != nil {
return err
}
a.Logger.Debug("Latest root", "root", rootAcc.Hash)

currentRoot, err := core.HexToByteArray(rootAcc.Hash)
if err != nil {
return err
}
currentAccountTreeRoot, err := core.HexToByteArray(rootAcc.PublicKeyHash)
if err != nil {
return err
}

for _, tx := range txs {
fromAccProof, toAccProof, PDAproof, err := a.DB.GetTxVerificationData(tx)
rootAcc, err := a.DB.GetRoot()
if err != nil {
return err
}
a.Logger.Debug("Fetched latest account proofs", "tx", tx.String(), "fromMP", fromAccProof, "toMP", toAccProof, "PDAProof", PDAproof)
updatedRoot, _, _, err := a.LoadedBazooka.ProcessTx(currentRoot, currentAccountTreeRoot, tx, fromAccProof, toAccProof, PDAproof)

a.Logger.Debug("Latest root", "root", rootAcc.Hash)

currentRoot, err := core.HexToByteArray(rootAcc.Hash)
if err != nil {
return err
}

currentAccountTreeRoot, err := core.HexToByteArray(rootAcc.PublicKeyHash)
if err != nil {
return err
}

err = tx.Apply()
fromAccProof, toAccProof, PDAproof, err := a.GetTxVerificationData(tx)
if err != nil {
return err
}

a.Logger.Debug("Fetched latest account proofs", "tx", tx.String(), "fromMP", fromAccProof, "toMP", toAccProof, "PDAProof", PDAproof)

updatedRoot, updatedFrom, updatedTo, err := a.LoadedBazooka.ProcessTx(currentRoot, currentAccountTreeRoot, tx, fromAccProof, toAccProof, PDAproof)
if err != nil {
err := tx.UpdateStatus(core.TX_STATUS_REVERTED)
if err != nil {
a.Logger.Error("Unable to update transaction status", "tx", tx.String())
return err
}
}

// if the transactions is valid, apply it
err = tx.Apply(updatedFrom, updatedTo)
if err != nil {
return err
}
Expand All @@ -152,3 +162,54 @@ func (a *Aggregator) ProcessTx(txs []core.Tx) error {

return nil
}

// GetTxVerificationData fetches all the data required to prove validity fo transaction
func (a *Aggregator) GetTxVerificationData(tx core.Tx) (fromMerkleProof, toMerkleProof core.AccountMerkleProof, PDAProof core.PDAMerkleProof, err error) {
fromAcc, err := a.DB.GetAccountByID(tx.From)
if err != nil {
return
}

fromSiblings, err := a.DB.GetSiblings(fromAcc.Path)
if err != nil {
return
}
fromMerkleProof = core.NewAccountMerkleProof(fromAcc, fromSiblings)

toAcc, err := a.DB.GetAccountByID(tx.To)
if err != nil {
return
}
var toSiblings []core.UserAccount

mysqlTx := a.DB.Instance.Begin()
defer func() {
if r := recover(); r != nil {
mysqlTx.Rollback()
}
}()
dbCopy, _ := core.NewDB()
dbCopy.Instance = mysqlTx

updatedFromAccountBytes, _, err := a.LoadedBazooka.ApplyTransferTx(fromMerkleProof, tx)
if err != nil {
return
}

fromAcc.Data = updatedFromAccountBytes
err = dbCopy.UpdateAccount(fromAcc)
if err != nil {
return
}

// TODO add a check to ensure that DB copy of state matches the one returned by ApplyTransferTx

toSiblings, err = dbCopy.GetSiblings(toAcc.Path)
if err != nil {
return
}
mysqlTx.Rollback()
toMerkleProof = core.NewAccountMerkleProof(toAcc, toSiblings)
PDAProof = core.NewPDAProof(fromAcc.Path, fromAcc.PublicKey, fromSiblings)
return fromMerkleProof, toMerkleProof, PDAProof, nil
}
25 changes: 22 additions & 3 deletions bazooka/bazooka.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/BOPR/common"
"github.com/BOPR/config"

"github.com/BOPR/contracts/fraudproof"
"github.com/BOPR/contracts/logger"
"github.com/BOPR/contracts/rollup"
"github.com/BOPR/contracts/rolluputils"

"github.com/ethereum/go-ethereum/accounts/abi"
ethCmn "github.com/ethereum/go-ethereum/common"
Expand All @@ -23,7 +25,6 @@ type IBazooka interface {
FetchBatchInputData(txHash ethCmn.Hash) (txs [][]byte, err error)
}

// TODO use context to remove this completely
// Global Contract Caller Object
var LoadedBazooka Bazooka

Expand All @@ -39,6 +40,8 @@ type Bazooka struct {
// Rollup contract
RollupContract *rollup.Rollup
EventLogger *logger.Logger
RollupUtils *rolluputils.Rolluputils
FraudProof *fraudproof.Fraudproof
}

// NewContractCaller contract caller
Expand Down Expand Up @@ -71,8 +74,6 @@ func NewPreLoadedBazooka() (bazooka Bazooka, err error) {
return bazooka, err
}

// initialise all variables for merkle tree contract

// initialise all variables for event logger contract
loggerAddress := ethCmn.HexToAddress(config.GlobalCfg.LoggerAddress)
if bazooka.EventLogger, err = logger.NewLogger(loggerAddress, bazooka.EthClient); err != nil {
Expand All @@ -82,6 +83,24 @@ func NewPreLoadedBazooka() (bazooka Bazooka, err error) {
return bazooka, err
}

// initialise all variables for rollup utils contract
rollupUtilsAddress := ethCmn.HexToAddress(config.GlobalCfg.RollupUtilsAddress)
if bazooka.RollupUtils, err = rolluputils.NewRolluputils(rollupUtilsAddress, bazooka.EthClient); err != nil {
return bazooka, err
}
if bazooka.ContractABI[common.ROLLUP_UTILS], err = abi.JSON(strings.NewReader(rolluputils.RolluputilsABI)); err != nil {
return bazooka, err
}

// initialise all variables for event logger contract
fraudProofAddr := ethCmn.HexToAddress(config.GlobalCfg.FraudProofAddress)
if bazooka.FraudProof, err = fraudproof.NewFraudproof(fraudProofAddr, bazooka.EthClient); err != nil {
return bazooka, err
}
if bazooka.ContractABI[common.FRAUD_PROOF], err = abi.JSON(strings.NewReader(fraudproof.FraudproofABI)); err != nil {
return bazooka, err
}

bazooka.log = common.Logger.With("module", "bazooka")

return bazooka, nil
Expand Down
66 changes: 43 additions & 23 deletions bazooka/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/BOPR/common"
"github.com/BOPR/config"
"github.com/BOPR/contracts/rollup"
"github.com/BOPR/core"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethCmn "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -67,50 +68,69 @@ func (b *Bazooka) FetchBatchInputData(txHash ethCmn.Hash) (txs [][]byte, err err

// ProcessTx calls the ProcessTx function on the contract to verify the tx
// returns the updated accounts and the new balance root
func (b *Bazooka) ProcessTx(balanceTreeRoot, accountTreeRoot core.ByteArray, tx core.Tx, fromMerkleProof, toMerkleProof core.AccountMerkleProof, pdaProof core.PDAMerkleProof) (newBalanceRoot core.ByteArray, from, to core.UserAccount, err error) {
func (b *Bazooka) ProcessTx(balanceTreeRoot, accountTreeRoot core.ByteArray, tx core.Tx, fromMerkleProof, toMerkleProof core.AccountMerkleProof, pdaProof core.PDAMerkleProof) (newBalanceRoot core.ByteArray, from, to []byte, err error) {
txABIVersion := tx.ToABIVersion(int64(tx.From), int64(tx.To))
opts := bind.CallOpts{From: config.OperatorAddress()}
updatedRoot, newBalFrom, newBalTo, IsValidTx, err := b.RollupContract.ProcessTx(&opts,
typesAccountProofs := rollup.TypesAccountProofs{From: fromMerkleProof.ToABIVersion(), To: toMerkleProof.ToABIVersion()}
updatedRoot, newFromAccount, newToAccount, err_code, IsValidTx, err := b.RollupContract.ProcessTx(&opts,
balanceTreeRoot,
accountTreeRoot,
txABIVersion,
pdaProof.ToABIVersion(),
fromMerkleProof.ToABIVersion(),
toMerkleProof.ToABIVersion(),
typesAccountProofs,
)
if err != nil {
return
}

b.log.Info("Processed transaction", "success", IsValidTx, "newRoot", updatedRoot)
b.log.Info("Processed transaction", "IsSuccess", IsValidTx, "newRoot", updatedRoot)

if !IsValidTx {
b.log.Error("Invalid transaction", "error_code", err_code)
return newBalanceRoot, from, to, errors.New("Tx is invalid")
}

newBalanceRoot = core.BytesToByteArray(updatedRoot[:])
return newBalanceRoot, newFromAccount, newToAccount, nil
}

// TODO move to post procesTx function
fromMerkleProof.Account.Balance = newBalFrom.Uint64()
from = fromMerkleProof.Account

toMerkleProof.Account.Balance = newBalTo.Uint64()
to = toMerkleProof.Account
func (b *Bazooka) ApplyTransferTx(account core.AccountMerkleProof, tx core.Tx) ([]byte, core.ByteArray, error) {
txABIVersion := tx.ToABIVersion(int64(tx.From), int64(tx.To))
updatedAccountBytes, updatedRoot, err := b.RollupContract.ApplyTx(nil, account.ToABIVersion(), txABIVersion)
if err != nil {
return updatedAccountBytes, updatedRoot, err
}

return newBalanceRoot, from, to, nil
return updatedAccountBytes, updatedRoot, nil
}

func (b *Bazooka) VerifyPDAProof(accountsRoot core.ByteArray, pdaProof core.PDAMerkleProof) error {
opts := bind.CallOpts{From: config.OperatorAddress()}
return b.RollupContract.ValidatePubkeyAvailability(&opts, accountsRoot, pdaProof.ToABIVersion(), big.NewInt(2))
}
// func (b *Bazooka) CompressTx(tx core.Tx) ([]byte, core.ByteArray, error) {
// txABIVersion := tx.ToABIVersion(int64(tx.From), int64(tx.To))

func (b *Bazooka) ValidateSignature(tx core.Tx, pdaProof core.PDAMerkleProof) error {
opts := bind.CallOpts{From: config.OperatorAddress()}
return b.RollupContract.ValidateSignature(&opts, tx.ToABIVersion(int64(tx.From), int64(tx.To)), pdaProof.ToABIVersion())
// b.RollupUtils.CompressTx(nil, txABIVersion)
// }

// func (b *Bazooka) EncodeTx(from, to, token, amount int64) ([]byte, error) {
// return b.RollupUtils.BytesFromTxDeconstructed(nil, big.NewInt(from), big.NewInt(to), big.NewInt(token), big.NewInt(amount))
// }

// func (b *Bazooka) DecodeTx(txBytes []byte) (rolluputils.TypesTransaction, error) {
// tx, err := b.RollupUtils.TxFromBytes(nil, txBytes)
// return tx, err
// }

func (b *Bazooka) EncodeAccount(id, balance, nonce, token int64) (accountBytes []byte, err error) {
accountBytes, err = b.RollupUtils.BytesFromAccountDeconstructed(nil, big.NewInt(id), big.NewInt(balance), big.NewInt(nonce), big.NewInt(token))
if err != nil {
return
}
return accountBytes, nil
}

func (b *Bazooka) ValidateAccountMP(root core.ByteArray, accountMP core.AccountMerkleProof) error {
opts := bind.CallOpts{From: config.OperatorAddress()}
return b.RollupContract.ValidateAccountMP(&opts, root, accountMP.ToABIVersion())
func (b *Bazooka) DecodeAccount(accountBytes []byte) (ID, balance, nonce, token uint64, err error) {
account, err := b.RollupUtils.AccountFromBytes(nil, accountBytes)
if err != nil {
return
}

return account.ID.Uint64(), account.Balance.Uint64(), account.Nonce.Uint64(), account.TokenType.Uint64(), nil
}
2 changes: 1 addition & 1 deletion bazooka/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ func (b *Bazooka) SubmitBatch(updatedRoot core.ByteArray, txs []core.Tx) error {
if err != nil {
return err
}
b.log.Info("Sent a new batch!", "txHash",tx.Hash().String())
b.log.Info("Sent a new batch!", "txHash", tx.Hash().String())
return nil
}
3 changes: 0 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ func LoadGenesisData(genesis config.Genesis) {
allAccounts,
core.UserAccount{
AccountID: account.ID,
Balance: account.Balance,
TokenType: account.TokenType,
Nonce: account.Nonce,
Status: account.Status,
PublicKey: account.PublicKey,
PublicKeyHash: pubkeyHash,
Expand Down
Loading

0 comments on commit 044aa2e

Please sign in to comment.