KasMedia Logo

In the ever-evolving landscape of technology, innovation isn't just a desirable trait—it's a necessity for survival. Just as the ancient proverb warns that without vision, people perish, so too do tech projects flounder and fade without a constant drive for innovation. This can be seen in so many technology companies that were at the top of the industry at one time but failed to innovate and are no longer the premier in their field. A great example of this can be seen with companies like Kodak, which at one time was a powerhouse for film photography- and the go-to brand for all related products- cameras, video camcorders, film. While the company developed the first digital camera, they did not pay much attention to it and completely missed the revolution from film to digital photography. They are still around today, but filed for bankruptcy in 2012. 

There are also plenty of examples of how a lack of innovation can destroy a company in the digital assets space. One example of a cryptocurrency that had a top market cap and then faded due to a lack of innovation is Peercoin (PPC). It was one of the earliest cryptocurrencies introduced in 2012 and gained attention for being the first to use a combination of proof-of-stake and proof-of-work mechanisms. Initially, Peercoin garnered significant interest and achieved a relatively high market cap. However, over time, it struggled to keep pace with newer cryptocurrencies that introduced more advanced features and scalability solutions. The lack of substantial updates and innovation in Peercoin's development contributed to its decline in popularity and market relevance. While Peercoin still exists today, it has significantly diminished in terms of market cap and overall impact compared to its earlier years when it was considered one of the top cryptocurrencies. We are also witnessing a new class of proof-of-work zombie currencies such as Bitcoin Cash, Litecoin, and Ethereum Classic, which have refused to innovate beyond chain-based technology.

Kaspa, a pioneer in their proof-of-work protocol, understands that innovation is imperative to success. It never succumbs to the skeptics who say, "It can't be done." Instead, Kaspa embodies the spirit of relentless innovation, pushing boundaries and shaping the future of technology. The developers are working on upcoming protocol improvements in the current pipeline, Ghost, GhostDag, and DagKnight. Soon, they will launch smart contracts, KRC-20 tokens (similar to the Ethereum ERC-20 NFT token), and more. Most recently, the team has launched the mainnet code rewrite from the GoProgramming Language (Go, GoLang) to Rust. Let's dive in to learn more about what that means for Kaspa.

Why Kaspa in Rust?

Kaspa developer Michael Sutton first introduced the code rewrite in Rust Language in July 2022 in the official Kaspa Discord, and it was approved as the first Kaspa Improvement Proposal (KIP- 001). According to Sutton, the main reason for the code rewrite was to increase the block rate beyond 1 block per second (BPS), as this was not possible due to the system architecture of the preexisting code in Go. 

In addition to increasing the blockrate speed, migrating the Kaspa codebase to Rust also incorporates the following features: 

1. Scalability towards local node resources as the Rust codebase is built to more efficiently utilize compute resources, by using all the available CPU threads in parallel. Wherever possible, parallel processing is used, which allows it to scale beyond 10 BPS. 

2. Improved pruning due to the addition of a new algorithmic component to the current reachability algorithm that facilitates the dynamic removal of structural Directed Acyclic Graph (DAG) data, which enables real-time header pruning.

3. Highly performant while remaining low-level and optimized. Rust is only 10% less efficient than the C programming language, while still offering high-level programming constructs that are not available in C. 

4. Increased speed through alleviating software bottlenecks through the improved Rust pruning.

5. Enhanced security and safety that comes from the Rust language in preventing programmer errors on compile time coming from memory management and multi-threading bugs. 

The community raised 100M KAS to fund the rewrite, which is known in documentation as Rusty Kaspa. It has taken roughly a year and a half for a team of about fifteen core developers to complete. According to Sutton, referring to the process as a code rewrite is a bit of an understatement, as described below: 

Rust was a wide cross-kaspa-components change and redesign. It's important to note that the word "rewrite" is misleading. A simple rewrite to the current system would've been done months ago. But we noticed there's no point in stabilizing 1bps rust when the real goal was 10bps and beyond. As @BubblegumLtng wrote, once I noticed the magnitude of work required for the "incremental" increase to 10bps I made lots of efforts to increase the core dev circles (possibly being less efficient short term).

The testnet has been running successfully with no major issues for the past month. 

What is the Rust Programming Language?

The Rust programming language was created in 2018 to optimize performance, increase reliability, reduce hardware demands, enhance security, and offer more opportunities for developers. While Go was optimized for quicker compilation, Rust was created for faster execution. Rust is one of the fastest and most memory-safe languages, and its modular code is interoperable with other languages. Rust is considered a system-level programming language, like C++, which allows developers to control nearly all memory-related aspects. In contrast to C++, Rust is a more concise language, decreasing developers' time writing code. According to a Stack Overflow survey (2023), Rust is the preferred coding language for 80% of developers. It enables developers to discover more bugs when deployed through its robust error handling and has a higher probability of implementing the code as expected (Bitfield Consulting). Popular software Firefox, Dropbox, and Cloudflare are all written in Rust. 

With all of the benefits, Rust is a natural choice for the Kaspa codebase because it optimizes many parallel DB reads, which is exactly what happens when verifying a highly-parallel DAG like Kaspa. 

About Kaspa Calculations

For a simplified description, there are three main steps to pipeline design in Kaspa. First, the header is processed through header validation to ensure correct references, sequencing, and work completion. In Kaspa, the header includes things related to the network, known as a directed acyclic graph (DAG), such as timestamp, difficulty requirement, sequencing, nonce, etc.  

The next step includes block validation to confirm that transactions parse properly and have the correct input-output structure while processing the block data. In this step, we do not check whether the used UTXOs actually exist, as the network can process transactions with non-existing UTXOs due to the transaction Merkle tree in each block containing all accepted transactions in the entire merge set of the block and not just in the block itself. Blocks not containing UTXOs would result in a "valid but illegible for chain" block status and would not be appended to the chain.

The final step in the process involves the money state of the node, which reveals the Kaspa balance for each address. Kaspa, like Bitcoin, is a UTXO chain where purchases are completed using previous transaction outputs or wallet balances, known as unspent transaction outputs (UTXOs). Transactions are processed as a series of inputs and outputs to the UTXO. When blocks are added to the blockchain or DAG ledger, this is at the UTXO level. The Kaspa code establishes a computational pipeline where each function can be performed independently from one another and can utilize the multicore hardware for parallel validation. Performing these functions in Rust allows for enhanced modularity of the code, with a more efficient codebase. 
 

Higher Efficiency and Tighter Performance

Due to consensus stack improvements (GHOSTDAG and DAGKNIGHT), security is no longer a bottleneck to increasing block speed. Rusty Kaspa was written to be as efficient, with less bottlenecks. 

The Rust language is widely recognized for its high efficiency and support for parallel computation. It is particularly advantageous for Kaspa, given its rapid block rate and concurrent transaction processing. Kaspa can optimally leverage Rust's robust parallel computation capabilities and efficiently utilize hardware with simultaneous compute threads. This reduces hardware requirements with the ability to run on basic computers with four core processing units, a disc space of 200 GB, and a basic internet connection. This is evidenced by Kaspa testnet-11 achieving a rate of 10 blocks per second on hardware costing roughly $150. In fact, benchmarks indicate reliable support for up to 32 blocks per second on standard hardware. In addition to the testnet easily handling 10 BPS, it shows 50,000 transaction validations a second. The time required to validate transactions is not dependent on computational capacity. This is contrary to Bitcoin and makes mining accessible to tech-nascent users.

Rusty Kaspa utilizes low-level memory management, meaning that the developers control memory management. It does not use garbage collection like Go. With the garbage collection algorithm, memory builds up until the garbage collector runs, and if the collector does not run often enough, overflow occurs. This performance difference is negligible on a small scale but is most significant as the number of computations increases. 

Another update implemented in Rust rewrite that will increase performance is the enhancement to the ‘Difficulty Adjustment Algorithm’, proposed in the Kaspa Improvement Proposal (KIP-003). 

Higher Speed (BPS and TPS)

Kaspa was designed for parallel computation, which is the ability to process simultaneous blocks in parallel on diverse threads inside the same CPU. This breakthrough algorithm resulted in higher blocks per second while keeping the same block size, which increases transactions per second and ultimately results in faster throughput. 

Developer Michael Sutton stated that 100 BPS is a reasonable target. In theory, the only limitation to TPS would be the processing power. Increasing the speed of transactions per second will greatly enhance the user experience for sending and receiving funds and interacting with layer-2 applications. It is important to note that unlike Bitcoin and Ethereum, an increase in TPS does not diminish the network's security. 

Simplified and Modular Codebase

The rewrite aims to make the codebase extensible and more accessible so new community members outside of the core team will feel comfortable making changes to all areas of the codebase, not just the external realms. Rusty Kaspa allows developers to add new features and upgrades modularly, so they can easily add to the Kaspa codebase without editing the existing codebase and also utilize it to develop their own applications without worrying. 

Rewriting the code more efficiently also allows for more robust and improved documentation of the various sub-protocols and algorithms in the system, which can encourage more developers outside the core team to contribute to the project. As Rust already has a large and mature crypto ecosystem, developers can build faster with less but more secure code. More developers means more applications will be built on Kaspa. 

Optimized Data Storage and Pruning

Blockchain data pruning is the process of continually eliminating data that is no longer needed. In the case of Kaspa, this includes block data older than 24 hours and blocks pointing below the pruning block. Kaspa keeps the oldest UTXO stored block. 

Data storage and collection are major issues when it comes to blockchains, as more storage usage negatively affects the speed and efficiency of the network. With Go, data storage would accumulate at roughly 1 BPS * 1MB/block, equating to roughly 37TB of data per year. 

Kaspa in Go (Kaspad Golang) did not support any concurrency, meaning that logic related to the core consensus ran sequentially. As pruning the block data is part of the consensus logic, this action would have to be completed before moving on to the next step. With Go, there would be a few seconds to minutes, depending on the transaction load, where the block templates were not being updated. During this time, mining would be paused, while the garbage collector would run to conclude pruning. This would result in a queue of parallel blocks that were all unaware of each other, known as an “anti-chain” of blocks. Some of the blocks created would result as red blocks, and would not be rewarded, due to the K=18 block limit. 

With Rust, all of the parallel tasks can process at the same time, and block templates can be updated during the pruning process. This produces dynamic caching, prevents out-of-memory issues and increases network resiliency, even at high throughput.(Kaspa YouTube). 

Optimized Mempool and Header Pruning

Having an optimized mempool relates to updating block templates as quickly as possible, and plays a critical role in ensuring that the network operates seamlessly. In Kaspa, when constructing a new block template, the node checks the mempool for ready transactions. If the mempool is strained and cannot provide the transactions, it slows down the entire network and results in higher artificial latency between blocks. This could also result in duplicate transactions being mined.

Block reachability is related to how the nodes are connected within the DAG, which is stored within the header. Rusty Kaspa utilizes a new sub-component, where each new node is not required to keep the complete ledger of connections. Instead, Kaspa uses an algorithm inspired by this 1989 paper, “Efficient Management of Transitive Relationships In Large Data And Knowledge Bases,” to ensure that each node only retains essential information for the network. 

Kaspa has incorporated header pruning since its inception and always had on-the-fly Header Pruning on the bucket list. On-the-fly header pruning automatically removes all data over three days old from the Kaspa P2P node (KASPAD) - including headers and other data structures. This is enabled due to the new reachability sub-component. 

Parallel DB Reads

Rust allows parallel DB reads by design due to the concurrent design of the Rust codebase, which allows it to handle various storeting semantics with fine granularity. In Go, the database used was leveldb, while the database used in Rust is rocksdb. Rocksdb offers more advanced features like secondary process read-access to the same DB, enabling external access to the DB without requiring the node to be shut down. 

Rust Security

Rust verifies that the compiled code is sound, ensuring that the code does what the programmer wants. Unsafe Rust, on the other hand, allows programmers to perform workarounds not available in Safe Rust. Programs written in Rust are less likely to have bugs due to its borrow checker. Additionally, Rust is known as a memory-safe programming language (MSL), which removes weaknesses due to memory safety, accounting for most of the majority of code vulnerabilities. In December 2023, the United States Cybersecurity and Infrastructure Security Agency, United States National Security Agency, United States Federal Bureau of Investigation, and other international organizations singled out Rust as a preferred MSL in their publication, The Case for Memory Safe Roadmaps. The updated code also features a long-term solution detailed in KIP-009, to protect users against dust attacks.

Smart Contract Development

Rewriting the Kaspa codebase in Rust is beneficial to future smart contract development as the code is composed with greater robustness and modularity. Additionally, Rust compiles to WebAssembly (known as WASM), which is like a porting station that allows web deployment of any language that compiles to WASM. Wasm is becoming the standard in web technology, receiving substantial backing and increasing adoption from major corporations, browsers, and developers. This allows programmers to code their performance-critical software in a performance-critical language, like Rust, and deploy it anywhere. Unlike previously, when the code would have to be written in javascript to deploy on the web. 

The Rusty Kaspa WASM frontend framework opens the door for innovative development on Kaspa. It also enhances application security as there are no external libraries, so it cannot process arbitrary code.

What’s Next?

Rust is a performance-oriented programming language that is already popular in the cryptocurrency space. Top market-cap cryptocurrencies, including Solana (SOL), Near (NEAR), and Polkadot (DOT), are coded in the Rust language. This lays the groundwork for developers to build and implement smart contracts in the Kaspa ecosystem. Smart contract implementation opens unlimited opportunities in decentralized applications (D’apps) (Developer Nation). Developer Michael Suttoneven posted about Kaspa working with DecentralizedFinance (DeFi) apps: “Some of us believe that extremely high block rates enabled by Kaspa can be a game changer in the defi world.”

Users can watch the network create blocks here. This is a very exciting time for Kaspa, bringing it one step closer to mass adoption and revolutionizing the financial system one payment at a time. 

Additional Sources

Enjoyed reading this article?

More articles like this

Comments

No comments yet!

Post a comment

KasMedia logo