Skip to content

Commit

Permalink
imp(staking): add CreateValidator unit test for precompiled contract …
Browse files Browse the repository at this point in the history
…staking (#2050)

* imp(staking): add CreateValidatorMethod for TestIsTransaction

* chore: update CHANGELOG.md

* imp(staking): add TestCreateValidatorEvent

* imp(staking): add TestCreateValidator

* imp(staking): update TestCreateValidator

* imp(staking): remove create validator unit test pubkey is incorrect size

* imp(staking): update TestCreateValidator

* fix typos

---------

Co-authored-by: Vladislav Varadinov <vladislav.varadinov@gmail.com>
Co-authored-by: stepit <stefanofrancesco.pitton@gmail.com>
Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
  • Loading branch information
4 people committed Nov 22, 2023
1 parent b3b7994 commit 6e2812d
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (bank) [#2041](https://github.com/evmos/evmos/pull/2041) Add `supplyOf` query to bank extension.
- (staking) [#2046](https://github.com/evmos/evmos/pull/2046) Format any type ConsensusPubkey into a base64 string.
- (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
- (osmosis-outpost) [#2042](https://github.com/evmos/evmos/pull/2042) Add Osmosis's wasm hook validation functions to test
- (staking) [#2050](https://github.com/evmos/evmos/pull/2050) Add CreateValidator unit test for precompiled contract staking.
- (make) [#2052](https://github.com/evmos/evmos/pull/2052) Fix Makefile targets to compile ERC20 contracts.
- (distribution) [#2054](https://github.com/evmos/evmos/pull/2054) Change the validator address in the events from string type to address type.
- (staking) [#2053](https://github.com/evmos/evmos/pull/2053) Change the validator address in the events from string type to address type.
Expand Down
2 changes: 1 addition & 1 deletion precompiles/staking/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (p Precompile) EmitAllowanceChangeEvent(ctx sdk.Context, stateDB vm.StateDB
return nil
}

// EmitCreateValidatorEvent creates a new ctreate valdator event emitted on a CreateValidator transaction.
// EmitCreateValidatorEvent creates a new create validator event emitted on a CreateValidator transaction.
func (p Precompile) EmitCreateValidatorEvent(ctx sdk.Context, stateDB vm.StateDB, msg *stakingtypes.MsgCreateValidator, delegatorAddr common.Address) error {
// Prepare the event topics
event := p.ABI.Events[EventTypeCreateValidator]
Expand Down
77 changes: 77 additions & 0 deletions precompiles/staking/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,83 @@ func (s *PrecompileTestSuite) TestDecreaseAllowanceEvent() {
}
}

func (s *PrecompileTestSuite) TestCreateValidatorEvent() {
var (
delegationValue = big.NewInt(1205000000000000000)
method = s.precompile.Methods[staking.CreateValidatorMethod]
operatorAddress = sdk.ValAddress(s.address.Bytes()).String()
pubkey = "nfJ0axJC9dhta1MAE1EBFaVdxxkYzxYrBaHuJVjG//M="
)

testCases := []struct {
name string
malleate func() []interface{}
expErr bool
errContains string
postCheck func()
}{
{
name: "success - the correct event is emitted",
malleate: func() []interface{} {
return []interface{}{
staking.Description{
Moniker: "node0",
Identity: "",
Website: "",
SecurityContact: "",
Details: "",
},
staking.Commission{
Rate: sdk.OneDec().BigInt(),
MaxRate: sdk.OneDec().BigInt(),
MaxChangeRate: sdk.OneDec().BigInt(),
},
big.NewInt(1),
s.address,
operatorAddress,
pubkey,
delegationValue,
}
},
postCheck: func() {
log := s.stateDB.Logs()[0]
s.Require().Equal(log.Address, s.precompile.Address())

// Check event signature matches the one emitted
event := s.precompile.ABI.Events[staking.EventTypeCreateValidator]
s.Require().Equal(crypto.Keccak256Hash([]byte(event.Sig)), common.HexToHash(log.Topics[0].Hex()))
s.Require().Equal(log.BlockNumber, uint64(s.ctx.BlockHeight()))

// Check the fully unpacked event matches the one emitted
var createValidatorEvent staking.EventCreateValidator
err := cmn.UnpackLog(s.precompile.ABI, &createValidatorEvent, staking.EventTypeCreateValidator, *log)
s.Require().NoError(err)
s.Require().Equal(s.address, createValidatorEvent.DelegatorAddress)
s.Require().Equal(s.address, createValidatorEvent.ValidatorAddress)
s.Require().Equal(delegationValue, createValidatorEvent.Value)
},
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
s.SetupTest() // reset
operatorAddress = sdk.ValAddress(s.address.Bytes()).String()

contract := vm.NewContract(vm.AccountRef(s.address), s.precompile, big.NewInt(0), 200000)
_, err := s.precompile.CreateValidator(s.ctx, s.address, contract, s.stateDB, &method, tc.malleate())

if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.errContains)
} else {
s.Require().NoError(err)
tc.postCheck()
}
})
}
}

func (s *PrecompileTestSuite) TestDelegateEvent() {
var (
delegationAmt = big.NewInt(1500000000000000000)
Expand Down
5 changes: 5 additions & 0 deletions precompiles/staking/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (s *PrecompileTestSuite) TestIsTransaction() {
s.precompile.Methods[authorization.DecreaseAllowanceMethod].Name,
true,
},
{
staking.CreateValidatorMethod,
s.precompile.Methods[staking.CreateValidatorMethod].Name,
true,
},
{
staking.DelegateMethod,
s.precompile.Methods[staking.DelegateMethod].Name,
Expand Down

0 comments on commit 6e2812d

Please sign in to comment.