# AIA Chain State Transition Function

<figure><img src="/files/7kc4IUCU5EPn3honNMft" alt=""><figcaption></figcaption></figure>

The AIA Chain state transition function **APPLY(S, TX) → S'** can be defined as follows:

1. **Transaction Validation**:
   1. Verify that the transaction format is correct (i.e., it has the correct number of values), the signature is valid, and the Nonce value matches the one stored in the sender's account. If not, return an error.
2. **Transaction Fee Calculation**:
   1. Calculate the transaction fee by multiplying **STARTGAS × GASPRICE**. Then, determine the sender's address from the signature. Deduct the transaction fee from the sender's account balance and increment the sender's nonce value. If the account balance is insufficient, return an error.
3. **Fuel Initialization**:
   1. Initialize **GAS = STARTGAS** and deduct fuel for each byte in the transaction data. Each byte consumes fuel, and the fuel is deducted accordingly.
4. **State Transition**:
   1. Transfer the transaction value from the sender’s account to the recipient's account. If the recipient’s account does not exist, create it. If the recipient is a contract, execute the contract’s code until completion or until fuel runs out.
5. **Rollback on Failure**:
   1. If the transfer fails due to insufficient funds or exhausted fuel during contract execution, rollback all state changes except for the transaction fees, and pay the fees to the miner's account.
6. **Refund Remaining Fuel**:
   1. If the transaction executes successfully, refund the remaining fuel to the sender and pay the fuel cost to the miner.

Example: Contract Execution

Python

if !self.storage\[calldataload（0）]:

self.storage\[calldataload（0）] = calldataload（32）

***

Suppose the contract code is written in a higher-level language like Serpent (which compiles to AIA Chain VM code). Here’s how the state transition function would execute with a transaction of 10 AIA and 2000 units of fuel, with a fuel price of 0.001 AIA, and data consisting of 64 bytes where:

* Bytes 0-31 represent the number "2".
* Bytes 32-63 represent the string "CHARLIE".

The state transition function execution process is as follows:

1. **Transaction Validity Check**:
   1. Verify that the transaction format is correct and the signature is valid.
2. **Fee Deduction**:
   1. Check if the sender has at least **2000 × 0.001 = 2 AIA**. If so, deduct 2 AIA from the sender’s account.
3. **Fuel Initialization**:
   1. Initialize fuel to 2000 units. The transaction length is 170 bytes, with each byte costing 5 units of fuel. Deduct **170 × 5 = 850** units of fuel, leaving **1150 units** of fuel.
4. **Value Transfer**:
   1. Transfer 10 AIA from the sender’s account to the contract account.
5. **Contract Code Execution**:
   1. The contract checks if the storage at index 2 is used. If not, it sends a notification. If used, it sets the value at index 2 to "CHARLIE". Assume this execution consumes 187 units of fuel, leaving **963 units** of fuel.
6. **Refunding Remaining Fuel**:
   1. Refund **963 × 0.001 = 0.963 AIA** to the sender and return the generated state.

If the recipient is not a contract, the total transaction fee will simply be **GASPRICE × transaction byte length**, which is independent of the data sent with the transaction.

Messages behave the same way as transactions in terms of rollback. If a message exhausts its fuel, the execution of the message and all other executions triggered by it will be rolled back, but the parent execution will not be rolled back. This ensures that calling another contract is "safe"—for example, if contract A calls contract B using **G** units of fuel, it is guaranteed that A will consume no more than **G** units of fuel.

Finally, note that there is an opcode **CREATE** for creating contracts. The execution mechanism for creating contracts is similar to **CALL**, except the output of the execution determines the code of the newly created contract.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aia-chain-1.gitbook.io/aialabs/technical-framework/core-architecture/aia-chain-state-transition-function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
