Skip to content

Commit

Permalink
refactor: remove comet dep in modules (1/n) (#20270)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored May 6, 2024
1 parent fd5c2f7 commit ca15c9d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 38 deletions.
8 changes: 1 addition & 7 deletions x/auth/ante/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 20 additions & 2 deletions x/auth/ante/sigverify_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
4 changes: 1 addition & 3 deletions x/auth/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
25 changes: 25 additions & 0 deletions x/auth/types/address.go
Original file line number Diff line number Diff line change
@@ -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]
}
48 changes: 30 additions & 18 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion x/evidence/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions x/evidence/types/evidence.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"time"

"github.com/cometbft/cometbft/crypto"

sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/slashing/types"

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ca15c9d

Please sign in to comment.