mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-06 19:06:42 +01:00
Merge pull request #330 from hermeznetwork/feature/zki4-fees
ZKInputs with Fee txs compatible with circom
This commit is contained in:
24
common/zk.go
24
common/zk.go
@@ -203,7 +203,8 @@ type ZKInputs struct {
|
||||
// account creation type.
|
||||
ISOutIdx []*big.Int `json:"imOutIdx"` // uint64 (max nLevels bits), len: [nTx - 1]
|
||||
// rollup-tx
|
||||
// ISStateRoot root at the moment of the Tx (once processed), the state root value once the Tx is processed into the state tree
|
||||
// ISStateRoot root at the moment of the Tx (once processed), the state
|
||||
// root value once the Tx is processed into the state tree
|
||||
ISStateRoot []*big.Int `json:"imStateRoot"` // Hash, len: [nTx - 1]
|
||||
// ISExitTree root at the moment (once processed) of the Tx the value
|
||||
// once the Tx is processed into the exit tree
|
||||
@@ -211,10 +212,12 @@ type ZKInputs struct {
|
||||
// ISAccFeeOut accumulated fees once the Tx is processed. Contains the
|
||||
// array of FeeAccount Balances at each moment of each Tx processed.
|
||||
ISAccFeeOut [][]*big.Int `json:"imAccFeeOut"` // big.Int, len: [nTx - 1][maxFeeIdxs]
|
||||
// fee-tx
|
||||
// ISStateRootFee root at the moment of the Tx (once processed), the state root value once the Tx is processed into the state tree
|
||||
// fee-tx:
|
||||
// ISStateRootFee root at the moment of the Tx (once processed), the
|
||||
// state root value once the Tx is processed into the state tree
|
||||
ISStateRootFee []*big.Int `json:"imStateRootFee"` // Hash, len: [maxFeeIdxs - 1]
|
||||
// ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
|
||||
// ISInitStateRootFee state root once all L1-L2 tx are processed
|
||||
// (before computing the fees-tx)
|
||||
ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash
|
||||
// ISFinalAccFee final accumulated fees (before computing the fees-tx).
|
||||
// Contains the final values of the ISAccFeeOut parameter
|
||||
@@ -283,8 +286,8 @@ func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatc
|
||||
zki := &ZKInputs{}
|
||||
zki.Metadata.NTx = nTx
|
||||
zki.Metadata.MaxFeeIdxs = maxFeeIdxs
|
||||
zki.Metadata.NLevels = nLevels
|
||||
zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd
|
||||
zki.Metadata.NLevels = nLevels
|
||||
zki.Metadata.MaxL1Tx = maxL1Tx
|
||||
zki.Metadata.MaxTx = maxTx
|
||||
|
||||
@@ -424,6 +427,7 @@ func (z ZKInputs) HashGlobalData() (*big.Int, error) {
|
||||
func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
|
||||
var b []byte
|
||||
bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
|
||||
bytesNLevels := int(z.Metadata.NLevels / 8) //nolint:gomnd
|
||||
|
||||
// [MAX_NLEVELS bits] oldLastIdx
|
||||
oldLastIdx := make([]byte, bytesMaxLevels)
|
||||
@@ -490,16 +494,10 @@ func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
|
||||
|
||||
// [NLevels * MAX_TOKENS_FEE bits] feeTxsData
|
||||
for i := 0; i < len(z.FeeIdxs); i++ {
|
||||
var r []byte
|
||||
|
||||
padding := make([]byte, bytesMaxLevels/4) //nolint:gomnd
|
||||
r = append(r, padding...)
|
||||
|
||||
feeIdx := make([]byte, bytesMaxLevels/2) //nolint:gomnd
|
||||
feeIdx := make([]byte, bytesNLevels) //nolint:gomnd
|
||||
feeIdxBytes := z.FeeIdxs[i].Bytes()
|
||||
copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
|
||||
r = append(r, feeIdx...)
|
||||
b = append(b, r...)
|
||||
b = append(b, feeIdx...)
|
||||
}
|
||||
|
||||
// [16 bits] chainID
|
||||
|
||||
@@ -287,12 +287,6 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx)
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
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.TokenID3[iFee] = accCoord.TokenID.BigInt()
|
||||
s.zki.Nonce3[iFee] = accCoord.Nonce.BigInt()
|
||||
@@ -302,11 +296,18 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use
|
||||
s.zki.Ay3[iFee] = accCoord.PublicKey.Y
|
||||
s.zki.Balance3[iFee] = accCoord.Balance
|
||||
s.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr)
|
||||
s.zki.Siblings3[iFee] = siblingsToZKInputFormat(pFee.Siblings)
|
||||
|
||||
// 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++
|
||||
|
||||
File diff suppressed because one or more lines are too long
424
db/statedb/zkinputsgen_test.go
Normal file
424
db/statedb/zkinputsgen_test.go
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user