Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.solana.programs

import com.funkatronics.kborsh.BorshEncoder
import com.solana.publickey.SolanaPublicKey
import com.solana.transaction.TransactionInstruction
import kotlin.jvm.JvmStatic

object ComputeBudgetProgram : Program {
@JvmStatic
val PROGRAM_ID = SolanaPublicKey.from("ComputeBudget111111111111111111111111111111")

private const val PROGRAM_INDEX_REQUEST_HEAP_FRAME = 1.toByte()
private const val PROGRAM_INDEX_SET_COMPUTE_UNIT_LIMIT = 2.toByte()
private const val PROGRAM_INDEX_SET_COMPUTE_UNIT_PRICE = 3.toByte()
private const val PROGRAM_INDEX_SET_LOADED_ACCOUNTS_DATA_SIZE_LIMIT = 4.toByte()

/**
* Request a specific transaction-wide program heap region size in bytes. The value
* requested must be a multiple of 1024. This new heap region size applies to each
* program executed in the transaction, including all calls to CPIs.
*
* @param bytes the heap region size, in bytes
*/
@JvmStatic
fun requestHeapFrame(
bytes: UInt
): TransactionInstruction =
TransactionInstruction(PROGRAM_ID,
listOf(),
BorshEncoder().apply {
encodeByte(PROGRAM_INDEX_REQUEST_HEAP_FRAME)
encodeInt(bytes.toInt())
}.borshEncodedBytes
)

/**
* Set a specific compute unit limit that the transaction is allowed to consume.
*
* @param units the maximum compute units the that the transaction is allowed to consume
*/
@JvmStatic
fun setComputeUnitLimit(
units: UInt
): TransactionInstruction =
TransactionInstruction(PROGRAM_ID,
listOf(),
BorshEncoder().apply {
encodeByte(PROGRAM_INDEX_SET_COMPUTE_UNIT_LIMIT)
encodeInt(units.toInt())
}.borshEncodedBytes
)

/**
* Set a compute unit price in “micro-lamports” to pay a higher transaction fee for higher
* transaction prioritization.
*
* @param uLamports the micro-lamport unit price for the transaction
*/
@JvmStatic
fun setComputeUnitPrice(
uLamports: ULong
): TransactionInstruction =
TransactionInstruction(PROGRAM_ID,
listOf(),
BorshEncoder().apply {
encodeByte(PROGRAM_INDEX_SET_COMPUTE_UNIT_PRICE)
encodeLong(uLamports.toLong())
}.borshEncodedBytes
)

/**
* Set a specific transaction-wide account data size limit, in bytes, is allowed to load.
*
* @param bytes the account data size limit, in bytes
*/
@JvmStatic
fun setLoadedAccountsDataSizeLimit(
bytes: UInt
): TransactionInstruction =
TransactionInstruction(PROGRAM_ID,
listOf(),
BorshEncoder().apply {
encodeByte(PROGRAM_INDEX_SET_LOADED_ACCOUNTS_DATA_SIZE_LIMIT)
encodeLong(bytes.toLong())
}.borshEncodedBytes
)

override val programId = PROGRAM_ID
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.solana.programs

import com.solana.config.TestConfig
import com.solana.networking.KtorNetworkDriver
import com.solana.publickey.SolanaPublicKey
import com.solana.rpc.Commitment
import com.solana.rpc.SolanaRpcClient
import com.solana.rpc.TransactionOptions
import com.solana.transaction.Message
import com.solana.transaction.Transaction
import diglol.crypto.Ed25519
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class ComputeBudgetProgramTests {

@Test
fun `set Compute Limit and Price builds valid transaction`() = runTest {
// given
val keyPair = Ed25519.generateKeyPair()
val pubkey = SolanaPublicKey(keyPair.publicKey)
val rpc = SolanaRpcClient(TestConfig.RPC_URL, KtorNetworkDriver())
val message = "hello solana!"

// when
val airdropResponse = rpc.requestAirdrop(pubkey, 0.1f)
val blockhashResponse = rpc.getLatestBlockhash()

val transaction = Message.Builder()
.setRecentBlockhash(blockhashResponse.result!!.blockhash)
.addInstruction(MemoProgram.publishMemo(pubkey, message))
.addInstruction(ComputeBudgetProgram.setComputeUnitLimit(25000u))
.addInstruction(ComputeBudgetProgram.setComputeUnitPrice(10000u))
.build().run {
val sig = Ed25519.sign(keyPair, serialize())
Transaction(listOf(sig), this)
}

val response = withContext(Dispatchers.Default.limitedParallelism(1)) {
rpc.sendAndConfirmTransaction(
transaction, TransactionOptions(
commitment = Commitment.CONFIRMED,
skipPreflight = true
)
)
}

// then
assertNull(airdropResponse.error)
assertNotNull(airdropResponse.result)
assertNull(response.error)
assertNotNull(response.result)
}

@Test
fun `request heap frame builds valid transaction`() = runTest {
// given
val keyPair = Ed25519.generateKeyPair()
val pubkey = SolanaPublicKey(keyPair.publicKey)
val rpc = SolanaRpcClient(TestConfig.RPC_URL, KtorNetworkDriver())
val message = "hello solana!"

// when
val airdropResponse = rpc.requestAirdrop(pubkey, 0.1f)
val blockhashResponse = rpc.getLatestBlockhash()

val transaction = Message.Builder()
.setRecentBlockhash(blockhashResponse.result!!.blockhash)
.addInstruction(MemoProgram.publishMemo(pubkey, message))
.addInstruction(ComputeBudgetProgram.requestHeapFrame(40u*1024u))
.build().run {
val sig = Ed25519.sign(keyPair, serialize())
Transaction(listOf(sig), this)
}

val response = withContext(Dispatchers.Default.limitedParallelism(1)) {
rpc.sendAndConfirmTransaction(
transaction, TransactionOptions(
commitment = Commitment.CONFIRMED,
skipPreflight = true
)
)
}

// then
assertNull(airdropResponse.error)
assertNotNull(airdropResponse.result)
assertNull(response.error)
assertNotNull(response.result)
}

@Test
fun `set Loaded Accounts data size limit builds valid transaction`() = runTest {
// given
val keyPair = Ed25519.generateKeyPair()
val pubkey = SolanaPublicKey(keyPair.publicKey)
val rpc = SolanaRpcClient(TestConfig.RPC_URL, KtorNetworkDriver())
val message = "hello solana!"

// when
val airdropResponse = rpc.requestAirdrop(pubkey, 0.1f)
val blockhashResponse = rpc.getLatestBlockhash()

val transaction = Message.Builder()
.setRecentBlockhash(blockhashResponse.result!!.blockhash)
.addInstruction(MemoProgram.publishMemo(pubkey, message))
.addInstruction(ComputeBudgetProgram.setLoadedAccountsDataSizeLimit(300000u))
.build().run {
val sig = Ed25519.sign(keyPair, serialize())
Transaction(listOf(sig), this)
}

val response = withContext(Dispatchers.Default.limitedParallelism(1)) {
rpc.sendAndConfirmTransaction(
transaction, TransactionOptions(
commitment = Commitment.CONFIRMED,
skipPreflight = true
)
)
}

// then
assertNull(airdropResponse.error)
assertNotNull(airdropResponse.result)
assertNull(response.error)
assertNotNull(response.result)
}
}