Browse Source

Merge pull request #211 from hermeznetwork/feature/update-general

Feature/update general
feature/sql-semaphore1
arnau 3 years ago
committed by GitHub
parent
commit
6a8b454368
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 412 additions and 17 deletions
  1. +5
    -0
      api/api_test.go
  2. +3
    -0
      batchbuilder/batchbuilder_test.go
  3. +277
    -5
      common/fee.go
  4. +29
    -0
      common/fee_test.go
  5. +3
    -0
      coordinator/coordinator_test.go
  6. +57
    -10
      db/statedb/statedb.go
  7. +7
    -0
      db/statedb/statedb_test.go
  8. +17
    -2
      db/statedb/txprocessors.go
  9. +4
    -0
      db/statedb/txprocessors_test.go
  10. +2
    -0
      db/statedb/utils_test.go
  11. +5
    -0
      eth/main_test.go
  12. +1
    -0
      synchronizer/synchronizer_test.go
  13. +2
    -0
      txselector/txselector_test.go

+ 5
- 0
api/api_test.go

@ -151,6 +151,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
}()
sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0)
if err != nil {
panic(err)

+ 3
- 0
batchbuilder/batchbuilder_test.go

@ -2,6 +2,7 @@ package batchbuilder
import (
"io/ioutil"
"os"
"testing"
"github.com/hermeznetwork/hermez-node/db/statedb"
@ -12,12 +13,14 @@ import (
func TestBatchBuilder(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
synchDB, err := statedb.NewStateDB(dir, statedb.TypeBatchBuilder, 0)
assert.Nil(t, err)
bbDir, err := ioutil.TempDir("", "tmpBatchBuilderDB")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(bbDir))
_, err = NewBatchBuilder(bbDir, synchDB, nil, 0, 32)
assert.Nil(t, err)
}

+ 277
- 5
common/fee.go

@ -1,10 +1,9 @@
package common
import "math"
// Fee is a type that represents the percentage of tokens that will be paid in a transaction
// to incentivaise the materialization of it
type Fee float64
import (
"math"
"math/big"
)
// RecommendedFee is the recommended fee to pay in USD per transaction set by
// the coordinator according to the tx type (if the tx requires to create an
@ -38,3 +37,276 @@ const MaxFeePlan = 256
// FeePlan represents the fee model, a position in the array indicates the
// percentage of tokens paid in concept of fee for a transaction
var FeePlan = [MaxFeePlan]float64{}
// CalcFeeAmount calculates the fee amount in tokens from an amount and
// feeSelector (fee index).
func CalcFeeAmount(amount *big.Int, feeSel FeeSelector) *big.Int {
feeAmount := new(big.Int).Mul(amount, FeeFactorLsh79[int(feeSel)])
return feeAmount.Rsh(feeAmount, 79)
}
func init() {
setFeeFactorLsh79(&FeeFactorLsh79)
}
// FeeFactorLsh79 is the feeFactor << 79
var FeeFactorLsh79 [256]*big.Int
func setFeeFactorLsh79(feeFactorLsh79 *[256]*big.Int) {
feeFactorLsh79[0], _ = new(big.Int).SetString("0", 10)
feeFactorLsh79[1], _ = new(big.Int).SetString("1", 10)
feeFactorLsh79[2], _ = new(big.Int).SetString("6", 10)
feeFactorLsh79[3], _ = new(big.Int).SetString("19", 10)
feeFactorLsh79[4], _ = new(big.Int).SetString("60", 10)
feeFactorLsh79[5], _ = new(big.Int).SetString("191", 10)
feeFactorLsh79[6], _ = new(big.Int).SetString("604", 10)
feeFactorLsh79[7], _ = new(big.Int).SetString("1911", 10)
feeFactorLsh79[8], _ = new(big.Int).SetString("6044", 10)
feeFactorLsh79[9], _ = new(big.Int).SetString("19114", 10)
feeFactorLsh79[10], _ = new(big.Int).SetString("60446", 10)
feeFactorLsh79[11], _ = new(big.Int).SetString("191147", 10)
feeFactorLsh79[12], _ = new(big.Int).SetString("604462", 10)
feeFactorLsh79[13], _ = new(big.Int).SetString("1911479", 10)
feeFactorLsh79[14], _ = new(big.Int).SetString("6044629", 10)
feeFactorLsh79[15], _ = new(big.Int).SetString("19114795", 10)
feeFactorLsh79[16], _ = new(big.Int).SetString("60446290", 10)
feeFactorLsh79[17], _ = new(big.Int).SetString("191147955", 10)
feeFactorLsh79[18], _ = new(big.Int).SetString("604462909", 10)
feeFactorLsh79[19], _ = new(big.Int).SetString("1911479556", 10)
feeFactorLsh79[20], _ = new(big.Int).SetString("6044629098", 10)
feeFactorLsh79[21], _ = new(big.Int).SetString("19114795560", 10)
feeFactorLsh79[22], _ = new(big.Int).SetString("60446290980", 10)
feeFactorLsh79[23], _ = new(big.Int).SetString("191147955608", 10)
feeFactorLsh79[24], _ = new(big.Int).SetString("604462909807", 10)
feeFactorLsh79[25], _ = new(big.Int).SetString("1911479556084", 10)
feeFactorLsh79[26], _ = new(big.Int).SetString("6044629098073", 10)
feeFactorLsh79[27], _ = new(big.Int).SetString("19114795560840", 10)
feeFactorLsh79[28], _ = new(big.Int).SetString("60446290980731", 10)
feeFactorLsh79[29], _ = new(big.Int).SetString("191147955608404", 10)
feeFactorLsh79[30], _ = new(big.Int).SetString("604462909807314", 10)
feeFactorLsh79[31], _ = new(big.Int).SetString("1911479556084045", 10)
feeFactorLsh79[32], _ = new(big.Int).SetString("6044629098073146", 10)
feeFactorLsh79[33], _ = new(big.Int).SetString("6653288015630616", 10)
feeFactorLsh79[34], _ = new(big.Int).SetString("7323235338466790", 10)
feeFactorLsh79[35], _ = new(big.Int).SetString("8060642451758603", 10)
feeFactorLsh79[36], _ = new(big.Int).SetString("8872302163198820", 10)
feeFactorLsh79[37], _ = new(big.Int).SetString("9765691276621298", 10)
feeFactorLsh79[38], _ = new(big.Int).SetString("10749039466425620", 10)
feeFactorLsh79[39], _ = new(big.Int).SetString("11831405087254648", 10)
feeFactorLsh79[40], _ = new(big.Int).SetString("13022758617264914", 10)
feeFactorLsh79[41], _ = new(big.Int).SetString("14334074503647984", 10)
feeFactorLsh79[42], _ = new(big.Int).SetString("15777432256460256", 10)
feeFactorLsh79[43], _ = new(big.Int).SetString("17366127722012378", 10)
feeFactorLsh79[44], _ = new(big.Int).SetString("19114795560840448", 10)
feeFactorLsh79[45], _ = new(big.Int).SetString("21039544058494708", 10)
feeFactorLsh79[46], _ = new(big.Int).SetString("23158103510989148", 10)
feeFactorLsh79[47], _ = new(big.Int).SetString("25489989551801104", 10)
feeFactorLsh79[48], _ = new(big.Int).SetString("28056682924947216", 10)
feeFactorLsh79[49], _ = new(big.Int).SetString("30881827360160752", 10)
feeFactorLsh79[50], _ = new(big.Int).SetString("33991447372945972", 10)
feeFactorLsh79[51], _ = new(big.Int).SetString("37414187995827888", 10)
feeFactorLsh79[52], _ = new(big.Int).SetString("41181578649142096", 10)
feeFactorLsh79[53], _ = new(big.Int).SetString("45328323582075176", 10)
feeFactorLsh79[54], _ = new(big.Int).SetString("49892621559424256", 10)
feeFactorLsh79[55], _ = new(big.Int).SetString("54916517738950528", 10)
feeFactorLsh79[56], _ = new(big.Int).SetString("60446290980731456", 10)
feeFactorLsh79[57], _ = new(big.Int).SetString("66532880156306032", 10)
feeFactorLsh79[58], _ = new(big.Int).SetString("73232353384667904", 10)
feeFactorLsh79[59], _ = new(big.Int).SetString("80606424517586032", 10)
feeFactorLsh79[60], _ = new(big.Int).SetString("88723021631988016", 10)
feeFactorLsh79[61], _ = new(big.Int).SetString("97656912766212992", 10)
feeFactorLsh79[62], _ = new(big.Int).SetString("107490394664256192", 10)
feeFactorLsh79[63], _ = new(big.Int).SetString("118314050872546240", 10)
feeFactorLsh79[64], _ = new(big.Int).SetString("130227586172649136", 10)
feeFactorLsh79[65], _ = new(big.Int).SetString("143340745036479856", 10)
feeFactorLsh79[66], _ = new(big.Int).SetString("157774322564602240", 10)
feeFactorLsh79[67], _ = new(big.Int).SetString("173661277220123776", 10)
feeFactorLsh79[68], _ = new(big.Int).SetString("191147955608404480", 10)
feeFactorLsh79[69], _ = new(big.Int).SetString("210395440584946624", 10)
feeFactorLsh79[70], _ = new(big.Int).SetString("231581035109891488", 10)
feeFactorLsh79[71], _ = new(big.Int).SetString("254899895518011040", 10)
feeFactorLsh79[72], _ = new(big.Int).SetString("280566829249471584", 10)
feeFactorLsh79[73], _ = new(big.Int).SetString("308818273601607552", 10)
feeFactorLsh79[74], _ = new(big.Int).SetString("339914473729459712", 10)
feeFactorLsh79[75], _ = new(big.Int).SetString("374141879958278144", 10)
feeFactorLsh79[76], _ = new(big.Int).SetString("411815786491420928", 10)
feeFactorLsh79[77], _ = new(big.Int).SetString("453283235820751744", 10)
feeFactorLsh79[78], _ = new(big.Int).SetString("498926215594241536", 10)
feeFactorLsh79[79], _ = new(big.Int).SetString("549165177389505280", 10)
feeFactorLsh79[80], _ = new(big.Int).SetString("604462909807314560", 10)
feeFactorLsh79[81], _ = new(big.Int).SetString("665328801563060352", 10)
feeFactorLsh79[82], _ = new(big.Int).SetString("732323533846679040", 10)
feeFactorLsh79[83], _ = new(big.Int).SetString("806064245175860224", 10)
feeFactorLsh79[84], _ = new(big.Int).SetString("887230216319880192", 10)
feeFactorLsh79[85], _ = new(big.Int).SetString("976569127662129920", 10)
feeFactorLsh79[86], _ = new(big.Int).SetString("1074903946642562048", 10)
feeFactorLsh79[87], _ = new(big.Int).SetString("1183140508725462528", 10)
feeFactorLsh79[88], _ = new(big.Int).SetString("1302275861726491392", 10)
feeFactorLsh79[89], _ = new(big.Int).SetString("1433407450364798464", 10)
feeFactorLsh79[90], _ = new(big.Int).SetString("1577743225646022400", 10)
feeFactorLsh79[91], _ = new(big.Int).SetString("1736612772201237760", 10)
feeFactorLsh79[92], _ = new(big.Int).SetString("1911479556084044800", 10)
feeFactorLsh79[93], _ = new(big.Int).SetString("2103954405849466368", 10)
feeFactorLsh79[94], _ = new(big.Int).SetString("2315810351098914816", 10)
feeFactorLsh79[95], _ = new(big.Int).SetString("2548998955180110336", 10)
feeFactorLsh79[96], _ = new(big.Int).SetString("2805668292494715904", 10)
feeFactorLsh79[97], _ = new(big.Int).SetString("3088182736016075264", 10)
feeFactorLsh79[98], _ = new(big.Int).SetString("3399144737294597120", 10)
feeFactorLsh79[99], _ = new(big.Int).SetString("3741418799582781440", 10)
feeFactorLsh79[100], _ = new(big.Int).SetString("4118157864914209280", 10)
feeFactorLsh79[101], _ = new(big.Int).SetString("4532832358207517184", 10)
feeFactorLsh79[102], _ = new(big.Int).SetString("4989262155942415360", 10)
feeFactorLsh79[103], _ = new(big.Int).SetString("5491651773895053312", 10)
feeFactorLsh79[104], _ = new(big.Int).SetString("6044629098073145344", 10)
feeFactorLsh79[105], _ = new(big.Int).SetString("6653288015630603264", 10)
feeFactorLsh79[106], _ = new(big.Int).SetString("7323235338466789376", 10)
feeFactorLsh79[107], _ = new(big.Int).SetString("8060642451758603264", 10)
feeFactorLsh79[108], _ = new(big.Int).SetString("8872302163198801920", 10)
feeFactorLsh79[109], _ = new(big.Int).SetString("9765691276621297664", 10)
feeFactorLsh79[110], _ = new(big.Int).SetString("10749039466425620480", 10)
feeFactorLsh79[111], _ = new(big.Int).SetString("11831405087254624256", 10)
feeFactorLsh79[112], _ = new(big.Int).SetString("13022758617264914432", 10)
feeFactorLsh79[113], _ = new(big.Int).SetString("14334074503647985664", 10)
feeFactorLsh79[114], _ = new(big.Int).SetString("15777432256460224512", 10)
feeFactorLsh79[115], _ = new(big.Int).SetString("17366127722012377088", 10)
feeFactorLsh79[116], _ = new(big.Int).SetString("19114795560840450048", 10)
feeFactorLsh79[117], _ = new(big.Int).SetString("21039544058494664704", 10)
feeFactorLsh79[118], _ = new(big.Int).SetString("23158103510989152256", 10)
feeFactorLsh79[119], _ = new(big.Int).SetString("25489989551801102336", 10)
feeFactorLsh79[120], _ = new(big.Int).SetString("28056682924947156992", 10)
feeFactorLsh79[121], _ = new(big.Int).SetString("30881827360160751616", 10)
feeFactorLsh79[122], _ = new(big.Int).SetString("33991447372945973248", 10)
feeFactorLsh79[123], _ = new(big.Int).SetString("37414187995827814400", 10)
feeFactorLsh79[124], _ = new(big.Int).SetString("41181578649142091776", 10)
feeFactorLsh79[125], _ = new(big.Int).SetString("45328323582075174912", 10)
feeFactorLsh79[126], _ = new(big.Int).SetString("49892621559424155648", 10)
feeFactorLsh79[127], _ = new(big.Int).SetString("54916517738950524928", 10)
feeFactorLsh79[128], _ = new(big.Int).SetString("60446290980731453440", 10)
feeFactorLsh79[129], _ = new(big.Int).SetString("66532880156306030592", 10)
feeFactorLsh79[130], _ = new(big.Int).SetString("73232353384667897856", 10)
feeFactorLsh79[131], _ = new(big.Int).SetString("80606424517586026496", 10)
feeFactorLsh79[132], _ = new(big.Int).SetString("88723021631988023296", 10)
feeFactorLsh79[133], _ = new(big.Int).SetString("97656912766212980736", 10)
feeFactorLsh79[134], _ = new(big.Int).SetString("107490394664256208896", 10)
feeFactorLsh79[135], _ = new(big.Int).SetString("118314050872546246656", 10)
feeFactorLsh79[136], _ = new(big.Int).SetString("130227586172649144320", 10)
feeFactorLsh79[137], _ = new(big.Int).SetString("143340745036479856640", 10)
feeFactorLsh79[138], _ = new(big.Int).SetString("157774322564602232832", 10)
feeFactorLsh79[139], _ = new(big.Int).SetString("173661277220123770880", 10)
feeFactorLsh79[140], _ = new(big.Int).SetString("191147955608404492288", 10)
feeFactorLsh79[141], _ = new(big.Int).SetString("210395440584946647040", 10)
feeFactorLsh79[142], _ = new(big.Int).SetString("231581035109891506176", 10)
feeFactorLsh79[143], _ = new(big.Int).SetString("254899895518011031552", 10)
feeFactorLsh79[144], _ = new(big.Int).SetString("280566829249471578112", 10)
feeFactorLsh79[145], _ = new(big.Int).SetString("308818273601607499776", 10)
feeFactorLsh79[146], _ = new(big.Int).SetString("339914473729459748864", 10)
feeFactorLsh79[147], _ = new(big.Int).SetString("374141879958278111232", 10)
feeFactorLsh79[148], _ = new(big.Int).SetString("411815786491420934144", 10)
feeFactorLsh79[149], _ = new(big.Int).SetString("453283235820751683584", 10)
feeFactorLsh79[150], _ = new(big.Int).SetString("498926215594241556480", 10)
feeFactorLsh79[151], _ = new(big.Int).SetString("549165177389505314816", 10)
feeFactorLsh79[152], _ = new(big.Int).SetString("604462909807314599936", 10)
feeFactorLsh79[153], _ = new(big.Int).SetString("665328801563060338688", 10)
feeFactorLsh79[154], _ = new(big.Int).SetString("732323533846678994944", 10)
feeFactorLsh79[155], _ = new(big.Int).SetString("806064245175860330496", 10)
feeFactorLsh79[156], _ = new(big.Int).SetString("887230216319880134656", 10)
feeFactorLsh79[157], _ = new(big.Int).SetString("976569127662129774592", 10)
feeFactorLsh79[158], _ = new(big.Int).SetString("1074903946642561957888", 10)
feeFactorLsh79[159], _ = new(big.Int).SetString("1183140508725462433792", 10)
feeFactorLsh79[160], _ = new(big.Int).SetString("1302275861726491312128", 10)
feeFactorLsh79[161], _ = new(big.Int).SetString("1433407450364798566400", 10)
feeFactorLsh79[162], _ = new(big.Int).SetString("1577743225646022393856", 10)
feeFactorLsh79[163], _ = new(big.Int).SetString("1736612772201237577728", 10)
feeFactorLsh79[164], _ = new(big.Int).SetString("1911479556084044922880", 10)
feeFactorLsh79[165], _ = new(big.Int).SetString("2103954405849466404864", 10)
feeFactorLsh79[166], _ = new(big.Int).SetString("2315810351098914930688", 10)
feeFactorLsh79[167], _ = new(big.Int).SetString("2548998955180110643200", 10)
feeFactorLsh79[168], _ = new(big.Int).SetString("2805668292494716043264", 10)
feeFactorLsh79[169], _ = new(big.Int).SetString("3088182736016075390976", 10)
feeFactorLsh79[170], _ = new(big.Int).SetString("3399144737294597488640", 10)
feeFactorLsh79[171], _ = new(big.Int).SetString("3741418799582781374464", 10)
feeFactorLsh79[172], _ = new(big.Int).SetString("4118157864914209210368", 10)
feeFactorLsh79[173], _ = new(big.Int).SetString("4532832358207517097984", 10)
feeFactorLsh79[174], _ = new(big.Int).SetString("4989262155942415302656", 10)
feeFactorLsh79[175], _ = new(big.Int).SetString("5491651773895053148160", 10)
feeFactorLsh79[176], _ = new(big.Int).SetString("6044629098073145999360", 10)
feeFactorLsh79[177], _ = new(big.Int).SetString("6653288015630603124736", 10)
feeFactorLsh79[178], _ = new(big.Int).SetString("7323235338466790211584", 10)
feeFactorLsh79[179], _ = new(big.Int).SetString("8060642451758603304960", 10)
feeFactorLsh79[180], _ = new(big.Int).SetString("8872302163198802395136", 10)
feeFactorLsh79[181], _ = new(big.Int).SetString("9765691276621298794496", 10)
feeFactorLsh79[182], _ = new(big.Int).SetString("10749039466425620627456", 10)
feeFactorLsh79[183], _ = new(big.Int).SetString("11831405087254623813632", 10)
feeFactorLsh79[184], _ = new(big.Int).SetString("13022758617264913645568", 10)
feeFactorLsh79[185], _ = new(big.Int).SetString("14334074503647983566848", 10)
feeFactorLsh79[186], _ = new(big.Int).SetString("15777432256460225511424", 10)
feeFactorLsh79[187], _ = new(big.Int).SetString("17366127722012377874432", 10)
feeFactorLsh79[188], _ = new(big.Int).SetString("19114795560840447655936", 10)
feeFactorLsh79[189], _ = new(big.Int).SetString("21039544058494662475776", 10)
feeFactorLsh79[190], _ = new(big.Int).SetString("23158103510989150355456", 10)
feeFactorLsh79[191], _ = new(big.Int).SetString("25489989551801103286272", 10)
feeFactorLsh79[192], _ = new(big.Int).SetString("28056682924947158335488", 10)
feeFactorLsh79[193], _ = new(big.Int).SetString("30881827360160754958336", 10)
feeFactorLsh79[194], _ = new(big.Int).SetString("33991447372945971740672", 10)
feeFactorLsh79[195], _ = new(big.Int).SetString("37414187995827814793216", 10)
feeFactorLsh79[196], _ = new(big.Int).SetString("41181578649142095249408", 10)
feeFactorLsh79[197], _ = new(big.Int).SetString("45328323582075170979840", 10)
feeFactorLsh79[198], _ = new(big.Int).SetString("49892621559424153026560", 10)
feeFactorLsh79[199], _ = new(big.Int).SetString("54916517738950525190144", 10)
feeFactorLsh79[200], _ = new(big.Int).SetString("60446290980731462090752", 10)
feeFactorLsh79[201], _ = new(big.Int).SetString("66532880156306033344512", 10)
feeFactorLsh79[202], _ = new(big.Int).SetString("73232353384667900018688", 10)
feeFactorLsh79[203], _ = new(big.Int).SetString("80606424517586030952448", 10)
feeFactorLsh79[204], _ = new(big.Int).SetString("88723021631988017659904", 10)
feeFactorLsh79[205], _ = new(big.Int).SetString("97656912766212987944960", 10)
feeFactorLsh79[206], _ = new(big.Int).SetString("107490394664256189497344", 10)
feeFactorLsh79[207], _ = new(big.Int).SetString("118314050872546242330624", 10)
feeFactorLsh79[208], _ = new(big.Int).SetString("130227586172649140649984", 10)
feeFactorLsh79[209], _ = new(big.Int).SetString("143340745036479844057088", 10)
feeFactorLsh79[210], _ = new(big.Int).SetString("157774322564602238337024", 10)
feeFactorLsh79[211], _ = new(big.Int).SetString("173661277220123782938624", 10)
feeFactorLsh79[212], _ = new(big.Int).SetString("191147955608404493336576", 10)
feeFactorLsh79[213], _ = new(big.Int).SetString("210395440584946633146368", 10)
feeFactorLsh79[214], _ = new(big.Int).SetString("231581035109891511943168", 10)
feeFactorLsh79[215], _ = new(big.Int).SetString("254899895518011024474112", 10)
feeFactorLsh79[216], _ = new(big.Int).SetString("280566829249471583354880", 10)
feeFactorLsh79[217], _ = new(big.Int).SetString("308818273601607516028928", 10)
feeFactorLsh79[218], _ = new(big.Int).SetString("339914473729459734183936", 10)
feeFactorLsh79[219], _ = new(big.Int).SetString("374141879958278114377728", 10)
feeFactorLsh79[220], _ = new(big.Int).SetString("411815786491420902162432", 10)
feeFactorLsh79[221], _ = new(big.Int).SetString("453283235820751760130048", 10)
feeFactorLsh79[222], _ = new(big.Int).SetString("498926215594241513488384", 10)
feeFactorLsh79[223], _ = new(big.Int).SetString("549165177389505251901440", 10)
feeFactorLsh79[224], _ = new(big.Int).SetString("604462909807314587353088", 10)
feeFactorLsh79[225], _ = new(big.Int).SetString("6044629098073145873530880", 10)
feeFactorLsh79[226], _ = new(big.Int).SetString("60446290980731458735308800", 10)
feeFactorLsh79[227], _ = new(big.Int).SetString("604462909807314587353088000", 10)
feeFactorLsh79[228], _ = new(big.Int).SetString("6044629098073145873530880000", 10)
feeFactorLsh79[229], _ = new(big.Int).SetString("60446290980731458735308800000", 10)
feeFactorLsh79[230], _ = new(big.Int).SetString("604462909807314587353088000000", 10)
feeFactorLsh79[231], _ = new(big.Int).SetString("6044629098073145873530880000000", 10)
feeFactorLsh79[232], _ = new(big.Int).SetString("60446290980731458735308800000000", 10)
feeFactorLsh79[233], _ = new(big.Int).SetString("604462909807314587353088000000000", 10)
feeFactorLsh79[234], _ = new(big.Int).SetString("6044629098073145873530880000000000", 10)
feeFactorLsh79[235], _ = new(big.Int).SetString("60446290980731458735308800000000000", 10)
feeFactorLsh79[236], _ = new(big.Int).SetString("604462909807314587353088000000000000", 10)
feeFactorLsh79[237], _ = new(big.Int).SetString("6044629098073145873530880000000000000", 10)
feeFactorLsh79[238], _ = new(big.Int).SetString("60446290980731458735308800000000000000", 10)
feeFactorLsh79[239], _ = new(big.Int).SetString("604462909807314587353088000000000000000", 10)
feeFactorLsh79[240], _ = new(big.Int).SetString("6044629098073145873530880000000000000000", 10)
feeFactorLsh79[241], _ = new(big.Int).SetString("60446290980731458735308800000000000000000", 10)
feeFactorLsh79[242], _ = new(big.Int).SetString("604462909807314587353088000000000000000000", 10)
feeFactorLsh79[243], _ = new(big.Int).SetString("6044629098073145873530880000000000000000000", 10)
feeFactorLsh79[244], _ = new(big.Int).SetString("60446290980731458735308800000000000000000000", 10)
feeFactorLsh79[245], _ = new(big.Int).SetString("604462909807314587353088000000000000000000000", 10)
feeFactorLsh79[246], _ = new(big.Int).SetString("6044629098073145873530880000000000000000000000", 10)
feeFactorLsh79[247], _ = new(big.Int).SetString("60446290980731458735308800000000000000000000000", 10)
feeFactorLsh79[248], _ = new(big.Int).SetString("604462909807314587353088000000000000000000000000", 10)
feeFactorLsh79[249], _ = new(big.Int).SetString("6044629098073145873530880000000000000000000000000", 10)
feeFactorLsh79[250], _ = new(big.Int).SetString("60446290980731458735308800000000000000000000000000", 10)
feeFactorLsh79[251], _ = new(big.Int).SetString("604462909807314587353088000000000000000000000000000", 10)
feeFactorLsh79[252], _ = new(big.Int).SetString("6044629098073145873530880000000000000000000000000000", 10)
feeFactorLsh79[253], _ = new(big.Int).SetString("60446290980731458735308800000000000000000000000000000", 10)
feeFactorLsh79[254], _ = new(big.Int).SetString("604462909807314587353088000000000000000000000000000000", 10)
feeFactorLsh79[255], _ = new(big.Int).SetString("6044629098073145873530880000000000000000000000000000000", 10)
}

+ 29
- 0
common/fee_test.go

@ -0,0 +1,29 @@
package common
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCalcFeeAmount(t *testing.T) {
v := big.NewInt(1000)
feeAmount := CalcFeeAmount(v, FeeSelector(225)) // 1000%
assert.Equal(t, "10000", feeAmount.String())
feeAmount = CalcFeeAmount(v, FeeSelector(224)) // 100%
assert.Equal(t, "1000", feeAmount.String())
feeAmount = CalcFeeAmount(v, FeeSelector(200)) // 10%
assert.Equal(t, "100", feeAmount.String())
feeAmount = CalcFeeAmount(v, FeeSelector(193)) // 5.11%
assert.Equal(t, "51", feeAmount.String())
feeAmount = CalcFeeAmount(v, FeeSelector(176)) // 1%
assert.Equal(t, "10", feeAmount.String())
feeAmount = CalcFeeAmount(v, FeeSelector(152)) // 0.1%
assert.Equal(t, "1", feeAmount.String())
}

+ 3
- 0
coordinator/coordinator_test.go

@ -25,6 +25,7 @@ func newTestModules(t *testing.T) (*txselector.TxSelector, *batchbuilder.BatchBu
synchDBPath, err := ioutil.TempDir("", "tmpSynchDB")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(synchDBPath))
synchSdb, err := statedb.NewStateDB(synchDBPath, statedb.TypeSynchronizer, nLevels)
assert.Nil(t, err)
@ -44,11 +45,13 @@ func newTestModules(t *testing.T) (*txselector.TxSelector, *batchbuilder.BatchBu
txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(txselDir))
txsel, err := txselector.NewTxSelector(txselDir, synchSdb, l2DB, 10, 10, 10)
assert.Nil(t, err)
bbDir, err := ioutil.TempDir("", "tmpBatchBuilderDB")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(bbDir))
bb, err := batchbuilder.NewBatchBuilder(bbDir, synchSdb, nil, 0, uint64(nLevels))
assert.Nil(t, err)

+ 57
- 10
db/statedb/statedb.go

@ -5,10 +5,10 @@ import (
"fmt"
"math/big"
"os"
"os/exec"
"strconv"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/log"
"github.com/iden3/go-merkletree"
"github.com/iden3/go-merkletree/db"
"github.com/iden3/go-merkletree/db/pebble"
@ -115,7 +115,7 @@ func NewStateDB(path string, typ TypeStateDB, nLevels int) (*StateDB, error) {
}
// make reset (get checkpoint) at currentBatch
err = sdb.Reset(sdb.currentBatch)
err = sdb.reset(sdb.currentBatch, false)
if err != nil {
return nil, err
}
@ -174,6 +174,8 @@ func (s *StateDB) MakeCheckpoint() error {
if err != nil {
return err
}
} else if err != nil && !os.IsNotExist(err) {
return err
}
// execute Checkpoint
@ -196,14 +198,59 @@ func (s *StateDB) DeleteCheckpoint(batchNum common.BatchNum) error {
return os.RemoveAll(checkpointPath)
}
func pebbleMakeCheckpoint(source, dest string) error {
// Remove dest folder (if it exists) before doing the checkpoint
if _, err := os.Stat(dest); !os.IsNotExist(err) {
err := os.RemoveAll(dest)
if err != nil {
return err
}
} else if err != nil && !os.IsNotExist(err) {
return err
}
sto, err := pebble.NewPebbleStorage(source, false)
if err != nil {
return err
}
defer func() {
errClose := sto.Pebble().Close()
if errClose != nil {
log.Errorw("Pebble.Close", "err", errClose)
}
}()
// execute Checkpoint
err = sto.Pebble().Checkpoint(dest)
if err != nil {
return err
}
return nil
}
// Reset resets the StateDB to the checkpoint at the given batchNum. Reset
// does not delete the checkpoints between old current and the new current,
// those checkpoints will remain in the storage, and eventually will be
// deleted when MakeCheckpoint overwrites them.
func (s *StateDB) Reset(batchNum common.BatchNum) error {
return s.reset(batchNum, true)
}
// reset resets the StateDB to the checkpoint at the given batchNum. Reset
// does not delete the checkpoints between old current and the new current,
// those checkpoints will remain in the storage, and eventually will be
// deleted when MakeCheckpoint overwrites them. `closeCurrent` will close the
// currently opened db before doing the reset.
func (s *StateDB) reset(batchNum common.BatchNum, closeCurrent bool) error {
checkpointPath := s.path + PathBatchNum + strconv.Itoa(int(batchNum))
currentPath := s.path + PathCurrent
if closeCurrent {
if err := s.db.Pebble().Close(); err != nil {
return err
}
}
// remove 'current'
err := os.RemoveAll(currentPath)
if err != nil {
@ -222,8 +269,7 @@ func (s *StateDB) Reset(batchNum common.BatchNum) error {
}
// copy 'BatchNumX' to 'current'
cmd := exec.Command("cp", "-r", checkpointPath, currentPath) //nolint:gosec
err = cmd.Run()
err = pebbleMakeCheckpoint(checkpointPath, currentPath)
if err != nil {
return err
}
@ -482,20 +528,21 @@ func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) er
return fmt.Errorf("Checkpoint not exist in Synchronizer")
}
if err := l.db.Pebble().Close(); err != nil {
return err
}
// remove 'current'
err := os.RemoveAll(currentPath)
if err != nil {
return err
}
// copy synchronizer'BatchNumX' to 'current'
cmd := exec.Command("cp", "-r", synchronizerCheckpointPath, currentPath) //nolint:gosec
err = cmd.Run()
err = pebbleMakeCheckpoint(synchronizerCheckpointPath, currentPath)
if err != nil {
return err
}
// copy synchronizer-'BatchNumX' to 'BatchNumX'
cmd = exec.Command("cp", "-r", synchronizerCheckpointPath, checkpointPath) //nolint:gosec
err = cmd.Run()
// copy synchronizer'BatchNumX' to 'BatchNumX'
err = pebbleMakeCheckpoint(synchronizerCheckpointPath, checkpointPath)
if err != nil {
return err
}
@ -522,5 +569,5 @@ func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) er
return nil
}
// use checkpoint from LocalStateDB
return l.StateDB.Reset(batchNum)
return l.StateDB.reset(batchNum, true)
}

+ 7
- 0
db/statedb/statedb_test.go

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"math/big"
"os"
"testing"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
@ -38,6 +39,7 @@ func newAccount(t *testing.T, i int) *common.Account {
func TestNewStateDBIntermediateState(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)
@ -119,6 +121,7 @@ func TestNewStateDBIntermediateState(t *testing.T) {
func TestStateDBWithoutMT(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)
@ -172,6 +175,7 @@ func TestStateDBWithoutMT(t *testing.T) {
func TestStateDBWithMT(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
assert.Nil(t, err)
@ -223,6 +227,7 @@ func TestStateDBWithMT(t *testing.T) {
func TestCheckpoints(t *testing.T) {
dir, err := ioutil.TempDir("", "sdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
assert.Nil(t, err)
@ -290,6 +295,7 @@ func TestCheckpoints(t *testing.T) {
// Create a LocalStateDB from the initial StateDB
dirLocal, err := ioutil.TempDir("", "ldb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dirLocal))
ldb, err := NewLocalStateDB(dirLocal, sdb, TypeBatchBuilder, 32)
assert.Nil(t, err)
@ -310,6 +316,7 @@ func TestCheckpoints(t *testing.T) {
// Create a 2nd LocalStateDB from the initial StateDB
dirLocal2, err := ioutil.TempDir("", "ldb2")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dirLocal2))
ldb2, err := NewLocalStateDB(dirLocal2, sdb, TypeBatchBuilder, 32)
assert.Nil(t, err)

+ 17
- 2
db/statedb/txprocessors.go

@ -2,14 +2,16 @@ package statedb
import (
"errors"
"io/ioutil"
"math/big"
"os"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/log"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-merkletree"
"github.com/iden3/go-merkletree/db"
"github.com/iden3/go-merkletree/db/memory"
"github.com/iden3/go-merkletree/db/pebble"
)
var (
@ -61,7 +63,20 @@ func (s *StateDB) ProcessTxs(l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []
// TBD if ExitTree is only in memory or stored in disk, for the moment
// only needed in memory
if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
exitTree, err = merkletree.NewMerkleTree(memory.NewMemoryStorage(), s.mt.MaxLevels())
tmpDir, err := ioutil.TempDir("", "hermez-statedb-exittree")
if err != nil {
return nil, nil, err
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
log.Errorw("Deleting statedb temp exit tree", "err", err)
}
}()
sto, err := pebble.NewPebbleStorage(tmpDir, false)
if err != nil {
return nil, nil, err
}
exitTree, err = merkletree.NewMerkleTree(sto, s.mt.MaxLevels())
if err != nil {
return nil, nil, err
}

+ 4
- 0
db/statedb/txprocessors_test.go

@ -2,6 +2,7 @@ package statedb
import (
"io/ioutil"
"os"
"testing"
"github.com/hermeznetwork/hermez-node/common"
@ -17,6 +18,7 @@ func TestProcessTxsSynchronizer(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
assert.Nil(t, err)
@ -81,6 +83,7 @@ WIP
func TestProcessTxsBatchBuilder(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
assert.Nil(t, err)
@ -130,6 +133,7 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
func TestZKInputsGeneration(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
assert.Nil(t, err)

+ 2
- 0
db/statedb/utils_test.go

@ -3,6 +3,7 @@ package statedb
import (
"io/ioutil"
"math/big"
"os"
"testing"
ethCommon "github.com/ethereum/go-ethereum/common"
@ -15,6 +16,7 @@ import (
func TestGetIdx(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)

+ 5
- 0
eth/main_test.go

@ -115,6 +115,11 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
}()
ks = keystore.NewKeyStore(dir, keystore.LightScryptN, keystore.LightScryptP)
// Load ethereum accounts from private keys

+ 1
- 0
synchronizer/synchronizer_test.go

@ -29,6 +29,7 @@ func TestSync(t *testing.T) {
// Int State DB
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
stateDB, err := statedb.NewStateDB(dir, statedb.TypeSynchronizer, 32)
assert.Nil(t, err)

+ 2
- 0
txselector/txselector_test.go

@ -10,11 +10,13 @@ func initTest(t *testing.T, testSet string, maxL1UserTxs, maxL1OperatorTxs, maxT
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0)
require.Nil(t, err)
txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
require.Nil(t, err)
defer assert.Nil(t, os.RemoveAll(dir))
txsel, err := NewTxSelector(txselDir, sdb, l2DB, maxL1UserTxs, maxL1OperatorTxs, maxTxs)
require.Nil(t, err)

Loading…
Cancel
Save