-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_genesis.go
49 lines (40 loc) · 1020 Bytes
/
badger_genesis.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
package storage
import (
"github.com/MixinNetwork/mixin/common"
"github.com/dgraph-io/badger"
)
func (s *BadgerStore) LoadGenesis(rounds []*common.Round, snapshots []*common.SnapshotWithTopologicalOrder, transactions []*common.VersionedTransaction) error {
txn := s.snapshotsDB.NewTransaction(true)
defer txn.Discard()
if checkGenesisLoad(txn) {
return nil
}
for _, r := range rounds {
err := writeRound(txn, r.Hash, r)
if err != nil {
return err
}
}
for i, snap := range snapshots {
err := writeTransaction(txn, transactions[i])
if err != nil {
return err
}
err = writeSnapshot(txn, snap, transactions[i])
if err != nil {
return err
}
}
return txn.Commit()
}
func (s *BadgerStore) CheckGenesisLoad() (bool, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
return checkGenesisLoad(txn), nil
}
func checkGenesisLoad(txn *badger.Txn) bool {
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
it.Rewind()
return it.Valid()
}