Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/api/src/graphql/modules/LinkedMerkleWitnessResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@proto-kit/common";
import {
AsyncLinkedLeafStore,
AsyncMerkleTreeStore,
CachedLinkedLeafStore,
} from "@proto-kit/sequencer";

Expand Down Expand Up @@ -42,7 +43,9 @@ export class LinkedMerkleWitnessDTO {
export class LinkedMerkleWitnessResolver extends GraphqlModule<object> {
public constructor(
@inject("AsyncLinkedLeafStore")
private readonly treeStore: AsyncLinkedLeafStore
private readonly leafStore: AsyncLinkedLeafStore,
@inject("AsyncTreeStore")
private readonly treeStore: AsyncMerkleTreeStore
) {
super();
}
Expand All @@ -52,7 +55,10 @@ export class LinkedMerkleWitnessResolver extends GraphqlModule<object> {
"Allows retrieval of merkle witnesses corresponding to a specific path in the appchain's state tree. These proves are generally retrieved from the current 'proven' state",
})
public async witness(@Arg("path") path: string) {
const syncStore = await CachedLinkedLeafStore.new(this.treeStore);
const syncStore = await CachedLinkedLeafStore.new(
this.leafStore,
this.treeStore
);

const tree = new LinkedMerkleTree(syncStore.treeStore, syncStore);
await syncStore.preloadKey(BigInt(path));
Expand Down
10 changes: 8 additions & 2 deletions packages/api/src/graphql/modules/MerkleWitnessResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@proto-kit/common";
import {
AsyncLinkedLeafStore,
AsyncMerkleTreeStore,
CachedLinkedLeafStore,
} from "@proto-kit/sequencer";

Expand Down Expand Up @@ -92,7 +93,9 @@ export class LinkedTreeWitnessDTO {
export class MerkleWitnessResolver extends GraphqlModule<object> {
public constructor(
@inject("AsyncLinkedLeafStore")
private readonly treeStore: AsyncLinkedLeafStore
private readonly leafStore: AsyncLinkedLeafStore,
@inject("AsyncTreeStore")
private readonly treeStore: AsyncMerkleTreeStore
) {
super();
}
Expand All @@ -102,7 +105,10 @@ export class MerkleWitnessResolver extends GraphqlModule<object> {
"Allows retrieval of merkle witnesses corresponding to a specific path in the appchain's state tree. These proves are generally retrieved from the current 'proven' state",
})
public async witness(@Arg("path") path: string) {
const syncStore = await CachedLinkedLeafStore.new(this.treeStore);
const syncStore = await CachedLinkedLeafStore.new(
this.leafStore,
this.treeStore
);
await syncStore.preloadKey(BigInt(path));

const tree = new LinkedMerkleTree(syncStore.treeStore, syncStore);
Expand Down
23 changes: 22 additions & 1 deletion packages/persistance/src/PrismaDatabaseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PrismaBlockStorage } from "./services/prisma/PrismaBlockStorage";
import { PrismaSettlementStorage } from "./services/prisma/PrismaSettlementStorage";
import { PrismaMessageStorage } from "./services/prisma/PrismaMessageStorage";
import { PrismaTransactionStorage } from "./services/prisma/PrismaTransactionStorage";
import { PrismaLinkedLeafStore } from "./services/prisma/PrismaLinkedLeafStore";

export interface PrismaDatabaseConfig {
// Either object-based config or connection string
Expand Down Expand Up @@ -57,7 +58,7 @@ export class PrismaDatabaseConnection
StorageDependencyMinimumDependencies<{
readonly prisma: PrismaDatabaseConnection;
}>,
"blockTreeStore" | "asyncLinkedLeafStore" | "unprovenLinkedLeafStore"
"blockTreeStore" | "asyncTreeStore" | "unprovenTreeStore"
> {
return {
asyncStateService: {
Expand Down Expand Up @@ -94,6 +95,26 @@ export class PrismaDatabaseConnection
transactionStorage: {
useClass: PrismaTransactionStorage,
},

asyncLinkedLeafStore: {
useGenerated: (module) => {
return new PrismaLinkedLeafStore(
module.prisma,
module.prisma.tracer,
"batch"
);
},
},

unprovenLinkedLeafStore: {
useGenerated: (module) => {
return new PrismaLinkedLeafStore(
module.prisma,
module.prisma.tracer,
"block"
);
},
},
};
}

Expand Down
23 changes: 0 additions & 23 deletions packages/persistance/src/PrismaRedisDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
RedisConnectionModule,
RedisTransaction,
} from "./RedisConnection";
import { PrismaLinkedLeafStore } from "./services/prisma/PrismaLinkedLeafStore";

export interface PrismaRedisCombinedConfig {
prisma: PrismaDatabaseConfig;
Expand Down Expand Up @@ -68,28 +67,6 @@ export class PrismaRedisDatabase
return {
...PrismaDatabaseConnection.dependencies(),
...RedisConnectionModule.dependencies(),

asyncLinkedLeafStore: {
useGenerated: (module) => {
return new PrismaLinkedLeafStore(
module.prisma,
module.redis,
module.tracer,
"batch"
);
},
},

unprovenLinkedLeafStore: {
useGenerated: (module) => {
return new PrismaLinkedLeafStore(
module.prisma,
module.redis,
module.tracer,
"block"
);
},
},
};
}

Expand Down
6 changes: 3 additions & 3 deletions packages/persistance/src/RedisConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export class RedisConnectionModule

public static dependencies(): Pick<
StorageDependencyMinimumDependencies<{ redis: RedisConnectionModule }>,
"asyncMerkleStore" | "blockTreeStore" | "unprovenMerkleStore"
"blockTreeStore" | "asyncTreeStore" | "unprovenTreeStore"
> {
return {
asyncMerkleStore: {
asyncTreeStore: {
useGenerated: ({ redis }) =>
new RedisMerkleTreeStore(redis, redis.tracer),
},
unprovenMerkleStore: {
unprovenTreeStore: {
useGenerated: ({ redis }) =>
new RedisMerkleTreeStore(redis, redis.tracer, "unproven"),
},
Expand Down
25 changes: 6 additions & 19 deletions packages/persistance/src/services/prisma/PrismaLinkedLeafStore.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
import { noop, StoredLeaf } from "@proto-kit/common";
import { AsyncLinkedLeafStore, Tracer } from "@proto-kit/sequencer";
import { AsyncLinkedLeafStore, trace, Tracer } from "@proto-kit/sequencer";
import { injectable } from "tsyringe";
import { Prisma } from "@prisma/client";

import { PrismaConnection } from "../../PrismaDatabaseConnection";
import { RedisMerkleTreeStore } from "../redis/RedisMerkleTreeStore";
import { RedisConnection } from "../../RedisConnection";
import type { PrismaConnection } from "../../PrismaDatabaseConnection";

import { Decimal } from "./PrismaStateService";

@injectable()
export class PrismaLinkedLeafStore implements AsyncLinkedLeafStore {
private cache: StoredLeaf[] = [];

private readonly redisMerkleStore: RedisMerkleTreeStore;

public constructor(
private readonly connection: PrismaConnection,
redisConnection: RedisConnection,
tracer: Tracer,
public readonly tracer: Tracer,
private readonly mask: string = "base"
) {
this.redisMerkleStore = new RedisMerkleTreeStore(
redisConnection,
tracer,
mask
);
}

public get treeStore() {
return this.redisMerkleStore;
}
) {}

private assertCacheEmpty() {
if (this.cache.length > 0) {
Expand All @@ -42,6 +27,7 @@ export class PrismaLinkedLeafStore implements AsyncLinkedLeafStore {
noop();
}

@trace("LinkedLeafStore.commit")
public async commit(): Promise<void> {
if (this.cache.length > 0) {
const data = this.cache.map((entry) => ({
Expand Down Expand Up @@ -126,6 +112,7 @@ export class PrismaLinkedLeafStore implements AsyncLinkedLeafStore {
: undefined;
}

@trace("getPreviousLeaves", ([paths]) => ({ numPaths: paths.length }))
public async getPreviousLeavesAsync(paths: bigint[]) {
this.assertCacheEmpty();

Expand Down
12 changes: 10 additions & 2 deletions packages/sdk/src/query/StateServiceQueryModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CachedLinkedLeafStore,
AsyncLinkedLeafStore,
AppChainModule,
AsyncMerkleTreeStore,
} from "@proto-kit/sequencer";
import { Field } from "o1js";
import { inject, injectable } from "tsyringe";
Expand All @@ -30,18 +31,25 @@ export class StateServiceQueryModule
);
}

public get treeStore(): AsyncLinkedLeafStore {
public get leafStore(): AsyncLinkedLeafStore {
return this.sequencer.dependencyContainer.resolve("AsyncLinkedLeafStore");
}

public get treeStore(): AsyncMerkleTreeStore {
return this.sequencer.dependencyContainer.resolve("AsyncTreeStore");
}

public get(key: Field) {
return this.asyncStateService.get(key);
}

public async merkleWitness(
path: Field
): Promise<LinkedMerkleTreeReadWitness | undefined> {
const syncStore = await CachedLinkedLeafStore.new(this.treeStore);
const syncStore = await CachedLinkedLeafStore.new(
this.leafStore,
this.treeStore
);
await syncStore.preloadKey(path.toBigInt());

const tree = new LinkedMerkleTree(syncStore.treeStore, syncStore);
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/test/stprover-emit-sts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("StateTransition", () => {

await appChain.start();
appChain.setSigner(senderKey);
});
}, 30_000);

afterEach(async () => {
await appChain.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { Database } from "../../storage/Database";
import { AsyncLinkedLeafStore } from "../../state/async/AsyncLinkedLeafStore";
import { CachedLinkedLeafStore } from "../../state/lmt/CachedLinkedLeafStore";
import { ensureNotBusy } from "../../helpers/BusyGuard";
import { AsyncMerkleTreeStore } from "../../state/async/AsyncMerkleTreeStore";

import { BlockProofSerializer } from "./tasks/serializers/BlockProofSerializer";
import { BatchTracingService } from "./tracing/BatchTracingService";
Expand Down Expand Up @@ -47,7 +48,9 @@ const errors = {
export class BatchProducerModule extends SequencerModule {
public constructor(
@inject("AsyncLinkedLeafStore")
private readonly merkleStore: AsyncLinkedLeafStore,
private readonly leafStore: AsyncLinkedLeafStore,
@inject("AsyncTreeStore")
private readonly merkleStore: AsyncMerkleTreeStore,
@inject("BatchStorage") private readonly batchStorage: BatchStorage,
@inject("Database")
private readonly database: Database,
Expand Down Expand Up @@ -149,7 +152,10 @@ export class BatchProducerModule extends SequencerModule {
throw errors.blockWithoutTxs();
}

const merkleTreeStore = await CachedLinkedLeafStore.new(this.merkleStore);
const merkleTreeStore = await CachedLinkedLeafStore.new(
this.leafStore,
this.merkleStore
);

const trace = await this.batchTraceService.traceBatch(
blocks.map((block) => block),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
private readonly unprovenStateService: AsyncStateService,
@inject("UnprovenLinkedLeafStore")
private readonly unprovenLinkedLeafStore: AsyncLinkedLeafStore,
@inject("UnprovenTreeStore")
private readonly unprovenTreeStore: AsyncMerkleTreeStore,
@inject("BlockQueue")
private readonly blockQueue: BlockQueue,
@inject("TransactionStorage")
Expand Down Expand Up @@ -118,6 +120,7 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
await this.resultService.generateMetadataForNextBlock(
block,
this.unprovenLinkedLeafStore,
this.unprovenTreeStore,
this.blockTreeStore,
this.unprovenStateService
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ export class BlockResultService {
}))
public async generateMetadataForNextBlock(
block: Block,
merkleTreeStore: AsyncLinkedLeafStore,
leafStore: AsyncLinkedLeafStore,
treeStore: AsyncMerkleTreeStore,
blockHashTreeStore: AsyncMerkleTreeStore,
stateService: AsyncStateService
): Promise<{
Expand All @@ -202,7 +203,7 @@ export class BlockResultService {
block.beforeBlockStateTransitions
);

const inMemoryStore = await CachedLinkedLeafStore.new(merkleTreeStore);
const inMemoryStore = await CachedLinkedLeafStore.new(leafStore, treeStore);

const tree = await this.applyStateDiff(inMemoryStore, combinedDiff);

Expand Down
8 changes: 7 additions & 1 deletion packages/sequencer/src/settlement/BridgingModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { AsyncLinkedLeafStore } from "../state/async/AsyncLinkedLeafStore";
import { CachedLinkedLeafStore } from "../state/lmt/CachedLinkedLeafStore";
import { SettleableBatch } from "../storage/model/Batch";
import { SequencerModule } from "../sequencer/builder/SequencerModule";
import { AsyncMerkleTreeStore } from "../state/async/AsyncMerkleTreeStore";

import type { SettlementModule } from "./SettlementModule";
import { SettlementUtils } from "./utils/SettlementUtils";
Expand Down Expand Up @@ -114,6 +115,8 @@ export class BridgingModule extends SequencerModule<BridgingModuleConfig> {
private readonly outgoingMessageCollector: OutgoingMessageCollector,
@inject("AsyncLinkedLeafStore")
private readonly linkedLeafStore: AsyncLinkedLeafStore,
@inject("AsyncTreeStore")
private readonly treeStore: AsyncMerkleTreeStore,
@inject("FeeStrategy")
private readonly feeStrategy: FeeStrategy,
@inject("BaseLayer") private readonly baseLayer: MinaBaseLayer,
Expand Down Expand Up @@ -590,7 +593,10 @@ export class BridgingModule extends SequencerModule<BridgingModuleConfig> {

const bridgeContract = this.createBridgeContract(bridgeAddress, tokenId);

const cachedStore = await CachedLinkedLeafStore.new(this.linkedLeafStore);
const cachedStore = await CachedLinkedLeafStore.new(
this.linkedLeafStore,
this.treeStore
);
const tree = new LinkedMerkleTree(cachedStore.treeStore, cachedStore);

// Create withdrawal batches and send them as L1 transactions
Expand Down
4 changes: 0 additions & 4 deletions packages/sequencer/src/state/async/AsyncLinkedLeafStore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { StoredLeaf } from "@proto-kit/common";

import { AsyncMerkleTreeStore } from "./AsyncMerkleTreeStore";

export interface AsyncLinkedLeafStore {
treeStore: AsyncMerkleTreeStore;

openTransaction: () => Promise<void>;

commit: () => Promise<void>;
Expand Down
Loading
Loading