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

Revived submitBatch #42

Merged
merged 3 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ logs
build
config.toml
batches.json
users.json
users.json
genesis.json
191 changes: 85 additions & 106 deletions core/bazooka.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,90 @@ func (b *Bazooka) DecodeState(stateBytes []byte) (ID, balance, nonce, token *big
// Transactions
//

// SubmitBatch submits the batch on chain with updated root and compressed transactions
func (b *Bazooka) SubmitBatch(commitments []Commitment) error {
b.log.Info(
"Attempting to submit a new batch",
"NumOfCommitments",
len(commitments),
)

if len(commitments) == 0 {
b.log.Info("No transactions to submit, waiting....")
return nil
}

var txs [][]byte
var updatedRoots [][32]byte
var aggregatedSig [][2]*big.Int
var totalTxs int

for _, commitment := range commitments {
compressedTxs, err := b.CompressTxs(commitment.Txs)
if err != nil {
b.log.Error("Unable to compress txs", "error", err)
return err
}
txs = append(txs, compressedTxs)
updatedRoots = append(updatedRoots, commitment.UpdatedRoot)
totalTxs += len(commitment.Txs)

// TODO cleanup
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to create an issue that adds an AggregatedSignature wrapper with a method like ToSol()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#56

sig1 := commitment.AggregatedSignature[0:32]
sig2 := commitment.AggregatedSignature[32:64]
sig1bigInt := big.NewInt(0)
sig1bigInt.SetBytes(sig1)
sig2bigInt := big.NewInt(0)
sig2bigInt.SetBytes(sig2)
aggregatedSigBigInt := [2]*big.Int{sig1bigInt, sig2bigInt}
aggregatedSig = append(aggregatedSig, aggregatedSigBigInt)
}

b.log.Info("Batch prepared", "totalTransactions", totalTxs)

rollupAddress := ethCmn.HexToAddress(config.GlobalCfg.RollupAddress)
stakeAmount := big.NewInt(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should create an issue to make stakeAmount configurable too.


// TODO fix
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the thing we want to fix here? Is it adding a feeReceiver to the config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I have added 0 as fee receiver to fix build but we need to replace that with the correct one.

Lets talk here: #47

var feeReceivers []*big.Int
dummyReceivers := big.NewInt(0)
feeReceivers = append(feeReceivers, dummyReceivers)

switch txType := commitments[0].BatchType; txType {
case TX_TRANSFER_TYPE:
data, err := b.ContractABI[common.ROLLUP_CONTRACT_KEY].Pack("submitTransfer", updatedRoots, aggregatedSig, feeReceivers, txs)
if err != nil {
b.log.Error("Error packing data for submitBatch", "err", err)
return err
}

// generate call msg
callMsg := ethereum.CallMsg{
To: &rollupAddress,
Data: data,
Value: stakeAmount,
}

// generate auth
auth, err := b.generateAuthObj(b.EthClient, callMsg)
if err != nil {
b.log.Error("Error creating auth object", "error", err)
return err
}

tx, err := b.RollupContract.SubmitTransfer(auth, updatedRoots, aggregatedSig, feeReceivers, txs)
if err != nil {
b.log.Error("Error submitting batch", "err", err)
return err
}
b.log.Info("Sent a new batch!", "TxHash", tx.Hash().String())
default:
b.log.Error("Tx not indentified", "txType", commitments[0].BatchType)
}

return nil
}

func (b *Bazooka) FireDepositFinalisation(TBreplaced UserState, siblings []UserState, subTreeHeight uint64) (err error) {
// b.log.Info(
// "Attempting to finalise deposits",
Expand Down Expand Up @@ -412,115 +496,10 @@ func (b *Bazooka) FireDepositFinalisation(TBreplaced UserState, siblings []UserS
// return err
// }
// b.log.Info("Deposits successfully finalized!", "TxHash", tx.Hash())
// return nil
return nil
}

// SubmitBatch submits the batch on chain with updated root and compressed transactions
func (b *Bazooka) SubmitBatch(commitments []Commitment) error {
// b.log.Info(
// "Attempting to submit a new batch",
// "NumOfCommitments",
// len(commitments),
// )

// if len(commitments) == 0 {
// b.log.Info("No transactions to submit, waiting....")
// return nil
// }

// var txs [][]byte
// var updatedRoots [][32]byte
// var aggregatedSig [][2]*big.Int
// var totalTransactionsBeingCommitted int
// for _, commitment := range commitments {
// compressedTxs, err := b.CompressTxs(commitment.Txs)
// if err != nil {
// b.log.Error("Unable to compress txs", "error", err)
// return err
// }
// txs = append(txs, compressedTxs)
// updatedRoots = append(updatedRoots, commitment.UpdatedRoot)
// totalTransactionsBeingCommitted += len(commitment.Txs)
// sig1 := commitment.AggregatedSignature[0:32]
// sig2 := commitment.AggregatedSignature[32:64]
// sig1bigInt := big.NewInt(0)
// sig1bigInt.SetBytes(sig1)
// sig2bigInt := big.NewInt(0)
// sig2bigInt.SetBytes(sig2)
// aggregatedSigBigInt := [2]*big.Int{sig1bigInt, sig2bigInt}
// fmt.Println("creeated aggregated sig", aggregatedSigBigInt)
// aggregatedSig = append(aggregatedSig, aggregatedSigBigInt)
// }

// b.log.Info("Batch prepared", "totalTransactions", totalTransactionsBeingCommitted)
// data, err := b.ContractABI[common.ROLLUP_CONTRACT_KEY].Pack("submitBatch", txs, updatedRoots, uint8(commitments[0].BatchType), aggregatedSig)
// if err != nil {
// b.log.Error("Error packing data for submitBatch", "err", err)
// return err
// }

// rollupAddress := ethCmn.HexToAddress(config.GlobalCfg.RollupAddress)
// stakeAmount := big.NewInt(0)
// stakeAmount.SetString("3200000000000000000", 10)

// // generate call msg
// callMsg := ethereum.CallMsg{
// To: &rollupAddress,
// Data: data,
// Value: stakeAmount,
// }

// auth, err := b.GenerateAuthObj(b.EthClient, callMsg)
// if err != nil {
// b.log.Error("Error creating auth object", "error", err)
// return err
// }

// // lastTxBroadcasted, err := DBInstance.GetLastTransaction()
// // if err != nil {
// // return err
// // }

// // if lastTxBroadcasted.Nonce+1 != auth.Nonce.Uint64() {
// // b.log.Info("Replacing nonce", "nonceEstimated", auth.Nonce.String(), "replacedBy", lastTxBroadcasted.Nonce+1)
// // auth.Nonce = big.NewInt(int64(lastTxBroadcasted.Nonce + 1))
// // }

// // latestBatch, err := DBInstance.GetLatestBatch()
// // if err != nil {
// // return err
// // }

// // newBatch := Batch{
// // BatchID: latestBatch.BatchID + 1,
// // StateRoot: updatedRoot.String(),
// // Committer: config.OperatorAddress.String(),
// // Status: BATCH_BROADCASTED,
// // }

// // b.log.Info("Broadcasting a new batch", "newBatch", newBatch)
// // err = DBInstance.AddNewBatch(newBatch)
// // if err != nil {
// // return err
// // }

// tx, err := b.RollupContract.SubmitBatch(auth, txs, updatedRoots, uint8(commitments[0].BatchType), aggregatedSig)
// if err != nil {
// b.log.Error("Error submitting batch", "err", err)
// return err
// }
// b.log.Info("Sent a new batch!", "TxHash", tx.Hash().String())

// // err = DBInstance.LogBatch(0, txs[0].Type, updatedRoot.String(), compressedTxs)
// // if err != nil {
// // return err
// // }

return nil
}

func (b *Bazooka) GenerateAuthObj(client *ethclient.Client, callMsg ethereum.CallMsg) (auth *bind.TransactOpts, err error) {
func (b *Bazooka) generateAuthObj(client *ethclient.Client, callMsg ethereum.CallMsg) (auth *bind.TransactOpts, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We change the MixCaps to camelCase here, is the intention that we don't want to export generateAuthObj in the package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bingo!

// from address
fromAddress := config.OperatorAddress

Expand Down