refactor(types): audit QA (backport #21355) (#21422)

Co-authored-by: Akhil Kumar P <36399231+akhilkumarpilli@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
mergify[bot] 2024-08-27 12:36:55 +00:00 committed by GitHub
parent 316c67aed3
commit c288f4ae04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 162 additions and 59 deletions

View File

@ -45,8 +45,6 @@ message TxResponse {
// these events include those emitted by processing all the messages and those
// emitted from the ante. Whereas Logs contains the events, with
// additional metadata, emitted only by processing the messages.
//
// Since: cosmos-sdk 0.42.11, 0.44.5, 0.45
repeated cometbft.abci.v1.Event events = 13
[(gogoproto.nullable) = false, (cosmos_proto.field_added_in) = "cosmos-sdk 0.45"];
}

View File

@ -63,8 +63,6 @@ type TxResponse struct {
// these events include those emitted by processing all the messages and those
// emitted from the ante. Whereas Logs contains the events, with
// additional metadata, emitted only by processing the messages.
//
// Since: cosmos-sdk 0.42.11, 0.44.5, 0.45
Events []v1.Event `protobuf:"bytes,13,rep,name=events,proto3" json:"events"`
}

View File

@ -22,14 +22,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" //nolint:staticcheck // we're using this to support the legacy way of dealing with bech32
)
const (
pubStr = "pub"
valoper = "valoper"
valoperpub = "valoperpub"
valcons = "valcons"
valconspub = "valconspub"
)
type addressTestSuite struct {
suite.Suite
}
@ -42,17 +34,22 @@ func (s *addressTestSuite) SetupSuite() {
s.T().Parallel()
}
var invalidStrs = []string{
"hello, world!",
"0xAA",
"AAA",
types.Bech32PrefixAccAddr + "AB0C",
types.Bech32PrefixAccPub + "1234",
types.Bech32PrefixValAddr + "5678",
types.Bech32PrefixValPub + "BBAB",
types.Bech32PrefixConsAddr + "FF04",
types.Bech32PrefixConsPub + "6789",
}
var (
invalidStrs = []string{
"hello, world!",
"0xAA",
"AAA",
types.Bech32PrefixAccAddr + "AB0C",
types.Bech32PrefixAccPub + "1234",
types.Bech32PrefixValAddr + "5678",
types.Bech32PrefixValPub + "BBAB",
types.Bech32PrefixConsAddr + "FF04",
types.Bech32PrefixConsPub + "6789",
}
addr10byte = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
addr20byte = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
)
func (s *addressTestSuite) testMarshal(original, res interface{}, marshal func() ([]byte, error), unmarshal func([]byte) error) {
bz, err := marshal()
@ -161,6 +158,12 @@ func (s *addressTestSuite) TestUnmarshalYAMLWithInvalidInput() {
s.Require().Equal(types.ErrEmptyHexAddress, err)
}
func setConfigWithPrefix(conf *types.Config, prefix string) {
conf.SetBech32PrefixForAccount(prefix, types.GetBech32PrefixAccPub(prefix))
conf.SetBech32PrefixForValidator(types.GetBech32PrefixValAddr(prefix), types.GetBech32PrefixValPub(prefix))
conf.SetBech32PrefixForConsensusNode(types.GetBech32PrefixConsAddr(prefix), types.GetBech32PrefixConsPub(prefix))
}
// Test that the account address cache ignores the bech32 prefix setting, retrieving bech32 addresses from the cache.
// This will cause the AccAddress.String() to print out unexpected prefixes if the config was changed between bech32 lookups.
// See https://github.com/cosmos/cosmos-sdk/issues/15317.
@ -173,18 +176,14 @@ func (s *addressTestSuite) TestAddrCache() {
// Set SDK bech32 prefixes to 'osmo'
prefix := "osmo"
conf := types.GetConfig()
conf.SetBech32PrefixForAccount(prefix, prefix+pubStr)
conf.SetBech32PrefixForValidator(prefix+valoper, prefix+valoperpub)
conf.SetBech32PrefixForConsensusNode(prefix+valcons, prefix+valconspub)
setConfigWithPrefix(conf, prefix)
acc := types.AccAddress(pub.Address())
osmoAddrBech32 := acc.String()
// Set SDK bech32 to 'cosmos'
prefix = "cosmos"
conf.SetBech32PrefixForAccount(prefix, prefix+pubStr)
conf.SetBech32PrefixForValidator(prefix+valoper, prefix+valoperpub)
conf.SetBech32PrefixForConsensusNode(prefix+valcons, prefix+valconspub)
setConfigWithPrefix(conf, prefix)
// We name this 'addrCosmos' to prove a point, but the bech32 address will still begin with 'osmo' due to the cache behavior.
addrCosmos := types.AccAddress(pub.Address())
@ -210,18 +209,14 @@ func (s *addressTestSuite) TestAddrCacheDisabled() {
// Set SDK bech32 prefixes to 'osmo'
prefix := "osmo"
conf := types.GetConfig()
conf.SetBech32PrefixForAccount(prefix, prefix+pubStr)
conf.SetBech32PrefixForValidator(prefix+valoper, prefix+valoperpub)
conf.SetBech32PrefixForConsensusNode(prefix+valcons, prefix+valconspub)
setConfigWithPrefix(conf, prefix)
acc := types.AccAddress(pub.Address())
osmoAddrBech32 := acc.String()
// Set SDK bech32 to 'cosmos'
prefix = "cosmos"
conf.SetBech32PrefixForAccount(prefix, prefix+pubStr)
conf.SetBech32PrefixForValidator(prefix+valoper, prefix+valoperpub)
conf.SetBech32PrefixForConsensusNode(prefix+valcons, prefix+valconspub)
setConfigWithPrefix(conf, prefix)
addrCosmos := types.AccAddress(pub.Address())
cosmosAddrBech32 := addrCosmos.String()
@ -409,8 +404,6 @@ func (s *addressTestSuite) TestAddressInterface() {
}
func (s *addressTestSuite) TestBech32ifyAddressBytes() {
addr10byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
type args struct {
prefix string
bs []byte
@ -442,8 +435,6 @@ func (s *addressTestSuite) TestBech32ifyAddressBytes() {
}
func (s *addressTestSuite) TestMustBech32ifyAddressBytes() {
addr10byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
type args struct {
prefix string
bs []byte
@ -536,7 +527,7 @@ func (s *addressTestSuite) TestGetFromBech32() {
}
func (s *addressTestSuite) TestMustValAddressFromBech32() {
valAddress1 := types.ValAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
valAddress1 := types.ValAddress(addr20byte)
valAddress2 := types.MustValAddressFromBech32(valAddress1.String())
s.Require().Equal(valAddress1, valAddress2)
@ -589,7 +580,7 @@ func (s *addressTestSuite) TestGetBech32PrefixConsPub() {
}
func (s *addressTestSuite) TestMustAccAddressFromBech32() {
accAddress1 := types.AccAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
accAddress1 := types.AccAddress(addr20byte)
accAddress2 := types.MustAccAddressFromBech32(accAddress1.String())
s.Require().Equal(accAddress1, accAddress2)
}
@ -628,10 +619,9 @@ func (s *addressTestSuite) TestUnmarshalYAMLAccAddressWithEmptyString() {
}
func (s *addressTestSuite) TestFormatAccAddressAsString() {
addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
accAddr := types.AccAddress(addr20byte)
actual := fmt.Sprintf("%s", accAddr) // this will call internally the method AccAddress.Format
actual := fmt.Sprintf("%s", accAddr) // this will internally calls AccAddress.Format method
hrp := types.GetConfig().GetBech32AccountAddrPrefix()
expected, err := bech32.ConvertAndEncode(hrp, addr20byte)
@ -640,9 +630,9 @@ func (s *addressTestSuite) TestFormatAccAddressAsString() {
}
func (s *addressTestSuite) TestFormatAccAddressAsPointer() {
accAddr := types.AccAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
accAddr := types.AccAddress(addr20byte)
ptrAddr := &accAddr
actual := fmt.Sprintf("%p", ptrAddr) // this will call internally the method AccAddress.Format
actual := fmt.Sprintf("%p", ptrAddr) // this will internally calls AccAddress.Format method
expected := fmt.Sprintf("0x%x", uintptr(unsafe.Pointer(&accAddr)))
s.Require().Equal(expected, actual)
}
@ -684,7 +674,6 @@ func (s *addressTestSuite) TestUnmarshalYAMLValAddressWithEmptyString() {
}
func (s *addressTestSuite) TestFormatValAddressAsString() {
addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
accAddr := types.ValAddress(addr20byte)
actual := fmt.Sprintf("%s", accAddr)
@ -696,7 +685,7 @@ func (s *addressTestSuite) TestFormatValAddressAsString() {
}
func (s *addressTestSuite) TestFormatValAddressAsPointer() {
accAddr := types.AccAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
accAddr := types.AccAddress(addr20byte)
ptrAddr := &accAddr
actual := fmt.Sprintf("%p", ptrAddr)
expected := uintptr(unsafe.Pointer(&accAddr))
@ -704,7 +693,7 @@ func (s *addressTestSuite) TestFormatValAddressAsPointer() {
}
func (s *addressTestSuite) TestFormatValAddressWhenVerbIsDifferentFromSOrP() {
myAddr := types.ValAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
myAddr := types.ValAddress(addr20byte)
exp := "000102030405060708090A0B0C0D0E0F10111213"
spec := []string{"%v", "%#v", "%t", "%b", "%c", "%d", "%o", "%O", "%x", "%X", "%U", "%e", "%E", "%f", "%F", "%g", "%G"}
for _, v := range spec {
@ -740,7 +729,7 @@ func (s *addressTestSuite) TestUnmarshalYAMLConsAddressWithEmptyString() {
}
func (s *addressTestSuite) TestFormatConsAddressAsString() {
consAddr := types.ConsAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
consAddr := types.ConsAddress(addr20byte)
actual := fmt.Sprintf("%s", consAddr)
hrp := types.GetConfig().GetBech32ConsensusAddrPrefix()
@ -750,7 +739,7 @@ func (s *addressTestSuite) TestFormatConsAddressAsString() {
}
func (s *addressTestSuite) TestFormatConsAddressAsPointer() {
consAddr := types.ConsAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
consAddr := types.ConsAddress(addr20byte)
ptrAddr := &consAddr
actual := fmt.Sprintf("%p", ptrAddr)
@ -759,7 +748,7 @@ func (s *addressTestSuite) TestFormatConsAddressAsPointer() {
}
func (s *addressTestSuite) TestFormatConsAddressWhenVerbIsDifferentFromSOrP() {
myAddr := types.ConsAddress([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
myAddr := types.ConsAddress(addr20byte)
exp := "000102030405060708090A0B0C0D0E0F10111213"
spec := []string{"%v", "%#v", "%t", "%b", "%c", "%d", "%o", "%O", "%x", "%X", "%U", "%e", "%E", "%f", "%F", "%g", "%G"}
for _, v := range spec {

View File

@ -285,7 +285,7 @@ func (coins Coins) Validate() error {
}
}
// IsSorted returns true when coins are order ASC sorted with denoms.
// IsSorted returns true when coins are sorted in ASC order by denoms.
func (coins Coins) IsSorted() bool {
for i := 1; i < len(coins); i++ {
if coins[i-1].Denom > coins[i].Denom {

View File

@ -45,11 +45,11 @@ type Context struct {
chainID string // Deprecated: Use HeaderService for chainID and CometService for the rest
txBytes []byte
logger log.Logger
voteInfo []abci.VoteInfo // Deprecated: use Cometinfo.LastCommit.Votes instead, will be removed after 0.51
voteInfo []abci.VoteInfo // Deprecated: use Cometinfo.LastCommit.Votes instead, will be removed after 0.52
gasMeter storetypes.GasMeter
blockGasMeter storetypes.GasMeter
checkTx bool // Deprecated: use execMode instead, will be removed after 0.51
recheckTx bool // if recheckTx == true, then checkTx must also be true // Deprecated: use execMode instead, will be removed after 0.51
checkTx bool // Deprecated: use execMode instead, will be removed after 0.52
recheckTx bool // if recheckTx == true, then checkTx must also be true // Deprecated: use execMode instead, will be removed after 0.52
sigverifyTx bool // when run simulation, because the private key corresponding to the account in the genesis.json randomly generated, we must skip the sigverify.
execMode ExecMode
minGasPrice DecCoins
@ -102,7 +102,7 @@ func (c Context) HeaderHash() []byte {
return hash
}
// Deprecated: getting consensus params from the context is deprecated and will be removed after 0.51
// Deprecated: getting consensus params from the context is deprecated and will be removed after 0.52
// Querying the consensus module for the parameters is required in server/v2
func (c Context) ConsensusParams() cmtproto.ConsensusParams {
return c.consParams
@ -138,7 +138,7 @@ func NewContext(ms storetypes.MultiStore, isCheckTx bool, logger log.Logger) Con
kvGasConfig: storetypes.KVGasConfig(),
transientKVGasConfig: storetypes.TransientGasConfig(),
headerInfo: header.Info{
Time: h.Time.UTC(),
Time: h.Time,
},
}
}
@ -161,7 +161,7 @@ func (c Context) WithBlockHeader(header cmtproto.Header) Context {
header.Time = header.Time.UTC()
c.header = header
// when calling withBlockheader on a new context chainID in the struct is empty
// when calling withBlockheader on a new context, chainID in the struct will be empty
c.chainID = header.ChainID
return c
}
@ -208,7 +208,7 @@ func (c Context) WithLogger(logger log.Logger) Context {
}
// WithVoteInfos returns a Context with an updated consensus VoteInfo.
// Deprecated: use WithCometinfo() instead, will be removed after 0.51
// Deprecated: use WithCometinfo() instead, will be removed after 0.52
func (c Context) WithVoteInfos(voteInfo []abci.VoteInfo) Context {
c.voteInfo = voteInfo
return c

View File

@ -10,6 +10,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"cosmossdk.io/core/comet"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
@ -231,3 +232,120 @@ func (s *contextTestSuite) TestUnwrapSDKContext() {
sdkCtx2 = types.UnwrapSDKContext(ctx)
s.Require().Equal(sdkCtx, sdkCtx2)
}
func (s *contextTestSuite) TestTryUnwrapSDKContext() {
sdkCtx := types.NewContext(nil, false, nil)
ctx := types.WrapSDKContext(sdkCtx)
unwrappedCtx, ok := types.TryUnwrapSDKContext(ctx)
s.Require().True(ok)
s.Require().Equal(sdkCtx, unwrappedCtx)
// test case where context doesn't have sdk.Context
ctxWithoutSDK := context.Background()
unwrappedCtx, ok = types.TryUnwrapSDKContext(ctxWithoutSDK)
s.Require().False(ok)
s.Require().Equal(types.Context{}, unwrappedCtx)
// test try unwrapping when we've used context.WithValue
ctx = context.WithValue(sdkCtx, dummyCtxKey{}, "bar")
unwrappedCtx, ok = types.TryUnwrapSDKContext(ctx)
s.Require().True(ok)
s.Require().Equal(sdkCtx, unwrappedCtx)
}
func (s *contextTestSuite) TestToSDKEvidence() {
misbehaviors := []abci.Misbehavior{
{
Type: abci.MisbehaviorType(1),
Height: 100,
Time: time.Now(),
TotalVotingPower: 10,
Validator: abci.Validator{
Address: []byte("address1"),
Power: 5,
},
},
}
expEvidences := []comet.Evidence{
{
Type: comet.MisbehaviorType(1),
Height: 100,
Time: misbehaviors[0].Time,
TotalVotingPower: 10,
Validator: comet.Validator{
Address: []byte("address1"),
Power: 5,
},
},
}
// test ToSDKEvidence method
evidence := types.ToSDKEvidence(misbehaviors)
s.Require().Len(evidence, len(misbehaviors))
s.Require().Equal(expEvidences, evidence)
}
func (s *contextTestSuite) TestToSDKCommitInfo() {
commitInfo := abci.CommitInfo{
Round: 1,
Votes: []abci.VoteInfo{
{
Validator: abci.Validator{
Address: []byte("address1"),
Power: 5,
},
BlockIdFlag: cmtproto.BlockIDFlagCommit,
},
},
}
expCommit := comet.CommitInfo{
Round: 1,
Votes: []comet.VoteInfo{
{
Validator: comet.Validator{
Address: []byte("address1"),
Power: 5,
},
BlockIDFlag: comet.BlockIDFlagCommit,
},
},
}
// test ToSDKCommitInfo method
commit := types.ToSDKCommitInfo(commitInfo)
s.Require().Equal(expCommit, commit)
}
func (s *contextTestSuite) TestToSDKExtendedCommitInfo() {
extendedCommitInfo := abci.ExtendedCommitInfo{
Round: 1,
Votes: []abci.ExtendedVoteInfo{
{
Validator: abci.Validator{
Address: []byte("address1"),
Power: 5,
},
BlockIdFlag: cmtproto.BlockIDFlagCommit,
},
},
}
expCommitInfo := comet.CommitInfo{
Round: 1,
Votes: []comet.VoteInfo{
{
Validator: comet.Validator{
Address: []byte("address1"),
Power: 5,
},
BlockIDFlag: comet.BlockIDFlagCommit,
},
},
}
// test ToSDKExtendedCommitInfo
commitInfo := types.ToSDKExtendedCommitInfo(extendedCommitInfo)
s.Require().Equal(expCommitInfo, commitInfo)
}