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

@ -236,3 +236,23 @@ export interface FnConfig {
*/
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

@ -1,6 +1,48 @@
import { SecurityTetraplet } from '@fluencelabs/avm';
import type { MultiaddrInput } from '@multiformats/multiaddr';
import type { PeerIdB58 } from './commonTypes';
// import { KeyPair } from '../keypair';
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
@ -104,3 +146,84 @@ export interface PeerConfig {
// 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

@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"moduleResolution": "node"

View File

@ -21,6 +21,7 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/interface": "workspace:*",
"@fluencelabs/avm": "0.31.10",
"@fluencelabs/marine-js": "0.3.44",
"multiformats": "11.0.1",
@ -58,7 +59,6 @@
"@fluencelabs/aqua-api": "0.9.1-373",
"@fluencelabs/aqua-lib": "0.6.0",
"@fluencelabs/fluence-network-environment": "1.0.13",
"@multiformats/multiaddr": "11.3.0",
"@types/bs58": "4.0.1",
"@types/platform": "1.3.4",
"@types/uuid": "8.3.2",

View File

@ -1,4 +1,4 @@
import { aqua2ts, ts2aqua } from '../../../compilerSupport/conversions.js';
import { aqua2ts, ts2aqua } from '../conversions';
const i32 = { tag: 'scalar', name: 'i32' } as const;

View File

@ -1,5 +1,12 @@
import { ArrowWithoutCallbacks, FnConfig, FunctionCallDef, NonArrowType } from './interface.js';
import { IFluencePeer } from '../../interfaces/index';
import {
ArrowWithoutCallbacks,
FnConfig,
FunctionCallDef,
NonArrowType,
getArgumentTypes,
isReturnTypeVoid,
} from '@fluencelabs/interface';
import { IFluencePeer } from '@fluencelabs/interface';
import {
injectRelayService,
@ -81,23 +88,3 @@ export function callFunctionImpl(
return promise;
}
const isReturnTypeVoid = (def: FunctionCallDef): boolean => {
if (def.arrow.codomain.tag === 'nil') {
return true;
}
return def.arrow.codomain.items.length == 0;
};
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;
};

View File

@ -1,7 +1,7 @@
import { jsonify } from '../utils.js';
import { jsonify } from '../js-peer/utils.js';
import { match } from 'ts-pattern';
import { ArrowType, ArrowWithoutCallbacks, NonArrowType } from './interface.js';
import { CallServiceData } from '../../interfaces/commonTypes.js';
import { ArrowType, ArrowWithoutCallbacks, NonArrowType } from '@fluencelabs/interface';
import { CallServiceData } from '../interfaces/commonTypes.js';
/**
* Convert value from its representation in aqua language to representation in typescript

View File

@ -1,5 +1,5 @@
import type { IFluencePeer } from '../../interfaces/index';
import { ServiceDef } from './interface.js';
import { IFluencePeer } from '@fluencelabs/interface';
import { ServiceDef } from '@fluencelabs/interface';
import { registerGlobalService, userHandlerService } from './services.js';
export const registerServiceImpl = (

View File

@ -1,12 +1,12 @@
import { SecurityTetraplet } from '@fluencelabs/avm';
import { match } from 'ts-pattern';
import { Particle } from '../Particle.js';
import { CallParams, CallServiceData, GenericCallServiceHandler, ResultCodes } from '../../interfaces/commonTypes.js';
import { IFluencePeer } from '../../interfaces/index';
import { Particle } from '../js-peer/Particle.js';
import { CallServiceData, GenericCallServiceHandler, ResultCodes } from '../interfaces/commonTypes.js';
import { IFluencePeer, CallParams } from '@fluencelabs/interface';
import { aquaArgs2Ts, responseServiceValue2ts, returnType2Aqua, ts2aqua } from './conversions.js';
import { ArrowWithoutCallbacks, FunctionCallConstants, FunctionCallDef, NonArrowType } from './interface.js';
import { ArrowWithoutCallbacks, FunctionCallConstants, FunctionCallDef, NonArrowType } from '@fluencelabs/interface';
export interface ServiceDescription {
serviceId: string;

View File

@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { FluenceConnection, ParticleHandler, PeerIdB58 } from '../interfaces/index.js';
import { PeerIdB58 } from '@fluencelabs/interface';
import { FluenceConnection, ParticleHandler } from '../interfaces/index.js';
import { pipe } from 'it-pipe';
import { encode, decode } from 'it-length-prefixed';
import type { PeerId } from '@libp2p/interface-peer-id';
@ -82,7 +83,6 @@ export class RelayConnection extends FluenceConnection {
connectionEncryption: [noise()],
});
const relayMultiaddr = multiaddr(options.relayAddress);
const relayPeerId = relayMultiaddr.getPeerId();
if (relayPeerId === null) {
@ -158,7 +158,6 @@ export class RelayConnection extends FluenceConnection {
);
});
log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId.toString());
try {

View File

@ -14,48 +14,8 @@
* limitations under the License.
*/
import { SecurityTetraplet } from '@fluencelabs/avm';
/**
* 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>;
}
import type { PeerIdB58 } from '@fluencelabs/interface';
import type { SecurityTetraplet } from '@fluencelabs/avm';
export enum ResultCodes {
success = 0,

View File

@ -14,79 +14,13 @@
* limitations under the License.
*/
import type { PeerIdB58 } from '@fluencelabs/interface';
import type { JSONArray, JSONObject, LogLevel } from '@fluencelabs/marine-js/dist/types';
import type { RunParameters, CallResultsArray, InterpreterResult } from '@fluencelabs/avm';
import type { WorkerImplementation } from 'threads/dist/types/master';
import { PeerConfig } from './peerConfig';
export type PeerIdB58 = string;
export type ParticleHandler = (particle: string) => void;
/**
* 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;
}
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;
};
/**
* Base class for connectivity layer to Fluence Network
*/

View File

@ -16,7 +16,7 @@
import 'buffer';
import { RelayConnection } from '../connection/index.js';
import { FluenceConnection, IAvmRunner, IFluencePeer, IMarine, PeerStatus } from '../interfaces/index.js';
import { FluenceConnection, IAvmRunner, IMarine } from '../interfaces/index.js';
import { KeyPair } from '../keypair/index.js';
import {
CallServiceData,
@ -24,7 +24,16 @@ import {
GenericCallServiceHandler,
ResultCodes,
} from '../interfaces/commonTypes.js';
import { PeerIdB58 } from '../interfaces/commonTypes.js';
import {
PeerIdB58,
CallParams,
PeerConfig,
ConnectionOption,
IFluencePeer,
PeerStatus,
CallFunctionArgs,
RegisterServiceArgs,
} from '@fluencelabs/interface';
import { Particle, ParticleExecutionStage, ParticleQueueItem } from './Particle.js';
import { throwIfNotSupported, dataToString, jsonify, isString, ServiceError } from './utils.js';
import { concatMap, filter, pipe, Subject, tap } from 'rxjs';
@ -39,8 +48,9 @@ import { JSONValue } from '@fluencelabs/avm';
import { LogLevel } from '@fluencelabs/marine-js/dist/types';
import { NodeUtils, Srv } from './builtins/SingleModuleSrv.js';
import { registerNodeUtils } from './_aqua/node-utils.js';
import { ConnectionOption, PeerConfig } from '../interfaces/peerConfig.js';
import type { MultiaddrInput } from '@multiformats/multiaddr';
import { callFunctionImpl } from '../compilerSupport/callFunction.js';
import { registerServiceImpl } from '../compilerSupport/registerService.js';
const DEFAULT_TTL = 7000;
@ -222,6 +232,16 @@ export class FluencePeer implements IFluencePeer {
}
// internal api
get compilerSupport() {
return {
callFunction: (args: CallFunctionArgs): Promise<unknown> => {
return callFunctionImpl(args.def, args.script, args.config, this, args.args);
},
registerService: (args: RegisterServiceArgs): void => {
return registerServiceImpl(this, args.def, args.serviceId, args.service);
},
};
}
/**
* @private Is not intended to be used manually. Subject to change

View File

@ -1,5 +1,6 @@
import { CallParams } from '@fluencelabs/interface';
import { toUint8Array } from 'js-base64';
import { CallParams, CallServiceData } from '../../../interfaces/commonTypes.js';
import { CallServiceData } from '../../../interfaces/commonTypes.js';
import { builtInServices } from '../../builtins/common.js';
import { KeyPair } from '../../../keypair/index.js';
import { Sig, defaultSigGuard } from '../../builtins/Sig.js';

View File

@ -5,8 +5,8 @@ import { FluencePeer, PeerConfig2 as PeerConfig } from '../FluencePeer.js';
import { Particle } from '../Particle.js';
import { MakeServiceCall } from '../utils.js';
import { avmModuleLoader, controlModuleLoader } from '../utilsForNode.js';
import { ServiceDef } from '../compilerSupport/interface.js';
import { callFunctionImpl } from '../compilerSupport/callFunction.js';
import { ServiceDef } from '@fluencelabs/interface';
import { callFunctionImpl } from '../../compilerSupport/callFunction.js';
import { marineLogFunction } from '../utils.js';
import { MarineBackgroundRunner } from '../../marine/worker/index.js';

View File

@ -6,8 +6,8 @@
* Aqua version: 0.7.7-362
*
*/
import { CallParams } from '../../interfaces/commonTypes.js';
import { registerServiceImpl } from '../compilerSupport/registerService.js';
import { CallParams } from '@fluencelabs/interface';
import { registerServiceImpl } from '../../compilerSupport/registerService.js';
import { FluencePeer } from '../FluencePeer.js';
// Services

View File

@ -6,8 +6,8 @@
* Aqua version: 0.7.7-362
*
*/
import { CallParams } from '../../interfaces/commonTypes.js';
import { registerServiceImpl } from '../compilerSupport/registerService.js';
import { CallParams } from '@fluencelabs/interface';
import { registerServiceImpl } from '../../compilerSupport/registerService.js';
import { FluencePeer } from '../FluencePeer.js';
// Services

View File

@ -6,8 +6,8 @@
* Aqua version: 0.7.7-362
*
*/
import { CallParams } from '../../interfaces/commonTypes.js';
import { registerServiceImpl } from '../compilerSupport/registerService.js';
import { CallParams } from '@fluencelabs/interface';
import { registerServiceImpl } from '../../compilerSupport/registerService.js';
import { FluencePeer } from '../FluencePeer.js';
// Services

View File

@ -1,4 +1,4 @@
import { CallParams, PeerIdB58 } from '../../interfaces/commonTypes.js';
import { CallParams, PeerIdB58 } from '@fluencelabs/interface';
import { KeyPair } from '../../keypair/index.js';
import { SigDef } from '../_aqua/services.js';
import { allowOnlyParticleOriginatedAt, allowServiceFn, and, or, SecurityGuard } from './securityGuard.js';

View File

@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid';
import { SrvDef } from '../_aqua/single-module-srv.js';
import { NodeUtilsDef } from '../_aqua/node-utils.js';
import { FluencePeer } from '../FluencePeer.js';
import { CallParams } from '../../interfaces/commonTypes.js';
import { CallParams } from '@fluencelabs/interface';
import { Buffer } from 'buffer';
import { allowOnlyParticleOriginatedAt, SecurityGuard } from './securityGuard.js';

View File

@ -1,5 +1,5 @@
import { SecurityTetraplet } from '@fluencelabs/avm';
import { CallParams, PeerIdB58 } from '../../interfaces/commonTypes.js';
import { CallParams, PeerIdB58 } from '@fluencelabs/interface';
type ArgName = string | null;

View File

@ -1,6 +1,6 @@
import { PeerIdB58 } from '@fluencelabs/interface';
import { FluenceConnection, ParticleHandler } from '../interfaces/index.js';
import { keyPairFromBase64Sk } from '../keypair/index.js';
import { PeerIdB58 } from '../interfaces/commonTypes.js';
import { FluencePeer } from './FluencePeer.js';
import { MarineBackgroundRunner } from '../marine/worker/index.js';
import { avmModuleLoader, controlModuleLoader } from './utilsForNode';

View File

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