fix: remove zero address validation (#205)

* fix: remove zero address validation

* test: update test invalid Ethereum address

* test: fix ValidateBassic test for invalid To address input

* remove unnecesary comment

* fix: remove zero address validation

* test: update test invalid Ethereum address

* test: fix ValidateBassic test for invalid To address input

* remove unnecesary comment
This commit is contained in:
crypto-facs 2021-06-29 11:08:07 -04:00 committed by GitHub
parent a2e3d35df2
commit 1363a45e93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 30 deletions

View File

@ -19,7 +19,6 @@ func IsZeroAddress(address string) bool {
}
// ValidateAddress returns an error if the provided string is either not a hex formatted string address
// the it matches the zero address 0x00000000000000000000.
func ValidateAddress(address string) error {
if !ethcmn.IsHexAddress(address) {
return sdkerrors.Wrapf(
@ -27,10 +26,5 @@ func ValidateAddress(address string) error {
address,
)
}
if IsZeroAddress(address) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "provided address cannot be the zero address")
}
return nil
}

View File

@ -18,6 +18,9 @@ import (
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
)
//Not valid Ethereum address
const invalidAddress = "0x0000"
func (suite *KeeperTestSuite) TestQueryAccount() {
var (
req *types.QueryAccountRequest
@ -29,7 +32,7 @@ func (suite *KeeperTestSuite) TestQueryAccount() {
malleate func()
expPass bool
}{
{"zero address",
{"invalid address",
func() {
suite.app.BankKeeper.SetBalance(suite.ctx, suite.address.Bytes(), ethermint.NewPhotonCoinInt64(0))
expAccount = &types.QueryAccountResponse{
@ -38,7 +41,7 @@ func (suite *KeeperTestSuite) TestQueryAccount() {
Nonce: 0,
}
req = &types.QueryAccountRequest{
Address: ethcmn.Address{}.String(),
Address: invalidAddress,
}
},
false,
@ -91,14 +94,14 @@ func (suite *KeeperTestSuite) TestQueryCosmosAccount() {
malleate func()
expPass bool
}{
{"zero address",
{"invalid address",
func() {
suite.app.BankKeeper.SetBalance(suite.ctx, suite.address.Bytes(), ethermint.NewPhotonCoinInt64(0))
expAccount = &types.QueryCosmosAccountResponse{
CosmosAddress: sdk.AccAddress(ethcmn.Address{}.Bytes()).String(),
}
req = &types.QueryCosmosAccountRequest{
Address: ethcmn.Address{}.String(),
Address: invalidAddress,
}
},
false,
@ -169,12 +172,12 @@ func (suite *KeeperTestSuite) TestQueryBalance() {
malleate func()
expPass bool
}{
{"zero address",
{"invalid address",
func() {
suite.app.BankKeeper.SetBalance(suite.ctx, suite.address.Bytes(), ethermint.NewPhotonCoinInt64(0))
expBalance = "0"
req = &types.QueryBalanceRequest{
Address: ethcmn.Address{}.String(),
Address: invalidAddress,
}
},
false,
@ -223,10 +226,10 @@ func (suite *KeeperTestSuite) TestQueryStorage() {
malleate func()
expPass bool
}{
{"zero address",
{"invalid address",
func() {
req = &types.QueryStorageRequest{
Address: ethcmn.Address{}.String(),
Address: invalidAddress,
}
},
false,
@ -287,10 +290,10 @@ func (suite *KeeperTestSuite) TestQueryCode() {
malleate func()
expPass bool
}{
{"zero address",
{"invalid address",
func() {
req = &types.QueryCodeRequest{
Address: ethcmn.Address{}.String(),
Address: invalidAddress,
}
exp := &types.QueryCodeResponse{}
expCode = exp.Code
@ -597,7 +600,7 @@ func (suite *KeeperTestSuite) TestQueryValidatorAccount() {
malleate func()
expPass bool
}{
{"zero address",
{"invalid address",
func() {
suite.app.BankKeeper.SetBalance(suite.ctx, suite.address.Bytes(), ethermint.NewPhotonCoinInt64(0))
expAccount = &types.QueryValidatorAccountResponse{

View File

@ -17,6 +17,8 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)
const invalidFromAddress = "0x0000"
type MsgsTestSuite struct {
suite.Suite
@ -63,7 +65,7 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_Constructor() {
func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() {
testCases := []struct {
msg string
to *ethcmn.Address
to string
amount *big.Int
gasPrice *big.Int
from string
@ -71,21 +73,46 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() {
chainID *big.Int
expectPass bool
}{
{msg: "pass with recipient - Legacy Tx", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(100000), expectPass: true},
{msg: "pass with recipient - AccessList Tx", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(0), accessList: &ethtypes.AccessList{}, chainID: big.NewInt(1), expectPass: true},
{msg: "pass contract - Legacy Tx", to: nil, amount: big.NewInt(100), gasPrice: big.NewInt(100000), expectPass: true},
{msg: "invalid recipient", to: &ethcmn.Address{}, amount: big.NewInt(-1), gasPrice: big.NewInt(1000), expectPass: false},
{msg: "nil amount", to: &suite.to, amount: nil, gasPrice: big.NewInt(1000), expectPass: true},
{msg: "negative amount", to: &suite.to, amount: big.NewInt(-1), gasPrice: big.NewInt(1000), expectPass: true},
{msg: "nil gas price", to: &suite.to, amount: big.NewInt(100), gasPrice: nil, expectPass: false},
{msg: "negative gas price", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(-1), expectPass: true},
{msg: "zero gas price", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(0), expectPass: true},
{msg: "invalid from address", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(0), from: ethcmn.Address{}.Hex(), expectPass: false},
{msg: "chain ID not set on AccessListTx", to: &suite.to, amount: big.NewInt(100), gasPrice: big.NewInt(0), accessList: &ethtypes.AccessList{}, chainID: nil, expectPass: false},
{msg: "pass with recipient - Legacy Tx", to: suite.to.Hex(), amount: big.NewInt(100), gasPrice: big.NewInt(100000), expectPass: true},
{msg: "pass with recipient - AccessList Tx", to: suite.to.Hex(), amount: big.NewInt(100), gasPrice: big.NewInt(0), accessList: &ethtypes.AccessList{}, chainID: big.NewInt(1), expectPass: true},
{msg: "pass contract - Legacy Tx", to: "", amount: big.NewInt(100), gasPrice: big.NewInt(100000), expectPass: true},
{msg: "invalid recipient", to: invalidFromAddress, amount: big.NewInt(-1), gasPrice: big.NewInt(1000), expectPass: false},
{msg: "nil amount", to: suite.to.Hex(), amount: nil, gasPrice: big.NewInt(1000), expectPass: true},
{msg: "negative amount", to: suite.to.Hex(), amount: big.NewInt(-1), gasPrice: big.NewInt(1000), expectPass: true},
{msg: "nil gas price", to: suite.to.Hex(), amount: big.NewInt(100), gasPrice: nil, expectPass: false},
{msg: "negative gas price", to: suite.to.Hex(), amount: big.NewInt(100), gasPrice: big.NewInt(-1), expectPass: true},
{msg: "zero gas price", to: suite.to.Hex(), amount: big.NewInt(100), gasPrice: big.NewInt(0), expectPass: true},
{msg: "invalid from address", to: suite.to.Hex(), amount: big.NewInt(100), gasPrice: big.NewInt(0), from: invalidFromAddress, expectPass: false},
{msg: "chain ID not set on AccessListTx", to: suite.to.Hex(), amount: big.NewInt(100), gasPrice: big.NewInt(0), accessList: &ethtypes.AccessList{}, chainID: nil, expectPass: false},
}
for i, tc := range testCases {
msg := NewMsgEthereumTx(tc.chainID, 0, tc.to, tc.amount, 0, tc.gasPrice, nil, tc.accessList)
// recreate txData
txData := TxData{
Nonce: 0,
GasLimit: 0,
To: tc.to,
}
if tc.accessList != nil {
txData.Accesses = NewAccessList(tc.accessList)
if tc.chainID != nil {
txData.ChainID = tc.chainID.Bytes()
}
}
if tc.amount != nil {
txData.Amount = tc.amount.Bytes()
}
if tc.gasPrice != nil {
txData.GasPrice = tc.gasPrice.Bytes()
}
msg := MsgEthereumTx{
Data: &txData,
}
msg.From = tc.from
err := msg.ValidateBasic()