Fruzhin icon indicating copy to clipboard operation
Fruzhin copied to clipboard

refactor(block execution)!: showcase a MWE with explicit `Context` object

Open David-Petrov opened this issue 1 year ago • 1 comments

Description

Decouple the Spring app from runtime invocations. This PR showcases a variant of this decoupling using an explicit Context object to pass to the runtime builder, in which object all necessary dependencies for the host API endpoints are contained, thus granting the caller the ability to define all interactions from the runtime to the outside world through the behaviour of these dependencies.

The main goal of this MWE is to execute the first block in Kusama without ever starting the Spring app (i.e. in isolation). In more practical terms: to rewrite the previously existing BlockExecutorTest, and the new variant can be found in BlockExecutorIsolatedTest. The diff between the two tests is the starting point for grasping all the changes.

The bulk of refactoring is isolated in the package com.limechain.runtime.research.hybrid, where I've copied a lot of existing classes in an isolated environment and made changes on the copies, since applying changes in the already existing production classes quickly proved to be non-viable for the purpose of achieving a MWE.

Breakdown on the main changes within com.limechain.runtime.research.hybrid:

  • org.wasmer.Memory and FreeingBumpHeapAllocator have been put behind interfaces;
  • TrieAccessors are now injected the TrieStorage dependency on construction;
  • regarding the host api:
    • an Endpoint enumeration of all host api endpoints has been introduced, holding "just data", i.e. the signatures of the host api endpoints;
    • a HostApiImpl class is introduced to explicitly hold implementations of the endpoints and to simplify creation of ImportObjects exhaustively;
  • Runtime and RuntimeBuilder have been renamed to WasmRuntimeInstace and WasmRuntimeInstanceBuilder (names are WIP) and now obtain an explicit Context object on construction;

Other notable changes (outside the hybrid package) which were necessary to achieve this decoupling:

  • remove the BlockState field of TrieStorage, as it breaks separation of concerns;
  • TrieStorage is no longer a static singleton instance, as it is an implicit member of the Context (through the TrieAccessor);
  • [SIDE FIX / QUESTIONABLE] The runtime builder is now used for the runtimeVersionV1 endpoint instead of manual instantiation. More details at this commit: 21547d7005434d58ac867b4bca050219a99987fc.

David-Petrov avatar May 21 '24 08:05 David-Petrov

Quality Gate Failed Quality Gate failed

Failed conditions
22.8% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

sonarqubecloud[bot] avatar May 21 '24 09:05 sonarqubecloud[bot]

Closing this as already implemented in #456.

David-Petrov avatar May 31 '24 13:05 David-Petrov