Merge pull request #350 from hermeznetwork/feature/zkinputs6

Update ZKInputs generation:
This commit is contained in:
Eduard S
2020-12-15 15:44:31 +01:00
committed by GitHub
4 changed files with 113 additions and 62 deletions

View File

@@ -61,9 +61,5 @@ func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBa
MaxL1Tx: 64, MaxL1Tx: 64,
} }
ptOut, err := bb.localStateDB.ProcessTxs(ptc, coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs) ptOut, err := bb.localStateDB.ProcessTxs(ptc, coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs)
if err != nil {
return nil, tracerr.Wrap(err)
}
err = bb.localStateDB.MakeCheckpoint()
return ptOut.ZKInputs, tracerr.Wrap(err) return ptOut.ZKInputs, tracerr.Wrap(err)
} }

View File

@@ -221,7 +221,7 @@ type ZKInputs struct {
ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash
// ISFinalAccFee final accumulated fees (before computing the fees-tx). // ISFinalAccFee final accumulated fees (before computing the fees-tx).
// Contains the final values of the ISAccFeeOut parameter // Contains the final values of the ISAccFeeOut parameter
ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs - 1] ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs]
} }
func bigIntsToStrings(v interface{}) interface{} { func bigIntsToStrings(v interface{}) interface{} {
@@ -384,7 +384,7 @@ func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatc
} }
zki.ISStateRootFee = newSlice(maxFeeIdxs - 1) zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
zki.ISInitStateRootFee = big.NewInt(0) zki.ISInitStateRootFee = big.NewInt(0)
zki.ISFinalAccFee = newSlice(maxFeeIdxs - 1) zki.ISFinalAccFee = newSlice(maxFeeIdxs)
return zki return zki
} }

View File

@@ -191,6 +191,11 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
return nil, tracerr.Wrap(err) return nil, tracerr.Wrap(err)
} }
s.zki.Metadata.L1TxsData = append(s.zki.Metadata.L1TxsData, l1TxData) s.zki.Metadata.L1TxsData = append(s.zki.Metadata.L1TxsData, l1TxData)
l1TxDataAvailability, err := l1coordinatortxs[i].BytesDataAvailability(s.zki.Metadata.NLevels)
if err != nil {
return nil, tracerr.Wrap(err)
}
s.zki.Metadata.L1TxsDataAvailability = append(s.zki.Metadata.L1TxsDataAvailability, l1TxDataAvailability)
s.zki.ISOutIdx[s.i] = s.idx.BigInt() s.zki.ISOutIdx[s.i] = s.idx.BigInt()
s.zki.ISStateRoot[s.i] = s.mt.Root().BigInt() s.zki.ISStateRoot[s.i] = s.mt.Root().BigInt()
@@ -222,7 +227,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
if s.zki != nil { if s.zki != nil {
// get the feePlanTokens // get the feePlanTokens
feePlanTokens, err := s.getFeePlanTokens(coordIdxs, l2txs) feePlanTokens, err := s.getFeePlanTokens(coordIdxs)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return nil, tracerr.Wrap(err) return nil, tracerr.Wrap(err)
@@ -252,9 +257,6 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
s.zki.ISExitRoot[s.i] = exitTree.Root().BigInt() s.zki.ISExitRoot[s.i] = exitTree.Root().BigInt()
} }
} }
if s.i == nTx-1 {
s.zki.ISFinalAccFee = formatAccumulatedFees(collectedFees, s.zki.FeePlanTokens)
}
} }
if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder { if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
if exitIdx != nil && exitTree != nil { if exitIdx != nil && exitTree != nil {
@@ -281,6 +283,8 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
s.zki.TxCompressedData[i] = new(big.Int).SetBytes(common.SignatureConstantBytes) s.zki.TxCompressedData[i] = new(big.Int).SetBytes(common.SignatureConstantBytes)
} }
} }
isFinalAccFee := formatAccumulatedFees(collectedFees, s.zki.FeePlanTokens)
copy(s.zki.ISFinalAccFee, isFinalAccFee)
// before computing the Fees txs, set the ISInitStateRootFee // before computing the Fees txs, set the ISInitStateRootFee
s.zki.ISInitStateRootFee = s.mt.Root().BigInt() s.zki.ISInitStateRootFee = s.mt.Root().BigInt()
} }
@@ -289,34 +293,34 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
// Coordinator Idxs // Coordinator Idxs
iFee := 0 iFee := 0
for idx, accumulatedFee := range s.accumulatedFees { for idx, accumulatedFee := range s.accumulatedFees {
// send the fee to the Idx of the Coordinator for the TokenID cmp := accumulatedFee.Cmp(big.NewInt(0))
accCoord, err := s.GetAccount(idx) if cmp == 1 { // accumulatedFee>0
if err != nil { // send the fee to the Idx of the Coordinator for the TokenID
log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx) accCoord, err := s.GetAccount(idx)
return nil, tracerr.Wrap(err) if err != nil {
} log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx)
if s.zki != nil { return nil, tracerr.Wrap(err)
s.zki.TokenID3[iFee] = accCoord.TokenID.BigInt() }
s.zki.Nonce3[iFee] = accCoord.Nonce.BigInt() if s.zki != nil {
if babyjub.PointCoordSign(accCoord.PublicKey.X) { s.zki.TokenID3[iFee] = accCoord.TokenID.BigInt()
s.zki.Sign3[iFee] = big.NewInt(1) s.zki.Nonce3[iFee] = accCoord.Nonce.BigInt()
if babyjub.PointCoordSign(accCoord.PublicKey.X) {
s.zki.Sign3[iFee] = big.NewInt(1)
}
s.zki.Ay3[iFee] = accCoord.PublicKey.Y
s.zki.Balance3[iFee] = accCoord.Balance
s.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr)
}
accCoord.Balance = new(big.Int).Add(accCoord.Balance, accumulatedFee)
pFee, err := s.UpdateAccount(idx, accCoord)
if err != nil {
log.Error(err)
return nil, tracerr.Wrap(err)
}
if s.zki != nil {
s.zki.Siblings3[iFee] = siblingsToZKInputFormat(pFee.Siblings)
s.zki.ISStateRootFee[iFee] = s.mt.Root().BigInt()
} }
s.zki.Ay3[iFee] = accCoord.PublicKey.Y
s.zki.Balance3[iFee] = accCoord.Balance
s.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr)
// add Coord Idx to ZKInputs.FeeTxsData
s.zki.FeeIdxs[iFee] = idx.BigInt()
}
accCoord.Balance = new(big.Int).Add(accCoord.Balance, accumulatedFee)
pFee, err := s.UpdateAccount(idx, accCoord)
if err != nil {
log.Error(err)
return nil, tracerr.Wrap(err)
}
if s.zki != nil {
s.zki.Siblings3[iFee] = siblingsToZKInputFormat(pFee.Siblings)
s.zki.ISStateRootFee[iFee] = s.mt.Root().BigInt()
} }
iFee++ iFee++
} }
@@ -324,6 +328,10 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
for i := len(s.accumulatedFees); i < int(ptc.MaxFeeTx)-1; i++ { for i := len(s.accumulatedFees); i < int(ptc.MaxFeeTx)-1; i++ {
s.zki.ISStateRootFee[i] = s.mt.Root().BigInt() s.zki.ISStateRootFee[i] = s.mt.Root().BigInt()
} }
// add Coord Idx to ZKInputs.FeeTxsData
for i := 0; i < len(coordIdxs); i++ {
s.zki.FeeIdxs[i] = coordIdxs[i].BigInt()
}
} }
if s.typ == TypeTxSelector { if s.typ == TypeTxSelector {
@@ -368,7 +376,6 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
// compute last ZKInputs parameters // compute last ZKInputs parameters
s.zki.GlobalChainID = big.NewInt(0) // TODO, 0: ethereum, this will be get from config file s.zki.GlobalChainID = big.NewInt(0) // TODO, 0: ethereum, this will be get from config file
// zki.FeeIdxs = ? // TODO, this will be get from the config file
s.zki.Metadata.NewStateRootRaw = s.mt.Root() s.zki.Metadata.NewStateRootRaw = s.mt.Root()
s.zki.Metadata.NewExitRootRaw = exitTree.Root() s.zki.Metadata.NewExitRootRaw = exitTree.Root()
@@ -384,35 +391,15 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
// getFeePlanTokens returns an array of *big.Int containing a list of tokenIDs // getFeePlanTokens returns an array of *big.Int containing a list of tokenIDs
// corresponding to the given CoordIdxs and the processed L2Txs // corresponding to the given CoordIdxs and the processed L2Txs
func (s *StateDB) getFeePlanTokens(coordIdxs []common.Idx, l2txs []common.PoolL2Tx) ([]*big.Int, error) { func (s *StateDB) getFeePlanTokens(coordIdxs []common.Idx) ([]*big.Int, error) {
// get Coordinator TokenIDs corresponding to the Idxs where the Fees var tBI []*big.Int
// will be sent
coordTokenIDs := make(map[common.TokenID]bool)
for i := 0; i < len(coordIdxs); i++ { for i := 0; i < len(coordIdxs); i++ {
acc, err := s.GetAccount(coordIdxs[i]) acc, err := s.GetAccount(coordIdxs[i])
if err != nil { if err != nil {
log.Errorf("could not get account to determine TokenID of CoordIdx %d not found: %s", coordIdxs[i], err.Error()) log.Errorf("could not get account to determine TokenID of CoordIdx %d not found: %s", coordIdxs[i], err.Error())
return nil, tracerr.Wrap(err) return nil, tracerr.Wrap(err)
} }
coordTokenIDs[acc.TokenID] = true tBI = append(tBI, acc.TokenID.BigInt())
}
tokenIDs := make(map[common.TokenID]bool)
for i := 0; i < len(l2txs); i++ {
// as L2Tx does not have parameter TokenID, get it from the
// AccountsDB (in the StateDB)
acc, err := s.GetAccount(l2txs[i].FromIdx)
if err != nil {
log.Errorf("could not get account to determine TokenID of L2Tx: FromIdx %d not found: %s", l2txs[i].FromIdx, err.Error())
return nil, tracerr.Wrap(err)
}
if _, ok := coordTokenIDs[acc.TokenID]; ok {
tokenIDs[acc.TokenID] = true
}
}
var tBI []*big.Int
for t := range tokenIDs {
tBI = append(tBI, t.BigInt())
} }
return tBI, nil return tBI, nil
} }

File diff suppressed because one or more lines are too long