-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.go
127 lines (103 loc) · 3.13 KB
/
contract.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package contract
import (
"errors"
"fmt"
"github.com/0xDaoChain/dao-chain/state"
"github.com/0xDaoChain/dao-chain/types"
"github.com/0xDaoChain/dao-chain/validators"
"github.com/0xDaoChain/dao-chain/validators/store"
"github.com/hashicorp/go-hclog"
lru "github.com/hashicorp/golang-lru"
)
const (
// How many validator sets are stored in the cache
// Cache 3 validator sets for 3 epochs
DefaultValidatorSetCacheSize = 3
)
var (
ErrSignerNotFound = errors.New("signer not found")
ErrInvalidValidatorsTypeAssertion = errors.New("invalid type assertion for Validators")
)
type ContractValidatorStore struct {
logger hclog.Logger
blockchain store.HeaderGetter
executor Executor
// LRU cache for the validators
validatorSetCache *lru.Cache
}
type Executor interface {
BeginTxn(types.Hash, *types.Header, types.Address) (*state.Transition, error)
}
func NewContractValidatorStore(
logger hclog.Logger,
blockchain store.HeaderGetter,
executor Executor,
validatorSetCacheSize int,
) (*ContractValidatorStore, error) {
var (
validatorsCache *lru.Cache
err error
)
if validatorSetCacheSize > 0 {
if validatorsCache, err = lru.New(validatorSetCacheSize); err != nil {
return nil, fmt.Errorf("unable to create validator set cache, %w", err)
}
}
return &ContractValidatorStore{
logger: logger,
blockchain: blockchain,
executor: executor,
validatorSetCache: validatorsCache,
}, nil
}
func (s *ContractValidatorStore) SourceType() store.SourceType {
return store.Contract
}
func (s *ContractValidatorStore) GetValidatorsByHeight(
validatorType validators.ValidatorType,
height uint64,
) (validators.Validators, error) {
cachedValidators, err := s.loadCachedValidatorSet(height)
if err != nil {
return nil, err
}
if cachedValidators != nil {
return cachedValidators, nil
}
transition, err := s.getTransitionForQuery(height)
if err != nil {
return nil, err
}
fetchedValidators, err := FetchValidators(validatorType, transition, types.ZeroAddress)
if err != nil {
return nil, err
}
s.saveToValidatorSetCache(height, fetchedValidators)
return fetchedValidators, nil
}
func (s *ContractValidatorStore) getTransitionForQuery(height uint64) (*state.Transition, error) {
header, ok := s.blockchain.GetHeaderByNumber(height)
if !ok {
return nil, fmt.Errorf("header not found at %d", height)
}
return s.executor.BeginTxn(header.StateRoot, header, types.ZeroAddress)
}
// loadCachedValidatorSet loads validators from validatorSetCache
func (s *ContractValidatorStore) loadCachedValidatorSet(height uint64) (validators.Validators, error) {
cachedRawValidators, ok := s.validatorSetCache.Get(height)
if !ok {
return nil, nil
}
validators, ok := cachedRawValidators.(validators.Validators)
if !ok {
return nil, ErrInvalidValidatorsTypeAssertion
}
return validators, nil
}
// saveToValidatorSetCache saves validators to validatorSetCache
func (s *ContractValidatorStore) saveToValidatorSetCache(height uint64, validators validators.Validators) bool {
if s.validatorSetCache == nil {
return false
}
return s.validatorSetCache.Add(height, validators)
}