Skip to content

Commit

Permalink
Merge pull request #192 from aura-nw/cleanup-evmutil
Browse files Browse the repository at this point in the history
Cleanup evmutil and adapt evmos precompiles
  • Loading branch information
kienvc committed May 22, 2024
2 parents 175f80a + 5a1e13b commit 5867eee
Show file tree
Hide file tree
Showing 74 changed files with 16,956 additions and 8,943 deletions.
13 changes: 4 additions & 9 deletions app/evm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"golang.org/x/exp/maps"

// staking and distribution precompiles
distprecompile "github.com/aura-nw/aura/precompiles/distribution"
stakingprecompile "github.com/aura-nw/aura/precompiles/staking"

authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
channelkeeper "github.com/cosmos/ibc-go/v7/modules/core/04-channel/keeper"
bankprecompile "github.com/evmos/evmos/v16/precompiles/bank"
distprecompile "github.com/evmos/evmos/v16/precompiles/distribution"
ics20precompile "github.com/evmos/evmos/v16/precompiles/ics20"
"github.com/evmos/evmos/v16/precompiles/p256"
stakingprecompile "github.com/evmos/evmos/v16/precompiles/staking"
erc20Keeper "github.com/evmos/evmos/v16/x/erc20/keeper"
transferkeeper "github.com/evmos/evmos/v16/x/ibc/transfer/keeper"
)
Expand Down Expand Up @@ -59,11 +60,6 @@ func Precompiles(
panic(fmt.Errorf("failed to instantiate ICS20 precompile: %w", err))
}

bankPrecompile, err := bankprecompile.NewPrecompile(bankKeeper, erc20Keeper)
if err != nil {
panic(fmt.Errorf("failed to instantiate bank precompile: %w", err))
}

// Stateless precompiles
precompiles[bech32Precompile.Address()] = bech32Precompile
precompiles[p256Precompile.Address()] = p256Precompile
Expand All @@ -72,7 +68,6 @@ func Precompiles(
precompiles[stakingPrecompile.Address()] = stakingPrecompile
precompiles[distributionPrecompile.Address()] = distributionPrecompile
precompiles[ibcTransferPrecompile.Address()] = ibcTransferPrecompile
precompiles[bankPrecompile.Address()] = bankPrecompile

return precompiles
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2
github.com/onsi/ginkgo/v2 v2.13.2
github.com/onsi/gomega v1.30.0
github.com/prometheus/client_golang v1.16.0
github.com/regen-network/cosmos-proto v0.3.1
github.com/spf13/cast v1.6.0
Expand Down Expand Up @@ -84,6 +86,7 @@ require (
github.com/creachadair/taskgroup v0.4.2 // indirect
github.com/crypto-org-chain/cronos/memiavl v0.0.5-0.20231027074119-c05c9c61c90e // indirect
github.com/crypto-org-chain/cronos/store v0.0.5-0.20231027074119-c05c9c61c90e // indirect
github.com/crypto-org-chain/cronos/versiondb v0.0.0-20231027074119-c05c9c61c90e // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
Expand Down Expand Up @@ -111,6 +114,7 @@ require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/golang/glog v1.1.2 // indirect
Expand All @@ -120,6 +124,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
Expand Down Expand Up @@ -217,6 +222,7 @@ require (
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/api v0.149.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240102182953-50ed04b92917 // indirect
Expand All @@ -239,7 +245,7 @@ replace (
// use Evmos geth fork
github.com/ethereum/go-ethereum => github.com/evmos/go-ethereum v1.10.26-evmos-rc2

github.com/evmos/evmos/v16 => github.com/aura-nw/evmos/v16 v16.0.3-aura-2
github.com/evmos/evmos/v16 => github.com/aura-nw/evmos/v16 v16.0.3-aura.3

// https://github.com/cosmos/cosmos-sdk/issues/14949
// pin the version of goleveldb to v1.0.1-0.20210819022825-2ae1ddf74ef7 required by SDK v47 upgrade guide.
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ github.com/armon/go-metrics v0.4.1 h1:hR91U9KYmb6bLBYLQjyM+3j+rcd/UhE+G78SFnF8gJ
github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
github.com/aura-nw/evmos/v16 v16.0.3-aura-2 h1:O6dRTOMdix5ItW8Y8QgQ0XTxxT8R+/mX6a4dVEZN9+g=
github.com/aura-nw/evmos/v16 v16.0.3-aura-2/go.mod h1:w0vtRYI4I0/O8eihq6ZuDvlca4ZiYCKN6vpakG9zHcc=
github.com/aura-nw/evmos/v16 v16.0.3-aura.3 h1:0+lDKyJqduHESvZ9KTW4zy/CfKWDJffi33ZWB5AS8U4=
github.com/aura-nw/evmos/v16 v16.0.3-aura.3/go.mod h1:w0vtRYI4I0/O8eihq6ZuDvlca4ZiYCKN6vpakG9zHcc=
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
Expand Down Expand Up @@ -408,6 +408,7 @@ github.com/crypto-org-chain/cronos/memiavl v0.0.5-0.20231027074119-c05c9c61c90e/
github.com/crypto-org-chain/cronos/store v0.0.5-0.20231027074119-c05c9c61c90e h1:3mq9zn5+49covVejnfwPIIx0i6pzRXxKJzz4PuVVn9g=
github.com/crypto-org-chain/cronos/store v0.0.5-0.20231027074119-c05c9c61c90e/go.mod h1:N6IfJDLTTo0vxyuY4MSMMX2TOdBd/tozpjnYGJmAfmE=
github.com/crypto-org-chain/cronos/versiondb v0.0.0-20231027074119-c05c9c61c90e h1:kN1HNLp2xmy8vIHpGWjsX8dCL6sXID2Tw4qZZk7XYd4=
github.com/crypto-org-chain/cronos/versiondb v0.0.0-20231027074119-c05c9c61c90e/go.mod h1:8W9LCw+FhR2sFI+nkjSi0ItZ0IeSyUjc6CSvuaYTU2s=
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -532,6 +533,7 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
Expand Down Expand Up @@ -646,6 +648,7 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10 h1:CqYfpuYIjnlNxM3msdyPRKabhXZWbKjf3Q8BWROFBso=
github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o=
github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw=
Expand Down Expand Up @@ -903,11 +906,13 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs=
github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM=
github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
Expand Down Expand Up @@ -1519,6 +1524,7 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
197 changes: 197 additions & 0 deletions precompiles/distribution/DistributionI.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.17;

import "../common/Types.sol";

/// @dev The DistributionI contract's address.
address constant DISTRIBUTION_PRECOMPILE_ADDRESS = 0x0000000000000000000000000000000000000801;

/// @dev Define all the available distribution methods.
string constant MSG_SET_WITHDRAWER_ADDRESS = "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress";
string constant MSG_WITHDRAW_DELEGATOR_REWARD = "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward";
string constant MSG_WITHDRAW_VALIDATOR_COMMISSION = "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission";

/// @dev The DistributionI contract's instance.
DistributionI constant DISTRIBUTION_CONTRACT = DistributionI(
DISTRIBUTION_PRECOMPILE_ADDRESS
);

struct ValidatorSlashEvent {
uint64 validatorPeriod;
Dec fraction;
}

struct ValidatorDistributionInfo {
string operatorAddress;
DecCoin[] selfBondRewards;
DecCoin[] commission;
}

struct DelegationDelegatorReward {
string validatorAddress;
DecCoin[] reward;
}

/// @author Evmos Team
/// @title Distribution Precompile Contract
/// @dev The interface through which solidity contracts will interact with Distribution
/// @custom:address 0x0000000000000000000000000000000000000801
interface DistributionI {
/// @dev ClaimRewards defines an Event emitted when rewards are claimed
/// @param delegatorAddress the address of the delegator
/// @param amount the amount being claimed
event ClaimRewards(
address indexed delegatorAddress,
uint256 amount
);

/// @dev SetWithdrawerAddress defines an Event emitted when a new withdrawer address is being set
/// @param caller the caller of the transaction
/// @param withdrawerAddress the newly set withdrawer address
event SetWithdrawerAddress(
address indexed caller,
string withdrawerAddress
);

/// @dev WithdrawDelegatorRewards defines an Event emitted when rewards from a delegation are withdrawn
/// @param delegatorAddress the address of the delegator
/// @param validatorAddress the address of the validator
/// @param amount the amount being withdrawn from the delegation
event WithdrawDelegatorRewards(
address indexed delegatorAddress,
address indexed validatorAddress,
uint256 amount
);

/// @dev WithdrawValidatorCommission defines an Event emitted when validator commissions are being withdrawn
/// @param validatorAddress is the address of the validator
/// @param commission is the total commission earned by the validator
event WithdrawValidatorCommission(
string indexed validatorAddress,
uint256 commission
);

/// TRANSACTIONS

/// @dev Claims all rewards from a select set of validators or all of them for a delegator.
/// @param delegatorAddress The address of the delegator
/// @param maxRetrieve The maximum number of validators to claim rewards from
/// @return success Whether the transaction was successful or not
function claimRewards(
address delegatorAddress,
uint32 maxRetrieve
) external returns (bool success);

/// @dev Change the address, that can withdraw the rewards of a delegator.
/// Note that this address cannot be a module account.
/// @param delegatorAddress The address of the delegator
/// @param withdrawerAddress The address that will be capable of withdrawing rewards for
/// the given delegator address
function setWithdrawAddress(
address delegatorAddress,
string memory withdrawerAddress
) external returns (bool success);

/// @dev Withdraw the rewards of a delegator from a validator
/// @param delegatorAddress The address of the delegator
/// @param validatorAddress The address of the validator
/// @return amount The amount of Coin withdrawn
function withdrawDelegatorRewards(
address delegatorAddress,
string memory validatorAddress
) external returns (Coin[] calldata amount);

/// @dev Withdraws the rewards commission of a validator.
/// @param validatorAddress The address of the validator
/// @return amount The amount of Coin withdrawn
function withdrawValidatorCommission(
string memory validatorAddress
) external returns (Coin[] calldata amount);

/// QUERIES
/// @dev Queries validator commission and self-delegation rewards for validator.
/// @param validatorAddress The address of the validator
/// @return distributionInfo The validator's distribution info
function validatorDistributionInfo(
string memory validatorAddress
)
external
view
returns (
ValidatorDistributionInfo calldata distributionInfo
);

/// @dev Queries the outstanding rewards of a validator address.
/// @param validatorAddress The address of the validator
/// @return rewards The validator's outstanding rewards
function validatorOutstandingRewards(
string memory validatorAddress
) external view returns (DecCoin[] calldata rewards);

/// @dev Queries the accumulated commission for a validator.
/// @param validatorAddress The address of the validator
/// @return commission The validator's commission
function validatorCommission(
string memory validatorAddress
) external view returns (DecCoin[] calldata commission);

/// @dev Queries the slashing events for a validator in a given height interval
/// defined by the starting and ending height.
/// @param validatorAddress The address of the validator
/// @param startingHeight The starting height
/// @param endingHeight The ending height
/// @param pageRequest Defines a pagination for the request.
/// @return slashes The validator's slash events
/// @return pageResponse The pagination response for the query
function validatorSlashes(
string memory validatorAddress,
uint64 startingHeight,
uint64 endingHeight,
PageRequest calldata pageRequest
)
external
view
returns (
ValidatorSlashEvent[] calldata slashes,
PageResponse calldata pageResponse
);

/// @dev Queries the total rewards accrued by a delegation from a specific address to a given validator.
/// @param delegatorAddress The address of the delegator
/// @param validatorAddress The address of the validator
/// @return rewards The total rewards accrued by a delegation.
function delegationRewards(
address delegatorAddress,
string memory validatorAddress
) external view returns (DecCoin[] calldata rewards);

/// @dev Queries the total rewards accrued by each validator, that a given
/// address has delegated to.
/// @param delegatorAddress The address of the delegator
/// @return rewards The total rewards accrued by each validator for a delegator.
/// @return total The total rewards accrued by a delegator.
function delegationTotalRewards(
address delegatorAddress
)
external
view
returns (
DelegationDelegatorReward[] calldata rewards,
DecCoin[] calldata total
);

/// @dev Queries all validators, that a given address has delegated to.
/// @param delegatorAddress The address of the delegator
/// @return validators The addresses of all validators, that were delegated to by the given address.
function delegatorValidators(
address delegatorAddress
) external view returns (string[] calldata validators);

/// @dev Queries the address capable of withdrawing rewards for a given delegator.
/// @param delegatorAddress The address of the delegator
/// @return withdrawAddress The address capable of withdrawing rewards for the delegator.
function delegatorWithdrawAddress(
address delegatorAddress
) external view returns (string memory withdrawAddress);

}

0 comments on commit 5867eee

Please sign in to comment.