-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.go
83 lines (70 loc) · 2.17 KB
/
fetcher.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
package contract
import (
"fmt"
"github.com/0xDaoChain/dao-chain/contracts/staking"
"github.com/0xDaoChain/dao-chain/crypto"
"github.com/0xDaoChain/dao-chain/state"
"github.com/0xDaoChain/dao-chain/types"
"github.com/0xDaoChain/dao-chain/validators"
)
// FetchValidators fetches validators from a contract switched by validator type
func FetchValidators(
validatorType validators.ValidatorType,
transition *state.Transition,
from types.Address,
) (validators.Validators, error) {
switch validatorType {
case validators.ECDSAValidatorType:
return FetchECDSAValidators(transition, from)
case validators.BLSValidatorType:
return FetchBLSValidators(transition, from)
}
return nil, fmt.Errorf("unsupported validator type: %s", validatorType)
}
// FetchECDSAValidators queries a contract for validator addresses and returns ECDSAValidators
func FetchECDSAValidators(
transition *state.Transition,
from types.Address,
) (validators.Validators, error) {
valAddrs, err := staking.QueryValidators(transition, from)
if err != nil {
return nil, err
}
ecdsaValidators := validators.NewECDSAValidatorSet()
for _, addr := range valAddrs {
if err := ecdsaValidators.Add(validators.NewECDSAValidator(addr)); err != nil {
return nil, err
}
}
return ecdsaValidators, nil
}
// FetchBLSValidators queries a contract for validator addresses & BLS Public Keys and returns ECDSAValidators
func FetchBLSValidators(
transition *state.Transition,
from types.Address,
) (validators.Validators, error) {
valAddrs, err := staking.QueryValidators(transition, from)
if err != nil {
return nil, err
}
blsPublicKeys, err := staking.QueryBLSPublicKeys(transition, from)
if err != nil {
return nil, err
}
blsValidators := validators.NewBLSValidatorSet()
for idx := range valAddrs {
// ignore the validator whose BLS Key is not set
// because BLS validator needs to have both Address and BLS Public Key set
// in the contract
if _, err := crypto.UnmarshalBLSPublicKey(blsPublicKeys[idx]); err != nil {
continue
}
if err := blsValidators.Add(validators.NewBLSValidator(
valAddrs[idx],
blsPublicKeys[idx],
)); err != nil {
return nil, err
}
}
return blsValidators, nil
}