From f5fd7914d88d4bc412061639ba5b22beb46a0301 Mon Sep 17 00:00:00 2001 From: Rootul P Date: Mon, 6 Nov 2023 10:30:22 +0100 Subject: [PATCH] fix: address serialization in PayForBlobs event (#2793) Closes https://github.com/celestiaorg/celestia-app/issues/2781 Note this _shouldn't_ be consensus breaking b/c the ABCI response results hash shouldn't depend on events. [`deterministicResponseDeliverTx`](https://github.com/celestiaorg/celestia-core/blob/ffdb652634fb1b6b6426c0f1a2c5adec8af80253/types/results.go#L45-L55) filters events from the response. Thanks @cmwaters for identifying. ## Testing I tested on a Robusta devnet named app-v130-node-v0110. See https://github.com/celestiaorg/celestia-app/pull/2794#issuecomment-1787411696 and https://github.com/celestiaorg/celestia-app/pull/2794#issuecomment-1788888796 --- x/blob/keeper/keeper.go | 2 +- x/blob/keeper/keeper_test.go | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 x/blob/keeper/keeper_test.go diff --git a/x/blob/keeper/keeper.go b/x/blob/keeper/keeper.go index 34ad7f5e01..5111bd70ae 100644 --- a/x/blob/keeper/keeper.go +++ b/x/blob/keeper/keeper.go @@ -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 diff --git a/x/blob/keeper/keeper_test.go b/x/blob/keeper/keeper_test.go new file mode 100644 index 0000000000..44dcc4a0cb --- /dev/null +++ b/x/blob/keeper/keeper_test.go @@ -0,0 +1,64 @@ +package keeper + +import ( + "bytes" + "fmt" + "testing" + + "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/pkg/blob" + 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 := blob.New(namespace, blobData, appconsts.ShareVersionZero) + msg, err := types.NewMsgPayForBlobs(signer, blob) + require.NoError(t, err) + return msg +}