From c1c63ee948dd84bef73cab5eab7c9e31b083fad3 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 1 Nov 2023 13:05:00 +0100 Subject: [PATCH] Fix: pfb count computing --- pkg/indexer/decode/handle/blob.go | 14 +++- pkg/indexer/decode/handle/test/blob_test.go | 76 ++++++++++++++++++++- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/pkg/indexer/decode/handle/blob.go b/pkg/indexer/decode/handle/blob.go index 6921f8c0..64be0def 100644 --- a/pkg/indexer/decode/handle/blob.go +++ b/pkg/indexer/decode/handle/blob.go @@ -15,7 +15,7 @@ import ( // MsgPayForBlobs pays for the inclusion of a blob in the block. func MsgPayForBlobs(level types.Level, m *appBlobTypes.MsgPayForBlobs) (storageTypes.MsgType, []storage.AddressWithType, []storage.Namespace, int64, error) { var blobsSize int64 - namespaces := make([]storage.Namespace, len(m.Namespaces)) + uniqueNs := make(map[string]*storage.Namespace) for nsI, ns := range m.Namespaces { if len(m.BlobSizes) < nsI { @@ -26,7 +26,7 @@ func MsgPayForBlobs(level types.Level, m *appBlobTypes.MsgPayForBlobs) (storageT appNS := namespace.Namespace{Version: ns[0], ID: ns[1:]} size := int64(m.BlobSizes[nsI]) blobsSize += size - namespaces[nsI] = storage.Namespace{ + namespace := storage.Namespace{ FirstHeight: level, Version: appNS.Version, NamespaceID: appNS.ID, @@ -34,6 +34,16 @@ func MsgPayForBlobs(level types.Level, m *appBlobTypes.MsgPayForBlobs) (storageT PfbCount: 1, Reserved: appNS.IsReserved(), } + if n, ok := uniqueNs[namespace.String()]; ok { + n.Size += size + } else { + uniqueNs[namespace.String()] = &namespace + } + } + + namespaces := make([]storage.Namespace, 0, len(uniqueNs)) + for _, namespace := range uniqueNs { + namespaces = append(namespaces, *namespace) } addresses, err := createAddresses(addressesData{ diff --git a/pkg/indexer/decode/handle/test/blob_test.go b/pkg/indexer/decode/handle/test/blob_test.go index ee36afd1..fe8eee28 100644 --- a/pkg/indexer/decode/handle/test/blob_test.go +++ b/pkg/indexer/decode/handle/test/blob_test.go @@ -4,16 +4,17 @@ package handle_test import ( + "testing" + "github.com/celenium-io/celestia-indexer/internal/storage" storageTypes "github.com/celenium-io/celestia-indexer/internal/storage/types" - "github.com/celenium-io/celestia-indexer/internal/test_suite" + testsuite "github.com/celenium-io/celestia-indexer/internal/test_suite" "github.com/celenium-io/celestia-indexer/pkg/indexer/decode" appBlobTypes "github.com/celestiaorg/celestia-app/x/blob/types" "github.com/cosmos/cosmos-sdk/types" "github.com/fatih/structs" "github.com/shopspring/decimal" "github.com/stretchr/testify/assert" - "testing" ) // MsgPayForBlob @@ -80,3 +81,74 @@ func TestDecodeMsg_SuccessOnPayForBlob(t *testing.T) { assert.Equal(t, msgExpected, dm.Msg) assert.Equal(t, addressesExpected, dm.Addresses) } + +func createMultipleMsgPayForBlob() types.Msg { + msgPayForBlob := appBlobTypes.MsgPayForBlobs{ + Signer: "celestia1zefjxuq43xmjq9x4hhw23wkvvz6st5uhv40tys", + Namespaces: [][]byte{ + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 44, 204, 197, 144, 206, 197, 121, 37, 22}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 44, 204, 197, 144, 206, 197, 121, 37, 22}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 44, 204, 197, 144, 206, 197, 121, 37, 22}, + }, + BlobSizes: []uint32{1, 2, 3}, + ShareCommitments: [][]byte{ + {176, 28, 134, 119, 32, 117, 87, 107, 231, 67, 121, 255, 209, 106, 52, 99, 88, 183, 85, 36, 67, 137, 98, 199, 144, 159, 13, 178, 111, 190, 121, 36}, + {176, 28, 134, 119, 32, 117, 87, 107, 231, 67, 121, 255, 209, 106, 52, 99, 88, 183, 85, 36, 67, 137, 98, 199, 144, 159, 13, 178, 111, 190, 121, 37}, + {176, 28, 134, 119, 32, 117, 87, 107, 231, 67, 121, 255, 209, 106, 52, 99, 88, 183, 85, 36, 67, 137, 98, 199, 144, 159, 13, 178, 111, 190, 121, 38}, + }, + ShareVersions: []uint32{0, 0, 0}, + } + return &msgPayForBlob +} + +func TestDecodeMsg_ManyUpdatesInOnePayForBlob(t *testing.T) { + msgPayForBlob := createMultipleMsgPayForBlob() + blob, now := testsuite.EmptyBlock() + position := 0 + + dm, err := decode.Message(msgPayForBlob, blob.Height, blob.Block.Time, position, storageTypes.StatusSuccess) + + addressesExpected := []storage.AddressWithType{ + { + Type: storageTypes.MsgAddressTypeSigner, + Address: storage.Address{ + Id: 0, + Height: blob.Height, + LastHeight: blob.Height, + Address: "celestia1zefjxuq43xmjq9x4hhw23wkvvz6st5uhv40tys", + Hash: []byte{0x16, 0x53, 0x23, 0x70, 0x15, 0x89, 0xb7, 0x20, 0x14, 0xd5, 0xbd, 0xdc, 0xa8, 0xba, 0xcc, 0x60, 0xb5, 0x5, 0xd3, 0x97}, + Balance: storage.Balance{ + Id: 0, + Total: decimal.Zero, + }, + }, + }, + } + + msgExpected := storage.Message{ + Id: 0, + Height: blob.Height, + Time: now, + Position: 0, + Type: storageTypes.MsgPayForBlobs, + TxId: 0, + Data: structs.Map(msgPayForBlob), + Namespace: []storage.Namespace{ + { + Id: 0, + FirstHeight: blob.Height, + Version: 0, + NamespaceID: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 44, 204, 197, 144, 206, 197, 121, 37, 22}, + Size: 6, + PfbCount: 1, + Reserved: false, + }, + }, + Addresses: addressesExpected, + } + + assert.NoError(t, err) + assert.Equal(t, int64(6), dm.BlobsSize) + assert.Equal(t, msgExpected, dm.Msg) + assert.Equal(t, addressesExpected, dm.Addresses) +}