/
consuls.go
94 lines (76 loc) · 2.1 KB
/
consuls.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package storage
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/Gravity-Tech/gravity-core/common/account"
)
type Consul struct {
PubKey account.ConsulPubKey
Value uint64
}
func formSignConsulsByConsulKey(pubKey account.ConsulPubKey, chainType account.ChainType, roundId int64) []byte {
prefix := ""
switch chainType {
case account.Waves:
prefix = "waves"
case account.Ethereum:
prefix = "ethereum"
}
return formKey(string(SignConsulsResultByConsulKey), hexutil.Encode(pubKey[:]), prefix, fmt.Sprintf("%d", roundId))
}
func (storage *Storage) Consuls() ([]Consul, error) {
var consuls []Consul
key := []byte(ConsulsKey)
item, err := storage.txn.Get(key)
if err != nil {
return nil, err
}
b, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &consuls)
if err != nil {
return nil, err
}
return consuls, err
}
func (storage *Storage) SetConsuls(consuls []Consul) error {
return storage.setValue([]byte(ConsulsKey), consuls)
}
func (storage *Storage) ConsulsCandidate() ([]Consul, error) {
var consuls []Consul
key := []byte(ConsulsCandidateKey)
item, err := storage.txn.Get(key)
if err != nil {
return nil, err
}
b, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &consuls)
if err != nil {
return nil, err
}
return consuls, err
}
func (storage *Storage) SetConsulsCandidate(consuls []Consul) error {
return storage.setValue([]byte(ConsulsCandidateKey), consuls)
}
func (storage *Storage) SignConsulsByConsul(consulPubKey account.ConsulPubKey, chainType account.ChainType, roundId int64) ([]byte, error) {
key := formSignConsulsByConsulKey(consulPubKey, chainType, roundId)
item, err := storage.txn.Get(key)
if err != nil {
return nil, err
}
b, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
return b, err
}
func (storage *Storage) SetSignConsuls(consulsPubKey account.ConsulPubKey, chainType account.ChainType, roundId int64, sign []byte) error {
return storage.setValue(formSignConsulsByConsulKey(consulsPubKey, chainType, roundId), sign)
}