Take one down, patch it around,

127 little bug in the code...
This commit is contained in:
Pavel Murygin
2023-02-08 01:47:35 +04:00
parent b84336a92d
commit ba0ed1be72
67 changed files with 290 additions and 204 deletions

View File

@ -0,0 +1,29 @@
{
"name": "@fluencelabs/interface",
"version": "0.1.0",
"description": "Interfaces",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"engines": {
"node": ">=10",
"pnpm": ">=3"
},
"type": "module",
"scripts": {
"build": "tsc"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {},
"devDependencies": {
"@multiformats/multiaddr": "11.3.0",
"@fluencelabs/avm": "0.35.3"
}
}

View File

@ -0,0 +1,258 @@
type SomeNonArrowTypes = ScalarType | OptionType | ArrayType | StructType | TopType | BottomType;
export type NonArrowType = SomeNonArrowTypes | ProductType<SomeNonArrowTypes>;
export type TopType = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'topType';
};
export type BottomType = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'bottomType';
};
export type OptionType = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'option';
/**
* Underlying type of the option
*/
type: NonArrowType;
};
export type NilType = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'nil';
};
export type ArrayType = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'array';
/**
* Type of array elements
*/
type: NonArrowType;
};
/**
* All possible scalar type names
*/
export type ScalarNames =
| 'u8'
| 'u16'
| 'u32'
| 'u64'
| 'i8'
| 'i16'
| 'i32'
| 'i64'
| 'f32'
| 'f64'
| 'bool'
| 'string';
export type ScalarType = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'scalar';
/**
* Name of the scalar type
*/
name: ScalarNames;
};
export type StructType = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'struct';
/**
* Struct name
*/
name: string;
/**
* Struct fields
*/
fields: { [key: string]: NonArrowType };
};
export type LabeledProductType<T> =
| {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'labeledProduct';
/**
* Labelled product fields
*/
fields: { [key: string]: T };
}
| NilType;
export type UnlabeledProductType<T> =
| {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'unlabeledProduct';
/**
* Items in unlabelled product
*/
items: Array<T>;
}
| NilType;
export type ProductType<T> = UnlabeledProductType<T> | LabeledProductType<T>;
/**
* ArrowType is a profunctor pointing its domain to codomain.
* Profunctor means variance: Arrow is contravariant on domain, and variant on codomain.
*/
export type ArrowType<T> = {
/**
* Type descriptor. Used for pattern-matching
*/
tag: 'arrow';
/**
* Where this Arrow is defined
*/
domain: ProductType<T>;
/**
* Where this Arrow points to
*/
codomain: UnlabeledProductType<NonArrowType> | NilType;
};
/**
* Arrow which domain contains only non-arrow types
*/
export type ArrowWithoutCallbacks = ArrowType<NonArrowType>;
/**
* Arrow which domain does can contain both non-arrow types and arrows (which themselves cannot contain arrows)
*/
export type ArrowWithCallbacks = ArrowType<NonArrowType | ArrowWithoutCallbacks>;
export interface FunctionCallConstants {
/**
* The name of the relay variable
*/
relay: string;
/**
* The name of the serviceId used load variables at the beginning of the script
*/
getDataSrv: string;
/**
* The name of serviceId is used to execute callbacks for the current particle
*/
callbackSrv: string;
/**
* The name of the serviceId which is called to propagate return value to the generated function caller
*/
responseSrv: string;
/**
* The name of the functionName which is called to propagate return value to the generated function caller
*/
responseFnName: string;
/**
* The name of the serviceId which is called to report errors to the generated function caller
*/
errorHandlingSrv: string;
/**
* The name of the functionName which is called to report errors to the generated function caller
*/
errorFnName: string;
}
/**
* Definition of function (`func` instruction) generated by the Aqua compiler
*/
export interface FunctionCallDef {
/**
* The name of the function in Aqua language
*/
functionName: string;
/**
* Underlying arrow which represents function in aqua
*/
arrow: ArrowWithCallbacks;
/**
* Names of the different entities used in generated air script
*/
names: FunctionCallConstants;
}
/**
* Definition of service registration function (`service` instruction) generated by the Aqua compiler
*/
export interface ServiceDef {
/**
* Default service id. If the service has no default id the value should be undefined
*/
defaultServiceId?: string;
/**
* List of functions which the service consists of
*/
functions: LabeledProductType<ArrowWithoutCallbacks>;
}
/**
* Options to configure Aqua function execution
*/
export interface FnConfig {
/**
* Sets the TTL (time to live) for particle responsible for the function execution
* If the option is not set the default TTL from FluencePeer config is used
*/
ttl?: number;
}
export const getArgumentTypes = (
def: FunctionCallDef,
): {
[key: string]: NonArrowType | ArrowWithoutCallbacks;
} => {
if (def.arrow.domain.tag !== 'labeledProduct') {
throw new Error('Should be impossible');
}
return def.arrow.domain.fields;
};
export const isReturnTypeVoid = (def: FunctionCallDef): boolean => {
if (def.arrow.codomain.tag === 'nil') {
return true;
}
return def.arrow.codomain.items.length == 0;
};

View File

@ -0,0 +1,229 @@
import { SecurityTetraplet } from '@fluencelabs/avm';
import type { MultiaddrInput } from '@multiformats/multiaddr';
import { FnConfig, FunctionCallDef, ServiceDef } from './compilerSupport';
export * from './compilerSupport';
/**
* Peer ID's id as a base58 string (multihash/CIDv0).
*/
export type PeerIdB58 = string;
/**
* Additional information about a service call
* @typeparam ArgName
*/
export interface CallParams<ArgName extends string | null> {
/**
* The identifier of particle which triggered the call
*/
particleId: string;
/**
* The peer id which created the particle
*/
initPeerId: PeerIdB58;
/**
* Particle's timestamp when it was created
*/
timestamp: number;
/**
* Time to live in milliseconds. The time after the particle should be expired
*/
ttl: number;
/**
* Particle's signature
*/
signature?: string;
/**
* Security tetraplets
*/
tetraplets: ArgName extends string ? Record<ArgName, SecurityTetraplet[]> : Record<string, never>;
}
/**
* Node of the Fluence network specified as a pair of node's multiaddr and it's peer id
*/
type Node = {
peerId: PeerIdB58;
multiaddr: string;
};
export type ConnectionOption = string | MultiaddrInput | Node;
/**
* Configuration used when initiating Fluence Peer
*/
export interface PeerConfig {
/**
* Node in Fluence network to connect to.
* Can be in the form of:
* - string: multiaddr in string format
* - Multiaddr: multiaddr object, @see https://github.com/multiformats/js-multiaddr
* - Node: node structure, @see Node
* - Implementation of FluenceConnection class, @see FluenceConnection
* If not specified the will work locally and would not be able to send or receive particles.
*/
connectTo?: ConnectionOption;
/**
* @deprecated. AVM run through marine-js infrastructure.
* @see debug.marineLogLevel option to configure logging level of AVM
*/
// avmLogLevel?: LogLevel | 'off';
/**
* Specify the KeyPair to be used to identify the Fluence Peer.
* Will be generated randomly if not specified
*/
//KeyPair?: KeyPair;
/**
* When the peer established the connection to the network it sends a ping-like message to check if it works correctly.
* The options allows to specify the timeout for that message in milliseconds.
* If not specified the default timeout will be used
*/
checkConnectionTimeoutMs?: number;
/**
* When the peer established the connection to the network it sends a ping-like message to check if it works correctly.
* If set to true, the ping-like message will be skipped
* Default: false
*/
skipCheckConnection?: boolean;
/**
* The dialing timeout in milliseconds
*/
dialTimeoutMs?: number;
/**
* Sets the default TTL for all particles originating from the peer with no TTL specified.
* If the originating particle's TTL is defined then that value will be used
* If the option is not set default TTL will be 7000
*/
defaultTtlMs?: number;
/**
* This option allows to specify the location of various dependencies needed for marine-js.
* Each key specifies the location of the corresponding dependency.
* If Fluence peer is started inside browser the location is treated as the path to the file relative to origin.
* IF Fluence peer is started in nodejs the location is treated as the full path to file on the file system.
*/
// marineJS?: {
// /**
// * Configures path to the marine-js worker script.
// */
// workerScriptPath: string;
// /**
// * Configures the path to marine-js control wasm module
// */
// marineWasmPath: string;
// /**
// * Configures the path to AVM wasm module
// */
// avmWasmPath: string;
// };
/**
* Enables\disabled various debugging features
*/
// debug?: {
// /**
// * If set to true, newly initiated particle ids will be printed to console.
// * Useful to see what particle id is responsible for aqua function
// */
// printParticleId?: boolean;
// /**
// * Log level for marine services. By default logging is turned off.
// */
// marineLogLevel?: LogLevel;
// };
}
/**
* Information about Fluence Peer connection.
* Represented as object with the following keys:
* - `isInitialized`: Is the peer initialized or not.
* - `peerId`: Peer Id of the peer. Null if the peer is not initialized
* - `isConnected`: Is the peer connected to network or not
* - `relayPeerId`: Peer Id of the relay the peer is connected to. If the connection is direct relayPeerId is null
* - `isDirect`: True if the peer is connected to the network directly (not through relay)
*/
export type PeerStatus =
| {
isInitialized: false;
peerId: null;
isConnected: false;
relayPeerId: null;
}
| {
isInitialized: true;
peerId: PeerIdB58;
isConnected: false;
relayPeerId: null;
}
| {
isInitialized: true;
peerId: PeerIdB58;
isConnected: true;
relayPeerId: PeerIdB58;
}
| {
isInitialized: true;
peerId: PeerIdB58;
isConnected: true;
isDirect: true;
relayPeerId: null;
};
export interface IFluencePeer {
start(config?: PeerConfig): Promise<void>;
stop(): Promise<void>;
getStatus(): PeerStatus;
// TODO: come up with a working interface for
// - particle creation
// - particle initialization
// - service registration
internals: any;
compilerSupport: {
callFunction: (args: CallFunctionArgs) => Promise<unknown>;
registerService: (args: RegisterServiceArgs) => void;
};
}
export interface CallFunctionArgs {
def: FunctionCallDef;
script: string;
config: FnConfig;
args: { [key: string]: any };
}
export interface RegisterServiceArgs {
def: ServiceDef;
serviceId: string | undefined;
service: any;
}
export const asFluencePeer = (fluencePeerCandidate: unknown): IFluencePeer => {
if (isFluencePeer(fluencePeerCandidate)) {
return fluencePeerCandidate;
}
throw new Error('');
};
export const isFluencePeer = (fluencePeerCandidate: unknown): fluencePeerCandidate is IFluencePeer => {
if (fluencePeerCandidate && (fluencePeerCandidate as any).__isFluenceAwesome) {
return true;
}
return false;
};

View File

@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"moduleResolution": "node"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}