Skip to content

Commit 8ac79cc

Browse files
committed
Added NetworkUtils and removed BlockchainAccount utility
1 parent 3de9f4b commit 8ac79cc

File tree

8 files changed

+349
-116
lines changed

8 files changed

+349
-116
lines changed

packages/sequencer/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export * from "./worker/worker/WorkerReadyModule";
2222
export * from "./protocol/baselayer/BaseLayer";
2323
export * from "./protocol/baselayer/MinaBaseLayer";
2424
export * from "./protocol/baselayer/NoopBaseLayer";
25+
export * from "./protocol/baselayer/network-utils/MinaNetworkUtils";
26+
export * from "./protocol/baselayer/network-utils/RemoteNetworkUtils";
27+
export * from "./protocol/baselayer/network-utils/LightnetUtils";
28+
export * from "./protocol/baselayer/network-utils/LocalBlockchainUtils";
2529
export * from "./protocol/production/helpers/UntypedOption";
2630
export * from "./protocol/production/helpers/UntypedStateTransition";
2731
export * from "./protocol/production/tasks/TransactionProvingTask";

packages/sequencer/src/protocol/baselayer/MinaBaseLayer.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,38 @@ import {
1010
} from "../../sequencer/builder/SequencerModule";
1111
import { MinaTransactionSender } from "../../settlement/transactions/MinaTransactionSender";
1212
import { WithdrawalQueue } from "../../settlement/messages/WithdrawalQueue";
13+
import {
14+
Sequencer,
15+
SequencerModulesRecord,
16+
} from "../../sequencer/executor/Sequencer";
1317

1418
import { BaseLayer } from "./BaseLayer";
19+
import { LocalBlockchainUtils } from "./network-utils/LocalBlockchainUtils";
20+
import { LightnetUtils } from "./network-utils/LightnetUtils";
21+
import { RemoteNetworkUtils } from "./network-utils/RemoteNetworkUtils";
22+
23+
export type LocalMinaBaseLayerConfig = {
24+
type: "local";
25+
};
26+
27+
export type LightnetMinaBaseLayerConfig = {
28+
type: "lightnet";
29+
graphql: string;
30+
archive: string;
31+
accountManager?: string;
32+
};
33+
34+
export type RemoteMinaBaseLayerConfig = {
35+
type: "remote";
36+
graphql: string;
37+
archive: string;
38+
};
1539

1640
export interface MinaBaseLayerConfig {
1741
network:
18-
| {
19-
type: "local";
20-
}
21-
| {
22-
type: "lightnet";
23-
graphql: string;
24-
archive: string;
25-
accountManager?: string;
26-
}
27-
| {
28-
type: "remote";
29-
graphql: string;
30-
archive: string;
31-
};
42+
| LocalMinaBaseLayerConfig
43+
| LightnetMinaBaseLayerConfig
44+
| RemoteMinaBaseLayerConfig;
3245
}
3346

3447
@sequencerModule()
@@ -42,12 +55,20 @@ export class MinaBaseLayer
4255

4356
public constructor(
4457
@inject("AreProofsEnabled")
45-
private readonly areProofsEnabled: AreProofsEnabled
58+
private readonly areProofsEnabled: AreProofsEnabled,
59+
@inject("Sequencer")
60+
private readonly sequencer: Sequencer<SequencerModulesRecord>
4661
) {
4762
super();
4863
}
4964

5065
public dependencies() {
66+
const NetworkUtilsClass = match(this.config.network.type)
67+
.with("local", () => LocalBlockchainUtils)
68+
.with("lightnet", () => LightnetUtils)
69+
.with("remote", () => RemoteNetworkUtils)
70+
.exhaustive();
71+
5172
return {
5273
IncomingMessageAdapter: {
5374
useClass: MinaIncomingMessageAdapter,
@@ -60,9 +81,20 @@ export class MinaBaseLayer
6081
OutgoingMessageQueue: {
6182
useClass: WithdrawalQueue,
6283
},
84+
85+
NetworkUtils: {
86+
useClass: NetworkUtilsClass,
87+
},
6388
};
6489
}
6590

91+
public get networkUtils() {
92+
if (this.config.network.type === "remote") {
93+
throw new Error("NetworkUtils not available for remote networks");
94+
}
95+
return this.sequencer.dependencyContainer.resolve("NetworkUtils");
96+
}
97+
6698
public isLocalBlockChain(): boolean {
6799
return this.config.network.type === "local";
68100
}

packages/sequencer/src/protocol/baselayer/accounts/MinaBlockchainAccounts.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { inject, injectable } from "tsyringe";
2+
import {
3+
AccountUpdate,
4+
fetchAccount,
5+
fetchLastBlock,
6+
Lightnet,
7+
Mina,
8+
PrivateKey,
9+
PublicKey,
10+
} from "o1js";
11+
import { log, noop, range, sleep } from "@proto-kit/common";
12+
13+
import type {
14+
LightnetMinaBaseLayerConfig,
15+
MinaBaseLayer,
16+
MinaBaseLayerConfig,
17+
} from "../MinaBaseLayer";
18+
import { MinaTransactionSender } from "../../../settlement/transactions/MinaTransactionSender";
19+
20+
import { MinaNetworkUtils } from "./MinaNetworkUtils";
21+
22+
@injectable()
23+
export class LightnetUtils implements MinaNetworkUtils {
24+
public constructor(
25+
@inject("BaseLayer")
26+
private readonly baseLayer: MinaBaseLayer,
27+
@inject("TransactionSender")
28+
private readonly transactionSender: MinaTransactionSender
29+
) {}
30+
31+
public async waitForNetwork(): Promise<void> {
32+
const maxAttempts = 24;
33+
const delay = 5000;
34+
35+
let lastBlock;
36+
let attempt = 0;
37+
38+
const { config } = this.baseLayer;
39+
this.assertConfigLightnet(config);
40+
const graphqlEndpoint = config.network.graphql;
41+
42+
log.info("Waiting for network to be ready...");
43+
44+
while (!lastBlock) {
45+
attempt += 1;
46+
if (attempt > maxAttempts) {
47+
throw new Error(
48+
`Network was still not ready after ${(delay / 1000) * (attempt - 1)}s`
49+
);
50+
}
51+
try {
52+
// eslint-disable-next-line no-await-in-loop
53+
lastBlock = await fetchLastBlock(graphqlEndpoint);
54+
} catch (e) {
55+
// Ignore errors
56+
noop();
57+
}
58+
// eslint-disable-next-line no-await-in-loop
59+
await sleep(delay);
60+
}
61+
62+
log.provable.info("Network is ready", lastBlock);
63+
}
64+
65+
private assertConfigLightnet(
66+
config: MinaBaseLayerConfig
67+
): asserts config is { network: LightnetMinaBaseLayerConfig } {
68+
if (config.network.type !== "lightnet") {
69+
throw new Error("Config provided is not of type 'lightnet'");
70+
}
71+
}
72+
73+
public async faucet(
74+
receiver: PublicKey,
75+
fundingAmount = 1000 * 1e9,
76+
fee = 0.1 * 1e9
77+
) {
78+
const [faucetDonor] = await this.getFundedAccounts(1);
79+
80+
const account = await fetchAccount({ publicKey: receiver });
81+
82+
log.provable.info(
83+
`Dripping ${fundingAmount / 1e9} MINA from ${faucetDonor.toBase58()} to ${receiver.toBase58()}`
84+
);
85+
86+
const faucetDonorPublicKey = faucetDonor.toPublicKey();
87+
88+
const tx = await Mina.transaction(
89+
{
90+
sender: faucetDonorPublicKey,
91+
fee,
92+
},
93+
async () => {
94+
// if the destination account does not exist yet, pay the creation fee for it
95+
if (account.error) {
96+
AccountUpdate.fundNewAccount(faucetDonorPublicKey);
97+
}
98+
99+
AccountUpdate.createSigned(faucetDonorPublicKey).balance.subInPlace(
100+
fundingAmount
101+
);
102+
AccountUpdate.create(receiver).balance.addInPlace(fundingAmount);
103+
}
104+
);
105+
106+
tx.sign([faucetDonor]);
107+
108+
await this.transactionSender.proveAndSendTransaction(tx, "included");
109+
110+
log.provable.info(
111+
`Funded account ${receiver.toBase58()} with ${fundingAmount / 1e9} MINA`
112+
);
113+
114+
await Lightnet.releaseKeyPair({
115+
publicKey: faucetDonor.toPublicKey().toBase58(),
116+
lightnetAccountManagerEndpoint: this.getAccountManagerEndpoint(),
117+
});
118+
}
119+
120+
private getAccountManagerEndpoint() {
121+
const {
122+
baseLayer: { config },
123+
} = this;
124+
125+
this.assertConfigLightnet(config);
126+
127+
if (config.network.accountManager === undefined) {
128+
throw new Error(
129+
"Wanted to retrieve funded keypairs, but accountManager endpoint is missing in config"
130+
);
131+
}
132+
133+
return config.network.accountManager;
134+
}
135+
136+
public async getFundedAccounts(num: number = 1): Promise<PrivateKey[]> {
137+
const lightnetAccountManagerEndpoint = this.getAccountManagerEndpoint();
138+
139+
return await Promise.all(
140+
range(num).map(async (i) => {
141+
const pair = await Lightnet.acquireKeyPair({
142+
isRegularAccount: true,
143+
lightnetAccountManagerEndpoint,
144+
});
145+
return pair.privateKey;
146+
})
147+
);
148+
}
149+
}

0 commit comments

Comments
 (0)