- Blockletter
- Posts
- Optimistic Execution in the Cosmos SDK
Optimistic Execution in the Cosmos SDK
Things are looking more and more Optimistic in Cosmos and for the Cosmos SDK
In the ever-evolving landscape of blockchain technology, efficiency and speed are two factors that developers constantly strive to improve. Optimistic Execution (OE), a groundbreaking concept, is pushing the boundaries of block time and efficiency in Cosmos SDK chains. In this article, we'll delve into the world of Optimistic Execution, explore its implications, and understand how it works.
Optimistic Execution: A Game Changer
Optimistic Execution, often abbreviated as OE, is a revolutionary technique that allows Cosmos SDK chains to process blocks ahead of time. This approach aims to provide a swift response when requested by the consensus layer. It's "optimistic" because it begins block execution even though there's a possibility of rejection at a later stage, although such rejections are rare in most cases.
Reducing Block Time
One of the most significant advantages of Optimistic Execution is its potential to reduce block times substantially. In load tests on Sei Network, the implementation of OE slashed block time by approximately 300 milliseconds. For networks that produce blocks rapidly, like Injective, OE can facilitate the generation of sub-one-second blocks without additional effort.
Introducing Optimistic Execution
Before the introduction of Optimistic Execution, Cosmos SDK applications executed blocks and their transactions in the final step of ABCI FinalizeBlock. This resulted in the application receiving the complete block proposal only after all voting had concluded, leading to extended block times.
The introduction of ABCI++ brought a paradigm shift. With this update, the application layer gains access to the block proposal before the voting period commences. This allows for the optimistic execution of the block proposal in parallel with the voting process, effectively reducing block times.
The Need for Speed
The speed enhancement offered by Optimistic Execution is a result of the reduced average block time, which is calculated as P + Q - max(P, Q), where P represents the average voting period, and Q represents the average block execution time. In the case of Sei Network, P was approximately 600 milliseconds, and Q was about 300 milliseconds, resulting in a reduction of block time by approximately 300 milliseconds.
How It Works
CometBFT v0.38 introduced ABCI++, which includes new calls between the consensus layer and the app layer. One such call is ProcessProposal, which allows for application-dependent work on a proposed block. This early access to the block's contents enables optimistic execution.
The process involves starting a goroutine in ProcessProposal, which executes FinalizeBlock ahead of time. As CometBFT initiates the pre-commits round, FinalizeBlock is called, and the goroutine's result is awaited. In the best-case scenario, the goroutine has already finished, resulting in minimal delay.
Abort Mechanism
Optimistic Execution incorporates an abort mechanism for exceptional cases. An abort event is called in two situations: whenever ProcessProposal is called, the OE process is unconditionally aborted, and in FinalizeBlock, an abort occurs if the hash in the current request does not match the executing block's hash. Abort events are designed to ensure the app continues to function without errors or panics.
The worst-case scenario, albeit rare, involves a node reaching FinalizeBlock while executing the wrong block, leading to an abort and reprocessing of the block.
Future Improvements
The development team is actively exploring avenues to enhance Optimistic Execution further. This includes reducing the time required to abort a running execution. Potential improvements may involve making modules aware of a cancellable context, caching necessary context for block execution, and implementing better handling and abort mechanisms through channels.
Optimistic Execution is poised to revolutionize block processing in Cosmos SDK chains. By reducing block times and enhancing efficiency, it paves the way for faster and more responsive blockchain applications. As the Cosmos SDK community continues to evolve and innovate, Optimistic Execution is a remarkable step towards a more efficient and agile blockchain ecosystem. To delve deeper into this technology, don't forget to check out Cosmos SDK's RFC005 for more information.