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: emit events from staking precompile #2861

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [2788](https://github.com/zeta-chain/node/pull/2788) - add common importable zetacored rpc package
* [2784](https://github.com/zeta-chain/node/pull/2784) - staking precompiled contract
* [2795](https://github.com/zeta-chain/node/pull/2795) - support restricted address in Solana
* [2861](https://github.com/zeta-chain/node/pull/2861) - emit events from staking precompile

### Refactor

Expand Down
9 changes: 0 additions & 9 deletions cmd/zetae2e/local/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"fmt"
"path/filepath"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"github.com/zeta-chain/node/app"
"github.com/zeta-chain/node/e2e/config"
)

Expand All @@ -25,10 +23,3 @@ func GetConfig(cmd *cobra.Command) (config.Config, error) {

return config.ReadConfig(configFile)
}

// setCosmosConfig set account prefix to zeta
func setCosmosConfig() {
cosmosConf := sdk.GetConfig()
cosmosConf.SetBech32PrefixForAccount(app.Bech32PrefixAccAddr, app.Bech32PrefixAccPub)
cosmosConf.Seal()
}
4 changes: 2 additions & 2 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

"github.com/zeta-chain/node/app"
zetae2econfig "github.com/zeta-chain/node/cmd/zetae2e/config"
"github.com/zeta-chain/node/e2e/config"
"github.com/zeta-chain/node/e2e/e2etests"
Expand Down Expand Up @@ -143,8 +144,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
noError(utils.WaitForBlockHeight(ctx, waitForHeight, conf.RPCs.ZetaCoreRPC, logger))
}

// set account prefix to zeta
setCosmosConfig()
app.SetConfig()

zetaTxServer, err := txserver.NewZetaTxServer(
conf.RPCs.ZetaCoreRPC,
Expand Down
81 changes: 81 additions & 0 deletions e2e/contracts/teststaking/TestStaking.abi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,87 @@
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "staker",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validatorSrc",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validatorDst",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "MoveStake",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "staker",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validator",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Stake",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "staker",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validator",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Unstake",
"type": "event"
},
{
"stateMutability": "payable",
"type": "fallback"
Expand Down
2 changes: 1 addition & 1 deletion e2e/contracts/teststaking/TestStaking.bin

Large diffs are not rendered by default.

475 changes: 473 additions & 2 deletions e2e/contracts/teststaking/TestStaking.go

Large diffs are not rendered by default.

83 changes: 82 additions & 1 deletion e2e/contracts/teststaking/TestStaking.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions e2e/contracts/teststaking/TestStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ interface WZETA {

// @dev Purpose of this contract is to call staking precompile
contract TestStaking {
event Stake(
skosito marked this conversation as resolved.
Show resolved Hide resolved
address indexed staker,
address indexed validator,
uint256 amount
);

event Unstake(
address indexed staker,
address indexed validator,
uint256 amount
);

event MoveStake(
address indexed staker,
address indexed validatorSrc,
address indexed validatorDst,
uint256 amount
);

IStaking staking = IStaking(0x0000000000000000000000000000000000000066);
WZETA wzeta;
address owner;
Expand Down
42 changes: 38 additions & 4 deletions e2e/e2etests/test_precompiles_staking_through_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/node/cmd/zetacored/config"
Expand Down Expand Up @@ -99,7 +100,16 @@ func TestPrecompilesStakingThroughContract(r *runner.E2ERunner, args []string) {
// stake 1 to validator1 using testStaking smart contract without smart contract state update
tx, err = testStaking.Stake(r.ZEVMAuth, testStakingAddr, validators[0].OperatorAddress, big.NewInt(1))
require.NoError(r, err)
utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)

// check that stake event was emitted
stakeEvent, err := testStaking.ParseStake(*receipt.Logs[0])
require.NoError(r, err)
expectedValAddr, err := sdk.ValAddressFromBech32(validators[0].OperatorAddress)
require.NoError(r, err)
require.Equal(r, big.NewInt(1).Uint64(), stakeEvent.Amount.Uint64())
require.Equal(r, common.BytesToAddress(expectedValAddr.Bytes()), stakeEvent.Validator)
require.Equal(r, testStakingAddr, stakeEvent.Staker)

// check that bank balance is reduced by 1
balanceAfterStake, err := r.BankClient.Balance(r.Ctx, &banktypes.QueryBalanceRequest{
Expand All @@ -117,7 +127,14 @@ func TestPrecompilesStakingThroughContract(r *runner.E2ERunner, args []string) {
big.NewInt(2),
)
require.NoError(r, err)
utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)

// check that stake event was emitted
stakeEvent, err = testStaking.ParseStake(*receipt.Logs[0])
require.NoError(r, err)
require.Equal(r, big.NewInt(2).Uint64(), stakeEvent.Amount.Uint64())
require.Equal(r, common.BytesToAddress(expectedValAddr.Bytes()), stakeEvent.Validator)
require.Equal(r, testStakingAddr, stakeEvent.Staker)

// check that bank balance is reduced by 2 more, 3 in total
balanceAfterStake, err = r.BankClient.Balance(r.Ctx, &banktypes.QueryBalanceRequest{
Expand Down Expand Up @@ -148,7 +165,14 @@ func TestPrecompilesStakingThroughContract(r *runner.E2ERunner, args []string) {
// unstake 1 from validator1
tx, err = testStaking.Unstake(r.ZEVMAuth, testStakingAddr, validators[0].OperatorAddress, big.NewInt(1))
require.NoError(r, err)
utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)

// check that unstake event was emitted
unstakeEvent, err := testStaking.ParseUnstake(*receipt.Logs[0])
require.NoError(r, err)
require.Equal(r, big.NewInt(1).Uint64(), unstakeEvent.Amount.Uint64())
require.Equal(r, common.BytesToAddress(expectedValAddr.Bytes()), unstakeEvent.Validator)
require.Equal(r, testStakingAddr, unstakeEvent.Staker)

// check shares are set to 2
sharesAfterVal1, err = testStaking.GetShares(&bind.CallOpts{}, testStakingAddr, validators[0].OperatorAddress)
Expand All @@ -172,7 +196,17 @@ func TestPrecompilesStakingThroughContract(r *runner.E2ERunner, args []string) {
big.NewInt(1),
)
require.NoError(r, err)
utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)

// check that moveStake event was emitted
moveStake, err := testStaking.ParseMoveStake(*receipt.Logs[0])
require.NoError(r, err)
require.Equal(r, big.NewInt(1).Uint64(), moveStake.Amount.Uint64())
expectedValDstAddr, err := sdk.ValAddressFromBech32(validators[1].OperatorAddress)
require.NoError(r, err)
require.Equal(r, common.BytesToAddress(expectedValAddr.Bytes()), moveStake.ValidatorSrc)
require.Equal(r, common.BytesToAddress(expectedValDstAddr.Bytes()), moveStake.ValidatorDst)
require.Equal(r, testStakingAddr, moveStake.Staker)

// check shares for both validator1 and validator2 are 1
sharesAfterVal1, err = testStaking.GetShares(&bind.CallOpts{}, testStakingAddr, validators[0].OperatorAddress)
Expand Down
55 changes: 55 additions & 0 deletions precompiles/logs/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package logs

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
)

// AddLog adds log to stateDB
func AddLog(ctx sdk.Context, precompileAddr common.Address, stateDB vm.StateDB, topics []common.Hash, data []byte) {
stateDB.AddLog(&types.Log{
Address: precompileAddr,
Topics: topics,
Data: data,
BlockNumber: uint64(ctx.BlockHeight()),
})
}

// MakeTopics creates topics for log as wrapper around geth abi.MakeTopics function
func MakeTopics(event abi.Event, query ...[]interface{}) ([]common.Hash, error) {
topics := []common.Hash{event.ID}

topicsRes, err := abi.MakeTopics(
query...,
)
if err != nil {
return nil, err
}

for _, topic := range topicsRes {
topics = append(topics, topic[0])
}

return topics, nil
}

// PackBigInt is a helper function to pack a uint256 amount
func PackBigInt(amount *big.Int) ([]byte, error) {
uint256Type, err := abi.NewType("uint256", "", nil)
if err != nil {
return nil, err
}

arguments := abi.Arguments{
{
Type: uint256Type,
},
}

return arguments.Pack(amount)
}
81 changes: 81 additions & 0 deletions precompiles/staking/IStaking.abi
Original file line number Diff line number Diff line change
@@ -1,4 +1,85 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "staker",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validatorSrc",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validatorDst",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "MoveStake",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "staker",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validator",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Stake",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "staker",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "validator",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Unstake",
"type": "event"
},
{
"inputs": [],
"name": "getAllValidators",
Expand Down
Loading
Loading