-
Notifications
You must be signed in to change notification settings - Fork 202
/
hashToEpochIndex.go
48 lines (38 loc) · 1.06 KB
/
hashToEpochIndex.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
//go:generate protoc -I=proto -I=$GOPATH/src -I=$GOPATH/src/github.com/ElrondNetwork/protobuf/protobuf --gogoslick_out=. epochByHash.proto
package dblookupext
import (
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go/storage"
)
type epochByHashIndex struct {
marshalizer marshal.Marshalizer
storer storage.Storer
}
func newHashToEpochIndex(storer storage.Storer, marshalizer marshal.Marshalizer) *epochByHashIndex {
return &epochByHashIndex{
storer: storer,
marshalizer: marshalizer,
}
}
func (i *epochByHashIndex) getEpochByHash(hash []byte) (uint32, error) {
rawBytes, err := i.storer.Get(hash)
if err != nil {
return 0, err
}
record := &EpochByHash{}
err = i.marshalizer.Unmarshal(record, rawBytes)
if err != nil {
return 0, err
}
return record.Epoch, nil
}
func (i *epochByHashIndex) saveEpochByHash(hash []byte, epoch uint32) error {
record := &EpochByHash{
Epoch: epoch,
}
rawBytes, err := i.marshalizer.Marshal(record)
if err != nil {
return err
}
return i.storer.Put(hash, rawBytes)
}