Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getMsgInfo returns an ErrNotFound when there are no rows #12680

Merged
merged 6 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion chain/index/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func (si *SqliteIndexer) GetMsgInfo(ctx context.Context, messageCid cid.Cid) (*M
var tipsetKeyCidBytes []byte
var height int64

if err := si.queryMsgInfo(ctx, messageCid, &tipsetKeyCidBytes, &height); err != nil {
if err := si.readWithHeadIndexWait(ctx, func() error {
return si.queryMsgInfo(ctx, messageCid, &tipsetKeyCidBytes, &height)
}); err != nil {
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}

Expand Down
79 changes: 65 additions & 14 deletions chain/index/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,74 @@ func TestGetMsgInfo(t *testing.T) {
t.Logf("seed: %d", seed)
rng := pseudo.New(pseudo.NewSource(seed))
s, _, _ := setupWithHeadIndexed(t, 10, rng)
msgCid := randomCid(t, rng)
msgCidBytes := msgCid.Bytes()
tsKeyCid := randomCid(t, rng)

insertTipsetMessage(t, s, tipsetMessage{
tipsetKeyCid: tsKeyCid.Bytes(),
height: uint64(1),
reverted: false,
messageCid: msgCidBytes,
messageIndex: 1,
t.Run("message exists", func(t *testing.T) {
msgCid := randomCid(t, rng)
msgCidBytes := msgCid.Bytes()
tsKeyCid := randomCid(t, rng)

insertTipsetMessage(t, s, tipsetMessage{
tipsetKeyCid: tsKeyCid.Bytes(),
height: uint64(1),
reverted: false,
messageCid: msgCidBytes,
messageIndex: 1,
})

mi, err := s.GetMsgInfo(ctx, msgCid)
require.NoError(t, err)
require.Equal(t, msgCid, mi.Message)
require.Equal(t, tsKeyCid, mi.TipSet)
require.Equal(t, abi.ChainEpoch(1), mi.Epoch)
})

mi, err := s.GetMsgInfo(ctx, msgCid)
require.NoError(t, err)
require.Equal(t, msgCid, mi.Message)
require.Equal(t, tsKeyCid, mi.TipSet)
require.Equal(t, abi.ChainEpoch(1), mi.Epoch)
t.Run("message appears after head indexed", func(t *testing.T) {
msgCid := randomCid(t, rng)
tsKeyCid := randomCid(t, rng)

// Create a context with timeout
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

// Start a goroutine that will insert the message after a short delay
go func() {
time.Sleep(100 * time.Millisecond)
insertTipsetMessage(t, s, tipsetMessage{
tipsetKeyCid: tsKeyCid.Bytes(),
height: uint64(2),
reverted: false,
messageCid: msgCid.Bytes(),
messageIndex: 1,
})
}()

// Retry with backoff until message is found or context times out
var mi *MsgInfo
var err error
for {
mi, err = s.GetMsgInfo(ctx, msgCid)
if err == nil {
break
}
if ctx.Err() != nil {
t.Fatalf("timed out waiting for message to appear: %v", err)
}
time.Sleep(50 * time.Millisecond)
}
virajbhartiya marked this conversation as resolved.
Show resolved Hide resolved

require.NoError(t, err)
require.Equal(t, msgCid, mi.Message)
require.Equal(t, tsKeyCid, mi.TipSet)
require.Equal(t, abi.ChainEpoch(2), mi.Epoch)
})

t.Run("message not found", func(t *testing.T) {
nonExistentMsgCid := randomCid(t, rng)
mi, err := s.GetMsgInfo(ctx, nonExistentMsgCid)
require.Error(t, err)
require.True(t, errors.Is(err, ErrNotFound))
virajbhartiya marked this conversation as resolved.
Show resolved Hide resolved
require.Nil(t, mi)
})
}

func setupWithHeadIndexed(t *testing.T, headHeight abi.ChainEpoch, rng *pseudo.Rand) (*SqliteIndexer, *types.TipSet, *dummyChainStore) {
Expand Down
Loading