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 // 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 { func ValidateAddress(address string) error {
if !ethcmn.IsHexAddress(address) { if !ethcmn.IsHexAddress(address) {
return sdkerrors.Wrapf( return sdkerrors.Wrapf(
@ -27,10 +26,5 @@ func ValidateAddress(address string) error {
address, address,
) )
} }
if IsZeroAddress(address) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "provided address cannot be the zero address")
}
return nil return nil
} }

View File

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

View File

@ -17,6 +17,8 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
) )
const invalidFromAddress = "0x0000"
type MsgsTestSuite struct { type MsgsTestSuite struct {
suite.Suite suite.Suite
@ -63,7 +65,7 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_Constructor() {
func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() { func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() {
testCases := []struct { testCases := []struct {
msg string msg string
to *ethcmn.Address to string
amount *big.Int amount *big.Int
gasPrice *big.Int gasPrice *big.Int
from string from string
@ -71,21 +73,46 @@ func (suite *MsgsTestSuite) TestMsgEthereumTx_ValidateBasic() {
chainID *big.Int chainID *big.Int
expectPass bool 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 - 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, amount: big.NewInt(100), gasPrice: big.NewInt(0), accessList: &ethtypes.AccessList{}, chainID: big.NewInt(1), 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: nil, amount: big.NewInt(100), gasPrice: big.NewInt(100000), expectPass: true}, {msg: "pass contract - Legacy Tx", to: "", 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: "invalid recipient", to: invalidFromAddress, 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: "nil amount", to: suite.to.Hex(), 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: "negative amount", to: suite.to.Hex(), 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: "nil gas price", to: suite.to.Hex(), 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: "negative gas price", to: suite.to.Hex(), 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: "zero gas price", to: suite.to.Hex(), 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: "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, amount: big.NewInt(100), gasPrice: big.NewInt(0), accessList: &ethtypes.AccessList{}, chainID: nil, 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 { 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 msg.From = tc.from
err := msg.ValidateBasic() err := msg.ValidateBasic()