KLYNTAR Virtual Machines. Part 0: KLY-EVM — shardable, low-level managed, mutable, and WASM-improved EVM.
Tags: web3, blockchain, crypto, cryptocurrency
Published: 2023-03-05 23:39:45


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1r95uawm3p94u4turj09.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcdurkc095rnlro7twqt.png)


## Intro

Hi, we're back in touch! With this story, we start a new cycle of articles about KLY. Here we'll discuss virtual machines available on KLYNTAR. You can find much interesting information about low-level management, the workflow of VMs execution, cross-vm interaction, and so on! Also, we'll publish a set of articles about different use cases of how to write contracts, and how to use KLY features available for different VMs( EVM & WASM & others) - predeployed contracts, special logs, WORA principle, mutations, and much more!

## KLY-EVM

Definitely, the most popular VM in the blockchain industry is EVM. Projects with EVM compatibility propose a well-known set of tools for developers for a quick start with their ecosystem. You even shouldn't learn new languages or work with docs - just use solidity docs, web3 library, and life is good!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/buy2c013idse8tw0769y.gif)

Not all of the projects propose new features for their EVM port. But, it's not true in the case of KLYNTAR.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l89d4p40ptwz27ox579m.png)

## Multi-level sharding - the implementation on EVM level🔥🔥🔥

Probably, it's the hottest part of this article. As you remember from previous articles in our blog, we've discussed multi-level sharding implementation on KLY. It's here

{% embed https://klyntar.medium.com/klyntar-basics-part-0-architecture-%EF%B8%8F-fb948f89d73f %}

In the article about basics, you can get information about the first level of sharding - the whole ecosystem consists of a set of independent chains named symbiotes. Also, we're working on the next article in this cycle where you'll find out how sharding works on a single symbiote level, how the pool-per-chain principle works, and so on!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wfi21sonplpyzddpl1fp.png)


In this (still not published😅) article you can find the explanation of sharding inside a single symbiote and this illustration can help you.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvdynf89w0qc1ubevw9w.png)


Since all the subchains here are related to a single symbiote - all of them are part of a single system of accounts. Also, each symbiote will have its own _**chainID**_ (the number for the EVM registry and 256/512-bit BLAKE3 hash for the KLY ecosystem).
For example, our initial symbiote kNULL(which will be launched by KLYNTAR Team) will have 7331 as a chainID for EVM. You can see it here


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq92wah2oq33htmtulax.png)

{% embed https://github.com/ethereum-lists/chains/pull/2335 %}

So, exactly on the subchains level, we need to implement the sharding for EVM. But, to make it possible to securely bind accounts to the appropriate subchain(shard) we should dive deep into bytecodes, the workflow of EVM execution(via debugging), and so on. Should be interesting!

## General idea
Back to our picture with subchains


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9uuk86n2h4b10v8lleq.png)


_**> The simple idea of sharding on KLY is to bind accounts(default and accounts of smart contracts) to a single subchain and deny all attempts of state changes from txs from other subchains. This image can help you to understand:**_



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l76b8fbitkyj6t4uz547.png)

Here you see that if you bind your account to a subchain A this is a signal for everyone that the state of your contract or default account can be changed only via transactions via the same subchain. Why it's cool?!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuavcia46w34tepbv32t.png)


Because now, we can separate the work and explore each symbiote independently. In simple words - you can run the special software from the Bumblebee packet which will be the backend for explorers & wallets and set the subchain to track changes.

Since the block time is short and TTF(time-to-finality) is instant - you can easily get updates of state from each symbiote in a parallel way.

Sounds good and it seems we can implement it. For KLY-VM(WASM based) it's easy because all the state is changed via KLYNTAR core level and these operations are transparent for us. Even, advanced logic like cross-contract calls can be tracked, because this functionality is passed to the wasm module as an injected dependency from the JS context. For instance, here is an example from KLY-VM tests:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdugdm624d4oz60ldrl1.png)

[Here is example](https://github.com/KLYN74R/KlyntarCore/blob/4a6d798d885829378322265f007ed19ffc56a270/KLY_Tests/klyntar_vm/AssemblyScript/use.js)

In the same way, a contract changes its own state and the state of other accounts/contracts.

But what about EVM? Let's use reverse engineering skills and try to debug.

## Low-level control
To reverse the workflow of EVM on the deep level - we've built the graph of workflow. It helps us to dive into the bytecode level of executing, to see the stack changes and black-boxed values of EVM inside. This is how this graph looks like:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/do5hpdfqu28cm7xeze2b.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6niiepbe3aj1brsdksdu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dktsesnx6jfq8lrfptrf.png)

_**> Our goal is to allow changes of contract X only from transactions on subchain A. For this, we should track all the accounts whose state will be changed and revert & stop the execution in case the transaction tries to change the account state that is bound to subchain B**_

The dangerous moment here is that we should remember about so advanced stuff like cross-contract calls, delegations, and so on! We know about it😅

When EVM is going to change the state of some account the address of its will be added to the inner property _touched - this is the set of accounts whose state will be changed in a certain tx.

For demonstration, we have a repository with an example of a cross-contract call and the results of debug process. You can find & try it [here](https://medium.com/r/?url=https%3A%2F%2Fgithub.com%2FKLYN74R%2FKlyntarEVMTests%2Ftree%2Fmain%2Fbytecode_tracing_test)

Here we try to deploy 2 contracts - **_Caller_** and **_Receiver_**. Inside contract Caller.sol there is a function for cross-contract execution which calls a function inside Receiver.sol contract. We'll deploy using our account **0xbe862ad9abfe6f22bcb087716c7d89a26051f74c**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqyolc7fj6nfpxnnlasq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wx12e4fxtx55qgmu6i11.png)

After deployment, we can see these logs


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq0dip8ifbctj5y73096.jpg)

And, if we manually override the debug logic inside the EVM source code and call the function _**crossContractCallFromCallerToReceiver()**_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/429dgs97p6ijxobhnruq.png)

we can see this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5dqj01123d1uthenxir.png)

As you see - this set contains all the addresses that will be changed during operation.

This is exactly what we need because now we can override the logic of the function which add the address to this set. We need to stop execution once we detect binding to another subchain.

This function is **_touchAccount()_** inside the vmState.js module from _**@ethereumjs/vm/eei**_ package. The modified function looks like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogimor9aaargft0h0t7g.png)

In simple words, an exception will be thrown and vm will be reverted. The extra logic also contains work with mempool to allow the JSON-RPC server to accept the transactions, and test them in the sandbox with wished context(useful for load balancers, explorers, etc.) before adding to mempool(some filtration practice).



_**> So, in simple words & using examples we've demonstrated to you a proof of concept(PoC) of future KLY-EVM.**_


## Mutations principle


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05qmhnq0z6ia29nxsi0s.gif)

We wouldn't be ourselves if we didn't go even further. During research work, we noticed tons of interesting moments of EVM, and going to add them to KLY-EVM.

Since KLYNTAR is a mutable project, you'll have the ability to add mutations for EVM workflows.

For example, the stack tracing looks like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4l2zdj11vxi4ij1bb1y.png)

The _**interpreter.js**_ has the following code:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2dnqox6a256y43fxmw6.png)

…and opcodes list looks like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s63skymrw0jcvwxp3p9f.png)

**_> What it gives us? Why it's cool and what we can do with it?_**


We'll with this low-level control we can create different custom mutations of EVM - with different gas prices for opcodes, custom opcodes, custom inner logic, change the execution context for different contracts & addresses, use the off-chain functionality, and much more! It's like having a super-powerful debugger and doing everything that you want.

We promised you that KLY-EVM is a gigachad. Hope, we proved it)

## WORA - write functionality for EVM with WASM

It will be the topic of one of the next articles, but since we have a WASM-compatible VM on the board, we should remind you about the ability to use WASM modules for EVM.

Long story short, we assume that it will be worked as a callback mechanism where you stop the EVM execution, call the WASM module, and return to EVM execution with new/extra values.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwmp5ck635opcte0iu7m.png)

But wait for the article)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnt7pgo2iscwoeo5xw8t.gif)

## Summary

Damn, this article looks so hot! Detailed, simple, with examples — perfect! We’re working to make KLY the leading project. Not only in the crypto industry. But too.

With KLY-EVM we started another cycle of articles in our blog. Hope, you find it interesting. We’ll try to publish such articles often to get you a detailed explanation of all the components of KLY ecosystem.

Just imagine your first time launching the KLY node🤩

## Links

1. http://klyntar.org
2. https://t.me/KLYN74R
3. https://github.com/KLYN74R
4. https://mastering.klyntar.org
5. https://ru.mastering.klyntar.org
6. https://www.reddit.com/r/KLYN74R/
7. https://discord.com/invite/f7e7fCp97r
8. https://github.com/KLYN74R/KlyntarEVM