buckyos / documents

BSD 3-Clause "New" or "Revised" License
1 stars 3 forks source link

1.3 Introduction CYFS for Developers: Core components and functional modules #14

Open streetycat opened 1 year ago

streetycat commented 1 year ago

Core components and functional modules

I will introduce the distribution of project source code files, and then introduce the main core modules one by one.

Directory specification

The CYFS project directory is expanded as follows:

/--CYFS
    |--doc  // formal design documents
    |--scripts // some scripts for build/publish...
    |--src // the source code, We will specifically introduce the source code directory
    |--... // other files for solution: .gitignore, .eslintrc, package.json, .etc

All modules are divided into several categories and collected into corresponding directories in src:

  1. 3rd The main code comes from an external project, but with a few custom modifications.

  2. component Basic functional modules, most of which belong to this type, each implements its own functions, is referenced by other runnable instances, and has interdependence internally.

    If your module will be depended on by other modules, it should be collect in component. If part of your module will be depended on, you should split the part into a separate module.

    As examples: cyfs-base: It's the most basic module,Almost all modules depend on it. cyfs-core: It defines all normalized objects cyfs-util: Some auxiliary modules

    I will introduce them in other issues.

  3. meta The modules for MetaChain, it's just a conceptual version? I haven't dabbled in this part yet, I will introduce it in future.

  4. service Services in CYFS system, They run as independent processes on various CYFS devices.

    I will introduce them in other issues.

  5. tests

    Some test cases and sample programs. We can help to supplement and improve, it's a good entrance to enter the CYFS world, and contribute to the cause of Web3.

  6. tools

    Some tools to help users/developers to use the CYFS. We can design more useful tools.

  7. misc

    Others.

Services

  1. Devices

As introduced here, there are 2 types of devices in CYFS for users and developers. They are OOD(gateway) and runtime.The relationship is as follow:

Zone A <--> Zone Other
runtime A1 <--> OOD(gateway) A OOD(gateway) O <--> runtime O1
runtime A2 <--> <--> runtime O2
runtime ... <--> <--> runtime ...
runtime An <-->
  1. Services

In a zone, several services is running on the OOD always. These services are running depend on the gateway, the gateway is also a service, It is equivalent to nginx for http, receive and forward the requests to the target services.

gateway <--> app-manager
<--> chunk-manager
<--> file-manager
<--> ...
  1. DECApp

By the way, the server of a DECApp is also running as the services on OOD. They listen to requests from clients through the gateway and respond.

Thanks @weiqiushi for the correction

cyfs-base

This is the most basic component, and almost all other components depend on it. It implement the follow functions:

  1. The basic types or structures:
  1. The framework for encode/decode:
  1. Encapsulation of the crypto library:
  1. The 14 standard objects:
  1. Other structures for object
  1. cyfs-base-derive

Some usefull macro is implement in this crate:

  1. cyfs-base-meta

The basic structures for MetaChain.

cyfs-core

I mentioned 4 object categories in previous part. The Core object is collected in the crate cyfs-core.Many object types have been collected, most of those support the functions of the current CYFS system.

  1. common
  1. app

    It supports the running of the system service AppManager,Various related entity objects and behavior objects are defined here.

  1. group

There is a issue for it.The core objects for group is implemented in cyfs-core.

We use the blockchain structure to record the state evolution process of the Group.The consensus algorithm is Hotstuff.

  1. nft

    NFT is a popular application scenario in recent years.CYFS collect and defined its own NFT objects.In theory,any named data is NFT in CYFS,but most NFTs are files in fact.So NFT is limited to file types.

    • NFTList Is a file list:
    • nft_list: immutable, file list.
  2. trans

I don't know this part?

  1. zone It's the manager of the devices belonging to the same owner.

CyfsStack(cyfs-stack/cyfs-stack-loader/cyfs-lib)

CyfsStack integrates all functional modules provided by the CYFS system.Any service or DECApp will call the CyfsStack to use the CYFS. There are 2 solutions:

RPC

  1. Protocol for RPC
  1. Framework of RPC

There are client and service for RPC, the service is CyfsStack, and the client is SharedCyfsStack.

client service
Implement in SharedCyfsStack Implement in CyfsStack
Multiple instances Only one instance for one device
Open in any DECApp process Start with gateway(for OOD) or cyfs-runtime(for other devices)
Public for developers of DECApp Invisible for developers of DECApp
Module named by cyfs-${function-name}-lib Module named by cyfs-${function-name}
  1. Data flow diagram

The data will flow from the DECApp to the processor in service.

It's flow as follow:

graph TB
    subgraph DECApp
        User-->Interface[SharedCyfsStack: function with FunctionOutputRequest]
    end

    Interface-->FindTheTarget

    subgraph SharedCyfsStack
        FindTheTarget[find target device]-->RPC.send[RPC.send]
    end

    RPC.send.->RPC.recv
    RPC.send.->OtherDevice

    subgraph CyfsStack
        RPC.recv-->ObjectHttpListener
        ObjectHttpListener-->FunctionRequestHandlerEndpoint[FunctionRequestHandlerEndpoint.call]
        FunctionRequestHandlerEndpoint-->FunctionInputProcessor[FunctionInputProcessor.call_function]
        FunctionInputProcessor-->FindNextProcessor[output_processor = get_forward]
        FindNextProcessor-->InputOfOtherModule[input_processor]
        InputOfOtherModule-->NextN[next...]
        NextN-->Ending[ending_input_processor.call_function]
    end

    FindNextProcessor.->OtherDevice

    subgraph OtherDevice.CyfsStack
        OtherDevice[Other CyfsStack in different device]
    end

    OtherDevice.CyfsStack-.eq.-CyfsStack

We can integrate our own modules into the CYFS protocol stack according to the above framework.and you can also do it follow the issue step by step.

The crates

  1. cyfs-stack

It implements the CyfsStack and all processors to route the data for each module.It's inaccessable immediatly for DECApp, so it will never be depended on by any DECApp.There are some directories named in ${function-name} or ${function-name}_api.They are implemented with the function module:

  1. cyfs-lib

It implements the SharedCyfsStack and some interface for the default function modules.It's the entry interface for DECApp to call the CyfsStack.So,most DECApp will depend on this crate.

If you are extending new functionality to CYFS,It's suggested to make a new crate named in cyfs-${function-name}-lib to instead of implement in cyfs-lib,you can export a interface to initialize your module with the SharedCyfsStack.

  1. cyfs-stack-loader

There are so many parameters to initialize the CyfsStack, You are suggested to initialize the CyfsStack with cyfs-stack-loader if you want to initialize yourself, for example: