solana-message 4.1.0

Solana transaction message types.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#[cfg(feature = "frozen-abi")]
use solana_frozen_abi_macro::{frozen_abi, AbiEnumVisitor, AbiExample};
use {
    crate::{
        compiled_instruction::CompiledInstruction, legacy::Message as LegacyMessage,
        v0::MessageAddressTableLookup, MessageHeader,
    },
    solana_address::Address,
    solana_hash::Hash,
    solana_sanitize::{Sanitize, SanitizeError},
    std::collections::HashSet,
};
#[cfg(feature = "wincode")]
use {
    core::mem::MaybeUninit,
    wincode::{
        config::Config,
        io::{Reader, Writer},
        ReadResult, SchemaRead, SchemaReadContext, SchemaWrite, WriteResult,
    },
};
#[cfg(feature = "serde")]
use {
    serde::{
        de::{self, Deserializer, SeqAccess, Unexpected, Visitor},
        ser::{SerializeTuple, Serializer},
    },
    serde_derive::{Deserialize, Serialize},
    std::fmt,
};

mod sanitized;
pub mod v0;
pub mod v1;

pub use sanitized::*;

/// Bit mask that indicates whether a serialized message is versioned.
pub const MESSAGE_VERSION_PREFIX: u8 = 0x80;

/// Either a legacy message, v0 or a v1 message.
///
/// # Serialization
///
/// If the first bit is set, the remaining 7 bits will be used to determine
/// which message version is serialized starting from version `0`. If the first
/// is bit is not set, all bytes are used to encode the legacy `Message`
/// format.
#[cfg_attr(
    feature = "frozen-abi",
    frozen_abi(digest = "6CoVPUxkUvDrAvAkfyVXwVDHCSf77aufm7DEZy5mBVeX"),
    derive(AbiEnumVisitor, AbiExample)
)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum VersionedMessage {
    Legacy(LegacyMessage),
    V0(v0::Message),
    V1(v1::Message),
}

impl VersionedMessage {
    pub fn sanitize(&self) -> Result<(), SanitizeError> {
        match self {
            Self::Legacy(message) => message.sanitize(),
            Self::V0(message) => message.sanitize(),
            Self::V1(message) => message.sanitize(),
        }
    }

    pub fn header(&self) -> &MessageHeader {
        match self {
            Self::Legacy(message) => &message.header,
            Self::V0(message) => &message.header,
            Self::V1(message) => &message.header,
        }
    }

    pub fn static_account_keys(&self) -> &[Address] {
        match self {
            Self::Legacy(message) => &message.account_keys,
            Self::V0(message) => &message.account_keys,
            Self::V1(message) => &message.account_keys,
        }
    }

    pub fn address_table_lookups(&self) -> Option<&[MessageAddressTableLookup]> {
        match self {
            Self::Legacy(_) => None,
            Self::V0(message) => Some(&message.address_table_lookups),
            Self::V1(_) => None,
        }
    }

    /// Returns true if the account at the specified index signed this
    /// message.
    pub fn is_signer(&self, index: usize) -> bool {
        index < usize::from(self.header().num_required_signatures)
    }

    /// Returns true if the account at the specified index is writable by the
    /// instructions in this message. Since dynamically loaded addresses can't
    /// have write locks demoted without loading addresses, this shouldn't be
    /// used in the runtime.
    pub fn is_maybe_writable(
        &self,
        index: usize,
        reserved_account_keys: Option<&HashSet<Address>>,
    ) -> bool {
        match self {
            Self::Legacy(message) => message.is_maybe_writable(index, reserved_account_keys),
            Self::V0(message) => message.is_maybe_writable(index, reserved_account_keys),
            Self::V1(message) => message.is_maybe_writable(index, reserved_account_keys),
        }
    }

    /// Returns true if the account at the specified index is an input to some
    /// program instruction in this message.
    fn is_instruction_account(&self, key_index: usize) -> bool {
        if let Ok(key_index) = u8::try_from(key_index) {
            self.instructions()
                .iter()
                .any(|ix| ix.accounts.contains(&key_index))
        } else {
            false
        }
    }

    pub fn is_invoked(&self, key_index: usize) -> bool {
        match self {
            Self::Legacy(message) => message.is_key_called_as_program(key_index),
            Self::V0(message) => message.is_key_called_as_program(key_index),
            Self::V1(message) => message.is_key_called_as_program(key_index),
        }
    }

    /// Returns true if the account at the specified index is not invoked as a
    /// program or, if invoked, is passed to a program.
    pub fn is_non_loader_key(&self, key_index: usize) -> bool {
        !self.is_invoked(key_index) || self.is_instruction_account(key_index)
    }

    pub fn recent_blockhash(&self) -> &Hash {
        match self {
            Self::Legacy(message) => &message.recent_blockhash,
            Self::V0(message) => &message.recent_blockhash,
            Self::V1(message) => &message.lifetime_specifier,
        }
    }

    pub fn set_recent_blockhash(&mut self, recent_blockhash: Hash) {
        match self {
            Self::Legacy(message) => message.recent_blockhash = recent_blockhash,
            Self::V0(message) => message.recent_blockhash = recent_blockhash,
            Self::V1(message) => message.lifetime_specifier = recent_blockhash,
        }
    }

    /// Program instructions that will be executed in sequence and committed in
    /// one atomic transaction if all succeed.
    #[inline(always)]
    pub fn instructions(&self) -> &[CompiledInstruction] {
        match self {
            Self::Legacy(message) => &message.instructions,
            Self::V0(message) => &message.instructions,
            Self::V1(message) => &message.instructions,
        }
    }

    #[cfg(feature = "wincode")]
    pub fn serialize(&self) -> Vec<u8> {
        wincode::serialize(self).unwrap()
    }

    #[cfg(all(feature = "wincode", feature = "blake3"))]
    /// Compute the blake3 hash of this transaction's message
    pub fn hash(&self) -> Hash {
        let message_bytes = self.serialize();
        Self::hash_raw_message(&message_bytes)
    }

    #[cfg(feature = "blake3")]
    /// Compute the blake3 hash of a raw transaction message
    pub fn hash_raw_message(message_bytes: &[u8]) -> Hash {
        use blake3::traits::digest::Digest;
        let mut hasher = blake3::Hasher::new();
        hasher.update(b"solana-tx-message-v1");
        hasher.update(message_bytes);
        let hash_bytes: [u8; solana_hash::HASH_BYTES] = hasher.finalize().into();
        hash_bytes.into()
    }
}

impl Default for VersionedMessage {
    fn default() -> Self {
        Self::Legacy(LegacyMessage::default())
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for VersionedMessage {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Legacy(message) => {
                let mut seq = serializer.serialize_tuple(1)?;
                seq.serialize_element(message)?;
                seq.end()
            }
            Self::V0(message) => {
                let mut seq = serializer.serialize_tuple(2)?;
                seq.serialize_element(&MESSAGE_VERSION_PREFIX)?;
                seq.serialize_element(message)?;
                seq.end()
            }
            Self::V1(message) => {
                // Note that this format does not match the wire format per SIMD-0385.

                let mut seq = serializer.serialize_tuple(2)?;
                seq.serialize_element(&crate::v1::V1_PREFIX)?;
                seq.serialize_element(message)?;
                seq.end()
            }
        }
    }
}

#[cfg(feature = "serde")]
enum MessagePrefix {
    Legacy(u8),
    Versioned(u8),
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for MessagePrefix {
    fn deserialize<D>(deserializer: D) -> Result<MessagePrefix, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct PrefixVisitor;

        impl Visitor<'_> for PrefixVisitor {
            type Value = MessagePrefix;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("message prefix byte")
            }

            // Serde's integer visitors bubble up to u64 so check the prefix
            // with this function instead of visit_u8. This approach is
            // necessary because serde_json directly calls visit_u64 for
            // unsigned integers.
            fn visit_u64<E: de::Error>(self, value: u64) -> Result<MessagePrefix, E> {
                if value > u8::MAX as u64 {
                    Err(de::Error::invalid_type(Unexpected::Unsigned(value), &self))?;
                }

                let byte = value as u8;
                if byte & MESSAGE_VERSION_PREFIX != 0 {
                    Ok(MessagePrefix::Versioned(byte & !MESSAGE_VERSION_PREFIX))
                } else {
                    Ok(MessagePrefix::Legacy(byte))
                }
            }
        }

        deserializer.deserialize_u8(PrefixVisitor)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for VersionedMessage {
    fn deserialize<D>(deserializer: D) -> Result<VersionedMessage, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct MessageVisitor;

        impl<'de> Visitor<'de> for MessageVisitor {
            type Value = VersionedMessage;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("message bytes")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<VersionedMessage, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let prefix: MessagePrefix = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;

                match prefix {
                    MessagePrefix::Legacy(num_required_signatures) => {
                        // The remaining fields of the legacy Message struct after the first byte.
                        #[derive(Serialize, Deserialize)]
                        struct RemainingLegacyMessage {
                            pub num_readonly_signed_accounts: u8,
                            pub num_readonly_unsigned_accounts: u8,
                            #[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
                            pub account_keys: Vec<Address>,
                            pub recent_blockhash: Hash,
                            #[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
                            pub instructions: Vec<CompiledInstruction>,
                        }

                        let message: RemainingLegacyMessage =
                            seq.next_element()?.ok_or_else(|| {
                                // will never happen since tuple length is always 2
                                de::Error::invalid_length(1, &self)
                            })?;

                        Ok(VersionedMessage::Legacy(LegacyMessage {
                            header: MessageHeader {
                                num_required_signatures,
                                num_readonly_signed_accounts: message.num_readonly_signed_accounts,
                                num_readonly_unsigned_accounts: message
                                    .num_readonly_unsigned_accounts,
                            },
                            account_keys: message.account_keys,
                            recent_blockhash: message.recent_blockhash,
                            instructions: message.instructions,
                        }))
                    }
                    MessagePrefix::Versioned(version) => {
                        match version {
                            0 => {
                                Ok(VersionedMessage::V0(seq.next_element()?.ok_or_else(
                                    || {
                                        // will never happen since tuple length is always 2
                                        de::Error::invalid_length(1, &self)
                                    },
                                )?))
                            }
                            1 => {
                                Ok(VersionedMessage::V1(seq.next_element()?.ok_or_else(
                                    || {
                                        // will never happen since tuple length is always 2
                                        de::Error::invalid_length(1, &self)
                                    },
                                )?))
                            }
                            127 => {
                                // 0xff is used as the first byte of the off-chain messages
                                // which corresponds to version 127 of the versioned messages.
                                // This explicit check is added to prevent the usage of version 127
                                // in the runtime as a valid transaction.
                                Err(de::Error::custom("off-chain messages are not accepted"))
                            }
                            _ => Err(de::Error::invalid_value(
                                de::Unexpected::Unsigned(version as u64),
                                &"a valid transaction message version",
                            )),
                        }
                    }
                }
            }
        }

        deserializer.deserialize_tuple(2, MessageVisitor)
    }
}

#[cfg(feature = "wincode")]
unsafe impl<C: Config> SchemaWrite<C> for VersionedMessage {
    type Src = Self;

    // V0 and V1 add +1 for message version prefix
    #[allow(clippy::arithmetic_side_effects)]
    #[inline(always)]
    fn size_of(src: &Self::Src) -> WriteResult<usize> {
        match src {
            VersionedMessage::Legacy(message) => {
                <LegacyMessage as SchemaWrite<C>>::size_of(message)
            }
            VersionedMessage::V0(message) => {
                Ok(1 + <v0::Message as SchemaWrite<C>>::size_of(message)?)
            }
            VersionedMessage::V1(message) => Ok(1 + message.size()),
        }
    }

    // V0 and V1 add +1 for message version prefix
    #[allow(clippy::arithmetic_side_effects)]
    #[inline(always)]
    fn write(mut writer: impl Writer, src: &Self::Src) -> WriteResult<()> {
        match src {
            VersionedMessage::Legacy(message) => {
                <LegacyMessage as SchemaWrite<C>>::write(writer, message)
            }
            VersionedMessage::V0(message) => {
                <u8 as SchemaWrite<C>>::write(&mut writer, &MESSAGE_VERSION_PREFIX)?;
                <v0::Message as SchemaWrite<C>>::write(writer, message)
            }
            VersionedMessage::V1(message) => {
                <u8 as SchemaWrite<C>>::write(writer.by_ref(), &crate::v1::V1_PREFIX)?;
                <v1::Message as SchemaWrite<C>>::write(writer, message)
            }
        }
    }
}

#[cfg(feature = "wincode")]
unsafe impl<'de, C: Config> SchemaReadContext<'de, C, u8> for VersionedMessage {
    type Dst = Self;

    fn read_with_context(
        discriminant: u8,
        reader: impl Reader<'de>,
        dst: &mut MaybeUninit<Self::Dst>,
    ) -> ReadResult<()> {
        // If the first bit is set, the remaining 7 bits will be used to determine
        // which message version is serialized starting from version `0`. If the first
        // is bit is not set, all bytes are used to encode the legacy `Message`
        // format.
        if discriminant & MESSAGE_VERSION_PREFIX != 0 {
            use wincode::error::invalid_tag_encoding;

            let version = discriminant & !MESSAGE_VERSION_PREFIX;
            return match version {
                0 => {
                    let msg = <v0::Message as SchemaRead<C>>::get(reader)?;
                    dst.write(VersionedMessage::V0(msg));
                    Ok(())
                }
                1 => {
                    let message = <v1::Message as SchemaRead<C>>::get(reader)?;
                    dst.write(VersionedMessage::V1(message));

                    Ok(())
                }
                _ => Err(invalid_tag_encoding(version as usize)),
            };
        };
        let legacy =
            <LegacyMessage as SchemaReadContext<C, _>>::get_with_context(discriminant, reader)?;
        dst.write(VersionedMessage::Legacy(legacy));

        Ok(())
    }
}
#[cfg(feature = "wincode")]
unsafe impl<'de, C: Config> SchemaRead<'de, C> for VersionedMessage {
    type Dst = Self;

    #[inline]
    fn read(mut reader: impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>) -> ReadResult<()> {
        let discriminant = reader.take_byte()?;
        <VersionedMessage as SchemaReadContext<C, _>>::read_with_context(discriminant, reader, dst)
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        crate::{v0::MessageAddressTableLookup, v1::V1_PREFIX},
        proptest::{
            collection::vec,
            option::of,
            prelude::{any, Just},
            prop_compose, proptest,
            strategy::Strategy,
        },
        solana_instruction::{AccountMeta, Instruction},
    };

    #[derive(Clone, Debug)]
    struct TestMessageData {
        required_signatures: u8,
        lifetime: [u8; 32],
        accounts: Vec<[u8; 32]>,
        priority_fee: Option<u64>,
        compute_unit_limit: Option<u32>,
        loaded_accounts_data_size_limit: Option<u32>,
        heap_size: Option<u32>,
        program_id_index: u8,
        instr_accounts: Vec<u8>,
        data: Vec<u8>,
    }

    #[test]
    fn test_legacy_message_serialization() {
        let program_id0 = Address::new_unique();
        let program_id1 = Address::new_unique();
        let id0 = Address::new_unique();
        let id1 = Address::new_unique();
        let id2 = Address::new_unique();
        let id3 = Address::new_unique();
        let instructions = vec![
            Instruction::new_with_bincode(program_id0, &0, vec![AccountMeta::new(id0, false)]),
            Instruction::new_with_bincode(program_id0, &0, vec![AccountMeta::new(id1, true)]),
            Instruction::new_with_bincode(
                program_id1,
                &0,
                vec![AccountMeta::new_readonly(id2, false)],
            ),
            Instruction::new_with_bincode(
                program_id1,
                &0,
                vec![AccountMeta::new_readonly(id3, true)],
            ),
        ];

        let mut message = LegacyMessage::new(&instructions, Some(&id1));
        message.recent_blockhash = Hash::new_unique();
        let wrapped_message = VersionedMessage::Legacy(message.clone());

        // bincode
        {
            let bytes = bincode::serialize(&message).unwrap();
            assert_eq!(bytes, bincode::serialize(&wrapped_message).unwrap());

            let message_from_bytes: LegacyMessage = bincode::deserialize(&bytes).unwrap();
            let wrapped_message_from_bytes: VersionedMessage =
                bincode::deserialize(&bytes).unwrap();

            assert_eq!(message, message_from_bytes);
            assert_eq!(wrapped_message, wrapped_message_from_bytes);
        }

        // serde_json
        {
            let string = serde_json::to_string(&message).unwrap();
            let message_from_string: LegacyMessage = serde_json::from_str(&string).unwrap();
            assert_eq!(message, message_from_string);
        }
    }

    #[test]
    fn test_versioned_message_serialization() {
        let message = VersionedMessage::V0(v0::Message {
            header: MessageHeader {
                num_required_signatures: 1,
                num_readonly_signed_accounts: 0,
                num_readonly_unsigned_accounts: 0,
            },
            recent_blockhash: Hash::new_unique(),
            account_keys: vec![Address::new_unique()],
            address_table_lookups: vec![
                MessageAddressTableLookup {
                    account_key: Address::new_unique(),
                    writable_indexes: vec![1],
                    readonly_indexes: vec![0],
                },
                MessageAddressTableLookup {
                    account_key: Address::new_unique(),
                    writable_indexes: vec![0],
                    readonly_indexes: vec![1],
                },
            ],
            instructions: vec![CompiledInstruction {
                program_id_index: 1,
                accounts: vec![0, 2, 3, 4],
                data: vec![],
            }],
        });

        let bytes = bincode::serialize(&message).unwrap();
        let message_from_bytes: VersionedMessage = bincode::deserialize(&bytes).unwrap();
        assert_eq!(message, message_from_bytes);

        let string = serde_json::to_string(&message).unwrap();
        let message_from_string: VersionedMessage = serde_json::from_str(&string).unwrap();
        assert_eq!(message, message_from_string);
    }

    prop_compose! {
        fn generate_message_data()
            (
                // Generate between 12 and 64 accounts since we need at least the
                // amount of `required_signatures`.
                accounts in vec(any::<[u8; 32]>(), 12..=64),
                lifetime in any::<[u8; 32]>(),
                priority_fee in of(any::<u64>()),
                compute_unit_limit in of(0..=1_400_000u32),
                loaded_accounts_data_size_limit in of(0..=20_480u32),
                heap_size in of((0..=32u32).prop_map(|n| n.saturating_mul(1024))),
                required_signatures in 1..=12u8,
            )
            (
                // The `program_id_index` cannot be 0 (payer).
                program_id_index in 1u8..accounts.len() as u8,
                // we need to have at least `required_signatures` accounts.
                instr_accounts in vec(
                    0u8..accounts.len() as u8,
                    (required_signatures as usize)..=accounts.len(),
                ),
                // Keep instruction data relatively small to avoid hitting the maximum
                // transaction size when combined with the accounts.
                data in vec(any::<u8>(), 0..=2048),
                accounts in Just(accounts),
                lifetime in Just(lifetime),
                priority_fee in Just(priority_fee),
                compute_unit_limit in Just(compute_unit_limit),
                loaded_accounts_data_size_limit in Just(loaded_accounts_data_size_limit),
                heap_size in Just(heap_size),
                required_signatures in Just(required_signatures),
            ) -> TestMessageData
        {
            TestMessageData {
                required_signatures,
                lifetime,
                accounts,
                priority_fee,
                compute_unit_limit,
                loaded_accounts_data_size_limit,
                heap_size,
                program_id_index,
                instr_accounts,
                data,
            }
        }
    }

    proptest! {
        #[test]
        fn test_v1_message_raw_bytes_roundtrip(test_data in generate_message_data()) {
            let accounts: Vec<Address> = test_data.accounts.into_iter()
                .map(Address::new_from_array).collect();
            let lifetime = Hash::new_from_array(test_data.lifetime);

            let mut builder = v1::MessageBuilder::new()
                .required_signatures(test_data.required_signatures)
                .lifetime_specifier(lifetime)
                .accounts(accounts)
                .instruction(CompiledInstruction {
                    program_id_index: test_data.program_id_index,
                    accounts: test_data.instr_accounts,
                    data: test_data.data,
                });

            // config values.
            if let Some(priority_fee) = test_data.priority_fee {
                builder = builder.priority_fee(priority_fee);
            }
            if let Some(compute_unit_limit) = test_data.compute_unit_limit {
                builder = builder.compute_unit_limit(compute_unit_limit);
            }
            if let Some(loaded_accounts_data_size_limit) = test_data.loaded_accounts_data_size_limit {
                builder = builder.loaded_accounts_data_size_limit(loaded_accounts_data_size_limit);
            }
            if let Some(heap_size) = test_data.heap_size {
                builder = builder.heap_size(heap_size);
            }

            let message = builder.build().unwrap();

            // Serialize V1 to raw bytes.
            let bytes = v1::serialize(&message);
            // Deserialize from raw bytes.
            let parsed = v1::deserialize(&bytes).unwrap();

            // Messages should match.
            assert_eq!(message, parsed);
            assert_eq!(message, wincode::deserialize(&bytes).unwrap());

            // Wrap in VersionedMessage and test `serialize()`.
            let versioned = VersionedMessage::V1(message);
            let serialized = versioned.serialize();

            // Assert that everything worked:
            // - serialized message is not empty.
            // - first byte is the version prefix with the correct version.
            // - remaining bytes match the original serialized message.
            assert!(!serialized.is_empty());
            assert_eq!(serialized[0], V1_PREFIX);
            assert_eq!(&serialized[1..], bytes.as_slice());
        }
    }

    #[test]
    fn test_v1_versioned_message_json_roundtrip() {
        let msg = v1::MessageBuilder::new()
            .required_signatures(1)
            .lifetime_specifier(Hash::new_unique())
            .accounts(vec![Address::new_unique(), Address::new_unique()])
            .priority_fee(1000)
            .compute_unit_limit(200_000)
            .instruction(CompiledInstruction {
                program_id_index: 1,
                accounts: vec![0],
                data: vec![1, 2, 3, 4],
            })
            .build()
            .unwrap();

        let vm = VersionedMessage::V1(msg);
        let s = serde_json::to_string(&vm).unwrap();
        let back: VersionedMessage = serde_json::from_str(&s).unwrap();
        assert_eq!(vm, back);
    }

    #[cfg(feature = "wincode")]
    #[test]
    fn test_v1_wincode_roundtrip() {
        let test_messages = [
            // Minimal message
            v1::MessageBuilder::new()
                .required_signatures(1)
                .lifetime_specifier(Hash::new_unique())
                .accounts(vec![Address::new_unique(), Address::new_unique()])
                .instruction(CompiledInstruction {
                    program_id_index: 1,
                    accounts: vec![0],
                    data: vec![],
                })
                .build()
                .unwrap(),
            // With config
            v1::MessageBuilder::new()
                .required_signatures(1)
                .lifetime_specifier(Hash::new_unique())
                .accounts(vec![Address::new_unique(), Address::new_unique()])
                .priority_fee(1000)
                .compute_unit_limit(200_000)
                .instruction(CompiledInstruction {
                    program_id_index: 1,
                    accounts: vec![0],
                    data: vec![1, 2, 3, 4],
                })
                .build()
                .unwrap(),
            // Multiple instructions
            v1::MessageBuilder::new()
                .required_signatures(2)
                .lifetime_specifier(Hash::new_unique())
                .accounts(vec![
                    Address::new_unique(),
                    Address::new_unique(),
                    Address::new_unique(),
                ])
                .heap_size(65536)
                .instructions(vec![
                    CompiledInstruction {
                        program_id_index: 2,
                        accounts: vec![0, 1],
                        data: vec![0xAA, 0xBB],
                    },
                    CompiledInstruction {
                        program_id_index: 2,
                        accounts: vec![1],
                        data: vec![0xCC],
                    },
                ])
                .build()
                .unwrap(),
        ];

        for message in test_messages {
            let versioned = VersionedMessage::V1(message.clone());

            // Wincode roundtrip
            let bytes = wincode::serialize(&versioned).expect("Wincode serialize failed");
            let deserialized: VersionedMessage =
                wincode::deserialize(&bytes).expect("Wincode deserialize failed");

            match deserialized {
                VersionedMessage::V1(parsed) => assert_eq!(parsed, message),
                _ => panic!("Expected V1 message"),
            }
        }
    }
}