/
config.go
222 lines (203 loc) · 6.61 KB
/
config.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package ethereum
import (
"encoding/hex"
"errors"
"fmt"
"math/big"
"strings"
"github.com/datachainlab/ethereum-ibc-relay-chain/pkg/utils"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/ethereum/go-ethereum/common"
"github.com/hyperledger-labs/yui-relayer/core"
)
var (
_ core.ChainConfig = (*ChainConfig)(nil)
_ codectypes.UnpackInterfacesMessage = (*ChainConfig)(nil)
)
const TxTypeAuto = "auto"
const TxTypeLegacy = "legacy"
const TxTypeDynamic = "dynamic"
func (c ChainConfig) Build() (core.Chain, error) {
return NewChain(c)
}
func (c ChainConfig) Validate() error {
isEmpty := func(s string) bool {
return strings.TrimSpace(s) == ""
}
var errs []error
if isEmpty(c.ChainId) {
errs = append(errs, fmt.Errorf("config attribute \"chain_id\" is empty"))
}
if isEmpty(c.RpcAddr) {
errs = append(errs, fmt.Errorf("config attribute \"rpc_addr\" is empty"))
}
if isEmpty(c.IbcAddress) {
errs = append(errs, fmt.Errorf("config attribute \"ibc_address\" is empty"))
}
if c.AverageBlockTimeMsec == 0 {
errs = append(errs, fmt.Errorf("config attribute \"average_block_time_msec\" is zero"))
}
if c.MaxRetryForInclusion == 0 {
errs = append(errs, fmt.Errorf("config attribute \"max_retry_for_inclusion\" is zero"))
}
if c.GasEstimateRate.Numerator == 0 {
errs = append(errs, fmt.Errorf("config attribute \"gas_estimate_rate.numerator\" is zero"))
}
if c.GasEstimateRate.Denominator == 0 {
errs = append(errs, fmt.Errorf("config attribute \"gas_estimate_rate.denominator\" is zero"))
}
if c.MaxGasLimit == 0 {
errs = append(errs, fmt.Errorf("config attribute \"max_gas_limit\" is zero"))
}
if c.Signer == nil {
errs = append(errs, fmt.Errorf("config attribute \"signer\" is empty"))
} else if err := c.Signer.GetCachedValue().(SignerConfig).Validate(); err != nil {
errs = append(errs, fmt.Errorf("config attribute \"signer\" is invalid: %v", err))
}
if c.AllowLcFunctions != nil {
if err := c.AllowLcFunctions.ValidateBasic(); err != nil {
errs = append(errs, fmt.Errorf("config attribute \"allow_lc_functions\" is invalid: %v", err))
}
}
if c.TxType != TxTypeAuto && c.TxType != TxTypeLegacy && c.TxType != TxTypeDynamic {
errs = append(errs, fmt.Errorf("config attribute \"tx_type\" is invalid"))
}
if c.TxType == TxTypeDynamic {
if c.DynamicTxGasConfig == nil {
errs = append(errs, fmt.Errorf("config attribute \"dynamic_tx_gas_config\" is empty"))
} else {
if err := c.DynamicTxGasConfig.ValidateBasic(); err != nil {
errs = append(errs, fmt.Errorf("config attribute \"dynamic_tx_gas_config\" is invalid: %v", err))
}
}
}
for i, path := range c.AbiPaths {
if isEmpty(path) {
errs = append(errs, fmt.Errorf("config attribute \"abi_paths[%d]\" is empty", i))
}
}
return errors.Join(errs...)
}
func (f Fraction) Validate() error {
if f.Denominator == 0 {
return errors.New("zero is invalid fraction denominator")
}
return nil
}
// Mul multiplies `n` by `f` (this function mutates `n`)
func (f Fraction) Mul(n *big.Int) {
n.Mul(n, new(big.Int).SetUint64(f.Numerator))
n.Div(n, new(big.Int).SetUint64(f.Denominator))
}
func (c ChainConfig) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if err := unpacker.UnpackAny(c.Signer, new(SignerConfig)); err != nil {
return fmt.Errorf("failed to unpack ChainConfig attribute \"signer\": %v", err)
}
return nil
}
func (c ChainConfig) IBCAddress() common.Address {
return common.HexToAddress(c.IbcAddress)
}
func (alf AllowLCFunctionsConfig) ValidateBasic() error {
if !common.IsHexAddress(alf.LcAddress) {
return fmt.Errorf("invalid contract address: %s", alf.LcAddress)
} else if alf.AllowAll && len(alf.Selectors) > 0 {
return fmt.Errorf("allowAll is true and selectors is not empty")
} else if !alf.AllowAll && len(alf.Selectors) == 0 {
return fmt.Errorf("allowAll is false and selectors is empty")
}
return nil
}
// CONTRACT: alf.ValidateBasic() must be called before calling this method.
func (alf AllowLCFunctionsConfig) ToAllowLCFunctions() (*AllowLCFunctions, error) {
if alf.AllowAll {
return &AllowLCFunctions{
LCAddress: common.HexToAddress(alf.LcAddress),
AllowALL: true,
}, nil
}
selectors := make([][4]byte, len(alf.Selectors))
for i, s := range alf.Selectors {
bz, err := hex.DecodeString(strings.TrimPrefix(s, "0x"))
if err != nil {
return nil, fmt.Errorf("failed to decode selector: selector=%v err=%v", s, err)
}
if len(bz) != 4 {
return nil, fmt.Errorf("invalid selector: %s", s)
}
copy(selectors[i][:], bz)
}
return &AllowLCFunctions{
LCAddress: common.HexToAddress(alf.LcAddress),
AllowALL: false,
Selectors: selectors,
}, nil
}
type AllowLCFunctions struct {
LCAddress common.Address
AllowALL bool
Selectors [][4]byte
}
func (lcf AllowLCFunctions) IsAllowed(address common.Address, selector [4]byte) bool {
if lcf.LCAddress != address {
return false
}
if lcf.AllowALL {
return true
}
for _, s := range lcf.Selectors {
if s == selector {
return true
}
}
return false
}
func (gsc *DynamicTxGasConfig) ValidateBasic() error {
if gsc.LimitPriorityFeePerGas != "" {
if _, err := utils.ParseEtherAmount(gsc.LimitPriorityFeePerGas); err != nil {
return fmt.Errorf("config attribute \"limit_priority_fee_per_gas\" is invalid: %v", err)
}
}
if gsc.PriorityFeeRate == nil {
return fmt.Errorf("config attribute \"priority_fee_rate\" is nil")
}
if err := gsc.PriorityFeeRate.Validate(); err != nil {
return fmt.Errorf("config attribute \"priority_fee_rate\" is invalid: %v", err)
}
if gsc.LimitFeePerGas != "" {
if _, err := utils.ParseEtherAmount(gsc.LimitFeePerGas); err != nil {
return fmt.Errorf("config attribute \"limit_fee_per_gas\" is invalid: %v", err)
}
}
if gsc.BaseFeeRate == nil {
return fmt.Errorf("config attribute \"base_fee_rate\" is nil")
}
if err := gsc.BaseFeeRate.Validate(); err != nil {
return fmt.Errorf("config attribute \"base_fee_rate\" is invalid: %v", err)
}
if gsc.MaxRetryForFeeHistory == 0 {
return fmt.Errorf("config attribute \"max_retry_for_fee_history\" is zero")
}
if gsc.FeeHistoryRewardPercentile == 0 {
return fmt.Errorf("config attribute \"fee_history_reward_percentile\" is zero")
}
return nil
}
func (c *DynamicTxGasConfig) GetLimitPriorityFeePerGas() *big.Int {
if c.LimitPriorityFeePerGas == "" {
return new(big.Int)
} else if limit, err := utils.ParseEtherAmount(c.LimitPriorityFeePerGas); err != nil {
panic(err)
} else {
return limit
}
}
func (c *DynamicTxGasConfig) GetLimitFeePerGas() *big.Int {
if c.LimitFeePerGas == "" {
return new(big.Int)
} else if limit, err := utils.ParseEtherAmount(c.LimitFeePerGas); err != nil {
panic(err)
} else {
return limit
}
}