2018-12-12 14:37:46 -08:00
|
|
|
# The Runtime
|
2018-11-06 17:00:58 -08:00
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
The runtime is a concurrent transaction processor. Transactions specify their
|
|
|
|
data dependencies upfront and dynamic memory allocation is explicit. By
|
|
|
|
separating program code from the state it operates on, the runtime is able to
|
|
|
|
choreograph concurrent access. Transactions accessing only credit-only
|
|
|
|
accounts are executed in parallel whereas transactions accessing writable
|
|
|
|
accounts are serialized. The runtime interacts with the program through an
|
|
|
|
entrypoint with a well-defined interface. The userdata stored in an account is
|
|
|
|
an opaque type, an array of bytes. The program has full control over its
|
|
|
|
contents.
|
|
|
|
|
|
|
|
The transaction structure specifies a list of public keys and signatures for
|
|
|
|
those keys and a sequential list of instructions that will operate over the
|
|
|
|
states associated with the account keys. For the transaction to be committed
|
|
|
|
all the instructions must execute successfully; if any abort the whole
|
|
|
|
transaction fails to commit.
|
|
|
|
|
2018-11-28 13:00:02 -08:00
|
|
|
### Account Structure
|
2018-11-25 15:58:38 -08:00
|
|
|
|
|
|
|
Accounts maintain a token balance and program-specific memory.
|
2018-11-06 17:00:58 -08:00
|
|
|
|
|
|
|
# Transaction Engine
|
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
The engine maps public keys to accounts and routes them to the program's
|
|
|
|
entrypoint.
|
2018-11-06 17:00:58 -08:00
|
|
|
|
|
|
|
## Execution
|
|
|
|
|
|
|
|
Transactions are batched and processed in a pipeline
|
|
|
|
|
2018-11-17 17:01:21 -08:00
|
|
|
<img alt="Runtime pipeline" src="img/runtime.svg" class="center"/>
|
2018-11-06 17:00:58 -08:00
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
At the *execute* stage, the loaded pages have no data dependencies, so all the
|
|
|
|
programs can be executed in parallel.
|
2018-11-06 17:00:58 -08:00
|
|
|
|
|
|
|
The runtime enforces the following rules:
|
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
1. Only the *owner* program may modify the contents of an account. This means
|
|
|
|
that upon assignment userdata vector is guaranteed to be zero.
|
2018-11-06 17:00:58 -08:00
|
|
|
2. Total balances on all the accounts is equal before and after execution of a
|
2018-11-25 15:58:38 -08:00
|
|
|
transaction.
|
|
|
|
3. After the transaction is executed, balances of credit-only accounts must be
|
|
|
|
greater than or equal to the balances before the transaction.
|
|
|
|
4. All instructions in the transaction executed atomically. If one fails, all
|
|
|
|
account modifications are discarded.
|
2018-11-06 17:00:58 -08:00
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
Execution of the program involves mapping the program's public key to an
|
|
|
|
entrypoint which takes a pointer to the transaction, and an array of loaded
|
|
|
|
pages.
|
2018-11-06 17:00:58 -08:00
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
## SystemProgram Interface
|
2018-11-06 17:00:58 -08:00
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
The interface is best described by the `Instruction::userdata` that the user
|
|
|
|
encodes.
|
|
|
|
|
|
|
|
* `CreateAccount` - This allows the user to create and assign an account to a
|
2018-11-06 17:00:58 -08:00
|
|
|
Program.
|
2018-11-25 15:58:38 -08:00
|
|
|
* `Assign` - allows the user to assign an existing account to a program.
|
|
|
|
* `Move` - moves tokens between account's that are associated with
|
|
|
|
* `Spawn` - spawn a new program from an account
|
2018-11-06 17:00:58 -08:00
|
|
|
|
|
|
|
## Notes
|
|
|
|
|
2018-11-25 15:58:38 -08:00
|
|
|
1. There is no dynamic memory allocation. Client's need to use `CreateAccount`
|
|
|
|
instructions to create memory before passing it to another program. This
|
|
|
|
instruction can be composed into a single transaction with the call to the
|
2018-11-06 17:00:58 -08:00
|
|
|
program itself.
|
2018-11-25 15:58:38 -08:00
|
|
|
2. Runtime guarantees that when memory is assigned to the program it is zero
|
2018-11-06 17:00:58 -08:00
|
|
|
initialized.
|
2018-11-25 15:58:38 -08:00
|
|
|
3. Runtime guarantees that a program's code is the only thing that can modify
|
2018-11-06 17:00:58 -08:00
|
|
|
memory that its assigned to
|
2018-11-25 15:58:38 -08:00
|
|
|
4. Runtime guarantees that the program can only spend tokens that are in
|
|
|
|
accounts that are assigned to it
|
|
|
|
5. Runtime guarantees the balances belonging to accounts are balanced before
|
2018-11-06 17:00:58 -08:00
|
|
|
and after the transaction
|
|
|
|
6. Runtime guarantees that multiple instructions all executed successfully when
|
|
|
|
a transaction is committed.
|
|
|
|
|
|
|
|
# Future Work
|
|
|
|
|
|
|
|
* [Continuations and Signals for long running
|
|
|
|
Transactions](https://github.com/solana-labs/solana/issues/1485)
|
|
|
|
|