Skip to content

Commit

Permalink
Merged adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanic committed Jan 13, 2024
1 parent 5b19bfd commit f3b53ac
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface UnprovenBlockMetadata {
stateRoot: bigint;
blockHashRoot: bigint;
afterNetworkState: NetworkState;
blockStateTransitions: StateTransition<unknown>[];
blockStateTransitions: UntypedStateTransition[];
blockHashWitness: BlockHashMerkleTreeWitness;
}

Expand Down Expand Up @@ -382,7 +382,9 @@ export class TransactionExecutionService {
afterNetworkState: resultingNetworkState,
stateRoot: stateRoot.toBigInt(),
blockHashRoot: newBlockHashRoot.toBigInt(),
blockStateTransitions: stateTransitions,
blockStateTransitions: stateTransitions.map((st) =>
UntypedStateTransition.fromStateTransition(st)
),
blockHashWitness,
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/sequencer/src/storage/StorageDependencyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface StorageDependencyMinimumDependencies extends DependencyRecord {
unprovenBlockStorage: DependencyDeclaration<UnprovenBlockStorage>;
unprovenStateService: DependencyDeclaration<CachedStateService>;
unprovenMerkleStore: DependencyDeclaration<CachedMerkleTreeStore>;
blockTreeStore: DependencyDeclaration<AsyncMerkleTreeStore>;
}

export interface StorageDependencyFactory extends DependencyFactory {
Expand Down
22 changes: 18 additions & 4 deletions packages/sequencer/src/storage/inmemory/InMemoryBlockStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
UnprovenBlock,
UnprovenBlockMetadata,
UnprovenBlockWithMetadata,
} from "../../protocol/production/unproven/TransactionExecutionService";
import { UnprovenBlockWithPreviousMetadata } from "../../protocol/production/BlockProducerModule";

Expand All @@ -29,16 +30,26 @@ export class InMemoryBlockStorage
return this.blocks.length;
}

public async getLatestBlock(): Promise<UnprovenBlock | undefined> {
return await this.getBlockAt((await this.getCurrentBlockHeight()) - 1);
public async getLatestBlock(): Promise<
UnprovenBlockWithMetadata | undefined
> {
const currentHeight = await this.getCurrentBlockHeight();
const block = await this.getBlockAt(currentHeight - 1);
const metadata = this.metadata[currentHeight - 1];
if (block === undefined) {
return undefined;
}
return {
block,
metadata,
};
}

public async popNewBlocks(
remove: boolean
): Promise<UnprovenBlockWithPreviousMetadata[]> {
const slice = this.blocks.slice(this.cursor);

// eslint-disable-next-line putout/putout
let metadata: (UnprovenBlockMetadata | undefined)[] = this.metadata.slice(
Math.max(this.cursor - 1, 0)
);
Expand All @@ -50,7 +61,10 @@ export class InMemoryBlockStorage
this.cursor = this.blocks.length;
}
return slice.map((block, index) => ({
block,
block: {
block,
metadata: metadata[index + 1]!,
},
lastBlockMetadata: metadata[index],
}));
}
Expand Down
3 changes: 3 additions & 0 deletions packages/sequencer/src/storage/inmemory/InMemoryDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class InMemoryDatabase
unprovenMerkleStore: {
useFactory: () => new CachedMerkleTreeStore(this.merkleStore),
},
blockTreeStore: {
useClass: InMemoryAsyncMerkleTreeStore
}
};
}

Expand Down
40 changes: 24 additions & 16 deletions packages/sequencer/test/integration/BlockProduction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import { UnsignedTransaction } from "../../src/mempool/PendingTransaction";
import { Sequencer } from "../../src/sequencer/executor/Sequencer";
import {
AsyncStateService,
BlockProducerModule, InMemoryDatabase,
ManualBlockTrigger
BlockProducerModule,
InMemoryDatabase,
ManualBlockTrigger,
} from "../../src";
import { LocalTaskWorkerModule } from "../../src/worker/worker/LocalTaskWorkerModule";

Expand All @@ -41,6 +42,7 @@ import { container } from "tsyringe";
describe("block production", () => {
let runtime: Runtime<{ Balance: typeof Balance }>;
let sequencer: Sequencer<{
Database: typeof InMemoryDatabase;
Mempool: typeof PrivateMempool;
LocalTaskWorkerModule: typeof LocalTaskWorkerModule;
BaseLayer: typeof NoopBaseLayer;
Expand Down Expand Up @@ -183,7 +185,7 @@ describe("block production", () => {
expect(block!.transactions[0].status.toBoolean()).toBe(true);
expect(block!.transactions[0].statusMessage).toBeUndefined();

let batch = await blockTrigger.produceProven()
let batch = await blockTrigger.produceProven();

expect(batch).toBeDefined();

Expand All @@ -195,7 +197,10 @@ describe("block production", () => {
"AsyncStateService"
);

const unprovenStateService = sequencer.dependencyContainer.resolve<AsyncStateService>("UnprovenStateService")
const unprovenStateService =
sequencer.dependencyContainer.resolve<AsyncStateService>(
"UnprovenStateService"
);

const balanceModule = runtime.resolve("Balance");
const balancesPath = Path.fromKey(
Expand All @@ -209,7 +214,9 @@ describe("block production", () => {
expect(newState).toBeDefined();
expect(newUnprovenState).toBeDefined();
expect(UInt64.fromFields(newState!)).toStrictEqual(UInt64.from(100));
expect(UInt64.fromFields(newUnprovenState!)).toStrictEqual(UInt64.from(100));
expect(UInt64.fromFields(newUnprovenState!)).toStrictEqual(
UInt64.from(100)
);

// Check that nonce has been set
const accountModule = protocol.resolve("AccountStateModule");
Expand Down Expand Up @@ -327,11 +334,15 @@ describe("block production", () => {
expect(block!.transactions[index].status.toBoolean()).toBe(true);
expect(block!.transactions[index].statusMessage).toBe(undefined);

const transitions = block!.transactions[index].stateTransitions
const transitions = block!.transactions[index].stateTransitions;

const fromBalance = increment * index;
expect(transitions[0].fromValue.value[0].toBigInt()).toStrictEqual(BigInt(fromBalance))
expect(transitions[1].toValue.value[0].toBigInt()).toStrictEqual(BigInt(fromBalance + increment))
expect(transitions[0].fromValue.value[0].toBigInt()).toStrictEqual(
BigInt(fromBalance)
);
expect(transitions[1].toValue.value[0].toBigInt()).toStrictEqual(
BigInt(fromBalance + increment)
);
});

const batch = await blockTrigger.produceProven();
Expand All @@ -357,8 +368,7 @@ describe("block production", () => {
);
}, 160_000);


it("Should produce a block with a mix of failing and succeeding transactions", async () => {
it("should produce a block with a mix of failing and succeeding transactions", async () => {
expect.assertions(6);

const pk1 = PrivateKey.random();
Expand All @@ -381,12 +391,12 @@ describe("block production", () => {
})
);

const block = await blockTrigger.produceBlock();
const [block, batch] = await blockTrigger.produceBlock();

expect(block).toBeDefined();

expect(block!.bundles).toHaveLength(1);
expect(block!.bundles[0]).toHaveLength(2);
expect(batch!.bundles).toHaveLength(1);
expect(batch!.bundles[0]).toHaveLength(2);

const stateService =
sequencer.dependencyContainer.resolve<AsyncStateService>(
Expand All @@ -410,9 +420,7 @@ describe("block production", () => {
const newState2 = await stateService.getAsync(balancesPath2);

expect(newState2).toBeDefined();
expect(UInt64.fromFields(newState2!)).toStrictEqual(
UInt64.from(100)
);
expect(UInt64.fromFields(newState2!)).toStrictEqual(UInt64.from(100));
}, 120_000);

it.skip.each([
Expand Down

0 comments on commit f3b53ac

Please sign in to comment.