Encoding

Encoding refers to the process of converting data from one format to another for security, efficiency, and compatibility. In blockchain networks, encoding is essential for:

  • Secure data storage and transmission

  • Efficient serialization of transactions and blocks

  • Interoperability with different blockchain protocols


Encoding Formats in LagomChain

1. Protocol Buffers (Protobuf) – Cosmos SDK Standard

With the Cosmos Stargate release, Protobuf is the primary encoding format for:

  • Client serialization (transactions, queries)

  • State serialization (genesis files, network parameters)

Protobuf Advantages:

  • Smaller and faster than JSON

  • Language-agnostic and supports multiple programming languages

  • Highly extensible for future upgrades

All EVM module types in LagomChain (such as transaction messages and query services) are implemented as protocol buffer messages.


2. Recursive Length Prefix (RLP) – Ethereum Compatibility

LagomChain supports Recursive Length Prefix (RLP), the primary encoding format in Ethereum’s execution layer.

RLP is used for:

  • Serializing Ethereum transactions

  • Encoding Merkle tree structures

  • Hash verification and signing

Ethereum transactions are signed using the RLP hash of the transaction data. Blocks are also identified by the RLP hash of their headers.

RLP Encoding Rules:

  • Integers are represented in big-endian format, without leading zeroes.

  • A single byte (0x00 to 0x7F) is encoded as itself.

  • Strings shorter than 55 bytes are prefixed with a length byte (0x80 + length).

  • Longer strings and lists require additional length encoding.

Although Ethereum 2.0 replaces RLP with Simple Serialize (SSZ) for its consensus layer, RLP remains the standard for Ethereum transactions and execution clients.


3. Amino – Legacy Cosmos SDK Encoding

The Cosmos SDK originally used Amino encoding, which remains supported for:

  • Backwards compatibility

  • Client encoding and Ledger device signing

However, LagomChain does not use Amino for EVM transactions.


RLP in LagomChain’s EVM Module

LagomChain encodes EVM transactions (MsgEthereumTx) using RLP to ensure Ethereum compatibility.

RLP Encoding for Ethereum Transactions:

  • Converts the transaction structure to Ethereum’s format.

  • Marshals the transaction data using RLP serialization.

Example: Encoding an Ethereum Transaction in LagomChain’s EVM Module (Go Code)

// TxEncoder overwrites sdk.TxEncoder to support MsgEthereumTx
func (g txConfig) TxEncoder() sdk.TxEncoder {
  return func(tx sdk.Tx) ([]byte, error) {
    msg, ok := tx.(*evmtypes.MsgEthereumTx)
    if ok {
      return msg.AsTransaction().MarshalBinary()
    }
    return g.TxConfig.TxEncoder()(tx)
  }
}

Example: Decoding an Ethereum Transaction in LagomChain’s EVM Module (Go Code)

// TxDecoder overwrites sdk.TxDecoder to support MsgEthereumTx
func (g txConfig) TxDecoder() sdk.TxDecoder {
  return func(txBytes []byte) (sdk.Tx, error) {
    tx := &ethtypes.Transaction{}
    err := tx.UnmarshalBinary(txBytes)
    if err == nil {
      msg := &evmtypes.MsgEthereumTx{}
      msg.FromEthereumTx(tx)
      return msg, nil
    }
    return g.TxConfig.TxDecoder()(txBytes)
  }
}

By integrating RLP encoding/decoding, LagomChain ensures that:

  • Transactions are formatted correctly for Ethereum clients and tools.

  • JSON-RPC messages conform to Ethereum’s expected structure.

  • The network remains fully EVM-compatible while operating on Cosmos SDK.

Last updated