Skip to content

Commit

Permalink
Encoded log collector happy path test
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaidashenko committed Jan 14, 2025
1 parent 75b9f6a commit e9527c4
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 756 deletions.
4 changes: 4 additions & 0 deletions pkg/solana/logpoller/job_get_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logpoller

import (
"context"
"errors"
"fmt"

"github.com/gagliardetto/solana-go"
Expand Down Expand Up @@ -75,6 +76,9 @@ func (j *getBlockJob) Run(ctx context.Context) error {
events := make([]ProgramEvent, 0, len(block.Transactions))
for idx, txWithMeta := range block.Transactions {
detail.trxIdx = idx
if txWithMeta.Transaction == nil {
return fmt.Errorf("failed to parse transaction %d in slot %d: %w", idx, j.slotNumber, errors.New("missing transaction field"))
}
tx, err := txWithMeta.GetTransaction()
if err != nil {
return fmt.Errorf("failed to parse transaction %d in slot %d: %w", idx, j.slotNumber, err)
Expand Down
11 changes: 10 additions & 1 deletion pkg/solana/logpoller/job_get_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ func TestGetBlockJob(t *testing.T) {
err := job.Run(tests.Context(t))
require.ErrorIs(t, err, expectedError)
})
t.Run("Error if transaction field is not present", func(t *testing.T) {
client := mocks.NewRPCClient(t)
lggr := logger.Sugared(logger.Test(t))
block := rpc.GetBlockResult{Transactions: []rpc.TransactionWithMeta{{Transaction: nil}}}
client.EXPECT().GetBlockWithOpts(mock.Anything, slotNumber, mock.Anything).Return(&block, nil).Once()
job := newGetBlockJob(client, make(chan Block), lggr, slotNumber)
err := job.Run(tests.Context(t))
require.ErrorContains(t, err, "failed to parse transaction 0 in slot 42: missing transaction field")
})
t.Run("Error if fails to get transaction", func(t *testing.T) {
client := mocks.NewRPCClient(t)
lggr := logger.Sugared(logger.Test(t))
Expand Down Expand Up @@ -100,7 +109,7 @@ func TestGetBlockJob(t *testing.T) {
txWithMeta1 := rpc.TransactionWithMeta{Transaction: txSigToDataBytes(tx1Signature), Meta: &rpc.TransactionMeta{LogMessages: []string{"log1", "log2"}}}
txWithMeta2 := rpc.TransactionWithMeta{Transaction: txSigToDataBytes(tx2Signature), Meta: &rpc.TransactionMeta{LogMessages: []string{"log3"}}}
// tx3 must be ignored due to error
txWithMeta3 := rpc.TransactionWithMeta{Transaction: txSigToDataBytes(tx2Signature), Meta: &rpc.TransactionMeta{LogMessages: []string{"log4"}, Err: fmt.Errorf("some error")}}
txWithMeta3 := rpc.TransactionWithMeta{Transaction: txSigToDataBytes(solana.Signature{10, 11}), Meta: &rpc.TransactionMeta{LogMessages: []string{"log4"}, Err: fmt.Errorf("some error")}}
height := uint64(41)
block := rpc.GetBlockResult{BlockHeight: &height, Blockhash: solana.Hash{1, 2, 3}, Transactions: []rpc.TransactionWithMeta{txWithMeta1, txWithMeta2, txWithMeta3}}
client.EXPECT().GetBlockWithOpts(mock.Anything, slotNumber, mock.Anything).Return(&block, nil).Once()
Expand Down
2 changes: 0 additions & 2 deletions pkg/solana/logpoller/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ type ProgramEventProcessor interface {
}

type RPCClient interface {
GetLatestBlockhash(ctx context.Context, commitment rpc.CommitmentType) (out *rpc.GetLatestBlockhashResult, err error)
GetBlocks(ctx context.Context, startSlot uint64, endSlot *uint64, commitment rpc.CommitmentType) (out rpc.BlocksResult, err error)
GetBlockWithOpts(context.Context, uint64, *rpc.GetBlockOpts) (*rpc.GetBlockResult, error)
GetSignaturesForAddressWithOpts(context.Context, solana.PublicKey, *rpc.GetSignaturesForAddressOpts) ([]*rpc.TransactionSignature, error)
GetSlot(ctx context.Context, commitment rpc.CommitmentType) (uint64, error)
Expand Down
Loading

0 comments on commit e9527c4

Please sign in to comment.