Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Sep 2, 2024
1 parent 0bef0e7 commit f991479
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 109 deletions.
4 changes: 2 additions & 2 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ func localE2ETest(cmd *cobra.Command, _ []string) {

if !skipPrecompiles {
precompiledContractTests = []string{
e2etests.TestZetaPrecompilesPrototypeName,
e2etests.TestZetaPrecompilesStakingName,
e2etests.TestPrecompilesPrototypeName,
e2etests.TestPrecompilesStakingName,
}
}

Expand Down
8 changes: 4 additions & 4 deletions e2e/e2etests/e2etests.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ const (
/*
Stateful precompiled contracts tests
*/
TestZetaPrecompilesPrototypeName = "precompile_contracts_prototype"
TestZetaPrecompilesStakingName = "precompile_contracts_staking"
TestPrecompilesPrototypeName = "precompile_contracts_prototype"
TestPrecompilesStakingName = "precompile_contracts_staking"
)

// AllE2ETests is an ordered list of all e2e tests
Expand Down Expand Up @@ -815,13 +815,13 @@ var AllE2ETests = []runner.E2ETest{
Stateful precompiled contracts tests
*/
runner.NewE2ETest(
TestZetaPrecompilesPrototypeName,
TestPrecompilesPrototypeName,
"test stateful precompiled contracts prototype",
[]runner.ArgDefinition{},
TestPrecompilesPrototype,
),
runner.NewE2ETest(
TestZetaPrecompilesStakingName,
TestPrecompilesStakingName,
"test stateful precompiled contracts staking",
[]runner.ArgDefinition{},
TestPrecompilesStaking,
Expand Down
23 changes: 14 additions & 9 deletions e2e/e2etests/test_precompiles_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2etests
import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/node/e2e/runner"
Expand All @@ -16,18 +17,22 @@ func TestPrecompilesStaking(r *runner.E2ERunner, args []string) {
stakingContract, err := staking.NewIStaking(staking.ContractAddress, r.ZEVMClient)
require.NoError(r, err, "Failed to create staking contract caller")

previousGasLimit := r.ZEVMAuth.GasLimit
r.ZEVMAuth.GasLimit = 10000000
defer func() {
r.ZEVMAuth.GasLimit = previousGasLimit
}()

validators, err := stakingContract.GetAllValidators(nil)
validators, err := stakingContract.GetAllValidators(&bind.CallOpts{})
require.NoError(r, err)
require.GreaterOrEqual(r, len(validators), 2)

// shares are 0 for both validators at the start
sharesBeforeVal1, err := stakingContract.GetShares(nil, r.ZEVMAuth.From, validators[0].OperatorAddress)
sharesBeforeVal1, err := stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[0].OperatorAddress)
require.NoError(r, err)
require.Equal(r, int64(0), sharesBeforeVal1.Int64())

sharesBeforeVal2, err := stakingContract.GetShares(nil, r.ZEVMAuth.From, validators[1].OperatorAddress)
sharesBeforeVal2, err := stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[1].OperatorAddress)
require.NoError(r, err)
require.Equal(r, int64(0), sharesBeforeVal2.Int64())

Expand All @@ -37,7 +42,7 @@ func TestPrecompilesStaking(r *runner.E2ERunner, args []string) {
utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)

// check shares are set to 3
sharesAfterVal1, err := stakingContract.GetShares(nil, r.ZEVMAuth.From, validators[0].OperatorAddress)
sharesAfterVal1, err := stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[0].OperatorAddress)
require.NoError(r, err)
require.Equal(r, big.NewInt(3e18).String(), sharesAfterVal1.String())

Expand All @@ -47,12 +52,12 @@ func TestPrecompilesStaking(r *runner.E2ERunner, args []string) {
utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)

// check shares are set to 2
sharesAfterVal1, err = stakingContract.GetShares(nil, r.ZEVMAuth.From, validators[0].OperatorAddress)
sharesAfterVal1, err = stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[0].OperatorAddress)
require.NoError(r, err)
require.Equal(r, big.NewInt(2e18).String(), sharesAfterVal1.String())

// transfer 1 stake from validator1 to validator2
tx, err = stakingContract.TransferStake(
// move 1 stake from validator1 to validator2
tx, err = stakingContract.MoveStake(
r.ZEVMAuth,
r.ZEVMAuth.From,
validators[0].OperatorAddress,
Expand All @@ -64,11 +69,11 @@ func TestPrecompilesStaking(r *runner.E2ERunner, args []string) {
utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)

// check shares for both validator1 and validator2 are 1
sharesAfterVal1, err = stakingContract.GetShares(nil, r.ZEVMAuth.From, validators[0].OperatorAddress)
sharesAfterVal1, err = stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[0].OperatorAddress)
require.NoError(r, err)
require.Equal(r, big.NewInt(1e18).String(), sharesAfterVal1.String())

sharesAfterVal2, err := stakingContract.GetShares(nil, r.ZEVMAuth.From, validators[1].OperatorAddress)
sharesAfterVal2, err := stakingContract.GetShares(&bind.CallOpts{}, r.ZEVMAuth.From, validators[1].OperatorAddress)
require.NoError(r, err)
require.Equal(r, big.NewInt(1e18).String(), sharesAfterVal2.String())
}
30 changes: 15 additions & 15 deletions precompiles/staking/IStaking.abi
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@
},
{
"internalType": "string",
"name": "validator",
"name": "validatorSrc",
"type": "string"
},
{
"internalType": "string",
"name": "validatorDst",
"type": "string"
},
{
Expand All @@ -76,12 +81,12 @@
"type": "uint256"
}
],
"name": "stake",
"name": "moveStake",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
"internalType": "int64",
"name": "completionTime",
"type": "int64"
}
],
"stateMutability": "nonpayable",
Expand All @@ -96,12 +101,7 @@
},
{
"internalType": "string",
"name": "validatorSrc",
"type": "string"
},
{
"internalType": "string",
"name": "validatorDst",
"name": "validator",
"type": "string"
},
{
Expand All @@ -110,12 +110,12 @@
"type": "uint256"
}
],
"name": "transferStake",
"name": "stake",
"outputs": [
{
"internalType": "int64",
"name": "completionTime",
"type": "int64"
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
Expand Down
44 changes: 22 additions & 22 deletions precompiles/staking/IStaking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions precompiles/staking/IStaking.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@
},
{
"internalType": "string",
"name": "validator",
"name": "validatorSrc",
"type": "string"
},
{
"internalType": "string",
"name": "validatorDst",
"type": "string"
},
{
Expand All @@ -77,12 +82,12 @@
"type": "uint256"
}
],
"name": "stake",
"name": "moveStake",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
"internalType": "int64",
"name": "completionTime",
"type": "int64"
}
],
"stateMutability": "nonpayable",
Expand All @@ -97,12 +102,7 @@
},
{
"internalType": "string",
"name": "validatorSrc",
"type": "string"
},
{
"internalType": "string",
"name": "validatorDst",
"name": "validator",
"type": "string"
},
{
Expand All @@ -111,12 +111,12 @@
"type": "uint256"
}
],
"name": "transferStake",
"name": "stake",
"outputs": [
{
"internalType": "int64",
"name": "completionTime",
"type": "int64"
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
Expand Down
6 changes: 3 additions & 3 deletions precompiles/staking/IStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ interface IStaking {
uint256 amount
) external returns (int64 completionTime);

/// @notice Transfer coins from validatorSrc to validatorDst
/// @notice Move coins from validatorSrc to validatorDst
/// @param staker Staker address
/// @param validatorSrc Validator from address
/// @param validatorDst Validator to address
/// @param amount Coins amount
/// @return completionTime Time when staket transfer is done
function transferStake(
/// @return completionTime Time when stake move is done
function moveStake(
address staker,
string memory validatorSrc,
string memory validatorDst,
Expand Down
16 changes: 8 additions & 8 deletions precompiles/staking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

const (
// write
StakeMethodName = "stake"
UnstakeMethodName = "unstake"
TransferStakeMethodName = "transferStake"
StakeMethodName = "stake"
UnstakeMethodName = "unstake"
MoveStakeMethodName = "moveStake"

// read
GetAllValidatorsMethodName = "getAllValidators"
Expand Down Expand Up @@ -55,7 +55,7 @@ func initABI() {
GasRequiredByMethod[methodID] = 10000
case UnstakeMethodName:
GasRequiredByMethod[methodID] = 10000
case TransferStakeMethodName:
case MoveStakeMethodName:
GasRequiredByMethod[methodID] = 10000
case GetAllValidatorsMethodName:
GasRequiredByMethod[methodID] = 0
Expand Down Expand Up @@ -126,7 +126,7 @@ func (c *Contract) GetAllValidators(
) ([]byte, error) {
validators := c.stakingKeeper.GetAllValidators(ctx)

validatorsRes := []Validator{}
validatorsRes := make([]Validator, len(validators))
for _, v := range validators {
validatorsRes = append(validatorsRes, Validator{
OperatorAddress: v.OperatorAddress,
Expand Down Expand Up @@ -291,7 +291,7 @@ func (c *Contract) Unstake(
return method.Outputs.Pack(res.GetCompletionTime().UTC().Unix())
}

func (c *Contract) TransferStake(
func (c *Contract) MoveStake(
ctx sdk.Context,
origin common.Address,
method *abi.Method,
Expand Down Expand Up @@ -409,10 +409,10 @@ func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, _ bool) ([]byte, erro
return nil, err
}
return res, nil
case TransferStakeMethodName:
case MoveStakeMethodName:
var res []byte
execErr := stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error {
res, err = c.TransferStake(ctx, evm.Origin, method, args)
res, err = c.MoveStake(ctx, evm.Origin, method, args)
return err
})
if execErr != nil {
Expand Down
Loading

0 comments on commit f991479

Please sign in to comment.