Welcome to a technical overview of program deployments! In this post, you can read about how program deploys work under the hood, how to get your SOL back from a deployed program, and keep up to date on new features and fixes! Feel free to jump directly to the topic you wish to read about**:**

Intro to Solana on-chain programs

Solana on-chain programs (otherwise known as smart contracts, heh) are stored in "executable" accounts on Solana. These accounts are pretty much like any other account but with the exception of having the "executable" flag enabled and the owner being assigned to a BPF loader. Besides those exceptions, they are governed by the same runtime rules as non-executable accounts, hold SOL tokens for rent fees, and store a data buffer which is managed by the BPF loader program. The latest BPF loader is called the "Upgradeable BPF Loader".

Overview of the Upgradeable BPF Loader

State accounts

The Upgradeable BPF loader program supports three different types of state accounts:

  1. Program account: This is the main account of an on-chain program and its address is commonly referred to as a "program id." Program id's are what transaction instructions reference in order to invoke a program. Program accounts are immutable once deployed so you can think of them as a proxy account to the byte-code and state stored in other accounts.
  2. Program data account: This account is what stores the executable byte-code of an on-chain program. When a program is upgraded, this account's data is updated with new byte-code. In addition to byte-code, program data accounts are also responsible for storing the slot when it was last modified and the address of the sole account authorized to modify the account (this address can be cleared to make a program immutable).
  3. Buffer accounts: These accounts temporarily store byte-code while a program is being actively deployed through a series of transactions. They also each store the address of the sole account which is authorized to do writes.

Instructions

The state accounts listed above can only be modified with one of the following instructions supported by the Upgradeable BPF Loader program:

  1. Initialize buffer: Creates a buffer account and stores an authority address which is allowed to modify the buffer.
  2. Write: Writes byte-code at a specified byte offset inside a buffer account. Writes are processed in small chunks due to a limitation of Solana transactions having a maximum serialized size of 1232 bytes.
  3. Deploy: Creates both a program account and a program data account. It fills the program data account by copying the byte-code stored in a buffer account. If the byte-code is valid, the program account will be set as executable, allowing it to be invoked. If the byte-code is invalid, the instruction will fail and all changes are reverted.
  4. Upgrade: Fills an existing program data account by copying executable byte-code from a buffer account. Similar to the deploy instruction, it will only succeed if the byte-code is valid.
  5. Set authority: Updates the authority of a program data or buffer account if the account's current authority has signed the transaction being processed. If the authority is deleted without replacement, it can never be set to a new address and the account can never be closed.
  6. Close: Clears the data of a program data account or buffer account and reclaims the SOL used for the rent exemption deposit.

How solana program deploy works

Deploying a program on Solana requires hundreds if not thousands of transactions due to the max size limit of 1232 bytes for Solana transactions. The Solana CLI takes care of this rapid firing of transactions with the solana program deploy subcommand but it might be useful to understand just how it works. The process can be broken down into the following 3 phases: