Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanic committed Jan 13, 2024
1 parent f3b53ac commit fdde774
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 116 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/graphql/modules/UnprovenBlockResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ComputedBlockTransactionModel } from "./BlockStorageResolver";
export class UnprovenBlockModel {
public static fromServiceLayerModel(unprovenBlock: UnprovenBlock) {
return new UnprovenBlockModel(
Number(unprovenBlock.networkState.block.height.toBigInt()),
Number(unprovenBlock.networkState.during.block.height.toBigInt()),
unprovenBlock.transactions.map((tx) =>
ComputedBlockTransactionModel.fromServiceLayerModel({
tx: tx.tx,
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/src/blockmodules/NoopBlockHook.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ProvableBlockHook } from "../protocol/ProvableBlockHook";
import { NetworkState } from "../model/network/NetworkState";
import { BlockProverState } from "../prover/block/BlockProver";
import { NoConfig } from "@proto-kit/common";

export class NoopBlockHook extends ProvableBlockHook<Record<string, never>> {
export class NoopBlockHook extends ProvableBlockHook<NoConfig> {
public afterBlock(
networkState: NetworkState,
state: BlockProverState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Field } from "o1js";

import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
import { BlockProverExecutionData } from "../prover/block/BlockProvable";
import { protocolState } from "../state/protocol/ProtocolState";
import { State } from "../state/State";
import { Field } from "o1js";
import { DefaultProvableHashList } from "../utils/ProvableHashList";
import { ProtocolTransaction } from "../model/transaction/ProtocolTransaction";

export class SequenceStateTransactionModule extends ProvableTransactionHook {
// Future idea to add this functionality as a module
// This is currently hardcoded in tracing however
class SequenceStateTransactionModule extends ProvableTransactionHook {
@protocolState() sequenceStateTransactionsList = State.from(Field);

public onTransaction(executionData: BlockProverExecutionData): void {
Expand Down
1 change: 0 additions & 1 deletion packages/protocol/src/prover/block/BlockProvable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class BlockProverPublicInput extends Struct({
networkStateHash: Field,
blockHashRoot: Field,
eternalTransactionsHash: Field,
// closed: Bool,
}) {}

export class BlockProverPublicOutput extends Struct({
Expand Down
23 changes: 12 additions & 11 deletions packages/protocol/src/prover/block/BlockProver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
type Proof,
Provable,
SelfProof,
Struct,
} from "o1js";
import { container, inject, injectable, injectAll } from "tsyringe";
import {
Expand All @@ -26,14 +25,6 @@ import {
StateTransitionProverPublicOutput,
} from "../statetransition/StateTransitionProvable";
import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";

import {
BlockProvable,
BlockProverExecutionData,
BlockProverProof,
BlockProverPublicInput,
BlockProverPublicOutput,
} from "./BlockProvable";
import {
ProvableStateTransition,
StateTransition,
Expand All @@ -42,6 +33,14 @@ import { ProvableTransactionHook } from "../../protocol/ProvableTransactionHook"
import { RuntimeMethodExecutionContext } from "../../state/context/RuntimeMethodExecutionContext";
import { ProvableBlockHook } from "../../protocol/ProvableBlockHook";
import { NetworkState } from "../../model/network/NetworkState";

import {
BlockProvable,
BlockProverExecutionData,
BlockProverProof,
BlockProverPublicInput,
BlockProverPublicOutput,
} from "./BlockProvable";
import {
BlockHashMerkleTreeWitness,
BlockHashTreeEntry,
Expand All @@ -62,7 +61,7 @@ const errors = {
`transactions hash not matching ${step}`,
};

// Should be equal to BlockProver.PublicInput and -Output
// Should be equal to BlockProver.PublicInput
export interface BlockProverState {
// The current state root of the block prover
stateRoot: Field;
Expand All @@ -84,7 +83,9 @@ export interface BlockProverState {
eternalTransactionsHash: Field;
}

const maxField = () => Field(Field.ORDER - 1n);
function maxField() {
return Field(Field.ORDER - 1n);
}

export class BlockProverProgrammable extends ZkProgrammable<
BlockProverPublicInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import {
Protocol,
ProtocolModulesRecord,
StateTransitionProof,
StateTransitionProverPublicInput,
} from "@proto-kit/protocol";
import { log, MOCK_PROOF } from "@proto-kit/common";

import { TaskQueue } from "../../worker/queue/TaskQueue";
import { Flow, FlowCreator } from "../../worker/flow/Flow";
import { PairTuple } from "../../helpers/utils";

import type { BlockTrace, TransactionTrace } from "./BlockProducerModule";
import type { BlockTrace } from "./BlockProducerModule";
import {
StateTransitionReductionTask,
StateTransitionTask,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ProvableHashList,
ProvableStateTransition,
ProvableStateTransitionType,
StateTransition,
StateTransitionProverPublicInput,
StateTransitionType,
} from "@proto-kit/protocol";
Expand All @@ -19,6 +18,7 @@ import { distinctByString } from "../../helpers/utils";
import { CachedMerkleTreeStore } from "../../state/merkle/CachedMerkleTreeStore";
import { CachedStateService } from "../../state/state/CachedStateService";
import { SyncCachedMerkleTreeStore } from "../../state/merkle/SyncCachedMerkleTreeStore";
import { AsyncMerkleTreeStore } from "../../state/async/AsyncMerkleTreeStore";

import type { TransactionTrace } from "./BlockProducerModule";
import type {
Expand All @@ -28,7 +28,6 @@ import type {
import { StateTransitionProofParameters } from "./tasks/StateTransitionTaskParameters";
import { UntypedStateTransition } from "./helpers/UntypedStateTransition";
import { BlockTrace } from "./BlockProducerModule";
import { AsyncMerkleTreeStore } from "../../state/async/AsyncMerkleTreeStore";

@injectable()
@scoped(Lifecycle.ContainerScoped)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import { log } from "@proto-kit/common";

import { Flow, FlowCreator } from "../../../worker/flow/Flow";
import { Task } from "../../../worker/flow/Task";
import { PairTuple } from "../../../helpers/utils";
import { log } from "@proto-kit/common";

interface ReductionState<Output> {
numMergesCompleted: 0;
queue: Output[];
}

/**
* This class builds and executes a flow that follows the map-reduce pattern.
* This works in 2 steps:
* 1. Mapping: Execute the mappingTask to transform from Input -> Output
* 2. Reduction: Find suitable pairs and merge them [Output, Output] -> Output
*
* We use this pattern extensively in our pipeline,
*/
export class ReductionTaskFlow<Input, Output> {
private flow: Flow<ReductionState<Output>>;
private readonly flow: Flow<ReductionState<Output>>;

private started = false;

public constructor(
private options: {
private readonly options: {
name: string;
inputLength: number;
mappingTask: Task<Input, Output>;
reductionTask: Task<PairTuple<Output>, Output>;
mergableFunction: (a: Output, b: Output) => boolean;
},
private flowCreator: FlowCreator
private readonly flowCreator: FlowCreator
) {
this.flow = flowCreator.createFlow<ReductionState<Output>>(options.name, {
numMergesCompleted: 0,
Expand Down Expand Up @@ -70,11 +81,11 @@ export class ReductionTaskFlow<Input, Output> {
options.inputLength - flow.state.numMergesCompleted === 1 &&
flow.tasksInProgress === 0
) {
log.info(`${this.options.name}: Resolved successfully`)
log.debug(`${options.name}: Resolved successfully`);
flow.resolve(flow.state.queue[0]);
return;
}
log.info(`${this.options.name}: Queue length: ${flow.state.queue.length}`)
log.trace(`${options.name}: Queue length: ${flow.state.queue.length}`);

if (flow.state.queue.length >= 2) {
const { availableReductions, touchedIndizes } =
Expand Down Expand Up @@ -102,27 +113,45 @@ export class ReductionTaskFlow<Input, Output> {
}
}

private started = false;

public async execute(inputs: Input[] = []): Promise<Output> {
private async initCompletionCallback(
callback: (output: Output) => Promise<void>
) {
if (this.started) {
throw new Error("Flow already started, use pushInput() to add inputs");
}
this.started = true;
return await this.flow.withFlow<Output>(async (resolve, reject) => {
inputs.forEach((input) => {
this.pushInput(input);
});
});
}

private async initCompletionCallback(callback: (output: Output) => Promise<void>){
const result = await this.flow.withFlow<Output>(async (resolve, reject) => {
});
const result = await this.flow.withFlow<Output>(async () => {});
await callback(result);
}

/**
* Execute the flow using a callback method that is invoked upon
* completion of the flow.
* Push inputs using pushInput()
* @param callback
*/
public onCompletion(callback: (output: Output) => Promise<void>) {
void this.initCompletionCallback(callback);
}

/**
* Execute the flow using the returned Promise that resolved when
* the flow is finished
* @param inputs initial inputs - doesnt have to be the complete set of inputs
*/
public async execute(inputs: Input[] = []): Promise<Output> {
if (this.started) {
throw new Error("Flow already started, use pushInput() to add inputs");
}
this.started = true;
return await this.flow.withFlow<Output>(async () => {
// eslint-disable-next-line unicorn/no-array-method-this-argument
await this.flow.forEach(inputs, async (input) => {
await this.pushInput(input);
});
});
}

public async pushInput(input: Input) {
await this.flow.pushTask(
this.options.mappingTask,
Expand Down
Loading

0 comments on commit fdde774

Please sign in to comment.