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: handle compute unit + lookup tables, and improve flaky tests #965

Merged
merged 2 commits into from
Dec 10, 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
8 changes: 3 additions & 5 deletions pkg/solana/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,12 @@ func TestClient_GetBlocks(t *testing.T) {

// Verify we can retrieve blocks
startSlot := uint64(1)
endSlot := uint64(6)
endSlot := uint64(10)
require.Eventually(t,
func() bool {
blocks, err := c.GetBlocks(ctx, startSlot, &endSlot)
if err != nil {
return false
}
return len(blocks) == 5
require.NoError(t, err) // don't mask error within false
return len(blocks) >= 2 // slots != blocks (expect multiple blocks for 10 slots)
},
requestTimeout, 500*time.Millisecond)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/solana/fees/computebudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ func set(tx *solana.Transaction, baseData instruction, appendToFront bool) error

// https://github.com/gagliardetto/solana-go/blob/618f56666078f8131a384ab27afd918d248c08b7/transaction.go#L293
tx.Message.Header.NumReadonlyUnsignedAccounts++

// lookup table addresses are indexed after the tx.Message.AccountKeys
// higher indices must be increased
// https://github.com/gagliardetto/solana-go/blob/da2193071f56059aa35010a239cece016c4e827f/transaction.go#L440
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If solana-go lib does the same, why do we need a wrapper here? Can not we use just the library instead? I'm thinking that we need to avoid having any other bug on our side in the future

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't do the exact same thing - this comment is mostly just pointing out that lookup table addresses are indexes that are larger than the normal message.accountkeys array

solana-go requires a user to predeclare all instructions and then compile things together
our utility allows modification of an compiled transaction to include compute budget instructions

for i, ix := range tx.Message.Instructions {
for j, v := range ix.Accounts {
if int(v) >= programIdx {
tx.Message.Instructions[i].Accounts[j]++
}
}
}
}

// get instruction data
Expand Down
38 changes: 38 additions & 0 deletions pkg/solana/fees/computebudget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,44 @@ func testSet[V instruction](t *testing.T, builder func(uint) V, setter func(*sol
assert.NoError(t, err)
assert.Equal(t, data, []byte(tx.Message.Instructions[computeIndex].Data))
})

t.Run("with_lookuptables", func(t *testing.T) {
t.Parallel()
receiver, err := solana.NewRandomPrivateKey()
require.NoError(t, err)

// build base tx (no fee)
tx, err := solana.NewTransaction([]solana.Instruction{
system.NewTransferInstruction(
0,
key.PublicKey(),
receiver.PublicKey(),
).Build(),
},
solana.Hash{},
solana.TransactionAddressTables(map[solana.PublicKey]solana.PublicKeySlice{
solana.PublicKey{}: solana.PublicKeySlice{receiver.PublicKey()},
}),
)
require.NoError(t, err)

// check current account indices
assert.Equal(t, 1, len(tx.Message.Instructions))
assert.Equal(t, []uint16{0, 2}, tx.Message.Instructions[0].Accounts)

// add fee
require.NoError(t, setter(tx, builder(0)))

// evaluate
assert.Equal(t, 2, len(tx.Message.Instructions))
computeUnitIndex := getIndex(len(tx.Message.Instructions))
transferIndex := 0
if computeUnitIndex == transferIndex {
transferIndex = 1
}
assert.Equal(t, uint16(2), tx.Message.Instructions[computeUnitIndex].ProgramIDIndex)
assert.Equal(t, []uint16{0, 3}, tx.Message.Instructions[transferIndex].Accounts)
})
}

func TestParse(t *testing.T) {
Expand Down
Loading