Skip to content

Commit

Permalink
Merge pull request #42 from thehubbleproject/patch34
Browse files Browse the repository at this point in the history
Revived submitBatch
  • Loading branch information
vaibhavchellani authored Nov 2, 2020
2 parents e51cf27 + 6765dd6 commit 82167ce
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 107 deletions.
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
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)

// TODO fix
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) {
// from address
fromAddress := config.OperatorAddress

Expand Down

0 comments on commit 82167ce

Please sign in to comment.