Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp(staking): replace bech32 address with evm hex address for staking precompile contract #2200

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 20 additions & 20 deletions precompiles/staking/StakingI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Commission {

/// @dev Represents a validator in the staking module.
struct Validator {
string operatorAddress;
address operatorAddress;
string consensusPubkey;
bool jailed;
BondStatus status;
Expand All @@ -65,9 +65,9 @@ struct RedelegationResponse {

/// @dev Represents a redelegation between a delegator and a validator.
struct Redelegation {
string delegatorAddress;
string validatorSrcAddress;
string validatorDstAddress;
address delegatorAddress;
address validatorSrcAddress;
address validatorDstAddress;
RedelegationEntry[] entries;
}

Expand All @@ -87,9 +87,9 @@ struct RedelegationEntry {

/// @dev Represents the output of the Redelegation query.
struct RedelegationOutput {
string delegatorAddress;
string validatorSrcAddress;
string validatorDstAddress;
address delegatorAddress;
address validatorSrcAddress;
address validatorDstAddress;
RedelegationEntry[] entries;
}

Expand All @@ -105,8 +105,8 @@ struct UnbondingDelegationEntry {

/// @dev Represents the output of the UnbondingDelegation query.
struct UnbondingDelegationOutput {
string delegatorAddress;
string validatorAddress;
address delegatorAddress;
address validatorAddress;
UnbondingDelegationEntry[] entries;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ interface StakingI is authorization.AuthorizationI {
/// @return success Whether or not the delegate was successful
function delegate(
address delegatorAddress,
string memory validatorAddress,
address validatorAddress,
uint256 amount
) external returns (bool success);

Expand All @@ -160,7 +160,7 @@ interface StakingI is authorization.AuthorizationI {
/// @return completionTime The time when the undelegation is completed
function undelegate(
address delegatorAddress,
string memory validatorAddress,
address validatorAddress,
uint256 amount
) external returns (int64 completionTime);

Expand All @@ -173,8 +173,8 @@ interface StakingI is authorization.AuthorizationI {
/// @return completionTime The time when the redelegation is completed
function redelegate(
address delegatorAddress,
string memory validatorSrcAddress,
string memory validatorDstAddress,
address validatorSrcAddress,
address validatorDstAddress,
uint256 amount
) external returns (int64 completionTime);

Expand All @@ -187,7 +187,7 @@ interface StakingI is authorization.AuthorizationI {
/// @return success Whether or not the unbonding delegation was cancelled
function cancelUnbondingDelegation(
address delegatorAddress,
string memory validatorAddress,
address validatorAddress,
uint256 amount,
uint256 creationHeight
) external returns (bool success);
Expand All @@ -199,7 +199,7 @@ interface StakingI is authorization.AuthorizationI {
/// @return balance The amount in Coin, that the delegator has delegated to the given validator.
function delegation(
address delegatorAddress,
string memory validatorAddress
address validatorAddress
) external view returns (uint256 shares, Coin calldata balance);

/// @dev Returns the delegation shares and coins, that are currently
Expand All @@ -209,7 +209,7 @@ interface StakingI is authorization.AuthorizationI {
/// @return unbondingDelegation The delegations that are currently unbonding.
function unbondingDelegation(
address delegatorAddress,
string memory validatorAddress
address validatorAddress
) external view returns (UnbondingDelegationOutput calldata unbondingDelegation);

/// @dev Queries validator info for a given validator address.
Expand Down Expand Up @@ -240,8 +240,8 @@ interface StakingI is authorization.AuthorizationI {
/// @return redelegation The active redelegations for the given delegator, source and destination validator combination.
function redelegation(
address delegatorAddress,
string memory srcValidatorAddress,
string memory dstValidatorAddress
address srcValidatorAddress,
address dstValidatorAddress
) external view returns (RedelegationOutput calldata redelegation);

/// @dev Queries all redelegations based on the specified criteria:
Expand All @@ -255,8 +255,8 @@ interface StakingI is authorization.AuthorizationI {
/// @return response Holds the redelegations for the given delegator, source and destination validator combination.
function redelegations(
address delegatorAddress,
string memory srcValidatorAddress,
string memory dstValidatorAddress,
address srcValidatorAddress,
address dstValidatorAddress,
PageRequest calldata pageRequest
)
external
Expand Down
8 changes: 4 additions & 4 deletions precompiles/staking/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@
"type": "address"
},
{
"internalType": "string",
"internalType": "address",
"name": "validatorAddress",
"type": "string"
"type": "address"
},
{
"internalType": "uint256",
Expand Down Expand Up @@ -923,9 +923,9 @@
"type": "address"
},
{
"internalType": "string",
"internalType": "address",
"name": "validatorAddress",
"type": "string"
"type": "address"
},
{
"internalType": "uint256",
Expand Down
21 changes: 14 additions & 7 deletions precompiles/staking/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
var (
// valAddr and valAddr2 are the two validator addresses used for testing
valAddr, valAddr2 sdk.ValAddress
valHexAddr common.Address

// defaultCallArgs and defaultApproveArgs are the default arguments for calling the smart contract and to
// call the approve method specifically.
Expand Down Expand Up @@ -65,6 +66,10 @@
valAddr = s.validators[0].GetOperator()
valAddr2 = s.validators[1].GetOperator()

validatorAddr, err := sdk.ValAddressFromBech32(s.validators[0].GetOperator().String())
luchenqun marked this conversation as resolved.
Show resolved Hide resolved
Expect(err).To(BeNil())
valHexAddr = common.BytesToAddress(validatorAddr.Bytes())

defaultCallArgs = contracts.CallArgs{
ContractAddr: s.precompile.Address(),
ContractABI: s.precompile.ABI,
Expand Down Expand Up @@ -95,7 +100,7 @@
delegateArgs := defaultCallArgs.
WithMethodName(staking.DelegateMethod).
WithArgs(
s.address, valAddr.String(), big.NewInt(2e18),
s.address, valHexAddr, big.NewInt(2e18),
)

failCheck := defaultLogCheck.
Expand Down Expand Up @@ -1591,7 +1596,7 @@

It("should delegate when not exceeding the allowance", func() {
cArgs := defaultDelegateArgs.WithArgs(
s.address, valAddr.String(), big.NewInt(1e18),
s.address, valHexAddr, big.NewInt(1e18),
)

logCheckArgs := passCheck.
Expand Down Expand Up @@ -1627,7 +1632,7 @@

delegateArgs := defaultDelegateArgs.
WithPrivKey(newPriv).
WithArgs(s.address, valAddr.String(), big.NewInt(1e18))
WithArgs(s.address, valHexAddr, big.NewInt(1e18))

_, _, err = contracts.CallContractAndCheckLogs(s.ctx, s.app, delegateArgs, execRevertedCheck)
Expect(err).To(HaveOccurred(), "error while calling the smart contract: %v", err)
Expand All @@ -1638,7 +1643,7 @@

It("should not delegate when validator does not exist", func() {
delegateArgs := defaultDelegateArgs.WithArgs(
s.address, nonExistingVal.String(), big.NewInt(1e18),
s.address, nonExistingAddr, big.NewInt(1e18),
)

_, _, err = contracts.CallContractAndCheckLogs(s.ctx, s.app, delegateArgs, execRevertedCheck)
Expand Down Expand Up @@ -1736,7 +1741,7 @@

It("should not undelegate if the delegation does not exist", func() {
undelegateArgs := defaultUndelegateArgs.WithArgs(
s.address, nonExistingVal.String(), big.NewInt(1e18),
s.address, nonExistingAddr, big.NewInt(1e18),
)

_, _, err = contracts.CallContractAndCheckLogs(s.ctx, s.app, undelegateArgs, execRevertedCheck)
Expand Down Expand Up @@ -1840,7 +1845,7 @@

It("should not redelegate if the delegation does not exist", func() {
redelegateArgs := defaultRedelegateArgs.WithArgs(
s.address, nonExistingVal.String(), valAddr2.String(), big.NewInt(1e18),
s.address, nonExistingAddr, valAddr2.String(), big.NewInt(1e18),
)

_, _, err = contracts.CallContractAndCheckLogs(s.ctx, s.app, redelegateArgs, execRevertedCheck)
Expand Down Expand Up @@ -1870,7 +1875,7 @@

It("should not redelegate when the validator does not exist", func() {
redelegateArgs := defaultRedelegateArgs.WithArgs(
s.address, valAddr.String(), nonExistingVal.String(), big.NewInt(1e18),
s.address, valAddr.String(), nonExistingAddr, big.NewInt(1e18),
)

_, _, err = contracts.CallContractAndCheckLogs(s.ctx, s.app, redelegateArgs, execRevertedCheck)
Expand Down Expand Up @@ -2771,12 +2776,14 @@
Describe("when batching multiple transactions", func() {
// validator is the validator address used for testing
var validator sdk.ValAddress
//var validatorAddr common.Address

Check failure on line 2779 in precompiles/staking/integration_test.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)

BeforeEach(func() {
delegations := s.app.StakingKeeper.GetAllDelegatorDelegations(s.ctx, s.address.Bytes())
Expect(delegations).ToNot(HaveLen(0), "expected address to have delegations")

validator = delegations[0].GetValidatorAddr()
//validatorAddr = common.BytesToAddress(delegations[0].GetValidatorAddr().Bytes())

Check failure on line 2786 in precompiles/staking/integration_test.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)

_ = erc20ContractAddr
})
Expand Down
6 changes: 3 additions & 3 deletions precompiles/staking/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *PrecompileTestSuite) TestRun() {
input, err := s.precompile.Pack(
staking.DelegateMethod,
s.address,
s.validators[0].GetOperator().String(),
common.BytesToAddress(s.validators[0].GetOperator().Bytes()),
big.NewInt(1000),
)
s.Require().NoError(err, "failed to pack input")
Expand All @@ -192,7 +192,7 @@ func (s *PrecompileTestSuite) TestRun() {
input, err := s.precompile.Pack(
staking.UndelegateMethod,
s.address,
s.validators[0].GetOperator().String(),
common.BytesToAddress(s.validators[0].GetOperator().Bytes()),
big.NewInt(1),
)
s.Require().NoError(err, "failed to pack input")
Expand Down Expand Up @@ -382,7 +382,7 @@ func (s *PrecompileTestSuite) TestRun() {
input, err := s.precompile.Pack(
staking.DelegateMethod,
s.address,
s.validators[0].GetOperator().String(),
common.BytesToAddress(s.validators[0].GetOperator().Bytes()),
big.NewInt(1000),
)
s.Require().NoError(err, "failed to pack input")
Expand Down