forked from hyperledger-archives/burrow
/
validator.go
63 lines (51 loc) · 1.2 KB
/
validator.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
package validator
import (
"fmt"
"math/big"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/acm"
amino "github.com/tendermint/go-amino"
)
func New(publicKey crypto.PublicKey, power *big.Int) *Validator {
v := &Validator{
PublicKey: publicKey,
Power: power.Uint64(),
}
v.FillAddress()
return v
}
func (v *Validator) String() string {
return fmt.Sprintf("Validator{Address: %v, PublicKey: %v, Power: %v}", v.Address, v.PublicKey, v.Power)
}
func (v *Validator) FillAddress() {
if v.Address == nil {
address := v.PublicKey.GetAddress()
v.Address = &address
}
}
func (v *Validator) BigPower() *big.Int {
return new(big.Int).SetUint64(v.Power)
}
func (v *Validator) GetAddress() crypto.Address {
return *v.Address
}
func FromAccount(acc *acm.Account, power uint64) *Validator {
address := acc.GetAddress()
return &Validator{
Address: &address,
PublicKey: acc.GetPublicKey(),
Power: power,
}
}
var cdc = amino.NewCodec()
func (v *Validator) Encode() ([]byte, error) {
return cdc.MarshalBinaryBare(v)
}
func Decode(bs []byte) (*Validator, error) {
v := new(Validator)
err := cdc.UnmarshalBinaryBare(bs, v)
if err != nil {
return nil, err
}
return v, nil
}