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: transactions populated from RPC requests retain original accounts order #27

Merged
merged 1 commit into from
Mar 22, 2023
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
16 changes: 15 additions & 1 deletion src/Solana.Unity.Rpc/Builders/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public class MessageBuilder
/// </summary>
private readonly AccountKeysList _accountKeysList;


/// <summary>
/// The list of <see cref="PublicKey"/>s present in the transaction.
/// Memorized only if the transaction was deserialized and used to serialize keeping the same order.
/// </summary>
public IList<PublicKey> AccountKeys = null;

/// <summary>
/// The list of instructions contained within this transaction.
/// </summary>
Expand Down Expand Up @@ -181,7 +188,7 @@ private List<AccountMeta> GetAccountKeys()
{
List<AccountMeta> newList = new();
var keysList = _accountKeysList.AccountList;
int feePayerIndex = Array.FindIndex<AccountMeta>( _accountKeysList.AccountList.ToArray(),
int feePayerIndex = Array.FindIndex( _accountKeysList.AccountList.ToArray(),
x => x.PublicKey == FeePayer.Key);

if (feePayerIndex == -1)
Expand All @@ -196,6 +203,13 @@ private List<AccountMeta> GetAccountKeys()

newList.AddRange(keysList);

if (AccountKeys != null &&
newList.Select(m => new PublicKey(m.PublicKey)).All(AccountKeys.Contains))
{
// Using the accounts keys order of the deserialized transaction.
newList = newList.OrderBy(m => AccountKeys.IndexOf(new PublicKey(m.PublicKey))).ToList();
}

return newList;
}

Expand Down
13 changes: 10 additions & 3 deletions src/Solana.Unity.Rpc/Models/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public class Transaction
/// The list of <see cref="TransactionInstruction"/>s present in the transaction.
/// </summary>
public List<TransactionInstruction> Instructions { get; set; }


/// <summary>
/// The list of <see cref="PublicKey"/>s present in the transaction.
/// Memorized when deserializing a transaction. Avoid to change the keys order when deserializing.
/// </summary>
private IList<PublicKey> _accountKeys;

/// <summary>
/// The recent block hash for the transaction.
Expand Down Expand Up @@ -83,7 +90,7 @@ public class Transaction
/// </summary>
public byte[] CompileMessage()
{
MessageBuilder messageBuilder = new() { FeePayer = FeePayer };
MessageBuilder messageBuilder = new() { FeePayer = FeePayer, AccountKeys = _accountKeys };

if (RecentBlockHash != null) messageBuilder.RecentBlockHash = RecentBlockHash;
if (NonceInformation != null) messageBuilder.NonceInformation = NonceInformation;
Expand Down Expand Up @@ -295,7 +302,8 @@ public static Transaction Populate(Message message, IList<byte[]> signatures = n
{
RecentBlockHash = message.RecentBlockhash,
Signatures = new List<SignaturePubKeyPair>(),
Instructions = new List<TransactionInstruction>()
Instructions = new List<TransactionInstruction>(),
_accountKeys = message.AccountKeys
};

if (message.Header.RequiredSignatures > 0)
Expand Down Expand Up @@ -373,7 +381,6 @@ public static Transaction Deserialize(ReadOnlySpan<byte> data)
TransactionBuilder.SignatureLength);
signatures.Add(signature.ToArray());
}

return Populate(
Message.Deserialize(data[
(encodedLength + (signaturesLength * TransactionBuilder.SignatureLength))..]),
Expand Down
60 changes: 59 additions & 1 deletion test/Solana.Unity.Rpc.Test/TransactionBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Solana.Unity.Rpc.Test
[TestClass]
public class TransactionBuilderTest
{
#region consts and fixture

private const string MnemonicWords =
"route clerk disease box emerge airport loud waste attitude film army tray" +
" forward deal onion eight catalog surface unit card window walnut wealth medal";
Expand Down Expand Up @@ -50,7 +52,7 @@ public class TransactionBuilderTest

private const string Nonce = "2S1kjspXLPs6jpNVXQfNMqZzzSrKLbGdr9Fxap5h1DLN";

private static byte[] CompiledMessageBytes =
private static readonly byte[] CompiledMessageBytes =
{
1, 0, 2, 5, 71, 105, 171, 151, 32, 75, 168, 63, 176, 202, 238, 23, 247, 134, 143, 30, 7, 78, 82, 21,
129, 160, 216, 157, 148, 55, 157, 170, 101, 183, 23, 178, 132, 220, 206, 171, 228, 52, 112, 149, 218,
Expand All @@ -62,7 +64,56 @@ public class TransactionBuilderTest
146, 241, 176, 13, 84, 249, 55, 39, 9, 212, 80, 57, 8, 193, 89, 211, 49, 162, 144, 45, 140, 117, 21, 46,
83, 2, 3, 3, 2, 4, 0, 4, 4, 0, 0, 0, 3, 2, 0, 1, 12, 2, 0, 0, 0, 0, 202, 154, 59, 0, 0, 0, 0
};


private static readonly byte[] PartialMintTransaction =
{
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 198, 122,
80, 175, 126, 116, 30, 75, 168, 187, 152, 16, 64, 179, 59, 224, 144, 151, 36, 13, 14, 133, 233, 5, 42,
138, 93, 244, 114, 229, 38, 153, 36, 32, 215, 4, 156, 111, 156, 79, 10, 195, 169, 206, 14, 224, 61, 161,
203, 221, 84, 147, 156, 111, 105, 39, 221, 254, 232, 16, 80, 244, 8, 24, 64, 247, 58, 100, 155, 188,
246, 211, 245, 31, 223, 197, 38, 83, 76, 134, 88, 154, 28, 231, 252, 137, 93, 161, 123, 28, 58, 97, 194,
60, 3, 229, 21, 171, 34, 4, 12, 23, 250, 52, 113, 38, 144, 219, 72, 173, 77, 83, 21, 135, 13, 231, 246,
33, 225, 50, 23, 124, 93, 71, 73, 98, 9, 3, 0, 9, 15, 99, 83, 83, 192, 154, 229, 156, 233, 167, 140, 68,
210, 223, 30, 190, 172, 183, 186, 71, 55, 181, 160, 248, 28, 102, 222, 141, 177, 113, 95, 226, 177, 94,
105, 164, 46, 221, 109, 208, 199, 6, 167, 45, 242, 202, 40, 66, 144, 122, 22, 144, 46, 136, 209, 118,
203, 170, 28, 65, 50, 187, 152, 208, 6, 114, 150, 224, 19, 224, 91, 98, 71, 211, 125, 184, 39, 237, 187,
89, 211, 131, 139, 236, 160, 38, 19, 23, 191, 138, 216, 163, 222, 206, 226, 125, 249, 62, 35, 165, 232,
110, 189, 68, 127, 226, 59, 11, 229, 71, 195, 119, 235, 119, 252, 109, 55, 161, 231, 196, 223, 130, 212,
224, 123, 239, 202, 59, 120, 67, 81, 88, 75, 45, 13, 25, 18, 206, 13, 89, 13, 50, 66, 102, 5, 91, 35,
89, 213, 83, 54, 240, 162, 243, 106, 16, 158, 193, 210, 136, 105, 184, 202, 172, 97, 221, 73, 177, 194,
188, 120, 1, 42, 26, 91, 35, 113, 67, 35, 104, 207, 128, 224, 58, 78, 219, 32, 77, 222, 179, 250, 163,
212, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
101, 224, 60, 225, 5, 248, 160, 138, 36, 138, 124, 82, 53, 94, 245, 92, 14, 203, 220, 204, 20, 223, 11,
86, 140, 188, 183, 165, 156, 193, 254, 6, 167, 213, 23, 25, 44, 92, 81, 33, 140, 201, 76, 61, 74, 241,
127, 88, 218, 238, 8, 155, 161, 253, 68, 227, 219, 217, 138, 0, 0, 0, 0, 6, 221, 246, 225, 215, 101,
161, 147, 217, 203, 225, 70, 206, 235, 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133,
126, 255, 0, 169, 11, 112, 101, 177, 227, 209, 124, 69, 56, 157, 82, 127, 107, 4, 195, 205, 88, 184,
108, 115, 26, 160, 253, 181, 73, 182, 209, 188, 3, 248, 41, 70, 78, 105, 0, 186, 223, 37, 238, 123, 248,
244, 63, 132, 69, 96, 196, 53, 31, 39, 4, 139, 46, 43, 55, 219, 161, 131, 129, 158, 238, 134, 110, 241,
140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142, 13, 131, 11, 90, 19, 153, 218, 255, 16,
132, 4, 142, 123, 216, 219, 233, 248, 89, 240, 43, 132, 95, 16, 240, 210, 26, 231, 103, 230, 214, 56,
146, 38, 165, 229, 206, 185, 132, 108, 107, 5, 133, 203, 109, 140, 121, 238, 114, 140, 31, 245, 64, 164,
50, 8, 172, 56, 194, 185, 38, 126, 211, 210, 189, 76, 5, 240, 248, 249, 25, 92, 67, 158, 215, 74, 23,
149, 137, 86, 54, 134, 235, 92, 235, 31, 36, 159, 123, 208, 120, 64, 10, 68, 155, 82, 180, 203, 125,
162, 78, 40, 129, 114, 14, 70, 28, 144, 71, 207, 219, 243, 142, 123, 179, 7, 6, 2, 0, 1, 52, 0, 0, 0, 0,
96, 77, 22, 0, 0, 0, 0, 0, 82, 0, 0, 0, 0, 0, 0, 0, 6, 221, 246, 225, 215, 101, 161, 147, 217, 203, 225,
70, 206, 235, 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133, 126, 255, 0, 169, 9, 2,
1, 8, 67, 0, 0, 114, 150, 224, 19, 224, 91, 98, 71, 211, 125, 184, 39, 237, 187, 89, 211, 131, 139, 236,
160, 38, 19, 23, 191, 138, 216, 163, 222, 206, 226, 125, 249, 1, 114, 150, 224, 19, 224, 91, 98, 71,
211, 125, 184, 39, 237, 187, 89, 211, 131, 139, 236, 160, 38, 19, 23, 191, 138, 216, 163, 222, 206, 226,
125, 249, 12, 7, 0, 4, 0, 1, 6, 9, 8, 0, 9, 3, 1, 4, 2, 9, 7, 1, 0, 0, 0, 0, 0, 0, 0, 10, 7, 5, 1, 2, 0,
2, 6, 8, 133, 1, 33, 11, 0, 0, 0, 84, 114, 97, 115, 104, 32, 73, 116, 101, 109, 115, 5, 0, 0, 0, 84, 82,
65, 83, 72, 59, 0, 0, 0, 104, 116, 116, 112, 115, 58, 47, 47, 109, 101, 116, 97, 46, 103, 97, 114, 98,
108, 101, 115, 46, 102, 117, 110, 47, 105, 116, 101, 109, 47, 51, 110, 67, 57, 112, 101, 50, 89, 115,
84, 75, 80, 106, 72, 81, 77, 51, 106, 115, 69, 65, 67, 118, 116, 89, 87, 55, 86, 99, 44, 1, 1, 1, 0, 0,
0, 114, 150, 224, 19, 224, 91, 98, 71, 211, 125, 184, 39, 237, 187, 89, 211, 131, 139, 236, 160, 38, 19,
23, 191, 138, 216, 163, 222, 206, 226, 125, 249, 1, 100, 0, 0, 1, 0, 10, 9, 3, 1, 2, 2, 0, 5, 9, 6, 8,
10, 17, 1, 0, 0, 0, 0, 0, 0, 0, 0, 10, 8, 5, 2, 0, 2, 11, 14, 7, 13, 1, 25};

#endregion

[TestMethod]
public void TestTransactionBuilderBuild()
{
Expand All @@ -79,6 +130,13 @@ public void TestTransactionBuilderBuild()
Assert.AreEqual(ExpectedTransactionHashWithTransferAndMemo, Convert.ToBase64String(tx));
}

[TestMethod]
public void TestTransactionDeserializeSerialize()
{
var transaction = Transaction.Deserialize(PartialMintTransaction);
CollectionAssert.AreEqual(transaction.Serialize(), PartialMintTransaction);
}

[TestMethod]
[ExpectedException(typeof(Exception))]
public void TestTransactionBuilderBuildNullBlockhashException()
Expand Down