forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncinginfo.go
69 lines (57 loc) · 2.1 KB
/
syncinginfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package state
import (
"context"
"errors"
"github.com/jackc/pgx/v4"
)
// SyncInfoDataOnStorage stores information regarding the syncing status of the node in the database
type SyncInfoDataOnStorage struct {
InitialSyncingBatch uint64
LastBatchNumberSeen uint64
LastBatchNumberConsolidated uint64
}
// SyncingInfo stores information regarding the syncing status of the node
type SyncingInfo struct {
InitialSyncingBlock uint64 // L2Block corresponding to InitialSyncingBatch
CurrentBlockNumber uint64 // last L2Block in state
EstimatedHighestBlock uint64 // estimated highest L2Block in state
// IsSynchronizing indicates if the node is syncing (true -> syncing, false -> fully synced)
IsSynchronizing bool
}
// GetSyncingInfo returns information regarding the syncing status of the node
func (p *State) GetSyncingInfo(ctx context.Context, dbTx pgx.Tx) (SyncingInfo, error) {
var info SyncingInfo
syncData, err := p.GetSyncInfoData(ctx, dbTx)
if errors.Is(err, ErrNotFound) {
return SyncingInfo{}, ErrStateNotSynchronized
} else if err != nil {
return SyncingInfo{}, err
}
info.InitialSyncingBlock, err = p.GetFirstL2BlockNumberForBatchNumber(ctx, syncData.InitialSyncingBatch, dbTx)
if errors.Is(err, ErrNotFound) {
return SyncingInfo{}, ErrStateNotSynchronized
} else if err != nil {
return SyncingInfo{}, err
}
lastBlockNumber, err := p.GetLastL2BlockNumber(ctx, dbTx)
if errors.Is(err, ErrNotFound) {
return SyncingInfo{}, ErrStateNotSynchronized
} else if err != nil {
return SyncingInfo{}, err
}
info.CurrentBlockNumber = lastBlockNumber
lastBatchNumber, err := p.GetLastBatchNumber(ctx, dbTx)
if errors.Is(err, ErrNotFound) {
return SyncingInfo{}, ErrStateNotSynchronized
} else if err != nil {
return SyncingInfo{}, err
}
info.IsSynchronizing = syncData.LastBatchNumberSeen > lastBatchNumber
if info.IsSynchronizing {
// Estimation of block counting 1 l2block per missing batch
info.EstimatedHighestBlock = lastBlockNumber + (syncData.LastBatchNumberConsolidated - lastBatchNumber)
} else {
info.EstimatedHighestBlock = lastBlockNumber
}
return info, nil
}