11import { Decoder , Encoder } from '@solana/codecs-core' ;
2+ import { SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_NOT_SUPPORTED , SolanaError } from '@solana/errors' ;
23
34import { TransactionVersion } from '../../transaction-message' ;
45import {
@@ -10,6 +11,7 @@ import {
1011const VERSION_FLAG_MASK = 0x80 ;
1112const VERSION_TEST_CASES = // Versions 0–127
1213 [ ...Array ( 128 ) . keys ( ) ] . map ( version => [ version | VERSION_FLAG_MASK , version as TransactionVersion ] as const ) ;
14+ const UNSUPPORTED_VERSION_TEST_CASES = VERSION_TEST_CASES . slice ( 1 ) ; // versions 1-127
1315
1416describe . each ( [ getTransactionVersionCodec , getTransactionVersionEncoder ] ) (
1517 'Transaction version encoder' ,
@@ -21,8 +23,15 @@ describe.each([getTransactionVersionCodec, getTransactionVersionEncoder])(
2123 it ( 'serializes no data when the version is `legacy`' , ( ) => {
2224 expect ( transactionVersion . encode ( 'legacy' ) ) . toEqual ( new Uint8Array ( ) ) ;
2325 } ) ;
24- it . each ( VERSION_TEST_CASES ) ( 'serializes to `%s` when the version is `%s`' , ( expected , version ) => {
25- expect ( transactionVersion . encode ( version ) ) . toEqual ( new Uint8Array ( [ expected ] ) ) ;
26+ it ( 'serializes to `0x80` when the version is `0`' , ( ) => {
27+ expect ( transactionVersion . encode ( 0 ) ) . toEqual ( new Uint8Array ( [ 0x80 ] ) ) ;
28+ } ) ;
29+ it . each ( UNSUPPORTED_VERSION_TEST_CASES ) ( 'fatals for unsupported version `%s`' , ( _byte , version ) => {
30+ expect ( ( ) => transactionVersion . encode ( version ) ) . toThrow (
31+ new SolanaError ( SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_NOT_SUPPORTED , {
32+ unsupportedVersion : version ,
33+ } ) ,
34+ ) ;
2635 } ) ;
2736 it . each ( [ - 1 as TransactionVersion , 128 as TransactionVersion ] ) (
2837 'throws when passed the out-of-range version `%s`' ,
@@ -40,9 +49,6 @@ describe.each([getTransactionVersionCodec, getTransactionVersionDecoder])(
4049 beforeEach ( ( ) => {
4150 transactionVersion = serializerFactory ( ) ;
4251 } ) ;
43- it . each ( VERSION_TEST_CASES ) ( 'deserializes `%s` to the version `%s`' , ( byte , expected ) => {
44- expect ( transactionVersion . decode ( new Uint8Array ( [ byte ] ) ) ) . toEqual ( expected ) ;
45- } ) ;
4652 it ( 'deserializes to `legacy` when missing the version flag' , ( ) => {
4753 expect (
4854 transactionVersion . decode (
@@ -51,5 +57,19 @@ describe.each([getTransactionVersionCodec, getTransactionVersionDecoder])(
5157 ) ,
5258 ) . toBe ( 'legacy' ) ;
5359 } ) ;
60+ it ( 'deserializes to 0 for a version 0 transaction' , ( ) => {
61+ expect (
62+ transactionVersion . decode (
63+ new Uint8Array ( [ 0 | VERSION_FLAG_MASK ] ) , // version 0 with the version flag
64+ ) ,
65+ ) . toBe ( 0 ) ;
66+ } ) ;
67+ it . each ( UNSUPPORTED_VERSION_TEST_CASES ) ( 'fatals for unsupported version `%s`' , ( byte , version ) => {
68+ expect ( ( ) => transactionVersion . decode ( new Uint8Array ( [ byte ] ) ) ) . toThrow (
69+ new SolanaError ( SOLANA_ERROR__TRANSACTION__VERSION_NUMBER_NOT_SUPPORTED , {
70+ unsupportedVersion : version ,
71+ } ) ,
72+ ) ;
73+ } ) ;
5474 } ,
5575) ;
0 commit comments