-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathoffchaindata.go
More file actions
89 lines (79 loc) · 2.47 KB
/
Copy pathoffchaindata.go
File metadata and controls
89 lines (79 loc) · 2.47 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package service
import (
"fmt"
"path/filepath"
"time"
"go.vocdoni.io/dvote/api/censusdb"
"go.vocdoni.io/dvote/data/downloader"
"go.vocdoni.io/dvote/db/metadb"
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/snapshot"
"go.vocdoni.io/dvote/vochain/offchaindatahandler"
"go.vocdoni.io/dvote/vochain/state"
"go.vocdoni.io/proto/build/go/models"
)
// OffChainDataHandler creates the offchain data downloader handler service and a censusDB.
func (vs *VocdoniService) OffChainDataHandler() error {
log.Infof("creating offchain data downloader service")
if vs.DataDownloader == nil {
vs.DataDownloader = downloader.NewDownloader(vs.Storage)
vs.DataDownloader.Start()
go vs.DataDownloader.PrintLogInfo(time.Second * 120)
}
if vs.CensusDB == nil {
db, err := metadb.New(vs.Config.DBType, filepath.Join(vs.Config.DataDir, "censusdb"))
if err != nil {
return err
}
vs.CensusDB = censusdb.NewCensusDB(db)
}
vs.OffChainData = offchaindatahandler.NewOffChainDataHandler(
vs.App,
vs.Indexer,
vs.DataDownloader,
vs.CensusDB,
vs.Config.SkipPreviousOffchainData,
)
snapshot.SetFnImportOffChainData(func(s *state.State) error {
log.Debugf("importing offchain data after snapshot restore")
// IPFS is not restored during StateSync, so:
// * fetch metadata from all accounts
// * fetch metadata for all elections
// * fetch censuses from all open elections
// all of these actions triggers pinning and repopulates IPFS
// accounts
accts, err := s.ListAccounts(true)
if err != nil {
return fmt.Errorf("couldn't list accounts: %w", err)
}
for addr, acct := range accts {
vs.OffChainData.OnSetAccount(addr.Bytes(), acct)
}
// elections
pids, err := s.ListProcessIDs(true)
if err != nil {
return fmt.Errorf("couldn't list process IDs: %w", err)
}
for _, pid := range pids {
p, err := s.Process(pid, true)
if err != nil {
log.Errorf("couldn't fetch process %x", pid)
}
if !(p.GetStatus() == models.ProcessStatus_READY ||
p.GetStatus() == models.ProcessStatus_PAUSED) {
// we want to download only censuses from open elections
// and skip downloading censuses from past elections, so if not READY or PAUSED
// set a nil CensusURI before passing to OffChainData.OnProcess()
p.CensusURI = nil
}
vs.OffChainData.OnProcess(p, 0)
}
if len(accts) > 0 || len(pids) > 0 {
if err := vs.OffChainData.Commit(0); err != nil {
log.Errorw(err, "offchaindata.Commit returned error")
}
}
return nil
})
return nil
}