Skip to content

Commit 1cede08

Browse files
committed
Fixed graphql test
1 parent 7de7a5b commit 1cede08

File tree

4 files changed

+296
-22
lines changed

4 files changed

+296
-22
lines changed

packages/sequencer/test/TestingSequencer.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,36 @@ export interface DefaultTestingSequencerModules extends SequencerModulesRecord {
3030
SequencerStartupModule: typeof SequencerStartupModule;
3131
}
3232

33-
export function testingSequencerFromModules<
33+
export function testingSequencerModules<
3434
AdditionalModules extends SequencerModulesRecord,
3535
AdditionalTaskWorkerModules extends TaskWorkerModulesRecord,
3636
>(
3737
modules: AdditionalModules,
3838
additionalTaskWorkerModules?: AdditionalTaskWorkerModules
39-
): TypedClass<Sequencer<DefaultTestingSequencerModules & AdditionalModules>> {
39+
) {
4040
const taskWorkerModule = LocalTaskWorkerModule.from({
4141
...VanillaTaskWorkerModules.withoutSettlement(),
4242
...additionalTaskWorkerModules,
4343
});
4444

45-
const defaultModules: DefaultTestingSequencerModules = {
45+
const defaultModules = {
4646
Database: InMemoryDatabase,
4747
Mempool: PrivateMempool,
4848
BaseLayer: NoopBaseLayer,
49-
// LocalTaskWorkerModule: taskWorkerModule,
49+
LocalTaskWorkerModule: taskWorkerModule,
5050
BatchProducerModule,
5151
BlockProducerModule,
5252
BlockTrigger: ManualBlockTrigger,
5353
TaskQueue: LocalTaskQueue,
5454
FeeStrategy: ConstantFeeStrategy,
55-
} as DefaultTestingSequencerModules;
55+
SequencerStartupModule,
56+
} satisfies DefaultTestingSequencerModules;
5657

57-
return Sequencer.from({
58-
modules: {
59-
...defaultModules,
60-
...modules,
61-
// We need to make sure that the taskworkermodule is initialized last
62-
LocalTaskWorkerModule: taskWorkerModule,
63-
SequencerStartupModule,
64-
},
65-
});
58+
return {
59+
...defaultModules,
60+
...modules,
61+
// We need to make sure that the taskworkermodule is initialized last
62+
LocalTaskWorkerModule: defaultModules.LocalTaskWorkerModule,
63+
SequencerStartupModule: defaultModules.SequencerStartupModule,
64+
};
6665
}

packages/sequencer/test/integration/BlockProduction.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
} from "../../src";
3434
import {
3535
DefaultTestingSequencerModules,
36-
testingSequencerFromModules,
36+
testingSequencerModules,
3737
} from "../TestingSequencer";
3838

3939
import { Balance } from "./mocks/Balance";
@@ -111,7 +111,10 @@ describe("block production", () => {
111111
},
112112
});
113113

114-
const sequencerClass = testingSequencerFromModules({});
114+
const modules = testingSequencerModules({});
115+
const sequencerClass = Sequencer.from({
116+
modules,
117+
});
115118

116119
// TODO Analyze how we can get rid of the library import for mandatory modules
117120
const protocolClass = Protocol.from({
@@ -130,8 +133,8 @@ describe("block production", () => {
130133

131134
app.configure({
132135
Sequencer: {
133-
Database: {},
134-
BlockTrigger: {},
136+
// Database: {},
137+
// BlockTrigger: {},
135138
Mempool: {},
136139
BatchProducerModule: {},
137140
BlockProducerModule: {},
@@ -140,7 +143,7 @@ describe("block production", () => {
140143
TaskQueue: {},
141144
FeeStrategy: {},
142145
SequencerStartupModule: {},
143-
},
146+
} as any,
144147
Runtime: {
145148
Balance: {},
146149
NoopRuntime: {},
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
import {
2+
AppChain,
3+
BlockStorageNetworkStateModule,
4+
InMemorySigner,
5+
InMemoryTransactionSender,
6+
StateServiceQueryModule,
7+
} from "@proto-kit/sdk";
8+
import { PrivateKey, PublicKey } from "o1js";
9+
import { Runtime, runtimeMethod, runtimeModule } from "@proto-kit/module";
10+
import { Protocol, State, state } from "@proto-kit/protocol";
11+
import {
12+
Balance,
13+
Balances,
14+
BalancesKey,
15+
TokenId,
16+
VanillaProtocolModules,
17+
VanillaRuntimeModules,
18+
UInt64,
19+
} from "@proto-kit/library";
20+
import { log } from "@proto-kit/common";
21+
import {
22+
BatchProducerModule,
23+
InMemoryDatabase,
24+
LocalTaskQueue,
25+
LocalTaskWorkerModule,
26+
NoopBaseLayer,
27+
PrivateMempool,
28+
Sequencer,
29+
BlockProducerModule,
30+
VanillaTaskWorkerModules,
31+
SequencerStartupModule,
32+
ManualBlockTrigger,
33+
} from "@proto-kit/sequencer";
34+
import {
35+
BatchStorageResolver,
36+
GraphqlSequencerModule,
37+
GraphqlServer,
38+
MempoolResolver,
39+
MerkleWitnessResolver,
40+
NodeStatusResolver,
41+
QueryGraphqlModule,
42+
BlockResolver,
43+
} from "@proto-kit/api";
44+
import { container } from "tsyringe";
45+
46+
@runtimeModule()
47+
export class TestBalances extends Balances {
48+
/**
49+
* We use `satisfies` here in order to be able to access
50+
* presets by key in a type safe way.
51+
*/
52+
// public static presets = {} satisfies Presets<object>;
53+
54+
@state() public totalSupply = State.from<UInt64>(UInt64);
55+
56+
@runtimeMethod()
57+
public async getBalanceForUser(
58+
tokenId: TokenId,
59+
address: PublicKey
60+
): Promise<Balance> {
61+
return await super.getBalance(tokenId, address);
62+
}
63+
64+
@runtimeMethod()
65+
public async addBalance(
66+
tokenId: TokenId,
67+
address: PublicKey,
68+
balance: UInt64
69+
) {
70+
const totalSupply = await this.totalSupply.get();
71+
await this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(balance));
72+
73+
const previous = await this.balances.get(
74+
new BalancesKey({ tokenId, address })
75+
);
76+
await this.balances.set(
77+
new BalancesKey({ tokenId, address }),
78+
previous.orElse(UInt64.zero).add(balance)
79+
);
80+
}
81+
}
82+
83+
export async function startGraphqlServer() {
84+
log.setLevel("DEBUG");
85+
86+
const appChain = AppChain.from({
87+
Runtime: Runtime.from({
88+
modules: VanillaRuntimeModules.with({
89+
Balances: TestBalances,
90+
}),
91+
}),
92+
93+
Protocol: Protocol.from({
94+
modules: VanillaProtocolModules.with({}),
95+
}),
96+
97+
Sequencer: Sequencer.from({
98+
modules: {
99+
Database: InMemoryDatabase,
100+
// Database: PrismaRedisDatabase,
101+
102+
Mempool: PrivateMempool,
103+
GraphqlServer,
104+
LocalTaskWorkerModule: LocalTaskWorkerModule.from(
105+
VanillaTaskWorkerModules.withoutSettlement()
106+
),
107+
108+
BaseLayer: NoopBaseLayer,
109+
BatchProducerModule,
110+
BlockProducerModule,
111+
BlockTrigger: ManualBlockTrigger,
112+
TaskQueue: LocalTaskQueue,
113+
// SettlementModule: SettlementModule,
114+
115+
Graphql: GraphqlSequencerModule.from({
116+
modules: {
117+
MempoolResolver,
118+
QueryGraphqlModule,
119+
BatchStorageResolver,
120+
BlockResolver,
121+
NodeStatusResolver,
122+
MerkleWitnessResolver,
123+
},
124+
125+
config: {
126+
MempoolResolver: {},
127+
QueryGraphqlModule: {},
128+
BatchStorageResolver: {},
129+
NodeStatusResolver: {},
130+
MerkleWitnessResolver: {},
131+
BlockResolver: {},
132+
},
133+
}),
134+
135+
SequencerStartupModule,
136+
},
137+
}),
138+
139+
modules: {
140+
Signer: InMemorySigner,
141+
TransactionSender: InMemoryTransactionSender,
142+
QueryTransportModule: StateServiceQueryModule,
143+
NetworkStateTransportModule: BlockStorageNetworkStateModule,
144+
},
145+
});
146+
147+
appChain.configure({
148+
Runtime: {
149+
Balances: {},
150+
},
151+
152+
Protocol: {
153+
BlockProver: {},
154+
StateTransitionProver: {},
155+
AccountState: {},
156+
BlockHeight: {},
157+
TransactionFee: {
158+
tokenId: 0n,
159+
feeRecipient: PrivateKey.random().toPublicKey().toBase58(),
160+
baseFee: 0n,
161+
methods: {},
162+
perWeightUnitFee: 0n,
163+
},
164+
LastStateRoot: {},
165+
},
166+
167+
Sequencer: {
168+
GraphqlServer: {
169+
port: 8080,
170+
host: "0.0.0.0",
171+
graphiql: true,
172+
},
173+
SequencerStartupModule: {},
174+
175+
// SettlementModule: {
176+
// address: PrivateKey.random().toPublicKey(),
177+
// feepayer: PrivateKey.random(),
178+
// },
179+
180+
Graphql: {
181+
QueryGraphqlModule: {},
182+
MempoolResolver: {},
183+
BatchStorageResolver: {},
184+
NodeStatusResolver: {},
185+
BlockResolver: {},
186+
MerkleWitnessResolver: {},
187+
},
188+
189+
Database: {
190+
// redis: {
191+
// host: "localhost",
192+
// port: 6379,
193+
// password: "password",
194+
// },
195+
// prisma: {
196+
// connection: {
197+
// host: "localhost",
198+
// password: "password",
199+
// username: "user",
200+
// port: 5432,
201+
// db: {
202+
// name: "protokit",
203+
// },
204+
// },
205+
// },
206+
},
207+
208+
Mempool: {},
209+
BatchProducerModule: {},
210+
LocalTaskWorkerModule: VanillaTaskWorkerModules.defaultConfig(),
211+
BaseLayer: {},
212+
TaskQueue: {},
213+
214+
BlockProducerModule: {
215+
allowEmptyBlock: true,
216+
},
217+
218+
BlockTrigger: {},
219+
},
220+
221+
TransactionSender: {},
222+
QueryTransportModule: {},
223+
NetworkStateTransportModule: {},
224+
225+
Signer: {
226+
signer: PrivateKey.random(),
227+
},
228+
});
229+
230+
await appChain.start(false, container.createChildContainer());
231+
// const pk = PublicKey.fromBase58(
232+
// "B62qmETai5Y8vvrmWSU8F4NX7pTyPqYLMhc1pgX3wD8dGc2wbCWUcqP"
233+
// );
234+
235+
const balances = appChain.runtime.resolve("Balances");
236+
237+
const priv = PrivateKey.fromBase58(
238+
"EKFEMDTUV2VJwcGmCwNKde3iE1cbu7MHhzBqTmBtGAd6PdsLTifY"
239+
);
240+
241+
const tokenId = TokenId.from(0);
242+
243+
const as = await appChain.query.protocol.AccountState.accountState.get(
244+
priv.toPublicKey()
245+
);
246+
const nonce = Number(as?.nonce.toString() ?? "0");
247+
248+
const tx = await appChain.transaction(
249+
priv.toPublicKey(),
250+
async () => {
251+
await balances.addBalance(tokenId, priv.toPublicKey(), UInt64.from(1000));
252+
},
253+
{
254+
nonce,
255+
}
256+
);
257+
appChain.resolve("Signer").config.signer = priv;
258+
await tx.sign();
259+
await tx.send();
260+
261+
const tx2 = await appChain.transaction(
262+
priv.toPublicKey(),
263+
async () => {
264+
await balances.addBalance(tokenId, priv.toPublicKey(), UInt64.from(1000));
265+
},
266+
{ nonce: nonce + 1 }
267+
);
268+
await tx2.sign();
269+
await tx2.send();
270+
271+
return appChain;
272+
}

packages/stack/test/graphql/graphql.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { beforeAll } from "@jest/globals";
2323
import { container } from "tsyringe";
2424

25-
import { startServer, TestBalances } from "../../src/scripts/graphql/server";
25+
import { startGraphqlServer, TestBalances } from "./graphql-server";
2626

2727
const pk = PrivateKey.random();
2828

@@ -93,12 +93,12 @@ function prepareClient() {
9393

9494
describe("graphql client test", () => {
9595
let appChain: ReturnType<typeof prepareClient>;
96-
let server: Awaited<ReturnType<typeof startServer>>;
96+
let server: Awaited<ReturnType<typeof startGraphqlServer>>;
9797
let trigger: ManualBlockTrigger;
9898
const tokenId = TokenId.from(0);
9999

100100
beforeAll(async () => {
101-
server = await startServer();
101+
server = await startGraphqlServer();
102102

103103
await sleep(2000);
104104

0 commit comments

Comments
 (0)