The Power of TDD and A Good Test Case

Django memang bukan framework yang menawarkan kemudahan concurrent programming, asynchronous programming , atau object-oriented programming. Django menawarkan kemudahan dalam melakukan TDD yang pada…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to add a new blockchain into Gravity

In order to extend the Gravity Protocol by adding support for a new blockchain, a sequence of several commits is expected into Gravity-Tech/gravity-core and a few other Github repositories mentioned throughout this article. These source modifications can be divided into four parts:

Furthermore, to conduct a cross-chain swap of a token from one blockchain to another, no additional tokens are required, except for the native tokens of the corresponding blockchain networks.

Below is the description of SuSy cross-chain transfer algorithm. It shows a transfer of a token from ORIGIN-CHAIN ​​to DESTINATION-CHAIN, where it will be issued as a swT (wrapped) token and sent to the recipient R in DESTINATION-CHAIN.

SuSy origin-destination cross-chain transfer scheme

A user (S) interacts with the LU-PORT smart contract by transferring an amount (A) of the T token to it and specifying the recipient’s public address in DESTINATION-CHAIN. The gateway smart contract automatically creates a unique SWAP-ID and sets the registered status. The received funds are blocked on the LU-PORT smart contract.

Information about this event is handled by extractors, the Gravity network’s services that process the received data and communicate it to Gravity. From the Gravity framework, the oracle moves hashed data about the new SWAP-ID and swap directions to the verification contract (NEBULA-SC), in which the signatures of the Gravity network validators and the legitimacy of the transferred context are verified.

Upon verification, the SEND-DATA-TX transaction is called, containing a set of data and instructions for issuing and sending swT tokens to the recipient (R).

Likewise, all data about this event is handled by Gravity network oracles, and, contingent upon successful execution, the “processed” status is set. After reaching a certain number of blocks at which the likelihood of a fork is minimal, it may be necessary to set the finalized status.

In the opposite direction, for transferring the swT token from DESTINATION CHAIN ​​to ORIGIN CHAIN ​​and unlocking T on the LU PORT contract, the procedure is similar. The only difference is in the final transactions, that is, the burning of the swT token on IB PORT and unlocking the T token on LU PORT, are reversed.

As regards the extension of SuSy dApp onto other blockchains, USER-SC plays the role of IB (Issue-Burn) and LU (Lock-Unlock) ports. USER-SC methods are meant to be invoked by NEBULA-SC. So, in order to have a working application on top of the protocol, one needs to:

To ensure compatibility with a new chain, a set of separate smart contracts is required. More importantly, there are two types of contracts: those used by the core of Gravity and application-specific contracts (e.g. SuSy).

Examples of an IB Port and a LU Port for Ethereum and Waves can be found here:

Two new smart contracts should be implemented in the new blockchain network with the following functionality:

For all statements 1–4, an open and public API must be implemented.

After creating the contracts, they need to be compiled into bytecode. The compiled bytecode files should be put into the “/abi” directory. Consider checking the “contracts” directory inside the Gravity-Tech/gateway repository to see examples of gateway contracts’ source code.

Key points to keep in mind:

In the createApp() function, a constructor for instantiation of a new adaptor should be provided:

An important part that relates to communication with a specific chain is the IBlockchainAdaptor interface:

Let’s review how the Ethereum adaptor meets the interface’s requirements.

First, we declare blockchain-specific types and a struct:

Ethereum implementation contains:

It is also necessary to implement a constructor function, such as NewEthereumAdaptor() below:

Let’s explore the GetHeight() and Sign() methods. The first one retrieves blockchain height and the second one implements bytes signing:

WaitTx must block the thread. A simple for-select pattern allows for handling cases where waiting for a transaction is required. Be cautious: queryTicker is used here to prevent a goroutine leak, so the 3 seconds timeout is hardcoded:

PubKey() resolves the adaptor’s public key. ValueType() instantiates a Nebula-SC using a corresponding ABI. Next, it calls DataType() in order to resolve the value type that the Nebula works with (Gravity supports Byte, Int, String types).

The AddPulse() implementation contains many technical details. Most importantly, this method performs:

SendValueToSubs() method is responsible for sending values to Nebula’s subscribers. Nebula is in one-to-many relation to subscribers.

SetOraclesToNebula() updates oracles of the Nebula. Signing and public key verification is mandatory:

SendConsulsToGravityContract() is similar, but it is used for consuls and in the context of Gravity contracts.

SignConsuls(), as well as SignOracles(), is responsible for signing the updated consuls/oracles. Both methods use practically the same algorithm.

LastPulseId() resolves the id of the last pulse of the Nebula, which is the last action’s ID performed by Nebula oracles. This method is crucial for Gravity oracles, because it is commonly used in iterators which compare current and previous pulses.

LastRound() determines the latest action in Gravity network. A round, essentially, is a specific index of blockchain height, where Gravity network mutated the state either through oracles/consuls update, score update, or pulse sending.

RoundExist() verifies whether a certain round exists.

In this file, a constant should be instantiated. In other places, where existing constants are mentioned, this new constant should be added as well:

This module is responsible for Nebula logic.

Here, a chain behaviour that relates to account address instantiation is specified.

This module is responsible for parsing of consul keys.

A crucial part of Gravity Oracle. node.go, as well as the entire oracle/node module, describes existing implementations and sets up oracle constraints.

Add a comment

Related posts:

On Aging

Aging is a mystery we can’t solve no matter how much we might desire to do so. We just live it, if we’re lucky. Although it might not always feel so lucky. But maybe, if we could hear the honest…

Un nuevo lugar.

Tendemos a pensar que molestamos a las otras personas con nuestros problemas, y a la vez tendemos a minimizarlos y pretender que los demás le van a restar importancia o en el mejor de los casos los…

3 Top Work From Home Tips

There are now two ways this can go. One option is that you work like so many other people who are confined to home offices. That means you’ll spend a huge chunk of your day procrastinating before you…