refactor: remove comet dep in modules (1/n) (#20270)

This commit is contained in:
Marko 2024-05-06 15:13:12 +02:00 committed by GitHub
parent fd5c2f772e
commit ca15c9d0e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 85 additions and 38 deletions

View File

@ -3,7 +3,6 @@ package ante_test
import (
"testing"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"
storetypes "cosmossdk.io/store/types"
@ -39,12 +38,7 @@ func TestSetupDecorator_BlockMaxGas(t *testing.T) {
suite.ctx = suite.ctx.
WithBlockHeight(1).
WithGasMeter(storetypes.NewGasMeter(0)).
WithConsensusParams(cmtproto.ConsensusParams{ // TODO: This is being ignored
Block: &cmtproto.BlockParams{
MaxGas: 100,
},
})
WithGasMeter(storetypes.NewGasMeter(0))
_, err = antehandler(suite.ctx, tx, false)
require.Error(t, err)

View File

@ -1,9 +1,9 @@
package ante_test
import (
crand "crypto/rand"
"testing"
cmtcrypto "github.com/cometbft/cometbft/crypto"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
@ -13,7 +13,7 @@ import (
// This benchmark is used to asses the ante.Secp256k1ToR1GasFactor value
func BenchmarkSig(b *testing.B) {
require := require.New(b)
msg := cmtcrypto.CRandBytes(1000)
msg := cRandBytes(1000)
skK := secp256k1.GenPrivKey()
pkK := skK.PubKey()
@ -42,3 +42,21 @@ func BenchmarkSig(b *testing.B) {
}
})
}
// randBytes generates a random byte slice of the specified length.
//
// It takes an integer parameter representing the number of bytes to generate.
// Returns a byte slice.
func randBytes(numBytes int) []byte {
b := make([]byte, numBytes)
_, err := crand.Read(b)
if err != nil {
panic(err)
}
return b
}
// This only uses the OS's randomness
func cRandBytes(numBytes int) []byte {
return randBytes(numBytes)
}

View File

@ -7,8 +7,6 @@ import (
"fmt"
"strings"
"github.com/cometbft/cometbft/crypto"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -222,7 +220,7 @@ func (ma ModuleAccount) Validate() error {
return errors.New("uninitialized ModuleAccount: BaseAccount is nil")
}
if ma.Address != sdk.AccAddress(crypto.AddressHash([]byte(ma.Name))).String() {
if ma.Address != sdk.AccAddress(AddressHash([]byte(ma.Name))).String() {
return fmt.Errorf("address %s cannot be derived from the module name '%s'", ma.Address, ma.Name)
}

25
x/auth/types/address.go Normal file
View File

@ -0,0 +1,25 @@
package types
import "crypto/sha256"
const TruncatedSize = 20
// AddressHash generates a hash of the given byte slice.
//
// bz: the byte slice to be hashed.
// []byte: the hashed result.
func AddressHash(bz []byte) []byte {
return SumTruncated(bz)
}
// SumTruncated calculates the SHA256 hash of the given byte slice and returns the first TruncatedSize bytes.
//
// Parameters:
// - bz: the byte slice to calculate the hash for.
//
// Returns:
// - []byte: the first TruncatedSize bytes of the calculated hash.
func SumTruncated(bz []byte) []byte {
hash := sha256.Sum256(bz)
return hash[:TruncatedSize]
}

View File

@ -10,10 +10,10 @@ import (
"testing"
"time"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
coreevent "cosmossdk.io/core/event"
"cosmossdk.io/core/header"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
@ -1372,28 +1372,32 @@ func (suite *KeeperTestSuite) TestMsgSendEvents() {
suite.mockSendCoins(suite.ctx, acc0, accAddrs[1])
require.NoError(suite.bankKeeper.SendCoins(suite.ctx, accAddrs[0], accAddrs[1], newCoins))
event1 := sdk.Event{
event1 := coreevent.Event{
Type: banktypes.EventTypeTransfer,
Attributes: []abci.EventAttribute{},
Attributes: []coreevent.Attribute{},
}
event1.Attributes = append(
event1.Attributes,
abci.EventAttribute{Key: banktypes.AttributeKeyRecipient, Value: acc1StrAddr},
coreevent.Attribute{Key: banktypes.AttributeKeyRecipient, Value: acc1StrAddr},
)
event1.Attributes = append(
event1.Attributes,
abci.EventAttribute{Key: banktypes.AttributeKeySender, Value: acc0StrAddr},
coreevent.Attribute{Key: banktypes.AttributeKeySender, Value: acc0StrAddr},
)
event1.Attributes = append(
event1.Attributes,
abci.EventAttribute{Key: sdk.AttributeKeyAmount, Value: newCoins.String()},
coreevent.Attribute{Key: sdk.AttributeKeyAmount, Value: newCoins.String()},
)
ctx := sdk.UnwrapSDKContext(suite.ctx)
// events are shifted due to the funding account events
events := ctx.EventManager().ABCIEvents()
events := ctx.EventManager().Events()
require.Equal(8, len(events))
require.Equal(abci.Event(event1), events[7])
require.Equal(event1.Type, events[7].Type)
for i := range event1.Attributes {
require.Equal(event1.Attributes[i].Key, events[7].Attributes[i].Key)
require.Equal(event1.Attributes[i].Value, events[7].Attributes[i].Value)
}
}
func (suite *KeeperTestSuite) TestMsgMultiSendEvents() {
@ -1453,32 +1457,40 @@ func (suite *KeeperTestSuite) TestMsgMultiSendEvents() {
events = ctx.EventManager().ABCIEvents()
require.Equal(25, len(events)) // 25 due to account funding + coin_spent + coin_recv events
event1 := sdk.Event{
event1 := coreevent.Event{
Type: banktypes.EventTypeTransfer,
Attributes: []abci.EventAttribute{},
Attributes: []coreevent.Attribute{},
}
event1.Attributes = append(
event1.Attributes,
abci.EventAttribute{Key: banktypes.AttributeKeyRecipient, Value: acc2StrAddr},
coreevent.Attribute{Key: banktypes.AttributeKeyRecipient, Value: acc2StrAddr},
)
event1.Attributes = append(
event1.Attributes,
abci.EventAttribute{Key: sdk.AttributeKeyAmount, Value: newCoins.String()})
event2 := sdk.Event{
coreevent.Attribute{Key: sdk.AttributeKeyAmount, Value: newCoins.String()})
event2 := coreevent.Event{
Type: banktypes.EventTypeTransfer,
Attributes: []abci.EventAttribute{},
Attributes: []coreevent.Attribute{},
}
event2.Attributes = append(
event2.Attributes,
abci.EventAttribute{Key: banktypes.AttributeKeyRecipient, Value: acc3StrAddr},
coreevent.Attribute{Key: banktypes.AttributeKeyRecipient, Value: acc3StrAddr},
)
event2.Attributes = append(
event2.Attributes,
abci.EventAttribute{Key: sdk.AttributeKeyAmount, Value: newCoins2.String()},
coreevent.Attribute{Key: sdk.AttributeKeyAmount, Value: newCoins2.String()},
)
// events are shifted due to the funding account events
require.Equal(abci.Event(event1), events[22])
require.Equal(abci.Event(event2), events[24])
require.Equal(event1.Type, events[22].Type)
for i := range event1.Attributes {
require.Equal(event1.Attributes[i].Key, events[22].Attributes[i].Key)
require.Equal(event1.Attributes[i].Value, events[22].Attributes[i].Value)
}
require.Equal(event2.Type, events[24].Type)
for i := range event2.Attributes {
require.Equal(event2.Attributes[i].Key, events[24].Attributes[i].Key)
require.Equal(event2.Attributes[i].Value, events[24].Attributes[i].Value)
}
}
func (suite *KeeperTestSuite) TestSpendableCoins() {

View File

@ -12,7 +12,6 @@ require (
cosmossdk.io/math v1.3.0
cosmossdk.io/store v1.1.0
cosmossdk.io/x/consensus v0.0.0-00010101000000-000000000000
github.com/cometbft/cometbft v0.38.7
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/cosmos-sdk v0.51.0
github.com/cosmos/gogoproto v1.4.12
@ -50,6 +49,7 @@ require (
github.com/cockroachdb/pebble v1.1.0 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft v0.38.7 // indirect
github.com/cometbft/cometbft-db v0.11.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.2 // indirect

View File

@ -1,11 +1,10 @@
package types
import (
"crypto/sha256"
"fmt"
"time"
"github.com/cometbft/cometbft/crypto/tmhash"
"cosmossdk.io/core/address"
"cosmossdk.io/core/comet"
"cosmossdk.io/x/evidence/exported"
@ -27,7 +26,10 @@ func (e *Equivocation) Hash() []byte {
if err != nil {
panic(err)
}
return tmhash.Sum(bz)
hash := sha256.Sum256(bz)
return hash[:]
}
// ValidateBasic performs basic stateless validation checks on an Equivocation object.

View File

@ -15,7 +15,6 @@ require (
cosmossdk.io/x/staking v0.0.0-00010101000000-000000000000
github.com/bits-and-blooms/bitset v1.10.0
github.com/cockroachdb/errors v1.11.1
github.com/cometbft/cometbft v0.38.7
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/cosmos-sdk v0.51.0
github.com/cosmos/gogoproto v1.4.12
@ -51,6 +50,7 @@ require (
github.com/cockroachdb/pebble v1.1.0 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft v0.38.7 // indirect
github.com/cometbft/cometbft-db v0.11.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.2 // indirect

View File

@ -4,8 +4,6 @@ import (
"context"
"time"
"github.com/cometbft/cometbft/crypto"
sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/slashing/types"
@ -50,7 +48,7 @@ func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddres
// AfterValidatorRemoved deletes the address-pubkey relation when a validator is removed,
func (h Hooks) AfterValidatorRemoved(ctx context.Context, consAddr sdk.ConsAddress, _ sdk.ValAddress) error {
return h.k.AddrPubkeyRelation.Remove(ctx, crypto.Address(consAddr))
return h.k.AddrPubkeyRelation.Remove(ctx, consAddr)
}
// AfterValidatorCreated adds the address-pubkey relation when a validator is created.