2020-09-09 13:53:14 +00:00
|
|
|
package types_test
|
2019-12-13 19:50:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-09-07 13:04:50 +00:00
|
|
|
"fmt"
|
2019-12-13 19:50:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-07-02 15:19:48 +00:00
|
|
|
|
2019-12-13 19:50:19 +00:00
|
|
|
tmamino "github.com/tendermint/tendermint/crypto/encoding/amino"
|
|
|
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
|
|
|
|
2020-09-08 15:58:19 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
2020-09-09 13:53:14 +00:00
|
|
|
"github.com/cosmos/ethermint/types"
|
2019-12-13 19:50:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-10-06 18:57:55 +00:00
|
|
|
tmamino.RegisterKeyType(ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName)
|
|
|
|
tmamino.RegisterKeyType(ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName)
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
type AccountTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
account *types.EthAccount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) SetupTest() {
|
2019-12-13 19:50:19 +00:00
|
|
|
pubkey := secp256k1.GenPrivKey().PubKey()
|
|
|
|
addr := sdk.AccAddress(pubkey.Address())
|
2020-09-09 13:53:14 +00:00
|
|
|
balance := sdk.NewCoins(types.NewPhotonCoin(sdk.OneInt()))
|
2020-08-23 21:41:54 +00:00
|
|
|
baseAcc := auth.NewBaseAccount(addr, balance, pubkey, 10, 50)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.account = &types.EthAccount{
|
|
|
|
BaseAccount: baseAcc,
|
|
|
|
CodeHash: []byte{1, 2},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccountTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(AccountTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *AccountTestSuite) TestEthAccount_Balance() {
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
denom string
|
|
|
|
initialCoins sdk.Coins
|
|
|
|
amount sdk.Int
|
|
|
|
}{
|
|
|
|
{"positive diff", types.AttoPhoton, sdk.Coins{}, sdk.OneInt()},
|
|
|
|
{"zero diff, same coin", types.AttoPhoton, sdk.NewCoins(types.NewPhotonCoin(sdk.ZeroInt())), sdk.ZeroInt()},
|
|
|
|
{"zero diff, other coin", sdk.DefaultBondDenom, sdk.NewCoins(types.NewPhotonCoin(sdk.ZeroInt())), sdk.ZeroInt()},
|
|
|
|
{"negative diff", types.AttoPhoton, sdk.NewCoins(types.NewPhotonCoin(sdk.NewInt(10))), sdk.NewInt(1)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupTest() // reset values
|
|
|
|
suite.account.SetCoins(tc.initialCoins)
|
|
|
|
|
|
|
|
suite.account.SetBalance(tc.denom, tc.amount)
|
|
|
|
suite.Require().Equal(tc.amount, suite.account.Balance(tc.denom))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-12-13 19:50:19 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
func (suite *AccountTestSuite) TestEthermintAccountJSON() {
|
|
|
|
bz, err := json.Marshal(suite.account)
|
|
|
|
suite.Require().NoError(err)
|
2019-12-13 19:50:19 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
bz1, err := suite.account.MarshalJSON()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(string(bz1), string(bz))
|
2019-12-13 19:50:19 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
var a types.EthAccount
|
|
|
|
suite.Require().NoError(a.UnmarshalJSON(bz))
|
|
|
|
suite.Require().Equal(suite.account.String(), a.String())
|
|
|
|
suite.Require().Equal(suite.account.PubKey, a.PubKey)
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
func (suite *AccountTestSuite) TestEthermintPubKeyJSON() {
|
2020-10-06 18:57:55 +00:00
|
|
|
privkey, err := ethsecp256k1.GenerateKey()
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
2019-12-13 19:50:19 +00:00
|
|
|
bz := privkey.PubKey().Bytes()
|
|
|
|
|
|
|
|
pubk, err := tmamino.PubKeyFromBytes(bz)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(pubk, privkey.PubKey())
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
func (suite *AccountTestSuite) TestSecpPubKeyJSON() {
|
2019-12-13 19:50:19 +00:00
|
|
|
pubkey := secp256k1.GenPrivKey().PubKey()
|
|
|
|
bz := pubkey.Bytes()
|
|
|
|
|
|
|
|
pubk, err := tmamino.PubKeyFromBytes(bz)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(pubk, pubkey)
|
2019-12-13 19:50:19 +00:00
|
|
|
}
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
func (suite *AccountTestSuite) TestEthermintAccount_String() {
|
2020-09-07 13:04:50 +00:00
|
|
|
config := sdk.GetConfig()
|
2020-09-09 13:53:14 +00:00
|
|
|
types.SetBech32Prefixes(config)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
bech32pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, suite.account.PubKey)
|
|
|
|
suite.Require().NoError(err)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
|
|
|
accountStr := fmt.Sprintf(`|
|
|
|
|
address: %s
|
2020-09-08 13:57:49 +00:00
|
|
|
eth_address: %s
|
2020-09-07 13:04:50 +00:00
|
|
|
coins:
|
|
|
|
- denom: aphoton
|
|
|
|
amount: "1"
|
|
|
|
public_key: %s
|
|
|
|
account_number: 10
|
|
|
|
sequence: 50
|
|
|
|
code_hash: "0102"
|
2020-09-09 13:53:14 +00:00
|
|
|
`, suite.account.Address, suite.account.EthAddress().String(), bech32pubkey)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Equal(accountStr, suite.account.String())
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
i, err := suite.account.MarshalYAML()
|
|
|
|
suite.Require().NoError(err)
|
2020-09-07 13:04:50 +00:00
|
|
|
|
|
|
|
var ok bool
|
|
|
|
accountStr, ok = i.(string)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().True(ok)
|
|
|
|
suite.Require().Contains(accountStr, suite.account.Address.String())
|
|
|
|
suite.Require().Contains(accountStr, bech32pubkey)
|
2020-09-07 13:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
func (suite *AccountTestSuite) TestEthermintAccount_MarshalJSON() {
|
|
|
|
bz, err := suite.account.MarshalJSON()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Contains(string(bz), suite.account.EthAddress().String())
|
2020-09-07 13:04:50 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
res := new(types.EthAccount)
|
2020-09-07 13:04:50 +00:00
|
|
|
err = res.UnmarshalJSON(bz)
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(suite.account, res)
|
2020-09-08 15:58:19 +00:00
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
bech32pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, suite.account.PubKey)
|
|
|
|
suite.Require().NoError(err)
|
2020-09-08 15:58:19 +00:00
|
|
|
|
|
|
|
// test that the sdk.AccAddress is populated from the hex address
|
|
|
|
jsonAcc := fmt.Sprintf(
|
|
|
|
`{"address":"","eth_address":"%s","coins":[{"denom":"aphoton","amount":"1"}],"public_key":"%s","account_number":10,"sequence":50,"code_hash":"0102"}`,
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.account.EthAddress().String(), bech32pubkey,
|
2020-09-08 15:58:19 +00:00
|
|
|
)
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
res = new(types.EthAccount)
|
2020-09-08 15:58:19 +00:00
|
|
|
err = res.UnmarshalJSON([]byte(jsonAcc))
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(suite.account.Address.String(), res.Address.String())
|
2020-09-08 15:58:19 +00:00
|
|
|
|
|
|
|
jsonAcc = fmt.Sprintf(
|
|
|
|
`{"address":"","eth_address":"","coins":[{"denom":"aphoton","amount":"1"}],"public_key":"%s","account_number":10,"sequence":50,"code_hash":"0102"}`,
|
|
|
|
bech32pubkey,
|
|
|
|
)
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
res = new(types.EthAccount)
|
2020-09-08 15:58:19 +00:00
|
|
|
err = res.UnmarshalJSON([]byte(jsonAcc))
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Error(err, "should fail if both address are empty")
|
2020-09-08 15:58:19 +00:00
|
|
|
|
|
|
|
// test that the sdk.AccAddress is populated from the hex address
|
|
|
|
jsonAcc = fmt.Sprintf(
|
|
|
|
`{"address": "%s","eth_address":"0x0000000000000000000000000000000000000000","coins":[{"denom":"aphoton","amount":"1"}],"public_key":"%s","account_number":10,"sequence":50,"code_hash":"0102"}`,
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.account.Address.String(), bech32pubkey,
|
2020-09-08 15:58:19 +00:00
|
|
|
)
|
|
|
|
|
2020-09-09 13:53:14 +00:00
|
|
|
res = new(types.EthAccount)
|
2020-09-08 15:58:19 +00:00
|
|
|
err = res.UnmarshalJSON([]byte(jsonAcc))
|
2020-09-09 13:53:14 +00:00
|
|
|
suite.Require().Error(err, "should fail if addresses mismatch")
|
2020-09-07 13:04:50 +00:00
|
|
|
}
|