-
Notifications
You must be signed in to change notification settings - Fork 199
/
accountsRepository.go
106 lines (89 loc) · 3.85 KB
/
accountsRepository.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
package state
import (
"fmt"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data/api"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/common/holders"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
type accountsRepository struct {
finalStateAccountsWrapper AccountsAdapterAPI
currentStateAccountsWrapper AccountsAdapterAPI
historicalStateAccountsWrapper AccountsAdapterAPI
}
// ArgsAccountsRepository is the DTO for the NewAccountsRepository constructor function
type ArgsAccountsRepository struct {
FinalStateAccountsWrapper AccountsAdapterAPI
CurrentStateAccountsWrapper AccountsAdapterAPI
HistoricalStateAccountsWrapper AccountsAdapterAPI
}
// NewAccountsRepository creates a new accountsRepository instance
func NewAccountsRepository(args ArgsAccountsRepository) (*accountsRepository, error) {
if check.IfNil(args.CurrentStateAccountsWrapper) {
return nil, fmt.Errorf("%w for CurrentStateAccountsWrapper", ErrNilAccountsAdapter)
}
if check.IfNil(args.FinalStateAccountsWrapper) {
return nil, fmt.Errorf("%w for FinalStateAccountsWrapper", ErrNilAccountsAdapter)
}
if check.IfNil(args.HistoricalStateAccountsWrapper) {
return nil, fmt.Errorf("%w for HistoricalStateAccountsWrapper", ErrNilAccountsAdapter)
}
return &accountsRepository{
finalStateAccountsWrapper: args.FinalStateAccountsWrapper,
currentStateAccountsWrapper: args.CurrentStateAccountsWrapper,
historicalStateAccountsWrapper: args.HistoricalStateAccountsWrapper,
}, nil
}
// GetAccountWithBlockInfo will return the account handler with the block info providing the address and the query option
func (repository *accountsRepository) GetAccountWithBlockInfo(address []byte, options api.AccountQueryOptions) (vmcommon.AccountHandler, common.BlockInfo, error) {
accountsAdapter, err := repository.selectStateAccounts(options)
if err != nil {
return nil, nil, err
}
convertedOptions := holders.NewRootHashHolder(options.BlockRootHash, options.HintEpoch)
return accountsAdapter.GetAccountWithBlockInfo(address, convertedOptions)
}
// GetCodeWithBlockInfo will return the code with the block info providing the code hash and the query option
func (repository *accountsRepository) GetCodeWithBlockInfo(codeHash []byte, options api.AccountQueryOptions) ([]byte, common.BlockInfo, error) {
accountsAdapter, err := repository.selectStateAccounts(options)
if err != nil {
return nil, nil, err
}
convertedOptions := holders.NewRootHashHolder(options.BlockRootHash, options.HintEpoch)
return accountsAdapter.GetCodeWithBlockInfo(codeHash, convertedOptions)
}
func (repository *accountsRepository) selectStateAccounts(options api.AccountQueryOptions) (AccountsAdapterAPI, error) {
if len(options.BlockRootHash) > 0 {
return repository.historicalStateAccountsWrapper, nil
}
if options.OnFinalBlock {
return repository.finalStateAccountsWrapper, nil
}
if options.OnStartOfEpoch.HasValue {
// TODO implement this
return nil, ErrFunctionalityNotImplemented
}
return repository.currentStateAccountsWrapper, nil
}
// GetCurrentStateAccountsWrapper gets the current state accounts wrapper
func (repository *accountsRepository) GetCurrentStateAccountsWrapper() AccountsAdapterAPI {
return repository.currentStateAccountsWrapper
}
// Close will handle the closing of the underlying components
func (repository *accountsRepository) Close() error {
errHistorical := repository.historicalStateAccountsWrapper.Close()
errFinal := repository.finalStateAccountsWrapper.Close()
errCurrent := repository.currentStateAccountsWrapper.Close()
if errHistorical != nil {
return errHistorical
}
if errFinal != nil {
return errFinal
}
return errCurrent
}
// IsInterfaceNil returns true if there is no value under the interface
func (repository *accountsRepository) IsInterfaceNil() bool {
return repository == nil
}