Compare commits

..

1 Commits

Author SHA1 Message Date
Oleksandr Brezhniev
e68c64db75 Faster synchronization with less calls to update Ethereum stats 2021-03-23 15:13:18 +02:00
19 changed files with 59 additions and 341 deletions

View File

@@ -1,29 +0,0 @@
name: goreleaser
on:
push:
tags:
- '*'
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

3
.gitignore vendored
View File

@@ -1,2 +1 @@
bin/
dist/
bin/

View File

@@ -1,35 +0,0 @@
before:
hooks:
- go mod download
- make migration-pack
builds:
- main: ./cli/node/main.go
binary: node
id: node
goos:
- linux
- darwin
goarch:
- amd64
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

View File

@@ -3,8 +3,8 @@
# Project variables.
PACKAGE := github.com/hermeznetwork/hermez-node
VERSION := $(shell git describe --tags --always)
COMMIT := $(shell git rev-parse --short HEAD)
DATE := $(shell date +%Y-%m-%dT%H:%M:%S%z)
BUILD := $(shell git rev-parse --short HEAD)
BUILD_DATE := $(shell date +%Y-%m-%dT%H:%M:%S%z)
PROJECT_NAME := $(shell basename "$(PWD)")
# Go related variables.
@@ -23,7 +23,7 @@ CONFIG ?= $(GOBASE)/cli/node/cfg.buidler.toml
POSTGRES_PASS ?= yourpasswordhere
# Use linker flags to provide version/build settings.
LDFLAGS=-ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)"
LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD) -X=main.Date=$(BUILD_DATE)"
# PID file will keep the process id of the server.
PID_PROOF_MOCK := /tmp/.$(PROJECT_NAME).proof.pid
@@ -94,11 +94,11 @@ install:
@echo " > Checking if there is any missing dependencies..."
$(GOENVVARS) go get $(GOCMD)/... $(get)
## run-node: Run Hermez node.
run-node:
## run: Run Hermez node.
run:
@bash -c "$(MAKE) clean build"
@echo " > Running $(PROJECT_NAME)"
@$(GOBIN)/$(GOBINARY) run --mode $(MODE) --cfg $(CONFIG)
@$(GOBIN)/$(GOBINARY) --mode $(MODE) --cfg $(CONFIG) run
## run-proof-mock: Run proof server mock API.
run-proof-mock: stop-proof-mock

View File

@@ -25,13 +25,13 @@ there are more information about the config file into [cli/node/README.md](cli/n
After setting the config, you can build and run the Hermez Node as a synchronizer:
```shell
$ make run-node
$ make run
```
Or build and run as a coordinator, and also passing the config file from other location:
```shell
$ MODE=sync CONFIG=cli/node/cfg.buidler.toml make run-node
$ MODE=sync CONFIG=cli/node/cfg.buidler.toml make run
```
To check the useful make commands:

View File

@@ -161,8 +161,6 @@ var SetBlockchain = `
> block
> batch
> block
ForceTransfer(0) D-B: 77777700000000000
> block
`
type testCommon struct {
@@ -364,12 +362,6 @@ func TestMain(m *testing.M) {
commonL1Txs = append(commonL1Txs, batch.L1CoordinatorTxs...)
}
}
// Add unforged L1 tx
unforgedTx := blocksData[len(blocksData)-1].Rollup.L1UserTxs[0]
if unforgedTx.BatchNum != nil {
panic("Unforged tx batch num should be nil")
}
commonL1Txs = append(commonL1Txs, unforgedTx)
// Generate Coordinators and add them to HistoryDB
const nCoords = 10
@@ -530,16 +522,11 @@ func TestMain(m *testing.M) {
WithdrawalDelay: uint64(3000),
}
stateAPIUpdater, err = stateapiupdater.NewUpdater(hdb, nodeConfig, &common.SCVariables{
stateAPIUpdater = stateapiupdater.NewUpdater(hdb, nodeConfig, &common.SCVariables{
Rollup: rollupVars,
Auction: auctionVars,
WDelayer: wdelayerVars,
}, constants, &stateapiupdater.RecommendedFeePolicy{
PolicyType: stateapiupdater.RecommendedFeePolicyTypeAvgLastHour,
})
if err != nil {
panic(err)
}
}, constants)
// Generate test data, as expected to be received/sended from/to the API
testCoords := genTestCoordinators(commonCoords)

View File

@@ -109,7 +109,7 @@ func (a *API) getFullBatch(c *gin.Context) {
// Fetch txs forged in the batch from historyDB
maxTxsPerBatch := uint(2048) //nolint:gomnd
txs, _, err := a.h.GetTxsAPI(
nil, nil, nil, nil, batchNum, nil, nil, nil, &maxTxsPerBatch, historydb.OrderAsc,
nil, nil, nil, nil, batchNum, nil, nil, &maxTxsPerBatch, historydb.OrderAsc,
)
if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
retSQLErr(err, c)

View File

@@ -2,12 +2,10 @@ package stateapiupdater
import (
"database/sql"
"fmt"
"sync"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/db/historydb"
"github.com/hermeznetwork/hermez-node/log"
"github.com/hermeznetwork/tracerr"
)
@@ -19,45 +17,11 @@ type Updater struct {
vars common.SCVariablesPtr
consts historydb.Constants
rw sync.RWMutex
rfp *RecommendedFeePolicy
}
// RecommendedFeePolicy describes how the recommended fee is calculated
type RecommendedFeePolicy struct {
PolicyType RecommendedFeePolicyType `validate:"required"`
StaticValue float64
}
// RecommendedFeePolicyType describes the different available recommended fee strategies
type RecommendedFeePolicyType string
const (
// RecommendedFeePolicyTypeStatic always give the same StaticValue as recommended fee
RecommendedFeePolicyTypeStatic RecommendedFeePolicyType = "Static"
// RecommendedFeePolicyTypeAvgLastHour set the recommended fee using the average fee of the last hour
RecommendedFeePolicyTypeAvgLastHour RecommendedFeePolicyType = "AvgLastHour"
)
func (rfp *RecommendedFeePolicy) valid() bool {
switch rfp.PolicyType {
case RecommendedFeePolicyTypeStatic:
if rfp.StaticValue == 0 {
log.Warn("RcommendedFee is set to 0 USD, and the policy is static")
}
return true
case RecommendedFeePolicyTypeAvgLastHour:
return true
default:
return false
}
}
// NewUpdater creates a new Updater
func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *common.SCVariables,
consts *historydb.Constants, rfp *RecommendedFeePolicy) (*Updater, error) {
if ok := rfp.valid(); !ok {
return nil, tracerr.Wrap(fmt.Errorf("Invalid recommended fee policy: %v", rfp.PolicyType))
}
consts *historydb.Constants) *Updater {
u := Updater{
hdb: hdb,
config: *config,
@@ -67,10 +31,9 @@ func NewUpdater(hdb *historydb.HistoryDB, config *historydb.NodeConfig, vars *co
ForgeDelay: config.ForgeDelay,
},
},
rfp: rfp,
}
u.SetSCVars(vars.AsPtr())
return &u, nil
return &u
}
// Store the State in the HistoryDB
@@ -102,27 +65,13 @@ func (u *Updater) SetSCVars(vars *common.SCVariablesPtr) {
// UpdateRecommendedFee update Status.RecommendedFee information
func (u *Updater) UpdateRecommendedFee() error {
switch u.rfp.PolicyType {
case RecommendedFeePolicyTypeStatic:
u.rw.Lock()
u.state.RecommendedFee = common.RecommendedFee{
ExistingAccount: u.rfp.StaticValue,
CreatesAccount: u.rfp.StaticValue,
CreatesAccountInternal: u.rfp.StaticValue,
}
u.rw.Unlock()
case RecommendedFeePolicyTypeAvgLastHour:
recommendedFee, err := u.hdb.GetRecommendedFee(u.config.MinFeeUSD, u.config.MaxFeeUSD)
if err != nil {
return tracerr.Wrap(err)
}
u.rw.Lock()
u.state.RecommendedFee = *recommendedFee
u.rw.Unlock()
default:
return tracerr.New("Invalid recommende fee policy")
recommendedFee, err := u.hdb.GetRecommendedFee(u.config.MinFeeUSD, u.config.MaxFeeUSD)
if err != nil {
return tracerr.Wrap(err)
}
u.rw.Lock()
u.state.RecommendedFee = *recommendedFee
u.rw.Unlock()
return nil
}

View File

@@ -529,15 +529,6 @@ paths:
type: integer
minimum: 1
maximum: 2049
- name: includePendingL1s
in: query
required: false
description: |
If set to true L1 transactions that have been added to the smart contract queue but haven't been forged yet are returned.
Warning: the correctness of the order is not guaranteed when using this filter, as the unforged transactions may change their position
once they are forged.
schema:
type: boolean
responses:
'200':
description: Successful operation.

View File

@@ -26,14 +26,6 @@ func (a *API) getHistoryTxs(c *gin.Context) {
retBadReq(err, c)
return
}
// IncludePendingL1s
includePendingL1s := new(bool)
*includePendingL1s = false
includePendingL1s, err = parseQueryBool("includePendingL1s", includePendingL1s, c)
if err != nil {
retBadReq(err, c)
return
}
// Pagination
fromItem, order, limit, err := parsePagination(c)
if err != nil {
@@ -43,7 +35,7 @@ func (a *API) getHistoryTxs(c *gin.Context) {
// Fetch txs from historyDB
txs, pendingItems, err := a.h.GetTxsAPI(
addr, bjj, tokenID, idx, batchNum, txType, includePendingL1s, fromItem, limit, order,
addr, bjj, tokenID, idx, batchNum, txType, fromItem, limit, order,
)
if err != nil {
retSQLErr(err, c)

View File

@@ -71,7 +71,7 @@ func (t txsSort) Less(i, j int) bool {
}
// i is forged
if jsf.BatchNum == nil {
return true // j is not forged
return false // j is not forged
}
// Both are forged
if *isf.BatchNum == *jsf.BatchNum {
@@ -111,7 +111,7 @@ func genTestTxs(
) []testTx {
txs := []testTx{}
// common.L1Tx ==> testTx
for i, l1 := range l1s {
for _, l1 := range l1s {
token := getTokenByID(l1.TokenID, tokens)
// l1.FromEthAddr and l1.FromBJJ can't be nil
fromEthAddr := string(apitypes.NewHezEthAddr(l1.FromEthAddr))
@@ -137,26 +137,15 @@ func genTestTxs(
},
Token: token,
}
// set BatchNum for user txs
if tx.L1Info.ToForgeL1TxsNum != nil {
// WARNING: this works just because the way "common" txs are generated using til
// any change on the test set could break this
// WARNING: this is an asumption, and the test input data can brake it easily
bn := common.BatchNum(*tx.L1Info.ToForgeL1TxsNum + 2)
tx.BatchNum = &bn
}
// If FromIdx is not nil
idxStr := idxToHez(l1.EffectiveFromIdx, token.Symbol)
tx.FromIdx = &idxStr
if i == len(l1s)-1 {
// Last tx of the L1 set is supposed to be unforged as per the til set.
// Unforged txs have some special propperties
tx.L1Info.DepositAmountSuccess = false
tx.L1Info.AmountSuccess = false
tx.BatchNum = nil
idxStrUnforged := idxToHez(l1.FromIdx, token.Symbol)
tx.FromIdx = &idxStrUnforged
}
// If tx has a normal ToIdx (>255), set FromEthAddr and FromBJJ
if l1.ToIdx >= common.UserThreshold {
// find account
@@ -272,26 +261,12 @@ func TestGetHistoryTxs(t *testing.T) {
fetchedTxs = append(fetchedTxs, tmp.(testTx))
}
}
// Get all (no filters, excluding unforged txs)
// Get all (no filters)
limit := 20
path := fmt.Sprintf("%s?limit=%d", endpoint, limit)
err := doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
assert.NoError(t, err)
forgedTxs := []testTx{}
for i := 0; i < len(tc.txs); i++ {
if tc.txs[i].BatchNum != nil {
forgedTxs = append(forgedTxs, tc.txs[i])
}
}
assertTxs(t, forgedTxs, fetchedTxs)
// Get all, including unforged txs
fetchedTxs = []testTx{}
path = fmt.Sprintf("%s?limit=%d&includePendingL1s=true", endpoint, limit)
err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
assert.NoError(t, err)
assertTxs(t, tc.txs, fetchedTxs)
// Get by ethAddr
account := tc.accounts[2]
fetchedTxs = []testTx{}
@@ -310,7 +285,7 @@ func TestGetHistoryTxs(t *testing.T) {
(tx.FromEthAddr != nil && *tx.FromEthAddr == string(account.EthAddr)) ||
(tx.ToEthAddr != nil && *tx.ToEthAddr == string(account.EthAddr)) ||
(tx.FromBJJ != nil && *tx.FromBJJ == string(account.PublicKey)) ||
(tx.ToBJJ != nil && *tx.ToBJJ == string(account.PublicKey)) && tx.BatchNum != nil {
(tx.ToBJJ != nil && *tx.ToBJJ == string(account.PublicKey)) {
accountTxs = append(accountTxs, tx)
}
}
@@ -337,7 +312,7 @@ func TestGetHistoryTxs(t *testing.T) {
assert.NoError(t, err)
tokenIDTxs := []testTx{}
for i := 0; i < len(tc.txs); i++ {
if tc.txs[i].BatchNum != nil && tc.txs[i].Token.TokenID == tokenID {
if tc.txs[i].Token.TokenID == tokenID {
tokenIDTxs = append(tokenIDTxs, tc.txs[i])
}
}
@@ -356,9 +331,6 @@ func TestGetHistoryTxs(t *testing.T) {
assert.NoError(t, err)
idxTxs := []testTx{}
for i := 0; i < len(tc.txs); i++ {
if tc.txs[i].BatchNum == nil {
continue
}
var fromIdx *common.Idx
if tc.txs[i].FromIdx != nil {
fromIdx, err = stringToIdx(*tc.txs[i].FromIdx, "")
@@ -416,7 +388,7 @@ func TestGetHistoryTxs(t *testing.T) {
assert.NoError(t, err)
txTypeTxs := []testTx{}
for i := 0; i < len(tc.txs); i++ {
if tc.txs[i].Type == txType && tc.txs[i].BatchNum != nil {
if tc.txs[i].Type == txType {
txTypeTxs = append(txTypeTxs, tc.txs[i])
}
}
@@ -448,9 +420,7 @@ func TestGetHistoryTxs(t *testing.T) {
assert.NoError(t, err)
flipedTxs := []testTx{}
for i := 0; i < len(tc.txs); i++ {
if tc.txs[len(tc.txs)-1-i].BatchNum != nil {
flipedTxs = append(flipedTxs, tc.txs[len(tc.txs)-1-i])
}
flipedTxs = append(flipedTxs, tc.txs[len(tc.txs)-1-i])
}
assertTxs(t, flipedTxs, fetchedTxs)
// Empty array

View File

@@ -73,9 +73,6 @@ of the node configuration. Please, check the `type APIServer` at
monitor the size of the folder to avoid running out of space.
- The node requires a PostgreSQL database. The parameters of the server and
database must be set in the `PostgreSQL` section.
- The node requires a web3 RPC server to work. The node has only been tested
with geth and may not work correctly with other ethereum nodes
implementations.
## Building

View File

@@ -145,11 +145,3 @@ Coordinator = true
BatchPath = "/tmp/iden3-test/hermez/batchesdebug"
LightScrypt = true
# RollupVerifierIndex = 0
[RecommendedFeePolicy]
# Strategy used to calculate the recommended fee that the API will expose.
# Available options:
# - Static: always return the same value (StaticValue) in USD
# - AvgLastHour: calculate using the average fee of the forged transactions during the last hour
PolicyType = "Static"
StaticValue = 0.99

View File

@@ -35,18 +35,18 @@ const (
)
var (
// version represents the program based on the git tag
version = "v0.1.0"
// commit represents the program based on the git commit
commit = "dev"
// date represents the date of application was built
date = ""
// Version represents the program based on the git tag
Version = "v0.1.0"
// Build represents the program based on the git commit
Build = "dev"
// Date represents the date of application was built
Date = ""
)
func cmdVersion(c *cli.Context) error {
fmt.Printf("Version = \"%v\"\n", version)
fmt.Printf("Build = \"%v\"\n", commit)
fmt.Printf("Date = \"%v\"\n", date)
fmt.Printf("Version = \"%v\"\n", Version)
fmt.Printf("Build = \"%v\"\n", Build)
fmt.Printf("Date = \"%v\"\n", Date)
return nil
}
@@ -421,7 +421,7 @@ func getConfigAPIServer(c *cli.Context) (*ConfigAPIServer, error) {
func main() {
app := cli.NewApp()
app.Name = "hermez-node"
app.Version = version
app.Version = Version
flags := []cli.Flag{
&cli.StringFlag{
Name: flagMode,

View File

@@ -8,7 +8,6 @@ import (
"github.com/BurntSushi/toml"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/api/stateapiupdater"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/priceupdater"
"github.com/hermeznetwork/tracerr"
@@ -300,8 +299,7 @@ type Node struct {
} `validate:"required"`
PostgreSQL PostgreSQL `validate:"required"`
Web3 struct {
// URL is the URL of the web3 ethereum-node RPC server. Only
// geth is officially supported.
// URL is the URL of the web3 ethereum-node RPC server
URL string `validate:"required"`
} `validate:"required"`
Synchronizer struct {
@@ -348,9 +346,8 @@ type Node struct {
// can wait to stablish a SQL connection
SQLConnectionTimeout Duration
} `validate:"required"`
RecommendedFeePolicy stateapiupdater.RecommendedFeePolicy `validate:"required"`
Debug NodeDebug `validate:"required"`
Coordinator Coordinator `validate:"-"`
Debug NodeDebug `validate:"required"`
Coordinator Coordinator `validate:"-"`
}
// APIServer is the api server configuration parameters

View File

@@ -1,43 +1,3 @@
/*
Package coordinator handles all the logic related to forging batches as a
coordinator in the hermez network.
The forging of batches is done with a pipeline in order to allow multiple
batches being forged in parallel. The maximum number of batches that can be
forged in parallel is determined by the number of available proof servers.
The Coordinator begins with the pipeline stopped. The main Coordinator
goroutine keeps listening for synchronizer events sent by the node package,
which allow the coordinator to determine if the configured forger address is
allowed to forge at the current block or not. When the forger address becomes
allowed to forge, the pipeline is started, and when it terminates being allowed
to forge, the pipeline is stopped.
The Pipeline consists of two goroutines. The first one is in charge of
preparing a batch internally, which involves making a selection of transactions
and calculating the ZKInputs for the batch proof, and sending these ZKInputs to
an idle proof server. This goroutine will keep preparing batches while there
are idle proof servers, if the forging policy determines that a batch should be
forged in the current state. The second goroutine is in charge of waiting for
the proof server to finish computing the proof, retreiving it, prepare the
arguments for the `forgeBatch` Rollup transaction, and sending the result to
the TxManager. All the batch information moves between functions and
goroutines via the BatchInfo struct.
Finally, the TxManager contains a single goroutine that makes forgeBatch
ethereum transactions for the batches sent by the Pipeline, and keeps them in a
list to check them periodically. In the periodic checks, the ethereum
transaction is checked for successfulness, and it's only forgotten after a
number of confirmation blocks have passed after being successfully mined. At
any point if a transaction failure is detected, the TxManager can signal the
Coordinator to reset the Pipeline in order to reforge the failed batches.
The Coordinator goroutine acts as a manager. The synchronizer events (which
notify about new blocks and associated new state) that it receives are
broadcasted to the Pipeline and the TxManager. This allows the Coordinator,
Pipeline and TxManager to have a copy of the current hermez network state
required to perform their duties.
*/
package coordinator
import (

View File

@@ -457,7 +457,6 @@ func (hdb *HistoryDB) GetTxAPI(txID common.TxID) (*TxAPI, error) {
func (hdb *HistoryDB) GetTxsAPI(
ethAddr *ethCommon.Address, bjj *babyjub.PublicKeyComp,
tokenID *common.TokenID, idx *common.Idx, batchNum *uint, txType *common.TxType,
includePendingL1s *bool,
fromItem, limit *uint, order string,
) ([]TxAPI, uint64, error) {
// Warning: amount_success and deposit_amount_success have true as default for
@@ -555,16 +554,12 @@ func (hdb *HistoryDB) GetTxsAPI(
args = append(args, fromItem)
nextIsAnd = true
}
// Include pending L1 txs? (deafault false)
if includePendingL1s == nil || (includePendingL1s != nil && !*includePendingL1s) {
if nextIsAnd {
queryStr += "AND "
} else {
queryStr += "WHERE "
}
queryStr += "tx.batch_num IS NOT NULL "
if nextIsAnd {
queryStr += "AND "
} else {
queryStr += "WHERE "
}
queryStr += "tx.batch_num IS NOT NULL "
// pagination
queryStr += "ORDER BY tx.item_id "

View File

@@ -1,18 +1,3 @@
/*
Package node does the initialization of all the required objects to either run
as a synchronizer or as a coordinator.
The Node contains several goroutines that run in the background or that
periodically perform tasks. One of this goroutines periodically calls the
`Synchronizer.Sync` function, allowing the synchronization of one block at a
time. After every call to `Synchronizer.Sync`, the Node sends a message to the
Coordinator to notify it about the new synced block (and associated state) or
reorg (and resetted state) in case one happens.
Other goroutines perform tasks such as: updating the token prices, update
metrics stored in the historyDB, update recommended fee stored in the
historyDB, run the http API server, run the debug http API server, etc.
*/
package node
import (
@@ -288,16 +273,7 @@ func NewNode(mode Mode, cfg *config.Node) (*Node, error) {
return nil, tracerr.Wrap(err)
}
stateAPIUpdater, err := stateapiupdater.NewUpdater(
historyDB,
&hdbNodeCfg,
initSCVars,
&hdbConsts,
&cfg.RecommendedFeePolicy,
)
if err != nil {
return nil, tracerr.Wrap(err)
}
stateAPIUpdater := stateapiupdater.NewUpdater(historyDB, &hdbNodeCfg, initSCVars, &hdbConsts)
var coord *coordinator.Coordinator
if mode == ModeCoordinator {

View File

@@ -1,35 +1,3 @@
/*
Package synchronizer synchronizes the hermez network state by querying events
emitted by the three smart contracts: `Hermez.sol` (referred as Rollup here),
`HermezAuctionProtocol.sol` (referred as Auction here) and
`WithdrawalDelayer.sol` (referred as WDelayer here).
The main entry point for synchronization is the `Sync` function, which at most
will synchronize one ethereum block, and all the hermez events that happened in
that block. During a `Sync` call, a reorg can be detected; in such case, uncle
blocks will be discarded, and only in a future `Sync` call correct blocks will
be synced.
The synchronization of the events in each smart contracts are done
in the methods `rollupSync`, `auctionSync` and `wdelayerSync`, which in turn
use the interface code to read each smart contract state and events found in
"github.com/hermeznetwork/hermez-node/eth". After these three methods are
called, an object of type `common.BlockData` is built containing all the
updates and events that happened in that block, and it is inserted in the
HistoryDB in a single SQL transaction.
`rollupSync` is the method that synchronizes batches sent via the `forgeBatch`
transaction in `Hermez.sol`. In `rollupSync`, for every batch, the accounts
state is updated in the StateDB by processing all transactions that have been
forged in that batch.
The consistency of the stored data is guaranteed by the HistoryDB: All the
block information is inserted in a single SQL transaction at the end of the
`Sync` method, once the StateDB has been updated. And every time the
Synchronizer starts, it continues from the last block in the HistoryDB. The
StateDB stores updates organized by checkpoints for every batch, and each batch
is only accessed if it appears in the HistoryDB.
*/
package synchronizer
import (
@@ -55,6 +23,12 @@ const (
// errStrUnknownBlock is the string returned by geth when querying an
// unknown block
errStrUnknownBlock = "unknown block"
// updateEthBlockNumThreshold is a threshold of number of ethereum blocks left to synchronize, such that
// if we have more blocks to sync than the defined value we can aggressively skip calling UpdateEth
updateEthBlockNumThreshold = 100
// While having more blocks to sync than updateEthBlockNumThreshold, UpdateEth will be called once in a
// defined number of blocks
updateEthFrequencyDivider = 100
)
var (
@@ -560,8 +534,11 @@ func (s *Synchronizer) Sync(ctx context.Context,
log.Debugf("ethBlock: num: %v, parent: %v, hash: %v",
ethBlock.Num, ethBlock.ParentHash.String(), ethBlock.Hash.String())
if err := s.stats.UpdateEth(s.ethClient); err != nil {
return nil, nil, tracerr.Wrap(err)
if nextBlockNum+updateEthBlockNumThreshold >= s.stats.Eth.LastBlock.Num ||
nextBlockNum%updateEthFrequencyDivider == 0 {
if err := s.stats.UpdateEth(s.ethClient); err != nil {
return nil, nil, tracerr.Wrap(err)
}
}
log.Debugw("Syncing...",