This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): add support for compiling to a new transactio…
…n type
- Loading branch information
1 parent
b45474d
commit 9445912
Showing
12 changed files
with
124 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/transactions/src/__tests__/new-compile-transaction-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import '@solana/test-matchers/toBeFrozenObject'; | ||
|
||
import { Address } from '@solana/addresses'; | ||
|
||
import { CompiledMessage, compileTransactionMessage } from '../message'; | ||
import { compileTransaction } from '../new-compile-transaction'; | ||
import { getCompiledMessageEncoder } from '../serializers'; | ||
|
||
jest.mock('../message'); | ||
jest.mock('../serializers/message'); | ||
|
||
type TransactionMessage = Parameters<typeof compileTransaction>[0]; | ||
|
||
describe('compileTransactionMessage', () => { | ||
const mockAddressA = '2aaa' as Address; | ||
const mockAddressB = '1aaa' as Address; | ||
const mockCompiledMessage = { | ||
header: { | ||
numSignerAccounts: 2, | ||
}, | ||
staticAccounts: [mockAddressA, mockAddressB], | ||
} as CompiledMessage; | ||
const mockCompiledMessageBytes = new Uint8Array(Array(100)).fill(1); | ||
beforeEach(() => { | ||
(compileTransactionMessage as jest.Mock).mockReturnValue(mockCompiledMessage); | ||
(getCompiledMessageEncoder as jest.Mock).mockReturnValue({ | ||
encode: jest.fn().mockReturnValue(mockCompiledMessageBytes), | ||
}); | ||
}); | ||
|
||
it('compiles the supplied `TransactionMessage` and sets the `messageBytes` property to the result', () => { | ||
const transaction = compileTransaction({} as TransactionMessage); | ||
expect(transaction).toHaveProperty('messageBytes', mockCompiledMessageBytes); | ||
}); | ||
it('compiles an array of signatures the length of the number of signers', () => { | ||
const transaction = compileTransaction({} as TransactionMessage); | ||
expect(Object.keys(transaction.signatures)).toHaveLength(mockCompiledMessage.header.numSignerAccounts); | ||
}); | ||
it("inserts signers into the correct position in the signatures' array", () => { | ||
const transaction = compileTransaction({} as TransactionMessage); | ||
expect(Object.keys(transaction.signatures)).toStrictEqual([ | ||
// Two signers, in the order they're found in `mockCompiledMessage.staticAccounts` | ||
mockAddressA, | ||
mockAddressB, | ||
]); | ||
}); | ||
it('inserts a null signature into the map for each signer', () => { | ||
const transaction = compileTransaction({} as TransactionMessage); | ||
expect(Object.values(transaction.signatures)).toStrictEqual([null, null]); | ||
}); | ||
it('freezes the returned transaction', () => { | ||
const transaction = compileTransaction({} as TransactionMessage); | ||
expect(transaction).toBeFrozenObject(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Address } from '@solana/addresses'; | ||
import { SignatureBytes } from '@solana/keys'; | ||
|
||
import { CompilableTransaction } from './compilable-transaction'; | ||
import { compileTransactionMessage } from './message'; | ||
import { getCompiledMessageEncoder } from './serializers/message'; | ||
|
||
type TypedArrayMutableProperties = 'copyWithin' | 'fill' | 'reverse' | 'set' | 'sort'; | ||
interface ReadonlyUint8Array extends Omit<Uint8Array, TypedArrayMutableProperties> { | ||
readonly [n: number]: number; | ||
} | ||
|
||
export type TransactionMessageBytes = ReadonlyUint8Array & { readonly __brand: unique symbol }; | ||
export type OrderedMap<K extends string, V> = Record<K, V>; | ||
|
||
export type NewTransaction = Readonly<{ | ||
messageBytes: TransactionMessageBytes; | ||
signatures: OrderedMap<Address, SignatureBytes | null>; | ||
}>; | ||
|
||
export function compileTransaction(transactionMessage: CompilableTransaction): NewTransaction { | ||
const compiledMessage = compileTransactionMessage(transactionMessage); | ||
const messageBytes = getCompiledMessageEncoder().encode( | ||
compiledMessage, | ||
) as ReadonlyUint8Array as TransactionMessageBytes; | ||
|
||
const transactionSigners = compiledMessage.staticAccounts.slice(0, compiledMessage.header.numSignerAccounts); | ||
const signatures: OrderedMap<Address, SignatureBytes | null> = {}; | ||
for (const signerAddress of transactionSigners) { | ||
signatures[signerAddress] = null; | ||
} | ||
|
||
const transaction: NewTransaction = { | ||
messageBytes: messageBytes as TransactionMessageBytes, | ||
signatures: Object.freeze(signatures), | ||
}; | ||
|
||
return Object.freeze(transaction); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters