This repo implements the Rosetta API for the Flow blockchain.
Architecture
_____ _ ____ _ _
| ___| | _____ __ | _ \ ___ ___ ___| |_| |_ __ _
| |_ | |/ _ \ \ /\ / / | |_) / _ \/ __|/ _ \ __| __/ _` |
| _| | | (_) \ V V / | _ < (_) \__ \ __/ |_| || (_| |
|_| |_|\___/ \_/\_/ |_| \_\___/|___/\___|\__|\__\__,_|
.───────────.
( Client )
`────┬▲─────'
││
││
││
││ Rosetta
││ API
││ Calls
││
││ /cmd/server
┏━━━━━━━╋╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ││ ┃
┃ ┌─────▼┴─────────────────────────────┐ ┌───────────────────────────────────────────┐ ┃
┃ │ │ │ │ ┃
┃ │ Rosetta API Server │ │ State Indexer │ ┃
┃ │ │ │ │ ┃
┃ └────┬────────────┬──────────────────┘ └─────────┬▲──────────────────────▲─────────┘ ┃
┃ │ │ ││ │ ┃
┃ │ │ Processes Blocks Validates Seals ┃
┃ │ │ and Stores Data in Against ┃
┃ │ │ the Local Index Consensus Data ┃
┃ │ │ ││ │ ┃
┃ │ │ ││ │ ┃
┃ │ │ ││ ┌──────────┴─────────┐ ┃
┃ │ │ ││ │ │ ┃
┃ │ ┌────────┴─────┐ .─────────. ││ │ Consensus Follower │ ┃
┃ │ │ │ ╱ ╲ ││ │ │ ┃
┃ │ │ Data API │◀───────( Index DB )◀─┘│ └───────────────────▲┘ ┃
┃ │ │ │ `. ,' │ │ ┃
┃ │ └────────────▲─┘ `───┬───' │ .─────────────────. │ ┃
┃ │ │ │ │ ( Prefetch Workers ) │ ┃
┃ │ │ │ │ `─────────────────' │ ┃
┃ │ │ │ │ │ │ ┃
┃ ┌────┴──────────────┐ │ ▼ │.─────────. │ │ ┃
┃ │ │ │ .─────────────────. ╱ ╲ │ │ ┃
┃ │ Construction API │ │ ( Balance Validator ) ( API Cache ) Warms Up Cache │ ┃
┃ │ │ │ `─────────────────' `. ,' │ │ ┃
┃ └─┬─────────────────┘ │ ▲ `────▲──'◀────────────┘ │ ┃
┃ │ │ │ │ │ ┃
┗━━━╋━━━━━━━━━━━━━━━━━━━╋━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━╋━━┛
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
│ │ │ Fetches │ │
│ │ │ On-Chain │ │
│ │ │ Balance │ │
│ Fetches Recent │ │ │ │
│ State │ │ │ │
│ │ │ Fetches Block │ Follows Live │
│ │ │ Data and Events │ Spork Consensus │
│ │ │ for Past/Live │ as an Unstaked │
│ Submits │ │ Sporks │ Access Node │
│ Transactions │ │ │ │
│ │ │ │ │
│ │ │ │ │
┏━━━▼━━━━━━━━━━━━━━━━━━━┻━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━┓ ┏━━━━━━━━━━━━━━━━━┻━━┓
┃ ┃ ┃ ┃
┃ Flow Access API Servers ┃ ┃ Flow Access Nodes ┃
┃ ┃ ┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━━━┛
Overview
Rosetta is an interoperability standard developed by Coinbase which was required to integrate any new blockchain to
Coinbase for fiat on-off ramps and other functionality that Coinbase offers. Rosetta has since been renamed to
Mesh and continues to honor the APIs originally used in Rosetta.
Rosetta's design is such that all aspects of integration must be handled through the implementation of the Rosetta standard which
models the full lifecycle of blockchain integration. The implication of this is that all interactions with Flow are expected
to be handled through Rosetta. Furthermore, due to EVM-centric historical design assumptions made by Rosetta certain additional
requirements must be enforced to ensure correct operation. The most important requirement is that all accounts which Rosetta
tracks are ones initially created by a root originator account owned by Coinbase. Sub-account creation is handled through the
Rosetta Construction API using only the configured originator accounts. This allows Rosetta to track only those specific accounts
rather than tracking all accounts on-chain (which it would normally do).
Design
Flow Rosetta consists of two primary systems:
The State Indexer exists to track the entire history of Coinbase created accounts and their value movements. This is because
the Flow Access API doesn't support an EVM JSON-RPC equivalent of eth_getBalance(address) while Rosetta assumes and requires
a getBalance equivalent to function.
Uses the Access API servers to get the relevant block data and events for
each of the configured network segments (aka, spork - a network upgrade requiring downtime).
After establishing a data integrity chain from a trusted starting point,
processes the events for sealed blocks, and stores the inferred state
changes and block metadata in a local Index DB.
Processes past sporks by starting with the root block data from the protocol
state snapshot of the live spork, and works backwards to the "genesis"
block, i.e. the configured "root" block of the first spork in the JSON
config.
Processes the live spork by starting with the root block of that spork and
works forward to the most recently sealed block.
Spins up a Consensus Follower, which is used to validate the seals for
blocks within a live spork.
Spins up concurrent prefetch workers who cache various Access API calls in
advance of the main block processing loop.
The Rosetta API Server: implements the Rosetta standard interfaces
This runs an HTTP server that implements various endpoints of the Rosetta
API.
For the Rosetta Data API, the data primarily comes from the local Index DB.
For certain secondary bits of state, e.g. account sequence numbers, the
information is fetched from the Access API servers for the latest spork.
For the Rosetta Construction API, the configured construction_access_nodes
are used to help get relevant metadata during transaction construction, and
for submitting the transactions.
Runs a background balance validation loop that compares indexed account
balances against on-chain state whenever the indexer is synced close to tip.
Code Layout
The code is structured into the following top-level directories:
access — Provides a client for the Flow Access API that acts as a wrapper
around the gRPC Access API client.
api — The core Rosetta API server implementation.
cache — Provides a storage-backed cache layer for automatically caching
Access API calls.
cli-conf — Provides the default configuration for validating the implemented
Rosetta Data API against rosetta-cli check:data.
cmd — Provides the main command line binaries. Primary amongst these is the
server command.
config — Handles the JSON config file for Flow Rosetta.
crypto — Provides utility functions for converting between Rosetta and Flow
public key formats for secp256k1 keys.
deque — Provides a double-ended, auto-scaling queue for storing finalized
block IDs.
environ — Contains some minor build/test scripts.
fees — Defines the constants used to calculate fees during transaction
construction.
indexdb — Provides a storage-backed index for persisting the processed block
data.
log — Provides a thin wrapper for logging.
model — Defines the core protobuf-based data models used during transaction
construction and for persisting complex data within the indexdb.
process — Provides support for signal handlers and for automatically running
registered functions on process exit.
script — Contains the code for the Cadence transaction scripts and the code
for the Proxy contract.
state — Implements the State Indexer.
timeout — Provides some utilities for auto-extending timeouts on successful
I/O operations.
trace — Initializes the OpenTelemetry tracing/metrics infrastructure and
provides some thin wrappers for using them.
version — Defines the constants for the Flow and Flow Rosetta versions.
Config files
The config files in the root of the repo are provided for reference and testing. Since Coinbase runs Rosetta
operationally the contents of testnet.json and mainnet.json likely do not reflect the actual configs used on
those networks.
FlowColdStorageProxy contract
The FlowColdStorageProxy contract in scripts/contracts is not currently deployed or used by Coinbase in either
testnet or mainnet. This was developed in preparation for integration with a Coinbase feature, the process for
which was never completed.
Non-conformance
A standard Rosetta implementation would typically track all accounts/address and their corresponding value movements
on-chain, from genesis with no exceptions. However, incompatibilities between Rosetta and Flow's models and APIs resulted
in a reduced scope of on-chain account tracking.
- We use the events generated from Flow transactions to infer changes in account
balances. Since the events on Flow do not fully track the movement of
resources on-chain.
A standard Rosetta would reconcile all balances, across all accounts on-chain from genesis.
- In order to support balance reconciliation by
rosetta-cli check:data, we
only track balance changes for accounts created by the configured
originators or any of its "descendants".
Rosetta assumptions regarding auditability of accounts mean that only the most basic, standard operations are permitted
to be performed on Coinbase created Cadence user account vaults. This is possible because users cannot take custody of
the Flow account which Coinbase uses on their behalf.
- For our balance tracking of these accounts to remain valid, they must never be
used in any "non-standard" transactions, e.g. moving FLOW to non-default Vault
resources, splitting a withdrawal across multiple deposits, etc.
Rosetta assumes that networks infrequently release breaking changes to a network requiring downtime and a halt in Rosetta
functionality. This implementation needs to be configured with the required network spork segments and will handle retrieval
of historical data from those transparently. Sporks frequency as of Q1 2025 is expected to be once yearly only.
Since Flow effectively hard forks every spork, the Access API may serve block
data from an old spork that is then not part of the next spork. If the Rosetta
Node isn't shutdown in time, then it's possible for invalid data to be
processed and indexed.
We can partially mitigate this by restarting the Rosetta Node with an
appropriate block height to resync_from. However, if other services have
consumed the previously indexed data from the Rosetta Node, these will also
need to be reset.
If the Access API servers aren't properly maintained and configured for
individual sporks, then it could result in data availability issues.
Rosetta configuration JSONs must be actively reconfigured and restarted with every new spork. Flow's
sporks.json serves as the canonical resource for spork
versioning on Flow. Its important that changes to spork versions are included in the Rosetta config for the
environment delays or inaccuracies in thoseupdates, this would result in downtime.
Rosetta assumes that a transaction hash uniquely identifies a transaction
within a block. Unfortunately, in Flow, the same transaction hash can be
included multiple times within the same block by being included in different
collections.
- In responses to the
/block endpoint, we default to using just the first
such transaction in a block, and will override it if any subsequent
transaction within the same block has any operations.
Installation
# download and install dependencies
make deps
# build
make build
Running interactively in Docker
docker build -t flow .
docker run -it -v $(pwd):/app/src/rosetta-flow flow bash
# first time
make
# when making changes
make go-build
./server <path/to/some-config.json>
Environment Variables
The Flow Rosetta cmd/server