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

feat: staking precompile #2784

Merged
merged 31 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
44dc9ce
add boilerplate for staking precompile with empty delegate method
skosito Aug 28, 2024
766a30a
add simple undelegate and redelegate methods
skosito Aug 28, 2024
9e97065
add new methods to run
skosito Aug 28, 2024
6984671
add simple tests
skosito Aug 28, 2024
6c2128b
add delegator caller checks
skosito Aug 28, 2024
dacf5fb
fix del addr origin check
skosito Aug 28, 2024
f16df7f
add unit tests for delegate method
skosito Aug 28, 2024
b335fc9
changelog
skosito Aug 28, 2024
f5dc0c3
add undelegate tests
skosito Aug 28, 2024
758864d
add redelegate unit tests
skosito Aug 28, 2024
f7aa77e
renamings
skosito Aug 29, 2024
d1df9ec
generate
skosito Aug 29, 2024
0003316
pr comments
skosito Aug 29, 2024
378ea6d
codecov
skosito Aug 29, 2024
781cdd4
renaming
skosito Aug 29, 2024
7f1999d
e2e test and fixes
skosito Aug 29, 2024
9eb8775
generate
skosito Aug 29, 2024
7f8c962
Merge branch 'develop' into stake-precompile
skosito Aug 29, 2024
a063f84
fix tests
skosito Aug 30, 2024
c425696
fix view methods required gas
skosito Aug 30, 2024
43f063e
add queries unit tests
skosito Aug 30, 2024
2e6cb8a
Merge branch 'develop' into stake-precompile
skosito Aug 30, 2024
5af835f
add more unit tests and refactor a bit
skosito Aug 30, 2024
17380ca
fmt
skosito Aug 30, 2024
e4ae503
Merge branch 'develop' into stake-precompile
skosito Aug 30, 2024
ecf6d12
fixes after merge
skosito Aug 30, 2024
0bef0e7
cleanup
skosito Aug 30, 2024
f991479
PR comments
skosito Sep 2, 2024
09f35e8
add todo with issue
skosito Sep 2, 2024
f88bfa1
PR comment and bug fix with validators append
skosito Sep 4, 2024
5e1f021
Merge branch 'develop' into stake-precompile
skosito Sep 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func New(
&app.FeeMarketKeeper,
tracer,
evmSs,
precompiles.StatefulContracts(&app.FungibleKeeper, appCodec, storetypes.TransientGasConfig()),
precompiles.StatefulContracts(&app.FungibleKeeper, app.StakingKeeper, appCodec, storetypes.TransientGasConfig()),
app.ConsensusParamsKeeper,
aggregateAllKeys(keys, tKeys, memKeys),
)
Expand Down
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* [2681](https://github.com/zeta-chain/node/pull/2681) - implement `MsgUpdateERC20CustodyPauseStatus` to pause or unpause ERC20 Custody contract (to be used for the migration process for smart contract V2)
* [2644](https://github.com/zeta-chain/node/pull/2644) - add created_timestamp to cctx status
* [2673](https://github.com/zeta-chain/node/pull/2673) - add relayer key importer, encryption and decryption
* [2633](https://github.com/zeta-chain/node/pull/2633) - support for stateful precompiled contracts.
* [2633](https://github.com/zeta-chain/node/pull/2633) - support for stateful precompiled contracts
* [2784](https://github.com/zeta-chain/node/pull/2784) - staking precompiled contract

### Refactor

Expand Down
22 changes: 18 additions & 4 deletions precompiles/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,49 @@
ethparams "github.com/ethereum/go-ethereum/params"
evmkeeper "github.com/zeta-chain/ethermint/x/evm/keeper"

stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/zeta-chain/zetacore/precompiles/prototype"
"github.com/zeta-chain/zetacore/x/fungible/keeper"
"github.com/zeta-chain/zetacore/precompiles/staking"
fungiblekeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
)

// EnabledStatefulContracts contains the list of all enabled stateful precompiles.
// This is useful for listing and reading from other packages, such as BlockedAddrs() function.
// Setting to false a contract here will disable it, not being included in the blockchain.
var EnabledStatefulContracts = map[common.Address]bool{
prototype.ContractAddress: true,
staking.ContractAddress: true,
}

// StatefulContracts returns all the registered precompiled contracts.
func StatefulContracts(
fungibleKeeper *keeper.Keeper,
fungibleKeeper *fungiblekeeper.Keeper,
stakingKeeper *stakingkeeper.Keeper,
cdc codec.Codec,
gasConfig storetypes.GasConfig,
) (precompiledContracts []evmkeeper.CustomContractFn) {
// Initialize at 0 the custom compiled contracts and the addresses.
precompiledContracts = make([]evmkeeper.CustomContractFn, 0)

// Define the regular contract function.
// Define the prototype contract function.
if EnabledStatefulContracts[prototype.ContractAddress] {
prototypeContract := func(_ sdktypes.Context, _ ethparams.Rules) vm.PrecompiledContract {
return prototype.NewIPrototypeContract(fungibleKeeper, cdc, gasConfig)
}

// Append the regular contract to the precompiledContracts slice.
// Append the prototype contract to the precompiledContracts slice.
precompiledContracts = append(precompiledContracts, prototypeContract)
}

// Define the staking contract function.
if EnabledStatefulContracts[prototype.ContractAddress] {
stakingContract := func(_ sdktypes.Context, _ ethparams.Rules) vm.PrecompiledContract {
return staking.NewIStakingContract(stakingKeeper, cdc, gasConfig)

Check warning on line 49 in precompiles/precompiles.go

View check run for this annotation

Codecov / codecov/patch

precompiles/precompiles.go#L49

Added line #L49 was not covered by tests
}

// Append the prototype contract to the precompiledContracts slice.
precompiledContracts = append(precompiledContracts, stakingContract)
}

return precompiledContracts
}
4 changes: 2 additions & 2 deletions precompiles/precompiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func Test_StatefulContracts(t *testing.T) {
k, ctx, _, _ := keeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keeper.FungibleKeeper(t)
gasConfig := storetypes.TransientGasConfig()

var encoding ethermint.EncodingConfig
Expand All @@ -25,7 +25,7 @@ func Test_StatefulContracts(t *testing.T) {
}

// StatefulContracts() should return all the enabled contracts.
contracts := StatefulContracts(k, appCodec, gasConfig)
contracts := StatefulContracts(k, &sdkk.StakingKeeper, appCodec, gasConfig)
require.NotNil(t, contracts, "StatefulContracts() should not return a nil slice")
require.Len(t, contracts, expectedContracts, "StatefulContracts() should return all the enabled contracts")

Expand Down
1 change: 0 additions & 1 deletion precompiles/prototype/prototype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ func Test_GetGasStabilityPoolBalance(t *testing.T) {
)

t.Run("should fail with invalid arguments", func(t *testing.T) {

t.Run("invalid number of arguments", func(t *testing.T) {
args := []interface{}{int64(1337), "second argument"}
_, err := contract.GetGasStabilityPoolBalance(ctx, &methodID, args)
Expand Down
94 changes: 94 additions & 0 deletions precompiles/staking/IStaking.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[
{
"inputs": [
{
"internalType": "address",
"name": "delegator",
"type": "address"
},
{
"internalType": "string",
"name": "validator",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "delegate",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "delegator",
"type": "address"
},
{
"internalType": "string",
"name": "validatorSrc",
"type": "string"
},
{
"internalType": "string",
"name": "validatorDst",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "redelegate",
"outputs": [
{
"internalType": "int64",
"name": "completionTime",
"type": "int64"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "delegator",
"type": "address"
},
{
"internalType": "string",
"name": "validator",
"type": "string"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "undelegate",
"outputs": [
{
"internalType": "int64",
"name": "completionTime",
"type": "int64"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
Loading
Loading