From ca15c9d0e76c5ecaf7ae7d0ecdd38f44c6a833a0 Mon Sep 17 00:00:00 2001 From: Marko Date: Mon, 6 May 2024 15:13:12 +0200 Subject: [PATCH] refactor: remove comet dep in modules (1/n) (#20270) --- x/auth/ante/setup_test.go | 8 +---- x/auth/ante/sigverify_benchmark_test.go | 22 ++++++++++-- x/auth/types/account.go | 4 +-- x/auth/types/address.go | 25 +++++++++++++ x/bank/keeper/keeper_test.go | 48 +++++++++++++++---------- x/evidence/go.mod | 2 +- x/evidence/types/evidence.go | 8 +++-- x/slashing/go.mod | 2 +- x/slashing/keeper/hooks.go | 4 +-- 9 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 x/auth/types/address.go diff --git a/x/auth/ante/setup_test.go b/x/auth/ante/setup_test.go index 3051c6b8f20f..c63fb02be858 100644 --- a/x/auth/ante/setup_test.go +++ b/x/auth/ante/setup_test.go @@ -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) diff --git a/x/auth/ante/sigverify_benchmark_test.go b/x/auth/ante/sigverify_benchmark_test.go index b598e6df1888..bd35b66341e0 100644 --- a/x/auth/ante/sigverify_benchmark_test.go +++ b/x/auth/ante/sigverify_benchmark_test.go @@ -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) +} diff --git a/x/auth/types/account.go b/x/auth/types/account.go index 529fc28914b3..791803174b5c 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -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) } diff --git a/x/auth/types/address.go b/x/auth/types/address.go new file mode 100644 index 000000000000..00d262847f5b --- /dev/null +++ b/x/auth/types/address.go @@ -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] +} diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index 2c8f114ee8e3..315702dd857b 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -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() { diff --git a/x/evidence/go.mod b/x/evidence/go.mod index 67a7701645c5..a2a331c8f9e3 100644 --- a/x/evidence/go.mod +++ b/x/evidence/go.mod @@ -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 diff --git a/x/evidence/types/evidence.go b/x/evidence/types/evidence.go index c33ec2a26638..735436a24dba 100644 --- a/x/evidence/types/evidence.go +++ b/x/evidence/types/evidence.go @@ -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. diff --git a/x/slashing/go.mod b/x/slashing/go.mod index b746d645f0bf..637243d89167 100644 --- a/x/slashing/go.mod +++ b/x/slashing/go.mod @@ -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 diff --git a/x/slashing/keeper/hooks.go b/x/slashing/keeper/hooks.go index d9c63bf5ee11..2bd081a3d867 100644 --- a/x/slashing/keeper/hooks.go +++ b/x/slashing/keeper/hooks.go @@ -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.