Arcturus Docs
  • Note!
  • Bitcoin and Decentralized Structure
  • Introducing to Arcturus Chain
  • For End User
    • How to add Arcturus to Metamask
    • Obtaining tARC from the Faucet
    • Faucet API
    • How to Use Arcscan
  • For Nerds
    • Proof Of Stake
    • Arcturus Chain
      • Accounts
      • Messages and Transactions
      • Messages
      • Arcturus State Transition Function
      • Code Execution
      • Blockchain and Mining
      • Applications
        • Token Systems
        • Financial Derivatives and Stable-Value Currencies
        • Identity and Reputation Systems
        • Decentralized File Storage
        • Decentralized Autonomous Organizations
        • Further Applications
      • Fees
      • Computation And Turing-Completeness
      • Currency And Issuance
      • Mining Centralization
      • Scalability
      • Conclusion
    • Arcturus Pay
      • About
      • Technolog & Use Cases
      • Developer Instructions
        • arcpay_dev-1
        • arcpay_dev-2
        • arcpay_dev-3
  • Updates & News
Powered by GitBook
On this page
  1. For Nerds
  2. Arcturus Chain

Code Execution

PreviousArcturus State Transition FunctionNextBlockchain and Mining

Last updated 10 months ago

The code in Arcturus contracts is written in a low-level, stack-based bytecode language, referred to as "Arcturus virtual machine code" or "EVM code". The code consists of a series of bytes, where each byte represents an operation. In general, code execution is an infinite loop that consists of repeatedly carrying out the operation at the current program counter (which begins at zero) and then incrementing the program counter by one, until the end of the code is reached or an error or STOP or RETURN instruction is detected. The operations have access to three types of space in which to store data:

  • The stack, a last-in-first-out container to which values can be pushed and popped

  • Memory, an infinitely expandable byte array

  • The contract's long-term storage, a key/value store. Unlike stack and memory, which reset after computation ends, storage persists for the long term.

The code can also access the value, sender, and data of the incoming message, as well as block header data, and can return a byte array of data as an output.

The formal execution model of EVM code is surprisingly simple. While the Arcturus virtual machine is running, its full computational state can be defined by the tuple (block_state, transaction, message, code, memory, stack, pc, gas), where block_state is the global state containing all accounts and includes balances and storage. At the start of every round of execution, the current instruction is found by taking the pc-th byte of code (or 0 if pc >= len(code)), and each instruction has its own definition in terms of how it affects the tuple. For example, ADD pops two items off the stack and pushes their sum, reduces gas by 1, and increments pc by 1, and SSTORE pops the top two items off the stack and inserts the second item into the contract's storage at the index specified by the first item. Although there are many ways to optimize Arcturus virtual machine execution via just-in-time compilation, a basic implementation of Arcturus can be done in a few hundred lines of code.

Here is an example of how you might simulate a simple Arcturus contract execution in Node.js:

javascriptCopy codeclass ArcturusVM {
    constructor(code) {
        this.code = code;
        this.pc = 0;
        this.stack = [];
        this.memory = [];
        this.storage = {};
        this.gas = 10000; // Starting gas
    }

    fetchInstruction() {
        return this.code[this.pc];
    }

    executeInstruction(instruction) {
        switch (instruction) {
            case 'PUSH':
                this.pc++;
                this.stack.push(this.code[this.pc]);
                break;
            case 'ADD':
                const a = this.stack.pop();
                const b = this.stack.pop();
                this.stack.push(a + b);
                this.gas -= 1;
                break;
            case 'SSTORE':
                const key = this.stack.pop();
                const value = this.stack.pop();
                this.storage[key] = value;
                this.gas -= 200;
                break;
            case 'STOP':
                this.gas = 0;
                break;
            default:
                throw new Error('Unknown instruction');
        }
        this.pc++;
    }

    run() {
        while (this.pc < this.code.length && this.gas > 0) {
            const instruction = this.fetchInstruction();
            this.executeInstruction(instruction);
        }
    }
}

// Example contract code: [PUSH 2, PUSH 3, ADD, PUSH 1, SSTORE, STOP]
const code = ['PUSH', 2, 'PUSH', 3, 'ADD', 'PUSH', 1, 'SSTORE', 'STOP'];
const vm = new ArcturusVM(code);
vm.run();

console.log('Final Stack:', vm.stack);
console.log('Final Storage:', vm.storage);
console.log('Remaining Gas:', vm.gas);

In this example, the contract code consists of a series of instructions: pushing values to the stack, adding them, storing a value in the contract's storage, and stopping the execution. The ArcturusVM class simulates the execution environment of an Arcturus contract. The run method loops through the instructions, executing each one and updating the program counter (pc), stack, storage, and gas accordingly.