Skip to content

Commit 5281978

Browse files
committed
reduce panics
1 parent a770fb1 commit 5281978

File tree

16 files changed

+43
-102
lines changed

16 files changed

+43
-102
lines changed

cmd/blockchaincmd/change_weight.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func setWeight(_ *cobra.Command, args []string) error {
167167
return fmt.Errorf("unable to find Validator Manager address")
168168
}
169169
validatorManagerAddress = sc.Networks[network.Name()].ValidatorManagerAddress
170-
validationID, err := validator.GetValidationID(rpcURL, nodeID, validatorManagerAddress)
170+
validationID, err := validator.GetValidationID(rpcURL, common.HexToAddress(validatorManagerAddress), nodeID)
171171
if err != nil {
172172
return err
173173
}

cmd/blockchaincmd/remove_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func removeValidator(_ *cobra.Command, args []string) error {
167167
}
168168
}
169169
validatorManagerAddress = sc.Networks[network.Name()].ValidatorManagerAddress
170-
validationID, err := validatorsdk.GetRegisteredValidator(
170+
validationID, err := validatorsdk.GetValidationID(
171171
rpcURL,
172172
common.HexToAddress(validatorManagerAddress),
173173
nodeID,

cmd/validatorcmd/getBalance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func getNodeValidationID(
194194
return ids.Empty, false, err
195195
}
196196
managerAddress := common.HexToAddress(validatorManagerAddress)
197-
validationID, err = validator.GetRegisteredValidator(rpcURL, managerAddress, nodeID)
197+
validationID, err = validator.GetValidationID(rpcURL, managerAddress, nodeID)
198198
if err != nil {
199199
return ids.Empty, false, err
200200
}

cmd/validatorcmd/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func list(_ *cobra.Command, args []string) error {
107107
return err
108108
}
109109
balance := uint64(0)
110-
validationID, err := validator.GetRegisteredValidator(rpcURL, managerAddress, nodeID)
110+
validationID, err := validator.GetValidationID(rpcURL, managerAddress, nodeID)
111111
if err != nil {
112112
ux.Logger.RedXToUser("could not get validation ID for node %s due to %s", nodeID, err)
113113
} else {

pkg/contract/contract.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,21 @@ func CallToMethod(
604604
return out, nil
605605
}
606606

607+
func GetMethodReturn[T any](methodName string, out []interface{}) (T, error) {
608+
empty := new(T)
609+
if len(out) == 0 {
610+
return *empty, fmt.Errorf("error at %s call: no return value", methodName)
611+
}
612+
if len(out) != 1 {
613+
return *empty, fmt.Errorf("error at %s call: expected 1 return value, got %d", methodName, len(out))
614+
}
615+
received, typeIsOk := out[0].(T)
616+
if !typeIsOk {
617+
return *empty, fmt.Errorf("error at %s call, expected %T, got %T", methodName, *empty, out[0])
618+
}
619+
return received, nil
620+
}
621+
607622
func DeployContract(
608623
rpcURL string,
609624
privateKey string,

pkg/contract/ownable.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
// See the file LICENSE for licensing terms.
33
package contract
44

5-
import (
6-
_ "embed"
7-
"fmt"
8-
9-
"github.com/ethereum/go-ethereum/common"
10-
)
5+
import "github.com/ethereum/go-ethereum/common"
116

127
// GetContractOwner gets owner for https://docs.openzeppelin.com/contracts/2.x/api/ownership#Ownable-owner contracts
138
func GetContractOwner(
@@ -22,10 +17,5 @@ func GetContractOwner(
2217
if err != nil {
2318
return common.Address{}, err
2419
}
25-
26-
ownerAddr, ok := out[0].(common.Address)
27-
if !ok {
28-
return common.Address{}, fmt.Errorf("error at owner() call, expected common.Address, got %T", out[0])
29-
}
30-
return ownerAddr, nil
20+
return GetMethodReturn[common.Address]("owner", out)
3121
}

pkg/ictt/operate.go

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ func ERC20TokenHomeGetTokenAddress(
5555
if err != nil {
5656
return common.Address{}, err
5757
}
58-
tokenAddress, b := out[0].(common.Address)
59-
if !b {
60-
return common.Address{}, fmt.Errorf("error at token call, expected common.Address, got %T", out[0])
61-
}
62-
return tokenAddress, nil
58+
return contract.GetMethodReturn[common.Address]("token", out)
6359
}
6460

6561
func NativeTokenHomeGetTokenAddress(
@@ -74,11 +70,7 @@ func NativeTokenHomeGetTokenAddress(
7470
if err != nil {
7571
return common.Address{}, err
7672
}
77-
tokenAddress, b := out[0].(common.Address)
78-
if !b {
79-
return common.Address{}, fmt.Errorf("error at wrappedToken call, expected common.Address, got %T", out[0])
80-
}
81-
return tokenAddress, nil
73+
return contract.GetMethodReturn[common.Address]("wrappedToken", out)
8274
}
8375

8476
func TokenRemoteIsCollateralized(
@@ -93,11 +85,7 @@ func TokenRemoteIsCollateralized(
9385
if err != nil {
9486
return false, err
9587
}
96-
isCollateralized, b := out[0].(bool)
97-
if !b {
98-
return false, fmt.Errorf("error at isCollateralized call, expected bool, got %T", out[0])
99-
}
100-
return isCollateralized, nil
88+
return contract.GetMethodReturn[bool]("isCollateralized", out)
10189
}
10290

10391
func TokenHomeGetDecimals(
@@ -112,11 +100,7 @@ func TokenHomeGetDecimals(
112100
if err != nil {
113101
return 0, err
114102
}
115-
decimals, b := out[0].(uint8)
116-
if !b {
117-
return 0, fmt.Errorf("error at tokenDecimals, expected uint8, got %T", out[0])
118-
}
119-
return decimals, nil
103+
return contract.GetMethodReturn[uint8]("tokenDecimals", out)
120104
}
121105

122106
type RegisteredRemote struct {
@@ -146,6 +130,9 @@ func TokenHomeGetRegisteredRemote(
146130
registeredRemote RegisteredRemote
147131
b bool
148132
)
133+
if len(out) != 4 {
134+
return RegisteredRemote{}, fmt.Errorf("error at registeredRemotes call, expected 4 return values, got %d", len(out))
135+
}
149136
registeredRemote.Registered, b = out[0].(bool)
150137
if !b {
151138
return RegisteredRemote{}, fmt.Errorf("error at registeredRemotes call, expected bool, got %T", out[0])
@@ -177,11 +164,7 @@ func ERC20TokenRemoteGetTokenHomeAddress(
177164
if err != nil {
178165
return common.Address{}, err
179166
}
180-
tokenHubAddress, b := out[0].(common.Address)
181-
if !b {
182-
return common.Address{}, fmt.Errorf("error at tokenHubAddress call, expected common.Address, got %T", out[0])
183-
}
184-
return tokenHubAddress, nil
167+
return contract.GetMethodReturn[common.Address]("tokenHomeAddress", out)
185168
}
186169

187170
func NativeTokenRemoteGetTotalNativeAssetSupply(
@@ -196,11 +179,7 @@ func NativeTokenRemoteGetTotalNativeAssetSupply(
196179
if err != nil {
197180
return nil, err
198181
}
199-
supply, b := out[0].(*big.Int)
200-
if !b {
201-
return nil, fmt.Errorf("error at totalNativeAssetSupply, expected *big.Int, got %T", out[0])
202-
}
203-
return supply, nil
182+
return contract.GetMethodReturn[*big.Int]("totalNativeAssetSupply", out)
204183
}
205184

206185
func ERC20TokenHomeSend(

pkg/interchain/operate.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package interchain
44

55
import (
66
_ "embed"
7-
"fmt"
87
"math/big"
98

109
"github.com/ava-labs/avalanche-cli/pkg/contract"
@@ -27,11 +26,7 @@ func GetNextMessageID(
2726
if err != nil {
2827
return ids.Empty, err
2928
}
30-
received, b := out[0].([32]byte)
31-
if !b {
32-
return ids.Empty, fmt.Errorf("error at getNextMessageID call, expected ids.ID, got %T", out[0])
33-
}
34-
return received, nil
29+
return contract.GetMethodReturn[[32]byte]("getNextMessageID", out)
3530
}
3631

3732
func MessageReceived(
@@ -48,11 +43,7 @@ func MessageReceived(
4843
if err != nil {
4944
return false, err
5045
}
51-
received, b := out[0].(bool)
52-
if !b {
53-
return false, fmt.Errorf("error at messageReceived call, expected bool, got %T", out[0])
54-
}
55-
return received, nil
46+
return contract.GetMethodReturn[bool]("messageReceived", out)
5647
}
5748

5849
func SendCrossChainMessage(

pkg/precompiles/allowlist.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package precompiles
44

55
import (
66
_ "embed"
7-
"fmt"
87
"math/big"
98

109
"github.com/ava-labs/avalanche-cli/pkg/contract"
@@ -109,9 +108,5 @@ func ReadAllowList(
109108
if err != nil {
110109
return nil, err
111110
}
112-
role, b := out[0].(*big.Int)
113-
if !b {
114-
return nil, fmt.Errorf("error at readAllowList, expected *big.Int, got %T", out[0])
115-
}
116-
return role, nil
111+
return contract.GetMethodReturn[*big.Int]("readAllowList", out)
117112
}

pkg/precompiles/warp.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package precompiles
44

55
import (
66
_ "embed"
7-
"fmt"
87

98
"github.com/ava-labs/avalanche-cli/pkg/contract"
109
"github.com/ava-labs/avalanchego/ids"
@@ -21,12 +20,5 @@ func WarpPrecompileGetBlockchainID(
2120
if err != nil {
2221
return ids.Empty, err
2322
}
24-
if len(out) == 0 {
25-
return ids.Empty, fmt.Errorf("error at getBlockchainID call: no return value")
26-
}
27-
received, ok := out[0].([32]byte)
28-
if !ok {
29-
return ids.Empty, fmt.Errorf("error at getBlockchainID call, expected ids.ID, got %T", out[0])
30-
}
31-
return received, nil
23+
return contract.GetMethodReturn[[32]byte]("getBlockchainID", out)
3224
}

0 commit comments

Comments
 (0)