Skip to content

Commit

Permalink
fix: address serialization in PayForBlobs event (backport #2793) (#2794)
Browse files Browse the repository at this point in the history
Backports #2793 to the v1.x branch
  • Loading branch information
rootulp authored Nov 6, 2023
1 parent c105756 commit 98d0550
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/blob/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k Keeper) PayForBlobs(goCtx context.Context, msg *types.MsgPayForBlobs) (*
ctx.GasMeter().ConsumeGas(gasToConsume, payForBlobGasDescriptor)

err := ctx.EventManager().EmitTypedEvent(
types.NewPayForBlobsEvent(sdk.AccAddress(msg.Signer).String(), msg.BlobSizes, msg.Namespaces),
types.NewPayForBlobsEvent(msg.Signer, msg.BlobSizes, msg.Namespaces),
)
if err != nil {
return &types.MsgPayForBlobsResponse{}, err
Expand Down
64 changes: 64 additions & 0 deletions x/blob/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package keeper

import (
"bytes"
"fmt"
"testing"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
appns "github.com/celestiaorg/celestia-app/pkg/namespace"
"github.com/celestiaorg/celestia-app/x/blob/types"
sdk "github.com/cosmos/cosmos-sdk/types"
proto "github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

// TestPayForBlobs verifies the attributes on the emitted event.
func TestPayForBlobs(t *testing.T) {
k, stateStore := keeper(t)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, nil)
signer := "celestia15drmhzw5kwgenvemy30rqqqgq52axf5wwrruf7"
namespace := appns.MustNewV0(bytes.Repeat([]byte{1}, appns.NamespaceVersionZeroIDSize))
namespaces := [][]byte{namespace.Bytes()}
blobData := []byte("blob")
blobSizes := []uint32{uint32(len(blobData))}

// verify no events exist yet
events := ctx.EventManager().Events().ToABCIEvents()
assert.Len(t, events, 0)

// emit an event by submitting a PayForBlob
msg := createMsgPayForBlob(t, signer, namespace, blobData)
_, err := k.PayForBlobs(ctx, msg)
require.NoError(t, err)

// verify that an event was emitted
events = ctx.EventManager().Events().ToABCIEvents()
assert.Len(t, events, 1)
protoEvent, err := sdk.ParseTypedEvent(events[0])
require.NoError(t, err)
event, err := convertToEventPayForBlobs(protoEvent)
require.NoError(t, err)

// verify the attributes of the event
assert.Equal(t, signer, event.Signer)
assert.Equal(t, namespaces, event.Namespaces)
assert.Equal(t, blobSizes, event.BlobSizes)
}

func convertToEventPayForBlobs(message proto.Message) (*types.EventPayForBlobs, error) {
if event, ok := message.(*types.EventPayForBlobs); ok {
return event, nil
}
return nil, fmt.Errorf("message is not of type EventPayForBlobs")
}

func createMsgPayForBlob(t *testing.T, signer string, namespace appns.Namespace, blobData []byte) *types.MsgPayForBlobs {
blob, err := types.NewBlob(namespace, blobData, appconsts.ShareVersionZero)
require.NoError(t, err)
msg, err := types.NewMsgPayForBlobs(signer, blob)
require.NoError(t, err)
return msg
}

0 comments on commit 98d0550

Please sign in to comment.