Posted on :: ::

For a while now I've been wondering how the code I write actually gets into the blockchain. When you're starting out, everything sounds so abstract that it's easier to run away. But here I want to explain it in a simple way. We are going to follow the journey of a piece of data from the moment it is born in your code until it becomes immutable on the chain.

1. The Origin: The "Living" Struct

It all starts in your Smart Contract. Imagine you are building a system for a financial institution. You have a clear and safe data structure with a struct, because no matter which blockchain we're talking about, if it uses Rust it will use a struct like this:

#[derive(Serialize, Deserialize)] // La magia de Serde comienza aquí
struct Transaccion {
    monto: u64,
    destino: String,
    moneda: String,
}

We just created the shape of our data. At this point, the data is "alive" in RAM. But there's a problem: RAM is volatile and local. If we want this data to live forever, we have to "freeze" it, because otherwise it can't be sent or written into the chain's record.

2. The Transformation: Compilation and Serialization

To understand it better: the way code compiles is very similar to what happens when we serialize with one of the crates we use every day: Serde.

When we compile, we are doing a kind of "serialization" toward the ledger. We translate our logic (the code) into instructions the network understands (in our case, Wasm or WebAssembly).

3. The Format: Why don't we use JSON?

If you've done web development, you're surely used to JSON.

However, on the blockchain, and even though each network has its own "language" (XDR on Stellar, SCALE on Polkadot, Borsh on Solana), they all agree on one thing: binary is king. For a network to be global, we can't afford to send heavy text. What we need is to send pure bytes: fast and safe. If we look at XDR, we see that:

  • It's Binary: It doesn't waste a single bit on spaces or quotes.
  • It's Strict: There's no doubt about whether a number is an integer or a decimal. For a bank, this precision is the difference between success and a million-dollar mistake.

Each blockchain will use the "language" with the best features for its purpose.

4. The Engine: Wasm and Soroban

When you send that transaction, the code that processes it is a Wasm binary. Why is that the key?

  • Portability: It runs the same in Singapore or in Buenos Aires.
  • Isolation: Your code runs in a "sandbox". If something fails, it doesn't break the whole blockchain.
  • Speed: Almost native execution. Finance in seconds, not days.

5. The Destination: The Ledger and the "Deserialize"

Once the bytes reach the node, the reverse process happens. The node receives the package in XDR, and since it knows the structure, it "deserializes" it (that is, it reads and unpacks it). If everything is correct, it gets written to the Ledger. What started as a simple Rust struct is now an immutable record.


Summary

  • Rust: Data safety.
  • Serde: The packing tool.
  • XDR: Stellar's efficient language.
  • Wasm: The engine that processes everything without errors.