Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wiyarmir committed Nov 8, 2024
1 parent 8a776c4 commit 09eb2d6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package net.avianlabs.solana.domain.core

import net.avianlabs.solana.tweetnacl.ed25519.PublicKey

public class Message internal constructor(
@ConsistentCopyVisibility
public data class Message private constructor(
public val feePayer: PublicKey?,
public val recentBlockHash: String?,
public val accountKeys: List<AccountMeta>,
public val instructions: List<TransactionInstruction>,
) {

override fun toString(): String =
"Message(feePayer=$feePayer, recentBlockHash=$recentBlockHash, accountKeys=$accountKeys, instructions=$instructions)"

public fun newBuilder(): Builder = Builder(
feePayer = feePayer,
recentBlockHash = recentBlockHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package net.avianlabs.solana.domain.core
import net.avianlabs.solana.tweetnacl.TweetNaCl
import net.avianlabs.solana.tweetnacl.ed25519.PublicKey
import net.avianlabs.solana.tweetnacl.vendor.decodeBase58
import net.avianlabs.solana.vendor.ShortVecEncoding
import net.avianlabs.solana.vendor.ShortVecLength
import net.avianlabs.solana.vendor.ShortvecEncoding
import okio.Buffer

private const val RECENT_BLOCK_HASH_LENGTH = 32
Expand Down Expand Up @@ -72,7 +72,7 @@ public fun Message.serialize(): ByteArray {
require(instructions.isNotEmpty()) { "No instructions provided" }
val keysList = compileAccountKeys(feePayer)
val accountKeysSize = keysList.size
val accountAddressesLength = ShortvecEncoding.encodeLength(accountKeysSize)
val accountAddressesLength = ShortVecEncoding.encodeLength(accountKeysSize)
val compiledInstructions = instructions.map { instruction ->
val keysSize = instruction.keys.size
val keyIndices = ByteArray(keysSize)
Expand All @@ -81,14 +81,14 @@ public fun Message.serialize(): ByteArray {
}
CompiledInstruction(
programIdIndex = keysList.findAccountIndex(instruction.programId).toByte(),
keyIndicesLength = ShortvecEncoding.encodeLength(keysSize),
keyIndicesLength = ShortVecEncoding.encodeLength(keysSize),
keyIndices = keyIndices,
dataLength = ShortvecEncoding.encodeLength(instruction.data.count()),
dataLength = ShortVecEncoding.encodeLength(instruction.data.count()),
data = instruction.data,
)
}
val accountsKeyBufferSize = accountKeysSize * TweetNaCl.Signature.PUBLIC_KEY_BYTES
val instructionsLength = ShortvecEncoding.encodeLength(compiledInstructions.size)
val instructionsLength = ShortVecEncoding.encodeLength(compiledInstructions.size)
val compiledInstructionsBytes = compiledInstructions.sumOf { it.bytes }
val bufferSize =
(Header.HEADER_LENGTH + RECENT_BLOCK_HASH_LENGTH + accountAddressesLength.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import net.avianlabs.solana.tweetnacl.TweetNaCl
import net.avianlabs.solana.tweetnacl.vendor.decodeBase58
import net.avianlabs.solana.tweetnacl.vendor.encodeToBase58String
import net.avianlabs.solana.vendor.ShortvecEncoding
import net.avianlabs.solana.vendor.ShortVecEncoding
import okio.Buffer

private val logger = KotlinLogging.logger {}

public class SignedTransaction(
@ConsistentCopyVisibility
public data class SignedTransaction internal constructor(
public val originalMessage: Message,
public val signedMessage: ByteArray,
public val signatures: List<String>,
Expand All @@ -28,16 +29,38 @@ public class SignedTransaction(
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
if (!super.equals(other)) return false

other as SignedTransaction

if (originalMessage != other.originalMessage) return false
if (!signedMessage.contentEquals(other.signedMessage)) return false
if (signatures != other.signatures) return false

return true
}

override fun hashCode(): Int {
var result = super.hashCode()
result = 31 * result + originalMessage.hashCode()
result = 31 * result + signedMessage.contentHashCode()
result = 31 * result + signatures.hashCode()
return result
}

override fun toString(): String =
"SignedTransaction(message=${originalMessage}, signatures=$signatures)"

public fun serialize(): ByteArray {
val signaturesSize = signatures.size
val signaturesLength = ShortvecEncoding.encodeLength(signaturesSize)
val signaturesLength = ShortVecEncoding.encodeLength(signaturesSize)
val bufferSize =
signaturesLength.size +
signaturesSize * TweetNaCl.Signature.SIGNATURE_BYTES +
signedMessage.size
signaturesSize * TweetNaCl.Signature.SIGNATURE_BYTES +
signedMessage.size
val out = Buffer()
out.write(signaturesLength)
for (signature in signatures) {
Expand All @@ -52,10 +75,10 @@ public class SignedTransaction(
val message = signedMessage
val messageLength = message.size
val signaturesSize = signatures.size
val signaturesLength = ShortvecEncoding.encodeLength(signaturesSize)
val signaturesLength = ShortVecEncoding.encodeLength(signaturesSize)
val signaturesSizeBytes = signaturesLength.size
val signatureSize = TweetNaCl.Signature.SIGNATURE_BYTES
val signatureSizeBytes = ShortvecEncoding.encodeLength(signatureSize)
val signatureSizeBytes = ShortVecEncoding.encodeLength(signatureSize)
val signatureSizeBytesLength = signatureSizeBytes.size
val expectedSize =
messageLength + signaturesSizeBytes + signaturesSize * (signatureSize + signatureSizeBytesLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public open class Transaction internal constructor(
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false

other as Transaction

return message == other.message
}

override fun hashCode(): Int {
return message.hashCode()
}

override fun toString(): String = "Transaction(message=$message)"

public class Builder internal constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlin.experimental.and

internal typealias ShortVecLength = ByteArray

internal object ShortvecEncoding {
internal object ShortVecEncoding {
internal fun encodeLength(len: Int): ShortVecLength {
val buffer = Buffer()
var length = len
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ class ShortvecEncodingTest {
fun encodeLength() {
assertContentEquals(
byteArrayOf(0) /* [0] */,
ShortvecEncoding.encodeLength(0)
ShortVecEncoding.encodeLength(0)
)
assertContentEquals(
byteArrayOf(1) /* [1] */,
ShortvecEncoding.encodeLength(1)
ShortVecEncoding.encodeLength(1)
)
assertContentEquals(
byteArrayOf(5) /* [5] */,
ShortvecEncoding.encodeLength(5)
ShortVecEncoding.encodeLength(5)
)
assertContentEquals(
byteArrayOf(127)/* [0x7f] */,
ShortvecEncoding.encodeLength(127) // 0x7f
ShortVecEncoding.encodeLength(127) // 0x7f
)
assertContentEquals(
byteArrayOf(-128, 1) /* [0x80, 0x01] */,
ShortvecEncoding.encodeLength(128) // 0x80
ShortVecEncoding.encodeLength(128) // 0x80
)
assertContentEquals(
byteArrayOf(-1, 1) /* [0xff, 0x01] */,
ShortvecEncoding.encodeLength(255) // 0xff
ShortVecEncoding.encodeLength(255) // 0xff
)
assertContentEquals(
byteArrayOf(-128, 2)/* [0x80, 0x02] */,
ShortvecEncoding.encodeLength(256) // 0x100
ShortVecEncoding.encodeLength(256) // 0x100
)
assertContentEquals(
byteArrayOf(-1, -1, 1)/* [0xff, 0xff, 0x01] */,
ShortvecEncoding.encodeLength(32767) // 0x7fff
ShortVecEncoding.encodeLength(32767) // 0x7fff
)
assertContentEquals(
byteArrayOf(-128, -128, -128, 1)/* [0x80, 0x80, 0x80, 0x01] */,
ShortvecEncoding.encodeLength(2097152) // 0x200000
ShortVecEncoding.encodeLength(2097152) // 0x200000
)
}
}

0 comments on commit 09eb2d6

Please sign in to comment.