Skip to content

Commit

Permalink
feat(bungee): support merging views according to a privacy policy
Browse files Browse the repository at this point in the history
Signed-off-by: eduv09 <eduardovasques10@tecnico.ulisboa.pt>
  • Loading branch information
eduv09 committed Mar 22, 2024
1 parent 2394e5e commit 392233c
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
34 changes: 34 additions & 0 deletions packages/cactus-plugin-bungee/src/main/typescript/plugin-bungee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from "./generated/openapi/typescript-axios";
import { Snapshot } from "./view-creation/snapshot";
import { View } from "./view-creation/view";
import { IntegratedView } from "./view-merging/integrated-view";
import {
NetworkDetails,
ObtainLedgerStrategy,
Expand Down Expand Up @@ -232,4 +233,37 @@ export class PluginBUNGEE implements ICactusPlugin, IPluginWebService {

return snapshot;
}

public mergeViews(
views: View[],
privacyPolicy: string = "no privacy policy",
): { view: IntegratedView; signature: string } {
const integratedView = new IntegratedView(privacyPolicy);

for (const view of views) {
integratedView.addParticipant(view.getSnapshot().getParticipant());
for (const state of view.getSnapshot().getStateBins()) {
if (integratedView.getExtendedState(state.getId()) == undefined) {
integratedView.createExtendedState(state.getId());
}
integratedView.addStateInExtendedState(
state.getId(),
view.getKey(),
state,
);
// eslint-disable-next-line prettier/prettier
if (parseInt(state.getInitialTime()) < parseInt(integratedView.getTI())) {
integratedView.setTI(state.getInitialTime());
}

if (parseInt(state.getFinalTime()) > parseInt(integratedView.getTF())) {
integratedView.setTF(state.getFinalTime());
}
}
}
return {
view: integratedView,
signature: this.sign(JSON.stringify(integratedView)),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class Snapshot {
return this.id;
}

public getParticipant(): string {
return this.participant;
}

public getTI() {
return this.tI;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class View {
this.snapshot = snapshot;
snapshot.pruneStates(this.tI, this.tF);
}

public getSnapshot(): Snapshot {
return this.snapshot;
}

public getKey() {
return this.key;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { State } from "../view-creation/state";

export class ExtendedState {
private states: Map<string, State>;
constructor() {
this.states = new Map<string, State>();
}
public getState(viewId: string): State | undefined {
return this.states.get(viewId);
}
public setState(viewId: string, state: State) {
if (this.getState(viewId) == undefined) {
this.states.set(viewId, state);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { State } from "../view-creation/state";
import { ExtendedState } from "./extended-state";

export class IntegratedView {
private stateList: Map<string, ExtendedState>;
private tI: string;
private tF: string;
private participants: string[];
private privacyPolicy: string;
constructor(privacyPolicy: string) {
this.stateList = new Map<string, ExtendedState>();
this.tI = "999999999999999";
this.tF = "0";
this.participants = [];
this.privacyPolicy = privacyPolicy;
}
public getTI(): string {
return this.tI;
}
public setTI(tI: string) {
this.tI = tI;
}
public getTF(): string {
return this.tF;
}
public setTF(TF: string) {
this.tF = TF;
}
public addParticipant(participant: string) {
this.participants.push(participant);
}
public getExtendedState(stateId: string): ExtendedState | undefined {
return this.stateList.get(stateId);
}

public createExtendedState(stateId: string) {
if (this.getExtendedState(stateId) == undefined) {
this.stateList.set(stateId, new ExtendedState());
}
}
public addStateInExtendedState(
stateId: string,
viewId: string,
state: State,
) {
const extendedState = this.getExtendedState(stateId);
if (extendedState != undefined) {
extendedState.setState(viewId, state);
}
}

public getState(stateId: string, viewId: string): State | undefined {
return this.getExtendedState(stateId)?.getState(viewId);
}
}
3 changes: 0 additions & 3 deletions packages/cactus-plugin-bungee/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
},
{
"path": "../cactus-core/tsconfig.json"
},
{
"path": "../../extensions/cactus-plugin-object-store-ipfs/tsconfig.json"
}
]
}

0 comments on commit 392233c

Please sign in to comment.