Browse Source

Merge pull request #330 from hermeznetwork/feature/zki4-fees

ZKInputs with Fee txs compatible with circom
feature/sql-semaphore1
Eduard S 4 years ago
committed by GitHub
parent
commit
8de7fe537a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 444 additions and 352 deletions
  1. +11
    -13
      common/zk.go
  2. +9
    -8
      db/statedb/txprocessors.go
  3. +0
    -331
      db/statedb/txprocessors_test.go
  4. +424
    -0
      db/statedb/zkinputsgen_test.go

+ 11
- 13
common/zk.go

@ -203,7 +203,8 @@ type ZKInputs struct {
// account creation type. // account creation type.
ISOutIdx []*big.Int `json:"imOutIdx"` // uint64 (max nLevels bits), len: [nTx - 1] ISOutIdx []*big.Int `json:"imOutIdx"` // uint64 (max nLevels bits), len: [nTx - 1]
// rollup-tx // 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] ISStateRoot []*big.Int `json:"imStateRoot"` // Hash, len: [nTx - 1]
// ISExitTree root at the moment (once processed) of the Tx the value // ISExitTree root at the moment (once processed) of the Tx the value
// once the Tx is processed into the exit tree // 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 // ISAccFeeOut accumulated fees once the Tx is processed. Contains the
// array of FeeAccount Balances at each moment of each Tx processed. // array of FeeAccount Balances at each moment of each Tx processed.
ISAccFeeOut [][]*big.Int `json:"imAccFeeOut"` // big.Int, len: [nTx - 1][maxFeeIdxs] 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] 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 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
@ -283,8 +286,8 @@ func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatc
zki := &ZKInputs{} zki := &ZKInputs{}
zki.Metadata.NTx = nTx zki.Metadata.NTx = nTx
zki.Metadata.MaxFeeIdxs = maxFeeIdxs zki.Metadata.MaxFeeIdxs = maxFeeIdxs
zki.Metadata.NLevels = nLevels
zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd
zki.Metadata.NLevels = nLevels
zki.Metadata.MaxL1Tx = maxL1Tx zki.Metadata.MaxL1Tx = maxL1Tx
zki.Metadata.MaxTx = maxTx zki.Metadata.MaxTx = maxTx
@ -424,6 +427,7 @@ func (z ZKInputs) HashGlobalData() (*big.Int, error) {
func (z ZKInputs) ToHashGlobalData() ([]byte, error) { func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
var b []byte var b []byte
bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
bytesNLevels := int(z.Metadata.NLevels / 8) //nolint:gomnd
// [MAX_NLEVELS bits] oldLastIdx // [MAX_NLEVELS bits] oldLastIdx
oldLastIdx := make([]byte, bytesMaxLevels) oldLastIdx := make([]byte, bytesMaxLevels)
@ -490,16 +494,10 @@ func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
// [NLevels * MAX_TOKENS_FEE bits] feeTxsData // [NLevels * MAX_TOKENS_FEE bits] feeTxsData
for i := 0; i < len(z.FeeIdxs); i++ { 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() feeIdxBytes := z.FeeIdxs[i].Bytes()
copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:]) copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
r = append(r, feeIdx...)
b = append(b, r...)
b = append(b, feeIdx...)
} }
// [16 bits] chainID // [16 bits] chainID

+ 9
- 8
db/statedb/txprocessors.go

@ -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) log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx)
return nil, tracerr.Wrap(err) 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 { if s.zki != nil {
s.zki.TokenID3[iFee] = accCoord.TokenID.BigInt() s.zki.TokenID3[iFee] = accCoord.TokenID.BigInt()
s.zki.Nonce3[iFee] = accCoord.Nonce.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.Ay3[iFee] = accCoord.PublicKey.Y
s.zki.Balance3[iFee] = accCoord.Balance s.zki.Balance3[iFee] = accCoord.Balance
s.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr) s.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr)
s.zki.Siblings3[iFee] = siblingsToZKInputFormat(pFee.Siblings)
// add Coord Idx to ZKInputs.FeeTxsData // add Coord Idx to ZKInputs.FeeTxsData
s.zki.FeeIdxs[iFee] = idx.BigInt() 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() s.zki.ISStateRootFee[iFee] = s.mt.Root().BigInt()
} }
iFee++ iFee++

+ 0
- 331
db/statedb/txprocessors_test.go
File diff suppressed because it is too large
View File


+ 424
- 0
db/statedb/zkinputsgen_test.go
File diff suppressed because it is too large
View File


Loading…
Cancel
Save