/
reveal.go
46 lines (35 loc) · 1.62 KB
/
reveal.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
package storage
import (
"encoding/base64"
"fmt"
"github.com/dgraph-io/badger"
"github.com/Gravity-Tech/gravity-core/common/account"
"github.com/ethereum/go-ethereum/common/hexutil"
)
func formRevealKey(nebulaId account.NebulaId, height int64, pulseId int64, commitHash []byte, oraclePubKey account.OraclesPubKey) []byte {
return formKey(string(RevealKey), hexutil.Encode(nebulaId[:]), fmt.Sprintf("%d", height), fmt.Sprintf("%d", pulseId), hexutil.Encode(commitHash), hexutil.Encode(oraclePubKey[:]))
}
func (storage *Storage) Reveal(nebulaId account.NebulaId, height int64, pulseId int64, commitHash []byte, oraclePubKey account.OraclesPubKey) ([]byte, error) {
b, err := storage.getValue(formRevealKey(nebulaId, height, pulseId, commitHash, oraclePubKey))
if err != nil {
return nil, err
}
return b, err
}
func (storage *Storage) Reveals(nebulaId account.NebulaId, height int64, pulseId int64) ([]string, error) {
it := storage.txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := formKey(string(RevealKey), hexutil.Encode(nebulaId[:]), fmt.Sprintf("%d", height), fmt.Sprintf("%d", pulseId))
var values []string
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
item.Value(func(v []byte) error {
values = append(values, base64.StdEncoding.EncodeToString(v))
return nil
})
}
return values, nil
}
func (storage *Storage) SetReveal(nebulaId account.NebulaId, height int64, pulseId int64, commitHash []byte, oraclePubKey account.OraclesPubKey, reveal []byte) error {
return storage.setValue(formRevealKey(nebulaId, height, pulseId, commitHash, oraclePubKey), reveal)
}