Skip to content

Commit

Permalink
feat(transaction-pool-service): calculate sender state on transaction…
Browse files Browse the repository at this point in the history
… removal (#722)

* Set heigh ton set totalRound

* Set only total round

* Remove console log

* Set correct configuration height

* Rename getLastHeight to getHeight

* Add set height

* Pass height to evm-worker

* Remove commit

* Remove import snapshot

* Pass transactions with gas used to transaction-pool-worker

* Pass transactions

* Remove failed transactions logic

* Replace array with map

* reAddTransactions on commit

* Remove forged transactions logic

* Add revert

* Remove fixInvalidStates

* Enable cleanup

* Move new milestone emits in store

* Remove direct configuration heigh set

* Fix p2p tests

* Fix api-evm tests

* Fix transaction-pool-service tests

* style: resolve style guide violations

---------

Co-authored-by: sebastijankuzner <[email protected]>
  • Loading branch information
sebastijankuzner and sebastijankuzner authored Oct 4, 2024
1 parent 2d57b9e commit d595d5f
Show file tree
Hide file tree
Showing 43 changed files with 157 additions and 259 deletions.
2 changes: 1 addition & 1 deletion packages/api-development/source/controllers/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NodeController extends Controller {

return {
data: {
constants: this.configuration.getMilestone(this.stateStore.getLastHeight()),
constants: this.configuration.getMilestone(this.stateStore.getHeight()),
core: {
version: this.app.version(),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api-development/source/controllers/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class RoundController extends Controller {
(_, index) => activeValidators[this.proposerSelector.getValidatorIndex(index)],
);

const height = this.stateStore.getLastHeight();
const height = this.stateStore.getHeight();

return {
height,
Expand Down
2 changes: 1 addition & 1 deletion packages/api-evm/source/actions/eth-block-number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe<{

beforeEach(async (context) => {
context.store = {
getLastHeight() {
getHeight() {
return height;
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/api-evm/source/actions/eth-block-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export class EthBlockNumberAction implements Contracts.Api.RPC.Action {
};

public async handle(parameters: []): Promise<string> {
return `0x${this.stateStore.getLastHeight().toString(16)}`;
return `0x${this.stateStore.getHeight().toString(16)}`;
}
}
18 changes: 8 additions & 10 deletions packages/bootstrap/source/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ export class Bootstrapper {
async #initState(): Promise<void> {
if (this.databaseService.isEmpty()) {
await this.#processGenesisBlock();
}
} else {
await this.#processBlocks();

await this.#processBlocks();
const commit = await this.databaseService.getLastCommit();
this.stateStore.setLastBlock(commit.block);
}

const commit = await this.databaseService.getLastCommit();
this.stateStore.setLastBlock(commit.block);
await this.validatorSet.restore();
}

Expand All @@ -133,16 +134,13 @@ export class Bootstrapper {
let totalRound = 0;

for await (const commit of this.databaseService.readCommits(
this.stateStore.getLastHeight() + 1,
this.stateStore.getHeight() + 1,
lastCommit.block.data.height,
)) {
totalRound += commit.block.data.round + 1;
totalRound += commit.proof.round + 1;
}

this.stateStore.setTotalRoundAndHeight(totalRound, lastCommit.block.data.height);
this.configuration.setHeight(lastCommit.block.data.height + 1);

console.log("Total round", totalRound, "Last height", lastCommit.block.data.height);
this.stateStore.setTotalRound(totalRound);
}

async #processCommit(commit: Contracts.Crypto.Commit): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/source/contracts/evm/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface WorkerFlags extends KeyValuePair {}
export interface WorkerScriptHandler {
boot(flags: WorkerFlags): Promise<void>;
setPeerCount(peerCount: number): Promise<void>;
commit(data: { block: string }): Promise<void>;
commit(height: number): Promise<void>;
}

export type WorkerFactory = () => Worker;
Expand All @@ -17,7 +17,7 @@ export type WorkerSubprocess = Subprocess<WorkerScriptHandler>;

export type WorkerSubprocessFactory = () => WorkerSubprocess;

export interface Worker extends WorkerScriptHandler, CommitHandler, EventListener {
export interface Worker extends Omit<WorkerScriptHandler, "commit">, CommitHandler, EventListener {
getQueueSize(): number;
kill(): Promise<number>;
}
6 changes: 4 additions & 2 deletions packages/contracts/source/contracts/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ export interface Store extends CommitHandler {
getGenesisCommit(): Commit;
setGenesisCommit(block: Commit): void;

getLastHeight(): number;
getLastBlock(): Block;
setLastBlock(block: Block): void;

setTotalRoundAndHeight(totalRound: number, height: number): void;
setHeight(height: number): void;
getHeight(): number;

setTotalRound(totalRound: number): void;
getTotalRound(): number;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { CommitHandler, Transaction } from "../crypto/index.js";
import { CommitHandler } from "../crypto/index.js";
export interface Client extends CommitHandler {
setFailedTransactions(transactions: Transaction[]): void;
getTransactionBytes(): Promise<Buffer[]>;
listSnapshots(): Promise<number[]>;
importSnapshot(height: number): Promise<void>;
getStatus(): Promise<{ height: number; version: string }>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export interface Mempool {

addTransaction(transaction: Transaction): Promise<void>;
removeTransaction(address: string, id: string): Promise<Transaction[]>;
removeForgedTransaction(address: string, id: string): Promise<Transaction[]>;

fixInvalidStates(): Promise<Transaction[]>;
reAddTransactions(addresses: string[]): Promise<Transaction[]>;

flush(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface SenderMempool {

addTransaction(transaction: Transaction): Promise<void>;
removeTransaction(id: string): Transaction[];
removeForgedTransaction(id: string): Transaction | undefined;
reAddTransactions(): Promise<Transaction[]>;
}

export type SenderMempoolFactory = (address: string) => Promise<SenderMempool>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ import { Transaction } from "../crypto/transactions.js";

export interface SenderState {
configure(address: string): Promise<SenderState>;
reset(): Promise<void>;
apply(transaction: Transaction): Promise<void>;
revert(transaction: Transaction): void;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Block, Transaction } from "../crypto/index.js";
import { Transaction } from "../crypto/index.js";

export interface Service {
getPoolSize(): number;

addTransaction(transaction: Transaction): Promise<void>;
reAddTransactions(): Promise<void>;
commit(block: Block, failedTransactionIds: string[]): Promise<void>;
commit(sendersAddresses: string[]): Promise<void>;
flush(): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommitHandler, Transaction } from "../crypto/index.js";
import { CommitHandler } from "../crypto/index.js";
import { EventListener } from "../kernel/index.js";
import { EventCallback, Subprocess } from "../kernel/ipc.js";
import { KeyValuePair } from "../types/index.js";
Expand All @@ -8,7 +8,7 @@ export interface WorkerFlags extends KeyValuePair {}
export interface WorkerScriptHandler {
boot(flags: WorkerFlags): Promise<void>;
getTransactions(): Promise<string[]>;
commit(data: { block: string; failedTransactions: string[] }): Promise<void>;
commit(height: number, sendersAddresses: string[]): Promise<void>;
setPeer(ip: string): Promise<void>;
forgetPeer(ip: string): Promise<void>;
start(): Promise<void>;
Expand All @@ -24,7 +24,6 @@ export type WorkerSubprocessFactory = () => WorkerSubprocess;
export interface Worker extends Omit<WorkerScriptHandler, "commit" | "getTransactions">, CommitHandler, EventListener {
getQueueSize(): number;
kill(): Promise<number>;
setFailedTransactions(transactions: Transaction[]): void;
getTransactionBytes(): Promise<Buffer[]>;
registerEventHandler(event: string, callback: EventCallback<any>): void;
}
18 changes: 2 additions & 16 deletions packages/evm-api-worker/source/handlers/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,10 @@ export class CommitHandler {
@inject(Identifiers.State.Store)
protected readonly stateStore!: Contracts.State.Store;

@inject(Identifiers.Cryptography.Configuration)
private readonly configuration!: Contracts.Crypto.Configuration;

@inject(Identifiers.Cryptography.Block.Factory)
private readonly blockFactory!: Contracts.Crypto.BlockFactory;

@inject(Identifiers.Services.Log.Service)
protected readonly logger!: Contracts.Kernel.Logger;

public async handle(data: { block: string }): Promise<void> {
try {
// TODO: Set height
this.configuration.setHeight(1);

const block = await this.blockFactory.fromHex(data.block);
this.stateStore.setLastBlock(block);
} catch (error) {
throw new Error(`Failed to commit block: ${error.message}`);
}
public async handle(height: number): Promise<void> {
this.stateStore.setHeight(height);
}
}
4 changes: 2 additions & 2 deletions packages/evm-api-worker/source/worker-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class WorkerScriptHandler implements Contracts.Evm.WorkerScriptHandler {
await this.#app.resolve(SetPeerCountHandler).handle(peerCount);
}

public async commit(data: { block: string; failedTransactions: string[] }): Promise<void> {
await this.#app.resolve(CommitHandler).handle(data);
public async commit(height: number): Promise<void> {
await this.#app.resolve(CommitHandler).handle(height);
}
}
12 changes: 1 addition & 11 deletions packages/evm-api-worker/source/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,10 @@ export class Worker implements Contracts.Evm.Worker {
}

async onCommit(unit: Contracts.Processor.ProcessableUnit): Promise<void> {
await this.ipcSubprocess.sendRequest("commit", {
block: unit.getBlock().serialized,
});
await this.ipcSubprocess.sendRequest("commit", unit.height);
}

public async setPeerCount(peerCount: number): Promise<void> {
await this.ipcSubprocess.sendRequest("setPeerCount", peerCount);
}

public async importSnapshot(height: number): Promise<void> {
await this.ipcSubprocess.sendRequest("importSnapshot", height);
}

public async commit(data: { block: string }): Promise<void> {
await this.ipcSubprocess.sendRequest("commit", data);
}
}
3 changes: 0 additions & 3 deletions packages/evm-consensus/source/validator-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ export class ValidatorSet implements Contracts.ValidatorSet.Service {
validatorWallets.push(validatorWallet);
}

console.log("Getting active validators", validatorWallets);
console.log(validatorWallets.map((v) => v));

return validatorWallets;
}
}
6 changes: 3 additions & 3 deletions packages/p2p/source/downloader/block-downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class BlockDownloader implements Contracts.P2P.Downloader {
#getLastRequestedBlockHeight(): number {
const latestJob = this.#downloadJobs.at(-1);
if (latestJob === undefined) {
return this.stateStore.getLastHeight();
return this.stateStore.getHeight();
}

return latestJob.heightTo;
Expand Down Expand Up @@ -213,7 +213,7 @@ export class BlockDownloader implements Contracts.P2P.Downloader {
}

#handleMissingBlocks(job: DownloadJob): void {
const configuration = this.configuration.getMilestone(this.stateStore.getLastHeight() + 1);
const configuration = this.configuration.getMilestone(this.stateStore.getHeight() + 1);

const size = job.blocks.reduce((size, block) => size + block.length, 0);

Expand Down Expand Up @@ -244,7 +244,7 @@ export class BlockDownloader implements Contracts.P2P.Downloader {

const newJob: DownloadJob = {
blocks: [],
heightFrom: index === 0 ? this.stateStore.getLastHeight() + 1 : job.heightFrom,
heightFrom: index === 0 ? this.stateStore.getHeight() + 1 : job.heightFrom,
heightTo: this.#downloadJobs.length === 1 ? this.#calculateHeightTo(peer) : job.heightTo,
peer,
peerHeight: peer.header.height - 1,
Expand Down
4 changes: 2 additions & 2 deletions packages/p2p/source/downloader/message-downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class MessageDownloader implements Contracts.P2P.Downloader {
public initialize(): void {
this.events.listen(Events.BlockEvent.Applied, {
handle: () => {
this.#downloadsByHeight.delete(this.stateStore.getLastHeight());
this.#fullDownloadsByHeight.delete(this.stateStore.getLastHeight());
this.#downloadsByHeight.delete(this.stateStore.getHeight());
this.#fullDownloadsByHeight.delete(this.stateStore.getHeight());
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe<{
const database = { findCommitBuffers: () => {} };
const store = {
getLastDownloadedBlock: () => {},
getLastHeight: () => {},
getHeight: () => {},
};

beforeEach((context) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe<{
const database = { findCommitBuffers: () => {} };
const store = {
getLastDownloadedBlock: () => {},
getLastHeight: () => {},
getHeight: () => {},
};

beforeEach((context) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class GetBlocksController implements Contracts.P2P.Controller {
const requestBlockHeight: number = request.payload.fromHeight;
const requestBlockLimit: number = request.payload.limit;

const lastHeight: number = this.stateStore.getLastHeight();
const lastHeight: number = this.stateStore.getHeight();
if (requestBlockHeight > lastHeight) {
return { blocks: [] };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("getHeaders", ({ it, assert }) => {
let port = 4007;
const version = "3.0.9";
const height = 387;
const store = { getLastHeight: () => height, isStarted: () => true };
const store = { getHeight: () => height, isStarted: () => true };
const appGet = {
[Identifiers.State.Store]: store,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/p2p/source/socket-server/utils/get-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getHeaders = (app: Contracts.Kernel.Application) => {
version: app.version(),
};

headers.height = app.get<Contracts.State.Store>(Identifiers.State.Store).getLastHeight();
headers.height = app.get<Contracts.State.Store>(Identifiers.State.Store).getHeight();

return headers;
};
12 changes: 0 additions & 12 deletions packages/processor/source/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export class BlockProcessor implements Contracts.Processor.BlockProcessor {
}
}

this.#setConfigurationHeight(unit);
await this.evm.onCommit(unit);
await this.validatorSet.onCommit(unit);
await this.proposerSelector.onCommit(unit);
Expand Down Expand Up @@ -148,17 +147,6 @@ export class BlockProcessor implements Contracts.Processor.BlockProcessor {
}
}

#setConfigurationHeight(unit: Contracts.Processor.ProcessableUnit): void {
// NOTE: The configuration is always set to the next height. To height which is going to be proposed.
this.configuration.setHeight(unit.height + 1);

if (this.configuration.isNewMilestone()) {
this.logger.notice(`Milestone change: ${JSON.stringify(this.configuration.getMilestoneDiff())}`);

void this.#emit(Events.CryptoEvent.MilestoneChanged);
}
}

#consumeGas(
block: Contracts.Crypto.Block,
processorResult: Contracts.Processor.BlockProcessorResult,
Expand Down
8 changes: 7 additions & 1 deletion packages/state/source/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ describe<{
sandbox: Sandbox;
store: Store;
logger: any;
eventDispatcher: any;
cryptoConfiguration: any;
}>("Store", ({ it, beforeEach, assert, spy, stub }) => {
beforeEach(async (context) => {
context.logger = {
notice: () => {},
};

context.eventDispatcher = {
dispatch: () => {},
};

context.cryptoConfiguration = {
getMilestoneDiff: () => ({}),
isNewMilestone: () => false,
Expand All @@ -23,6 +28,7 @@ describe<{
context.sandbox = new Sandbox();

context.sandbox.app.bind(Identifiers.Services.Log.Service).toConstantValue(context.logger);
context.sandbox.app.bind(Identifiers.Services.EventDispatcher.Service).toConstantValue(context.eventDispatcher);
context.sandbox.app.bind(Identifiers.Cryptography.Configuration).toConstantValue(context.cryptoConfiguration);
context.sandbox.app.bind(Identifiers.ServiceProvider.Configuration).toConstantValue({
getRequired: () => false, //snapshots.skipUnknownAttributes
Expand All @@ -32,7 +38,7 @@ describe<{
});

it("#initialize - should set height and totalRound", ({ store }) => {
assert.equal(store.getLastHeight(), 0);
assert.equal(store.getHeight(), 0);
assert.equal(store.getTotalRound(), 0);
});

Expand Down
Loading

0 comments on commit d595d5f

Please sign in to comment.