8000 imp(staking): change the validator address in the events from string type to address type by luchenqun · Pull Request #2053 · evmos/evmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

imp(staking): change the validator address in the events from string type to address type #2053

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

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -92,6 +92,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (bank) [#2045](https://github.com/evmos/evmos/pull/2045) Add unit tests for `supplyOf` query.
- (osmosis-outpost) [#2042](https://github.com/evmos/evmos/pull/2042) Add Osmosis's wasm hook validation functions to test
- (make) [#2052](https://github.com/evmos/evmos/pull/2052) Fix Makefile targets to compile ERC20 contracts.
- (staking) [#2053](https://github.com/evmos/evmos/pull/2053) Change the validator address in the events from string type to address type.
- (werc20) [#2057](https://github.com/evmos/evmos/pull/2057) WERC20 refactors and handling of fallback and receive functions.
- (werc20) [#2062](https://github.com/evmos/evmos/pull/2062) Remove name checking for `fallback` and `receive` functions.

Expand Down
10 changes: 5 additions & 5 deletions precompiles/staking/StakingI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ interface StakingI is authorization.AuthorizationI {
/// @param newShares The new delegation shares being held
event Delegate(
address indexed delegatorAddress,
string indexed validatorAddress,
address indexed validatorAddress,
uint256 amount,
uint256 newShares
);
Expand All @@ -299,7 +299,7 @@ interface StakingI is authorization.AuthorizationI {
/// @param completionTime The time at which the unbonding is completed
event Unbond(
address indexed delegatorAddress,
string indexed validatorAddress,
address indexed validatorAddress,
uint256 amount,
uint256 completionTime
);
Expand All @@ -313,8 +313,8 @@ interface StakingI is authorization.AuthorizationI {
/// @param completionTime The time at which the redelegation is completed
event Redelegate(
address indexed delegatorAddress,
string indexed validatorSrcAddress,
string indexed validatorDstAddress,
address indexed validatorSrcAddress,
address indexed validatorDstAddress,
uint256 amount,
uint256 completionTime
);
Expand All @@ -327,7 +327,7 @@ interface StakingI is authorization.AuthorizationI {
/// @param creationHeight The block height at which the unbonding of a delegation was initiated
event CancelUnbondingDelegation(
address indexed delegatorAddress,
string indexed validatorAddress,
address indexed validatorAddress,
uint256 amount,
uint256 creationHeight
);
Expand Down
20 changes: 10 additions & 10 deletions precompiles/staking/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
},
{
"indexed": true,
"internalType": "string",
"internalType": "address",
"name": "validatorAddress",
"type": "string"
"type": "address"
},
{
"indexed": false,
Expand Down Expand Up @@ -128,9 +128,9 @@
},
{
"indexed": true,
"internalType": "string",
"internalType": "address",
"name": "validatorAddress",
"type": "string"
"type": "address"
},
{
"indexed": false,
Expand Down Expand Up @@ -159,15 +159,15 @@
},
{
"indexed": true,
"internalType": "string",
"internalType": "address",
"name": "validatorSrcAddress",
"type": "string"
"type": "address"
},
{
"indexed": true,
"internalType": "string",
"internalType": "address",
"name": "validatorDstAddress",
"type": "string"
"type": "address"
},
{
"indexed": false,
Expand Down Expand Up @@ -221,9 +221,9 @@
},
{
"indexed": true,
"internalType": "string",
"internalType": "address",
"name": "validatorAddress",
"type": "string"
"type": "address"
},
{
"indexed": false,
Expand Down
59 changes: 28 additions & 31 deletions precompiles/staking/events.go
6D47
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (p Precompile) EmitCreateValidatorEvent(ctx sdk.Context, stateDB vm.StateDB
return err
}

topics, err := p.createCreateValidatorTxTopics(3, event, delegatorAddr, common.BytesToAddress(valAddr.Bytes()))
topics, err := p.createStakingTxTopics(3, event, delegatorAddr, common.BytesToAddress(valAddr.Bytes()))
if err != nil {
return err
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func (p Precompile) EmitDelegateEvent(ctx sdk.Context, stateDB vm.StateDB, msg *

// Prepare the event topics
event := p.ABI.Events[EventTypeDelegate]
topics, err := p.createStakingTxTopics(3, event, delegatorAddr, msg.ValidatorAddress)
topics, err := p.createStakingTxTopics(3, event, delegatorAddr, common.BytesToAddress(valAddr.Bytes()))
if err != nil {
return err
}
Expand All @@ -194,9 +194,14 @@ func (p Precompile) EmitDelegateEvent(ctx sdk.Context, stateDB vm.StateDB, msg *

// EmitUnbondEvent creates a new unbond event emitted on an Undelegate transaction.
func (p Precompile) EmitUnbondEvent(ctx sdk.Context, stateDB vm.StateDB, msg *stakingtypes.MsgUndelegate, delegatorAddr common.Address, completionTime int64) error {
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return err
}

// Prepare the event topics
event := p.ABI.Events[EventTypeUnbond]
topics, err := p.createStakingTxTopics(3, event, delegatorAddr, msg.ValidatorAddress)
topics, err := p.createStakingTxTopics(3, event, delegatorAddr, common.BytesToAddress(valAddr.Bytes()))
if err != nil {
return err
}
Expand All @@ -218,14 +223,24 @@ func (p Precompile) EmitUnbondEvent(ctx sdk.Context, stateDB vm.StateDB, msg *st

// EmitRedelegateEvent creates a new redelegate event emitted on a Redelegate transaction.
func (p Precompile) EmitRedelegateEvent(ctx sdk.Context, stateDB vm.StateDB, msg *stakingtypes.MsgBeginRedelegate, delegatorAddr common.Address, completionTime int64) error {
valSrcAddr, err := sdk.ValAddressFromBech32(msg.ValidatorSrcAddress)
if err != nil {
return err
}

valDstAddr, err := sdk.ValAddressFromBech32(msg.ValidatorDstAddress)
if err != nil {
return err
}

// Prepare the event topics
event := p.ABI.Events[EventTypeRedelegate]
topics, err := p.createStakingTxTopics(4, event, delegatorAddr, msg.ValidatorSrcAddress)
topics, err := p.createStakingTxTopics(4, event, delegatorAddr, common.BytesToAddress(valSrcAddr.Bytes()))
if err != nil {
return err
}

topics[3], err = cmn.MakeTopic(msg.ValidatorDstAddress)
topics[3], err = cmn.MakeTopic(common.BytesToAddress(valDstAddr.Bytes()))
if err != nil {
return err
}
Expand All @@ -247,9 +262,14 @@ func (p Precompile) EmitRedelegateEvent(ctx sdk.Context, stateDB vm.StateDB, msg

// EmitCancelUnbondingDelegationEvent creates a new cancel unbonding delegation event emitted on a CancelUnbondingDelegation transaction.
func (p Precompile) EmitCancelUnbondingDelegationEvent(ctx sdk.Context, stateDB vm.StateDB, msg *stakingtypes.MsgCancelUnbondingDelegation, delegatorAddr common.Address) error {
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return err
}

// Prepare the event topics
event := p.ABI.Events[EventTypeCancelUnbondingDelegation]
topics, err := p.createStakingTxTopics(3, event, delegatorAddr, msg.ValidatorAddress)
topics, err := p.createStakingTxTopics(3, event, delegatorAddr, common.BytesToAddress(valAddr.Bytes()))
if err != nil {
return err
}
Expand All @@ -269,31 +289,8 @@ func (p Precompile) EmitCancelUnbondingDelegationEvent(ctx sdk.Context, stateDB
return nil
}

// createStakingTxTopics creates the topics for staking transactions Delegate, Undelegate, Redelegate and CancelUnbondingDelegation.
func (p Precompile) createStakingTxTopics(topicsLen uint64, event abi.Event, delegatorAddr common.Address, validatorAddr string) ([]common.Hash, error) {
topics := make([]common.Hash, topicsLen)
// NOTE: If your solidity event contains indexed event types, then they become a topic rather than part of the data property of the log.
9E81 // In solidity you may only have up to 4 topics but only 3 indexed event types. The first topic is always the signature of the event.

// The first topic is always the signature of the event.
topics[0] = event.ID

var err error
topics[1], err = cmn.MakeTopic(delegatorAddr)
if err != nil {
return nil, err
}

topics[2], err = cmn.MakeTopic(validatorAddr)
if err != nil {
return nil, err
}

return topics, nil
}

// createCreateValidatorTxTopics creates the topics for staking transactions CreateValidator.
func (p Precompile) createCreateValidatorTxTopics(topicsLen uint64, event abi.Event, delegatorAddr common.Address, validatorAddr common.Address) ([]common.Hash, error) {
// createStakingTxTopics creates the topics for staking transactions CreateValidator, Delegate, Undelegate, Redelegate and CancelUnbondingDelegation.
func (p Precompile) createStakingTxTopics(topicsLen uint64, event abi.Event, delegatorAddr common.Address, validatorAddr common.Address) ([]common.Hash, error) {
topics := make([]common.Hash, topicsLen)
// NOTE: If your solidity event contains indexed event types, then they become a topic rather than part of the data property of the log.
// In solidity you may only have up to 4 topics but only 3 indexed event types. The first topic is always the signature of the event.
Expand Down
40 changes: 31 additions & 9 deletions precompiles/staking/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package staking_test
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/vm"
Expand Down Expand Up @@ -256,12 +258,16 @@ func (s *PrecompileTestSuite) TestDelegateEvent() {
s.Require().Equal(crypto.Keccak256Hash([]byte(event.Sig)), common.HexToHash(log.Topics[0].Hex()))
s.Require().Equal(log.BlockNumber, uint64(s.ctx.BlockHeight()))

optAddr, err := sdk.ValAddressFromBech32(s.validators[0].OperatorAddress)
s.Require().NoError(err)
optHexAddr := common.BytesToAddress(optAddr)

// Check the fully unpacked event matches the one emitted
var delegationEvent staking.EventDelegate
err := cmn.UnpackLog(s.precompile.ABI, &delegationEvent, staking.EventTypeDelegate, *log)
err = cmn.UnpackLog(s.precompile.ABI, &delegationEvent, staking.EventTypeDelegate, *log)
s.Require().NoError(err)
s.Require().Equal(s.address, delegationEvent.DelegatorAddress)
s.Require().Equal(crypto.Keccak256Hash([]byte(s.validators[0].OperatorAddress)), delegationEvent.ValidatorAddress)
s.Require().Equal(optHexAddr, delegationEvent.ValidatorAddress)
s.Require().Equal(delegationAmt, delegationEvent.Amount)
s.Require().Equal(newSharesExp, delegationEvent.NewShares)
},
Expand Down Expand Up @@ -317,12 +323,16 @@ func (s *PrecompileTestSuite) TestUnbondEvent() {
s.Require().Equal(crypto.Keccak256Hash([]byte(event.Sig)), common.HexToHash(log.Topics[0].Hex()))
s.Require().Equal(log.BlockNumber, uint64(s.ctx.BlockHeight()))

optAddr, err := sdk.ValAddressFromBech32(s.validators[0].OperatorAddress)
s.Require().NoError(err)
optHexAddr := common.BytesToAddress(optAddr)

// Check the fully unpacked event matches the one emitted
var unbondEvent staking.EventUnbond
err := cmn.UnpackLog(s.precompile.ABI, &unbondEvent, staking.EventTypeUnbond, *log)
err = cmn.UnpackLog(s.precompile.ABI, &unbondEvent, staking.EventTypeUnbond, *log)
s.Require().NoError(err)
s.Require().Equal(s.address, unbondEvent.DelegatorAddress)
s.Require().Equal(crypto.Keccak256Hash([]byte(s.validators[0].OperatorAddress)), unbondEvent.ValidatorAddress)
s.Require().Equal(optHexAddr, unbondEvent.ValidatorAddress)
s.Require().Equal(big.NewInt(1000000000000000000), unbondEvent.Amount)
},
},
Expand Down Expand Up @@ -378,12 +388,20 @@ func (s *PrecompileTestSuite) TestRedelegateEvent() {
s.Require().Equal(crypto.Keccak256Hash([]byte(event.Sig)), common.HexToHash(log.Topics[0].Hex()))
s.Require().Equal(log.BlockNumber, uint64(s.ctx.BlockHeight()))

optSrcAddr, err := sdk.ValAddressFromBech32(s.validators[0].OperatorAddress)
s.Require().NoError(err)
optSrcHexAddr := common.BytesToAddress(optSrcAddr)

optDstAddr, err := sdk.ValAddressFromBech32(s.validators[1].OperatorAddress)
s.Require().NoError(err)
optDstHexAddr := common.BytesToAddress(optDstAddr)

var redelegateEvent staking.EventRedelegate
err := cmn.UnpackLog(s.precompile.ABI, &redelegateEvent, staking.EventTypeRedelegate, *log)
err = cmn.UnpackLog(s.precompile.ABI, &redelegateEvent, staking.EventTypeRedelegate, *log)
s.Require().NoError(err)
s.Require().Equal(s.address, redelegateEvent.DelegatorAddress)
s.Require().Equal(crypto.Keccak256Hash([]byte(s.validators[0].OperatorAddress)), redelegateEvent.ValidatorSrcAddress)
s.Require().Equal(crypto.Keccak256Hash([]byte(s.validators[1].OperatorAddress)), redelegateEvent.ValidatorDstAddress)
s.Require().Equal(optSrcHexAddr, redelegateEvent.ValidatorSrcAddress)
s.Require().Equal(optDstHexAddr, redelegateEvent.ValidatorDstAddress)
s.Require().Equal(big.NewInt(1000000000000000000), redelegateEvent.Amount)
},
},
Expand Down Expand Up @@ -451,12 +469,16 @@ func (s *PrecompileTestSuite) TestCancelUnbondingDelegationEvent() {
s.Require().Equal(crypto.Keccak256Hash([]byte(event.Sig)), common.HexToHash(log.Topics[0].Hex()))
s.Require().Equal(log.BlockNumber, uint64(s.ctx.BlockHeight()))

optAddr, err := sdk.ValAddressFromBech32(s.validators[0].OperatorAddress)
s.Require().NoError(err)
optHexAddr := common.BytesToAddress(optAddr)

// Check event fields match the ones emitted
var cancelUnbondEvent staking.EventCancelUnbonding
err := cmn.UnpackLog(s.precompile.ABI, &cancelUnbondEvent, staking.EventTypeCancelUnbondingDelegation, *log)
err = cmn.UnpackLog(s.precompile.ABI, &cancelUnbondEvent, staking.EventTypeCancelUnbondingDelegation, *log)
s.Require().NoError(err)
s.Require().Equal(s.address, cancelUnbondEvent.DelegatorAddress)
s.Require().Equal(crypto.Keccak256Hash([]byte(s.validators[0].OperatorAddress)), cancelUnbondEvent.ValidatorAddress)
s.Require().Equal(optHexAddr, cancelUnbondEvent.ValidatorAddress)
s.Require().Equal(big.NewInt(1000000000000000000), cancelUnbondEvent.Amount)
s.Require().Equal(big.NewInt(2), cancelUnbondEvent.CreationHeight)
},
Expand Down
10 changes: 5 additions & 5 deletions precompiles/staking/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ import (
// EventDelegate defines the event data for the staking Delegate transaction.
type EventDelegate struct {
DelegatorAddress common.Address
ValidatorAddress common.Hash
ValidatorAddress common.Address
Amount *big.Int
NewShares *big.Int
}

// EventUnbond defines the event data for the staking Undelegate transaction.
type EventUnbond struct {
DelegatorAddress common.Address
ValidatorAddress common.Hash
ValidatorAddress common.Address
Amount *big.Int
CompletionTime *big.Int
}

// EventRedelegate defines the event data for the staking Redelegate transaction.
type EventRedelegate struct {
DelegatorAddress common.Address
ValidatorSrcAddress common.Hash
ValidatorDstAddress common.Hash
ValidatorSrcAddress common.Address
ValidatorDstAddress common.Address
Amount *big.Int
CompletionTime *big.Int
}

// EventCancelUnbonding defines the event data for the staking CancelUnbond transaction.
type EventCancelUnbonding struct {
DelegatorAddress common.Address
ValidatorAddress common.Hash
ValidatorAddress common.Address
Amount *big.Int
CreationHeight *big.Int
}
Expand Down
0