Skip to content

Commit 66dd661

Browse files
zsystmvladjdk
andauthored
fix: decode bech32 consensus address before converting to bytes (#267)
The consensus address was previously used in its bech32-encoded form (a 52-character string), which is incorrect. This led to attempts to interpret a bech32 string directly as a 20-byte address, resulting in invalid conversions and data loss. This fix ensures the bech32 consensus address is properly decoded into its original 20-byte form before further processing, preserving the correct address representation expected in EVM-compatible byte format. Co-authored-by: Vlad J <[email protected]>
1 parent e57a44e commit 66dd661

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

precompiles/slashing/query.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ const (
1717
GetParamsMethod = "getParams"
1818
)
1919

20-
// GetSigningInfo implements the query to get a validator's signing info.
20+
// GetSigningInfo handles the `getSigningInfo` precompile call.
21+
// It expects a single argument: the validator’s consensus address in hex format.
22+
// That address comes from the validator’s Tendermint ed25519 public key,
23+
// typically found in `$HOME/.evmd/config/priv_validator_key.json`.
2124
func (p *Precompile) GetSigningInfo(
2225
ctx sdk.Context,
2326
method *abi.Method,
@@ -34,7 +37,10 @@ func (p *Precompile) GetSigningInfo(
3437
return nil, err
3538
}
3639

37-
out := new(SigningInfoOutput).FromResponse(res)
40+
out, err := new(SigningInfoOutput).FromResponse(res)
41+
if err != nil {
42+
return nil, err
43+
}
3844
return method.Outputs.Pack(out.SigningInfo)
3945
}
4046

@@ -55,7 +61,10 @@ func (p *Precompile) GetSigningInfos(
5561
return nil, err
5662
}
5763

58-
out := new(SigningInfosOutput).FromResponse(res)
64+
out, err := new(SigningInfosOutput).FromResponse(res)
65+
if err != nil {
66+
return nil, err
67+
}
5968
return method.Outputs.Pack(out.SigningInfos, out.PageResponse)
6069
}
6170

precompiles/slashing/types.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,32 @@ func ParseSigningInfosArgs(method *abi.Method, args []interface{}) (*slashingtyp
7373
}, nil
7474
}
7575

76-
func (sio *SigningInfoOutput) FromResponse(res *slashingtypes.QuerySigningInfoResponse) *SigningInfoOutput {
76+
func (sio *SigningInfoOutput) FromResponse(res *slashingtypes.QuerySigningInfoResponse) (*SigningInfoOutput, error) {
77+
consAddr, err := types.ConsAddressFromBech32(res.ValSigningInfo.Address)
78+
if err != nil {
79+
return nil, fmt.Errorf("error parsing consensus address: %w", err)
80+
}
81+
7782
sio.SigningInfo = SigningInfo{
78-
ValidatorAddress: common.BytesToAddress([]byte(res.ValSigningInfo.Address)),
83+
ValidatorAddress: common.BytesToAddress(consAddr.Bytes()),
7984
StartHeight: res.ValSigningInfo.StartHeight,
8085
IndexOffset: res.ValSigningInfo.IndexOffset,
8186
JailedUntil: res.ValSigningInfo.JailedUntil.Unix(),
8287
Tombstoned: res.ValSigningInfo.Tombstoned,
8388
MissedBlocksCounter: res.ValSigningInfo.MissedBlocksCounter,
8489
}
85-
return sio
90+
return sio, nil
8691
}
8792

88-
func (sio *SigningInfosOutput) FromResponse(res *slashingtypes.QuerySigningInfosResponse) *SigningInfosOutput {
93+
func (sio *SigningInfosOutput) FromResponse(res *slashingtypes.QuerySigningInfosResponse) (*SigningInfosOutput, error) {
8994
sio.SigningInfos = make([]SigningInfo, len(res.Info))
9095
for i, info := range res.Info {
96+
consAddr, err := types.ConsAddressFromBech32(info.Address)
97+
if err != nil {
98+
return nil, fmt.Errorf("error parsing consensus address: %w", err)
99+
}
91100
sio.SigningInfos[i] = SigningInfo{
92-
ValidatorAddress: common.BytesToAddress([]byte(info.Address)),
101+
ValidatorAddress: common.BytesToAddress(consAddr.Bytes()),
93102
StartHeight: info.StartHeight,
94103
IndexOffset: info.IndexOffset,
95104
JailedUntil: info.JailedUntil.Unix(),
@@ -103,7 +112,7 @@ func (sio *SigningInfosOutput) FromResponse(res *slashingtypes.QuerySigningInfos
103112
Total: res.Pagination.Total,
104113
}
105114
}
106-
return sio
115+
return sio, nil
107116
}
108117

109118
// ValidatorUnjailed defines the data structure for the ValidatorUnjailed event.

tests/integration/precompiles/slashing/test_query.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
func (s *PrecompileTestSuite) TestGetSigningInfo() {
1818
method := s.precompile.Methods[slashing.GetSigningInfoMethod]
1919

20+
valSigners := s.network.GetValidators()
21+
val0ConsAddr, _ := valSigners[0].GetConsAddr()
22+
23+
consAddr := types.ConsAddress(val0ConsAddr)
2024
testCases := []struct {
2125
name string
2226
malleate func() []interface{}
@@ -52,8 +56,9 @@ func (s *PrecompileTestSuite) TestGetSigningInfo() {
5256
func() []interface{} {
5357
err := s.network.App.GetSlashingKeeper().SetValidatorSigningInfo(
5458
s.network.GetContext(),
55-
types.ConsAddress(s.keyring.GetAddr(0).Bytes()),
59+
consAddr,
5660
slashingtypes.ValidatorSigningInfo{
61+
Address: consAddr.String(),
5762
StartHeight: 1,
5863
IndexOffset: 2,
5964
MissedBlocksCounter: 1,
@@ -62,10 +67,11 @@ func (s *PrecompileTestSuite) TestGetSigningInfo() {
6267
)
6368
s.Require().NoError(err)
6469
return []interface{}{
65-
s.keyring.GetAddr(0),
70+
common.BytesToAddress(consAddr.Bytes()),
6671
}
6772
},
6873
func(signingInfo *slashing.SigningInfo) {
74+
s.Require().Equal(consAddr.Bytes(), signingInfo.ValidatorAddress.Bytes())
6975
s.Require().Equal(int64(1), signingInfo.StartHeight)
7076
s.Require().Equal(int64(2), signingInfo.IndexOffset)
7177
s.Require().Equal(int64(1), signingInfo.MissedBlocksCounter)
@@ -134,19 +140,26 @@ func (s *PrecompileTestSuite) TestGetSigningInfos() {
134140
s.Require().Len(signingInfos, 3)
135141
s.Require().Equal(uint64(3), pageResponse.Total)
136142

143+
valSigners := s.network.GetValidators()
144+
val0ConsAddr, _ := valSigners[0].GetConsAddr()
145+
val1ConsAddr, _ := valSigners[1].GetConsAddr()
146+
val2ConsAddr, _ := valSigners[2].GetConsAddr()
137147
// Check first validator's signing info
148+
s.Require().Equal(val0ConsAddr, signingInfos[0].ValidatorAddress.Bytes())
138149
s.Require().Equal(int64(0), signingInfos[0].StartHeight)
139150
s.Require().Equal(int64(1), signingInfos[0].IndexOffset)
140151
s.Require().Equal(int64(0), signingInfos[0].JailedUntil)
141152
s.Require().False(signingInfos[0].Tombstoned)
142153

143154
// Check second validator's signing info
155+
s.Require().Equal(val1ConsAddr, signingInfos[1].ValidatorAddress.Bytes())
144156
s.Require().Equal(int64(0), signingInfos[1].StartHeight)
145157
s.Require().Equal(int64(1), signingInfos[1].IndexOffset)
146158
s.Require().Equal(int64(0), signingInfos[1].JailedUntil)
147159
s.Require().False(signingInfos[1].Tombstoned)
148160

149161
// Check third validator's signing info
162+
s.Require().Equal(val2ConsAddr, signingInfos[2].ValidatorAddress.Bytes())
150163
s.Require().Equal(int64(0), signingInfos[2].StartHeight)
151164
s.Require().Equal(int64(1), signingInfos[2].IndexOffset)
152165
s.Require().Equal(int64(0), signingInfos[2].JailedUntil)
@@ -172,6 +185,9 @@ func (s *PrecompileTestSuite) TestGetSigningInfos() {
172185
s.Require().NotNil(pageResponse.NextKey)
173186

174187
// Check first validator's signing info
188+
valSigners := s.network.GetValidators()
189+
val0ConsAddr, _ := valSigners[0].GetConsAddr()
190+
s.Require().Equal(val0ConsAddr, signingInfos[0].ValidatorAddress.Bytes())
175191
s.Require().Equal(int64(0), signingInfos[0].StartHeight)
176192
s.Require().Equal(int64(1), signingInfos[0].IndexOffset)
177193
s.Require().Equal(int64(0), signingInfos[0].JailedUntil)

0 commit comments

Comments
 (0)