@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.03f7ef2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -6
- package/dest/archiver/archiver.d.ts +201 -94
- package/dest/archiver/archiver.d.ts.map +1 -1
- package/dest/archiver/archiver.js +1141 -396
- package/dest/archiver/archiver_store.d.ts +171 -83
- package/dest/archiver/archiver_store.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
- package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
- package/dest/archiver/archiver_store_test_suite.js +2389 -393
- package/dest/archiver/config.d.ts +7 -22
- package/dest/archiver/config.d.ts.map +1 -1
- package/dest/archiver/config.js +30 -14
- package/dest/archiver/errors.d.ts +33 -1
- package/dest/archiver/errors.d.ts.map +1 -1
- package/dest/archiver/errors.js +49 -0
- package/dest/archiver/index.d.ts +3 -4
- package/dest/archiver/index.d.ts.map +1 -1
- package/dest/archiver/index.js +1 -2
- package/dest/archiver/instrumentation.d.ts +14 -6
- package/dest/archiver/instrumentation.d.ts.map +1 -1
- package/dest/archiver/instrumentation.js +69 -17
- package/dest/archiver/kv_archiver_store/block_store.d.ts +91 -21
- package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/block_store.js +476 -86
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +4 -4
- package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_class_store.js +13 -19
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +12 -9
- package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/contract_instance_store.js +30 -16
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +80 -75
- package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/kv_archiver_store.js +142 -83
- package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
- package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/log_store.js +153 -113
- package/dest/archiver/kv_archiver_store/message_store.d.ts +25 -18
- package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
- package/dest/archiver/kv_archiver_store/message_store.js +152 -49
- package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
- package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
- package/dest/archiver/l1/bin/retrieve-calldata.js +149 -0
- package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
- package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
- package/dest/archiver/l1/calldata_retriever.js +471 -0
- package/dest/archiver/l1/data_retrieval.d.ts +90 -0
- package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
- package/dest/archiver/l1/data_retrieval.js +331 -0
- package/dest/archiver/l1/debug_tx.d.ts +19 -0
- package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
- package/dest/archiver/l1/debug_tx.js +73 -0
- package/dest/archiver/l1/spire_proposer.d.ts +70 -0
- package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
- package/dest/archiver/l1/spire_proposer.js +157 -0
- package/dest/archiver/l1/trace_tx.d.ts +97 -0
- package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
- package/dest/archiver/l1/trace_tx.js +91 -0
- package/dest/archiver/l1/types.d.ts +12 -0
- package/dest/archiver/l1/types.d.ts.map +1 -0
- package/dest/archiver/l1/types.js +3 -0
- package/dest/archiver/l1/validate_trace.d.ts +29 -0
- package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
- package/dest/archiver/l1/validate_trace.js +150 -0
- package/dest/archiver/structs/data_retrieval.d.ts +1 -1
- package/dest/archiver/structs/inbox_message.d.ts +15 -0
- package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
- package/dest/archiver/structs/inbox_message.js +39 -0
- package/dest/archiver/structs/published.d.ts +2 -11
- package/dest/archiver/structs/published.d.ts.map +1 -1
- package/dest/archiver/structs/published.js +1 -1
- package/dest/archiver/validation.d.ts +17 -0
- package/dest/archiver/validation.d.ts.map +1 -0
- package/dest/archiver/validation.js +98 -0
- package/dest/factory.d.ts +9 -14
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +22 -52
- package/dest/index.d.ts +2 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -1
- package/dest/rpc/index.d.ts +2 -3
- package/dest/rpc/index.d.ts.map +1 -1
- package/dest/rpc/index.js +1 -4
- package/dest/test/index.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts +16 -8
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +19 -14
- package/dest/test/mock_l1_to_l2_message_source.d.ts +9 -6
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +21 -7
- package/dest/test/mock_l2_block_source.d.ts +52 -13
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +140 -15
- package/dest/test/mock_structs.d.ts +10 -0
- package/dest/test/mock_structs.d.ts.map +1 -0
- package/dest/test/mock_structs.js +38 -0
- package/package.json +29 -30
- package/src/archiver/archiver.ts +1477 -501
- package/src/archiver/archiver_store.ts +197 -88
- package/src/archiver/archiver_store_test_suite.ts +2403 -350
- package/src/archiver/config.ts +38 -46
- package/src/archiver/errors.ts +85 -0
- package/src/archiver/index.ts +2 -3
- package/src/archiver/instrumentation.ts +91 -22
- package/src/archiver/kv_archiver_store/block_store.ts +640 -101
- package/src/archiver/kv_archiver_store/contract_class_store.ts +14 -24
- package/src/archiver/kv_archiver_store/contract_instance_store.ts +36 -28
- package/src/archiver/kv_archiver_store/kv_archiver_store.ts +193 -113
- package/src/archiver/kv_archiver_store/log_store.ts +205 -127
- package/src/archiver/kv_archiver_store/message_store.ts +213 -54
- package/src/archiver/l1/README.md +98 -0
- package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
- package/src/archiver/l1/calldata_retriever.ts +641 -0
- package/src/archiver/l1/data_retrieval.ts +512 -0
- package/src/archiver/l1/debug_tx.ts +99 -0
- package/src/archiver/l1/spire_proposer.ts +160 -0
- package/src/archiver/l1/trace_tx.ts +128 -0
- package/src/archiver/l1/types.ts +13 -0
- package/src/archiver/l1/validate_trace.ts +211 -0
- package/src/archiver/structs/inbox_message.ts +41 -0
- package/src/archiver/structs/published.ts +1 -11
- package/src/archiver/validation.ts +124 -0
- package/src/factory.ts +28 -69
- package/src/index.ts +1 -1
- package/src/rpc/index.ts +1 -5
- package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
- package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
- package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
- package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
- package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
- package/src/test/fixtures/trace_transaction-proxied.json +128 -0
- package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
- package/src/test/mock_archiver.ts +22 -16
- package/src/test/mock_l1_to_l2_message_source.ts +20 -8
- package/src/test/mock_l2_block_source.ts +186 -21
- package/src/test/mock_structs.ts +50 -0
- package/dest/archiver/data_retrieval.d.ts +0 -74
- package/dest/archiver/data_retrieval.d.ts.map +0 -1
- package/dest/archiver/data_retrieval.js +0 -283
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
- package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
- package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
- package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
- package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
- package/src/archiver/data_retrieval.ts +0 -422
- package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
- package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
- package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
|
@@ -4,70 +4,92 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
4
4
|
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
}
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
|
|
8
|
+
import { EpochCache } from '@aztec/epoch-cache';
|
|
9
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
10
|
+
import { BlockTagTooOldError, InboxContract, RollupContract } from '@aztec/ethereum/contracts';
|
|
11
|
+
import { maxBigint } from '@aztec/foundation/bigint';
|
|
12
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
13
|
+
import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
|
|
14
|
+
import { merge, pick } from '@aztec/foundation/collection';
|
|
15
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
9
16
|
import { createLogger } from '@aztec/foundation/log';
|
|
17
|
+
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
10
18
|
import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/running-promise';
|
|
11
19
|
import { count } from '@aztec/foundation/string';
|
|
12
|
-
import { elapsed } from '@aztec/foundation/timer';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import { L2BlockSourceEvents } from '@aztec/stdlib/block';
|
|
17
|
-
import {
|
|
20
|
+
import { DateProvider, Timer, elapsed } from '@aztec/foundation/timer';
|
|
21
|
+
import { isDefined } from '@aztec/foundation/types';
|
|
22
|
+
import { ContractClassPublishedEvent, PrivateFunctionBroadcastedEvent, UtilityFunctionBroadcastedEvent } from '@aztec/protocol-contracts/class-registry';
|
|
23
|
+
import { ContractInstancePublishedEvent, ContractInstanceUpdatedEvent } from '@aztec/protocol-contracts/instance-registry';
|
|
24
|
+
import { CommitteeAttestation, L2Block, L2BlockSourceEvents, PublishedL2Block } from '@aztec/stdlib/block';
|
|
25
|
+
import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
26
|
+
import { computePublicBytecodeCommitment, isValidPrivateFunctionMembershipProof, isValidUtilityFunctionMembershipProof } from '@aztec/stdlib/contract';
|
|
18
27
|
import { getEpochAtSlot, getEpochNumberAtTimestamp, getSlotAtTimestamp, getSlotRangeForEpoch, getTimestampRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
|
|
19
|
-
import {
|
|
28
|
+
import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
|
|
29
|
+
import { getTelemetryClient, trackSpan } from '@aztec/telemetry-client';
|
|
20
30
|
import { EventEmitter } from 'events';
|
|
21
31
|
import groupBy from 'lodash.groupby';
|
|
22
|
-
import { createPublicClient, fallback,
|
|
23
|
-
import {
|
|
24
|
-
import { NoBlobBodiesFoundError } from './errors.js';
|
|
32
|
+
import { createPublicClient, fallback, http } from 'viem';
|
|
33
|
+
import { InitialCheckpointNumberNotSequentialError, NoBlobBodiesFoundError } from './errors.js';
|
|
25
34
|
import { ArchiverInstrumentation } from './instrumentation.js';
|
|
35
|
+
import { retrieveCheckpointsFromRollup, retrieveL1ToL2Message, retrieveL1ToL2Messages, retrievedToPublishedCheckpoint } from './l1/data_retrieval.js';
|
|
36
|
+
import { validateAndLogTraceAvailability } from './l1/validate_trace.js';
|
|
37
|
+
import { validateCheckpointAttestations } from './validation.js';
|
|
38
|
+
function mapArchiverConfig(config) {
|
|
39
|
+
return {
|
|
40
|
+
pollingIntervalMs: config.archiverPollingIntervalMS,
|
|
41
|
+
batchSize: config.archiverBatchSize,
|
|
42
|
+
skipValidateBlockAttestations: config.skipValidateBlockAttestations,
|
|
43
|
+
maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
|
|
44
|
+
ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts
|
|
45
|
+
};
|
|
46
|
+
}
|
|
26
47
|
/**
|
|
27
|
-
* Pulls
|
|
48
|
+
* Pulls checkpoints in a non-blocking manner and provides interface for their retrieval.
|
|
28
49
|
* Responsible for handling robust L1 polling so that other components do not need to
|
|
29
50
|
* concern themselves with it.
|
|
30
51
|
*/ export class Archiver extends EventEmitter {
|
|
31
52
|
publicClient;
|
|
53
|
+
debugClient;
|
|
32
54
|
l1Addresses;
|
|
33
55
|
dataStore;
|
|
34
56
|
config;
|
|
35
|
-
|
|
57
|
+
blobClient;
|
|
58
|
+
epochCache;
|
|
59
|
+
dateProvider;
|
|
36
60
|
instrumentation;
|
|
37
61
|
l1constants;
|
|
38
62
|
log;
|
|
39
|
-
/**
|
|
40
|
-
* A promise in which we will be continually fetching new L2 blocks.
|
|
41
|
-
*/ runningPromise;
|
|
63
|
+
/** A loop in which we will be continually fetching new checkpoints. */ runningPromise;
|
|
42
64
|
rollup;
|
|
43
65
|
inbox;
|
|
44
66
|
store;
|
|
45
67
|
l1BlockNumber;
|
|
46
68
|
l1Timestamp;
|
|
69
|
+
initialSyncComplete;
|
|
70
|
+
initialSyncPromise;
|
|
71
|
+
/** Queue of blocks to be added to the store, processed by the sync loop. */ blockQueue;
|
|
47
72
|
tracer;
|
|
48
73
|
/**
|
|
49
74
|
* Creates a new instance of the Archiver.
|
|
50
75
|
* @param publicClient - A client for interacting with the Ethereum node.
|
|
76
|
+
* @param debugClient - A client for interacting with the Ethereum node for debug/trace methods.
|
|
51
77
|
* @param rollupAddress - Ethereum address of the rollup contract.
|
|
52
78
|
* @param inboxAddress - Ethereum address of the inbox contract.
|
|
53
79
|
* @param registryAddress - Ethereum address of the registry contract.
|
|
54
80
|
* @param pollingIntervalMs - The interval for polling for L1 logs (in milliseconds).
|
|
55
81
|
* @param store - An archiver data store for storage & retrieval of blocks, encrypted logs & contract data.
|
|
56
82
|
* @param log - A logger.
|
|
57
|
-
*/ constructor(publicClient, l1Addresses, dataStore, config,
|
|
58
|
-
super(), this.publicClient = publicClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.
|
|
83
|
+
*/ constructor(publicClient, debugClient, l1Addresses, dataStore, config, blobClient, epochCache, dateProvider, instrumentation, l1constants, log = createLogger('archiver')){
|
|
84
|
+
super(), this.publicClient = publicClient, this.debugClient = debugClient, this.l1Addresses = l1Addresses, this.dataStore = dataStore, this.config = config, this.blobClient = blobClient, this.epochCache = epochCache, this.dateProvider = dateProvider, this.instrumentation = instrumentation, this.l1constants = l1constants, this.log = log, this.initialSyncComplete = false, this.blockQueue = [];
|
|
59
85
|
this.tracer = instrumentation.tracer;
|
|
60
86
|
this.store = new ArchiverStoreHelper(dataStore);
|
|
61
|
-
this.rollup =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
this.
|
|
67
|
-
address: l1Addresses.inboxAddress.toString(),
|
|
68
|
-
abi: InboxAbi,
|
|
69
|
-
client: publicClient
|
|
70
|
-
});
|
|
87
|
+
this.rollup = new RollupContract(publicClient, l1Addresses.rollupAddress);
|
|
88
|
+
this.inbox = new InboxContract(publicClient, l1Addresses.inboxAddress);
|
|
89
|
+
this.initialSyncPromise = promiseWithResolvers();
|
|
90
|
+
// Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
|
|
91
|
+
// are done as fast as possible. This then gets updated once the initial sync completes.
|
|
92
|
+
this.runningPromise = new RunningPromise(()=>this.sync(), this.log, this.config.pollingIntervalMs / 10, makeLoggingErrorHandler(this.log, NoBlobBodiesFoundError, BlockTagTooOldError));
|
|
71
93
|
}
|
|
72
94
|
/**
|
|
73
95
|
* Creates a new instance of the Archiver and blocks until it syncs from chain.
|
|
@@ -79,59 +101,151 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
79
101
|
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
|
|
80
102
|
const publicClient = createPublicClient({
|
|
81
103
|
chain: chain.chainInfo,
|
|
82
|
-
transport: fallback(config.l1RpcUrls.map((url)=>http(url
|
|
104
|
+
transport: fallback(config.l1RpcUrls.map((url)=>http(url, {
|
|
105
|
+
batch: false
|
|
106
|
+
}))),
|
|
83
107
|
pollingInterval: config.viemPollingIntervalMS
|
|
84
108
|
});
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
109
|
+
// Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
|
|
110
|
+
const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
|
|
111
|
+
const debugClient = createPublicClient({
|
|
112
|
+
chain: chain.chainInfo,
|
|
113
|
+
transport: fallback(debugRpcUrls.map((url)=>http(url, {
|
|
114
|
+
batch: false
|
|
115
|
+
}))),
|
|
116
|
+
pollingInterval: config.viemPollingIntervalMS
|
|
89
117
|
});
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
rollup.
|
|
118
|
+
const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
|
|
119
|
+
const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] = await Promise.all([
|
|
120
|
+
rollup.getL1StartBlock(),
|
|
121
|
+
rollup.getL1GenesisTime(),
|
|
122
|
+
rollup.getProofSubmissionEpochs(),
|
|
123
|
+
rollup.getGenesisArchiveTreeRoot(),
|
|
124
|
+
rollup.getSlashingProposerAddress()
|
|
93
125
|
]);
|
|
126
|
+
const l1StartBlockHash = await publicClient.getBlock({
|
|
127
|
+
blockNumber: l1StartBlock,
|
|
128
|
+
includeTransactions: false
|
|
129
|
+
}).then((block)=>Buffer32.fromString(block.hash));
|
|
94
130
|
const { aztecEpochDuration: epochDuration, aztecSlotDuration: slotDuration, ethereumSlotDuration } = config;
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
batchSize: config.archiverBatchSize ?? 100
|
|
98
|
-
}, deps.blobSinkClient, await ArchiverInstrumentation.new(deps.telemetry, ()=>archiverStore.estimateSize()), {
|
|
131
|
+
const l1Constants = {
|
|
132
|
+
l1StartBlockHash,
|
|
99
133
|
l1StartBlock,
|
|
100
134
|
l1GenesisTime,
|
|
101
135
|
epochDuration,
|
|
102
136
|
slotDuration,
|
|
103
|
-
ethereumSlotDuration
|
|
104
|
-
|
|
137
|
+
ethereumSlotDuration,
|
|
138
|
+
proofSubmissionEpochs: Number(proofSubmissionEpochs),
|
|
139
|
+
genesisArchiveRoot: Fr.fromHexString(genesisArchiveRoot)
|
|
140
|
+
};
|
|
141
|
+
const opts = merge({
|
|
142
|
+
pollingIntervalMs: 10_000,
|
|
143
|
+
batchSize: 100,
|
|
144
|
+
maxAllowedEthClientDriftSeconds: 300,
|
|
145
|
+
ethereumAllowNoDebugHosts: false
|
|
146
|
+
}, mapArchiverConfig(config));
|
|
147
|
+
const epochCache = deps.epochCache ?? await EpochCache.create(config.l1Contracts.rollupAddress, config, deps);
|
|
148
|
+
const telemetry = deps.telemetry ?? getTelemetryClient();
|
|
149
|
+
const archiver = new Archiver(publicClient, debugClient, {
|
|
150
|
+
...config.l1Contracts,
|
|
151
|
+
slashingProposerAddress
|
|
152
|
+
}, archiverStore, opts, deps.blobClient, epochCache, deps.dateProvider ?? new DateProvider(), await ArchiverInstrumentation.new(telemetry, ()=>archiverStore.estimateSize()), l1Constants);
|
|
105
153
|
await archiver.start(blockUntilSynced);
|
|
106
154
|
return archiver;
|
|
107
155
|
}
|
|
156
|
+
/** Updates archiver config */ updateConfig(newConfig) {
|
|
157
|
+
this.config = merge(this.config, mapArchiverConfig(newConfig));
|
|
158
|
+
}
|
|
108
159
|
/**
|
|
109
160
|
* Starts sync process.
|
|
110
161
|
* @param blockUntilSynced - If true, blocks until the archiver has fully synced.
|
|
111
162
|
*/ async start(blockUntilSynced) {
|
|
112
|
-
if (this.runningPromise) {
|
|
163
|
+
if (this.runningPromise.isRunning()) {
|
|
113
164
|
throw new Error('Archiver is already running');
|
|
114
165
|
}
|
|
166
|
+
await this.blobClient.testSources();
|
|
167
|
+
await this.testEthereumNodeSynced();
|
|
168
|
+
await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
|
|
169
|
+
// Log initial state for the archiver
|
|
170
|
+
const { l1StartBlock } = this.l1constants;
|
|
171
|
+
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = l1StartBlock } = await this.store.getSynchPoint();
|
|
172
|
+
const currentL2Checkpoint = await this.getSynchedCheckpointNumber();
|
|
173
|
+
this.log.info(`Starting archiver sync to rollup contract ${this.l1Addresses.rollupAddress.toString()} from L1 block ${blocksSynchedTo} and L2 checkpoint ${currentL2Checkpoint}`, {
|
|
174
|
+
blocksSynchedTo,
|
|
175
|
+
messagesSynchedTo,
|
|
176
|
+
currentL2Checkpoint
|
|
177
|
+
});
|
|
178
|
+
// Start sync loop, and return the wait for initial sync if we are asked to block until synced
|
|
179
|
+
this.runningPromise.start();
|
|
115
180
|
if (blockUntilSynced) {
|
|
116
|
-
|
|
181
|
+
return this.waitForInitialSync();
|
|
117
182
|
}
|
|
118
|
-
this.runningPromise = new RunningPromise(()=>this.sync(false), this.log, this.config.pollingIntervalMs, makeLoggingErrorHandler(this.log, // Ignored errors will not log to the console
|
|
119
|
-
// We ignore NoBlobBodiesFound as the message may not have been passed to the blob sink yet
|
|
120
|
-
NoBlobBodiesFoundError));
|
|
121
|
-
this.runningPromise.start();
|
|
122
183
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
184
|
+
syncImmediate() {
|
|
185
|
+
return this.runningPromise.trigger();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Queues a block to be added to the archiver store and triggers processing.
|
|
189
|
+
* The block will be processed by the sync loop.
|
|
190
|
+
* Implements the L2BlockSink interface.
|
|
191
|
+
* @param block - The L2 block to add.
|
|
192
|
+
* @returns A promise that resolves when the block has been added to the store, or rejects on error.
|
|
193
|
+
*/ addBlock(block) {
|
|
194
|
+
return new Promise((resolve, reject)=>{
|
|
195
|
+
this.blockQueue.push({
|
|
196
|
+
block,
|
|
197
|
+
resolve,
|
|
198
|
+
reject
|
|
129
199
|
});
|
|
130
|
-
|
|
200
|
+
this.log.debug(`Queued block ${block.number} for processing`);
|
|
201
|
+
// Trigger an immediate sync, but don't wait for it - the promise resolves when the block is processed
|
|
202
|
+
this.syncImmediate().catch((err)=>{
|
|
203
|
+
this.log.error(`Sync immediate call failed: ${err}`);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
131
206
|
}
|
|
132
207
|
/**
|
|
133
|
-
*
|
|
134
|
-
|
|
208
|
+
* Processes all queued blocks, adding them to the store.
|
|
209
|
+
* Called at the beginning of each sync iteration.
|
|
210
|
+
* Blocks are processed in the order they were queued.
|
|
211
|
+
*/ async processQueuedBlocks() {
|
|
212
|
+
if (this.blockQueue.length === 0) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
// Take all blocks from the queue
|
|
216
|
+
const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
|
|
217
|
+
this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
|
|
218
|
+
// Process each block individually to properly resolve/reject each promise
|
|
219
|
+
for (const { block, resolve, reject } of queuedItems){
|
|
220
|
+
try {
|
|
221
|
+
await this.store.addBlocks([
|
|
222
|
+
block
|
|
223
|
+
]);
|
|
224
|
+
this.log.debug(`Added block ${block.number} to store`);
|
|
225
|
+
resolve();
|
|
226
|
+
} catch (err) {
|
|
227
|
+
this.log.error(`Failed to add block ${block.number} to store: ${err.message}`);
|
|
228
|
+
reject(err);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
waitForInitialSync() {
|
|
233
|
+
return this.initialSyncPromise.promise;
|
|
234
|
+
}
|
|
235
|
+
/** Checks that the ethereum node we are connected to has a latest timestamp no more than the allowed drift. Throw if not. */ async testEthereumNodeSynced() {
|
|
236
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
237
|
+
if (maxAllowedDelay === 0) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const { number, timestamp: l1Timestamp } = await this.publicClient.getBlock({
|
|
241
|
+
includeTransactions: false
|
|
242
|
+
});
|
|
243
|
+
const currentTime = BigInt(this.dateProvider.nowInSeconds());
|
|
244
|
+
if (currentTime - l1Timestamp > BigInt(maxAllowedDelay)) {
|
|
245
|
+
throw new Error(`Ethereum node is out of sync (last block synced ${number} at ${l1Timestamp} vs current time ${currentTime})`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async syncFromL1() {
|
|
135
249
|
/**
|
|
136
250
|
* We keep track of three "pointers" to L1 blocks:
|
|
137
251
|
* 1. the last L1 block that published an L2 block
|
|
@@ -141,14 +255,22 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
141
255
|
* We do this to deal with L1 data providers that are eventually consistent (e.g. Infura).
|
|
142
256
|
* We guard against seeing block X with no data at one point, and later, the provider processes the block and it has data.
|
|
143
257
|
* The archiver will stay back, until there's data on L1 that will move the pointers forward.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
258
|
+
*/ const { l1StartBlock, l1StartBlockHash } = this.l1constants;
|
|
259
|
+
const { blocksSynchedTo = l1StartBlock, messagesSynchedTo = {
|
|
260
|
+
l1BlockNumber: l1StartBlock,
|
|
261
|
+
l1BlockHash: l1StartBlockHash
|
|
262
|
+
} } = await this.store.getSynchPoint();
|
|
263
|
+
const currentL1Block = await this.publicClient.getBlock({
|
|
264
|
+
includeTransactions: false
|
|
265
|
+
});
|
|
266
|
+
const currentL1BlockNumber = currentL1Block.number;
|
|
267
|
+
const currentL1BlockHash = Buffer32.fromString(currentL1Block.hash);
|
|
268
|
+
this.log.trace(`Starting new archiver sync iteration`, {
|
|
269
|
+
blocksSynchedTo,
|
|
270
|
+
messagesSynchedTo,
|
|
271
|
+
currentL1BlockNumber,
|
|
272
|
+
currentL1BlockHash
|
|
273
|
+
});
|
|
152
274
|
// ********** Ensuring Consistency of data pulled from L1 **********
|
|
153
275
|
/**
|
|
154
276
|
* There are a number of calls in this sync operation to L1 for retrieving
|
|
@@ -166,64 +288,127 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
166
288
|
* data up to the currentBlockNumber captured at the top of this function. We might want to improve on this
|
|
167
289
|
* in future but for the time being it should give us the guarantees that we need
|
|
168
290
|
*/ // ********** Events that are processed per L1 block **********
|
|
169
|
-
await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber);
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
291
|
+
await this.handleL1ToL2Messages(messagesSynchedTo, currentL1BlockNumber, currentL1BlockHash);
|
|
292
|
+
// Get L1 timestamp for the current block
|
|
293
|
+
const currentL1Timestamp = !this.l1Timestamp || !this.l1BlockNumber || this.l1BlockNumber !== currentL1BlockNumber ? (await this.publicClient.getBlock({
|
|
294
|
+
blockNumber: currentL1BlockNumber
|
|
295
|
+
})).timestamp : this.l1Timestamp;
|
|
296
|
+
// Warn if the latest L1 block timestamp is too old
|
|
297
|
+
const maxAllowedDelay = this.config.maxAllowedEthClientDriftSeconds;
|
|
298
|
+
const now = this.dateProvider.nowInSeconds();
|
|
299
|
+
if (maxAllowedDelay > 0 && Number(currentL1Timestamp) <= now - maxAllowedDelay) {
|
|
300
|
+
this.log.warn(`Latest L1 block ${currentL1BlockNumber} timestamp ${currentL1Timestamp} is too old. Make sure your Ethereum node is synced.`, {
|
|
301
|
+
currentL1BlockNumber,
|
|
302
|
+
currentL1Timestamp,
|
|
303
|
+
now,
|
|
304
|
+
maxAllowedDelay
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
// ********** Events that are processed per checkpoint **********
|
|
178
308
|
if (currentL1BlockNumber > blocksSynchedTo) {
|
|
179
|
-
// First we retrieve new L2 blocks
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
//
|
|
183
|
-
//
|
|
309
|
+
// First we retrieve new checkpoints and L2 blocks and store them in the DB. This will also update the
|
|
310
|
+
// pending chain validation status, proven checkpoint number, and synched L1 block number.
|
|
311
|
+
const rollupStatus = await this.handleCheckpoints(blocksSynchedTo, currentL1BlockNumber);
|
|
312
|
+
// Then we prune the current epoch if it'd reorg on next submission.
|
|
313
|
+
// Note that we don't do this before retrieving checkpoints because we may need to retrieve
|
|
314
|
+
// checkpoints from more than 2 epochs ago, so we want to make sure we have the latest view of
|
|
184
315
|
// the chain locally before we start unwinding stuff. This can be optimized by figuring out
|
|
185
|
-
// up to which point we're pruning, and then requesting
|
|
186
|
-
await this.handleEpochPrune(
|
|
316
|
+
// up to which point we're pruning, and then requesting checkpoints up to that point only.
|
|
317
|
+
const { rollupCanPrune } = await this.handleEpochPrune(rollupStatus.provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp);
|
|
318
|
+
// If the last checkpoint we processed had an invalid attestation, we manually advance the L1 syncpoint
|
|
319
|
+
// past it, since otherwise we'll keep downloading it and reprocessing it on every iteration until
|
|
320
|
+
// we get a valid checkpoint to advance the syncpoint.
|
|
321
|
+
if (!rollupStatus.validationResult?.valid && rollupStatus.lastL1BlockWithCheckpoint !== undefined) {
|
|
322
|
+
await this.store.setCheckpointSynchedL1BlockNumber(rollupStatus.lastL1BlockWithCheckpoint);
|
|
323
|
+
}
|
|
324
|
+
// And lastly we check if we are missing any checkpoints behind us due to a possible L1 reorg.
|
|
325
|
+
// We only do this if rollup cant prune on the next submission. Otherwise we will end up
|
|
326
|
+
// re-syncing the checkpoints we have just unwound above. We also dont do this if the last checkpoint is invalid,
|
|
327
|
+
// since the archiver will rightfully refuse to sync up to it.
|
|
328
|
+
if (!rollupCanPrune && rollupStatus.validationResult?.valid) {
|
|
329
|
+
await this.checkForNewCheckpointsBeforeL1SyncPoint(rollupStatus, blocksSynchedTo, currentL1BlockNumber);
|
|
330
|
+
}
|
|
187
331
|
this.instrumentation.updateL1BlockHeight(currentL1BlockNumber);
|
|
188
332
|
}
|
|
189
|
-
|
|
190
|
-
|
|
333
|
+
// After syncing has completed, update the current l1 block number and timestamp,
|
|
334
|
+
// otherwise we risk announcing to the world that we've synced to a given point,
|
|
335
|
+
// but the corresponding blocks have not been processed (see #12631).
|
|
336
|
+
this.l1Timestamp = currentL1Timestamp;
|
|
337
|
+
this.l1BlockNumber = currentL1BlockNumber;
|
|
338
|
+
// We resolve the initial sync only once we've caught up with the latest L1 block number (with 1 block grace)
|
|
339
|
+
// so if the initial sync took too long, we still go for another iteration.
|
|
340
|
+
if (!this.initialSyncComplete && currentL1BlockNumber + 1n >= await this.publicClient.getBlockNumber()) {
|
|
341
|
+
this.log.info(`Initial archiver sync to L1 block ${currentL1BlockNumber} complete`, {
|
|
191
342
|
l1BlockNumber: currentL1BlockNumber,
|
|
192
343
|
syncPoint: await this.store.getSynchPoint(),
|
|
193
344
|
...await this.getL2Tips()
|
|
194
345
|
});
|
|
346
|
+
this.runningPromise.setPollingIntervalMS(this.config.pollingIntervalMs);
|
|
347
|
+
this.initialSyncComplete = true;
|
|
348
|
+
this.initialSyncPromise.resolve();
|
|
195
349
|
}
|
|
196
350
|
}
|
|
197
|
-
/**
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
351
|
+
/**
|
|
352
|
+
* Fetches logs from L1 contracts and processes them.
|
|
353
|
+
*/ async sync() {
|
|
354
|
+
// Process any queued blocks first, before doing L1 sync
|
|
355
|
+
await this.processQueuedBlocks();
|
|
356
|
+
// Now perform L1 sync
|
|
357
|
+
await this.syncFromL1();
|
|
358
|
+
}
|
|
359
|
+
/** Queries the rollup contract on whether a prune can be executed on the immediate next L1 block. */ async canPrune(currentL1BlockNumber, currentL1Timestamp) {
|
|
360
|
+
const time = (currentL1Timestamp ?? 0n) + BigInt(this.l1constants.ethereumSlotDuration);
|
|
361
|
+
const result = await this.rollup.canPruneAtTime(time, {
|
|
202
362
|
blockNumber: currentL1BlockNumber
|
|
203
363
|
});
|
|
364
|
+
if (result) {
|
|
365
|
+
this.log.debug(`Rollup contract allows pruning at L1 block ${currentL1BlockNumber} time ${time}`, {
|
|
366
|
+
currentL1Timestamp,
|
|
367
|
+
pruneTime: time,
|
|
368
|
+
currentL1BlockNumber
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
return result;
|
|
204
372
|
}
|
|
205
|
-
/** Checks if there'd be a reorg for the next
|
|
206
|
-
const
|
|
207
|
-
const
|
|
373
|
+
/** Checks if there'd be a reorg for the next checkpoint submission and start pruning now. */ async handleEpochPrune(provenCheckpointNumber, currentL1BlockNumber, currentL1Timestamp) {
|
|
374
|
+
const rollupCanPrune = await this.canPrune(currentL1BlockNumber, currentL1Timestamp);
|
|
375
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
376
|
+
const canPrune = localPendingCheckpointNumber > provenCheckpointNumber && rollupCanPrune;
|
|
208
377
|
if (canPrune) {
|
|
209
|
-
const
|
|
210
|
-
const
|
|
378
|
+
const timer = new Timer();
|
|
379
|
+
const pruneFrom = CheckpointNumber(provenCheckpointNumber + 1);
|
|
380
|
+
const header = await this.getCheckpointHeader(pruneFrom);
|
|
381
|
+
if (header === undefined) {
|
|
382
|
+
throw new Error(`Missing checkpoint header ${pruneFrom}`);
|
|
383
|
+
}
|
|
384
|
+
const pruneFromSlotNumber = header.slotNumber;
|
|
385
|
+
const pruneFromEpochNumber = getEpochAtSlot(pruneFromSlotNumber, this.l1constants);
|
|
386
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - provenCheckpointNumber;
|
|
387
|
+
const checkpointPromises = Array.from({
|
|
388
|
+
length: checkpointsToUnwind
|
|
389
|
+
}).fill(0).map((_, i)=>this.store.getCheckpointData(CheckpointNumber(i + pruneFrom)));
|
|
390
|
+
const checkpoints = await Promise.all(checkpointPromises);
|
|
391
|
+
const blockPromises = await Promise.all(checkpoints.filter(isDefined).map((cp)=>this.store.getBlocksForCheckpoint(CheckpointNumber(cp.checkpointNumber))));
|
|
392
|
+
const newBlocks = blockPromises.filter(isDefined).flat();
|
|
393
|
+
// TODO(pw/mbps): Don't convert to legacy blocks here
|
|
394
|
+
const blocks = (await Promise.all(newBlocks.map((x)=>this.getBlock(x.number)))).filter(isDefined);
|
|
211
395
|
// Emit an event for listening services to react to the chain prune
|
|
212
396
|
this.emit(L2BlockSourceEvents.L2PruneDetected, {
|
|
213
397
|
type: L2BlockSourceEvents.L2PruneDetected,
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
epochNumber: localPendingEpochNumber
|
|
398
|
+
epochNumber: pruneFromEpochNumber,
|
|
399
|
+
blocks
|
|
217
400
|
});
|
|
218
|
-
|
|
219
|
-
this.
|
|
220
|
-
|
|
221
|
-
this.
|
|
222
|
-
this.instrumentation.processPrune();
|
|
401
|
+
this.log.debug(`L2 prune from ${provenCheckpointNumber + 1} to ${localPendingCheckpointNumber} will occur on next checkpoint submission.`);
|
|
402
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
403
|
+
this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `to ${provenCheckpointNumber} due to predicted reorg at L1 block ${currentL1BlockNumber}. ` + `Updated latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
|
|
404
|
+
this.instrumentation.processPrune(timer.ms());
|
|
223
405
|
// TODO(palla/reorg): Do we need to set the block synched L1 block number here?
|
|
224
406
|
// Seems like the next iteration should handle this.
|
|
225
407
|
// await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
226
408
|
}
|
|
409
|
+
return {
|
|
410
|
+
rollupCanPrune
|
|
411
|
+
};
|
|
227
412
|
}
|
|
228
413
|
nextRange(end, limit) {
|
|
229
414
|
const batchSize = this.config.batchSize * this.l1constants.slotDuration / this.l1constants.ethereumSlotDuration;
|
|
@@ -240,167 +425,453 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
240
425
|
nextEnd
|
|
241
426
|
];
|
|
242
427
|
}
|
|
243
|
-
async handleL1ToL2Messages(
|
|
244
|
-
this.log.trace(`Handling L1 to L2 messages from ${
|
|
245
|
-
if (currentL1BlockNumber <=
|
|
428
|
+
async handleL1ToL2Messages(messagesSyncPoint, currentL1BlockNumber, _currentL1BlockHash) {
|
|
429
|
+
this.log.trace(`Handling L1 to L2 messages from ${messagesSyncPoint.l1BlockNumber} to ${currentL1BlockNumber}.`);
|
|
430
|
+
if (currentL1BlockNumber <= messagesSyncPoint.l1BlockNumber) {
|
|
246
431
|
return;
|
|
247
432
|
}
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
433
|
+
// Load remote and local inbox states.
|
|
434
|
+
const localMessagesInserted = await this.store.getTotalL1ToL2MessageCount();
|
|
435
|
+
const localLastMessage = await this.store.getLastL1ToL2Message();
|
|
436
|
+
const remoteMessagesState = await this.inbox.getState({
|
|
437
|
+
blockNumber: currentL1BlockNumber
|
|
438
|
+
});
|
|
439
|
+
this.log.trace(`Retrieved remote inbox state at L1 block ${currentL1BlockNumber}.`, {
|
|
440
|
+
localMessagesInserted,
|
|
441
|
+
localLastMessage,
|
|
442
|
+
remoteMessagesState
|
|
443
|
+
});
|
|
444
|
+
// Compare message count and rolling hash. If they match, no need to retrieve anything.
|
|
445
|
+
if (remoteMessagesState.totalMessagesInserted === localMessagesInserted && remoteMessagesState.messagesRollingHash.equals(localLastMessage?.rollingHash ?? Buffer16.ZERO)) {
|
|
446
|
+
this.log.trace(`No L1 to L2 messages to query between L1 blocks ${messagesSyncPoint.l1BlockNumber} and ${currentL1BlockNumber}.`);
|
|
253
447
|
return;
|
|
254
448
|
}
|
|
255
|
-
//
|
|
256
|
-
|
|
257
|
-
|
|
449
|
+
// Check if our syncpoint is still valid. If not, there was an L1 reorg and we need to re-retrieve messages.
|
|
450
|
+
// Note that we need to fetch it from logs and not from inbox state at the syncpoint l1 block number, since it
|
|
451
|
+
// could be older than 128 blocks and non-archive nodes cannot resolve it.
|
|
452
|
+
if (localLastMessage) {
|
|
453
|
+
const remoteLastMessage = await this.retrieveL1ToL2Message(localLastMessage.leaf);
|
|
454
|
+
this.log.trace(`Retrieved remote message for local last`, {
|
|
455
|
+
remoteLastMessage,
|
|
456
|
+
localLastMessage
|
|
457
|
+
});
|
|
458
|
+
if (!remoteLastMessage || !remoteLastMessage.rollingHash.equals(localLastMessage.rollingHash)) {
|
|
459
|
+
this.log.warn(`Rolling back L1 to L2 messages due to hash mismatch or msg not found.`, {
|
|
460
|
+
remoteLastMessage,
|
|
461
|
+
messagesSyncPoint,
|
|
462
|
+
localLastMessage
|
|
463
|
+
});
|
|
464
|
+
messagesSyncPoint = await this.rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint);
|
|
465
|
+
this.log.debug(`Rolled back L1 to L2 messages to L1 block ${messagesSyncPoint.l1BlockNumber}.`, {
|
|
466
|
+
messagesSyncPoint
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
// Retrieve and save messages in batches. Each batch is estimated to acommodate up to L2 'blockBatchSize' blocks,
|
|
471
|
+
let searchStartBlock = 0n;
|
|
472
|
+
let searchEndBlock = messagesSyncPoint.l1BlockNumber;
|
|
473
|
+
let lastMessage;
|
|
474
|
+
let messageCount = 0;
|
|
258
475
|
do {
|
|
259
476
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
260
477
|
this.log.trace(`Retrieving L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
|
|
261
|
-
const
|
|
262
|
-
this.log.verbose(`Retrieved ${
|
|
263
|
-
|
|
264
|
-
|
|
478
|
+
const messages = await retrieveL1ToL2Messages(this.inbox.getContract(), searchStartBlock, searchEndBlock);
|
|
479
|
+
this.log.verbose(`Retrieved ${messages.length} new L1 to L2 messages between L1 blocks ${searchStartBlock} and ${searchEndBlock}.`);
|
|
480
|
+
const timer = new Timer();
|
|
481
|
+
await this.store.addL1ToL2Messages(messages);
|
|
482
|
+
const perMsg = timer.ms() / messages.length;
|
|
483
|
+
this.instrumentation.processNewMessages(messages.length, perMsg);
|
|
484
|
+
for (const msg of messages){
|
|
265
485
|
this.log.debug(`Downloaded L1 to L2 message`, {
|
|
266
|
-
|
|
267
|
-
|
|
486
|
+
...msg,
|
|
487
|
+
leaf: msg.leaf.toString()
|
|
268
488
|
});
|
|
489
|
+
lastMessage = msg;
|
|
490
|
+
messageCount++;
|
|
491
|
+
}
|
|
492
|
+
}while (searchEndBlock < currentL1BlockNumber)
|
|
493
|
+
// Log stats for messages retrieved (if any).
|
|
494
|
+
if (messageCount > 0) {
|
|
495
|
+
this.log.info(`Retrieved ${messageCount} new L1 to L2 messages up to message with index ${lastMessage?.index} for checkpoint ${lastMessage?.checkpointNumber}`, {
|
|
496
|
+
lastMessage,
|
|
497
|
+
messageCount
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
// Warn if the resulting rolling hash does not match the remote state we had retrieved.
|
|
501
|
+
if (lastMessage && !lastMessage.rollingHash.equals(remoteMessagesState.messagesRollingHash)) {
|
|
502
|
+
this.log.warn(`Last message retrieved rolling hash does not match remote state.`, {
|
|
503
|
+
lastMessage,
|
|
504
|
+
remoteMessagesState
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
async retrieveL1ToL2Message(leaf) {
|
|
509
|
+
const currentL1BlockNumber = await this.publicClient.getBlockNumber();
|
|
510
|
+
let searchStartBlock = 0n;
|
|
511
|
+
let searchEndBlock = this.l1constants.l1StartBlock - 1n;
|
|
512
|
+
do {
|
|
513
|
+
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
514
|
+
const message = await retrieveL1ToL2Message(this.inbox.getContract(), leaf, searchStartBlock, searchEndBlock);
|
|
515
|
+
if (message) {
|
|
516
|
+
return message;
|
|
269
517
|
}
|
|
270
518
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
519
|
+
return undefined;
|
|
520
|
+
}
|
|
521
|
+
async rollbackL1ToL2Messages(localLastMessage, messagesSyncPoint) {
|
|
522
|
+
// Slowly go back through our messages until we find the last common message.
|
|
523
|
+
// We could query the logs in batch as an optimization, but the depth of the reorg should not be deep, and this
|
|
524
|
+
// is a very rare case, so it's fine to query one log at a time.
|
|
525
|
+
let commonMsg;
|
|
526
|
+
this.log.verbose(`Searching most recent common L1 to L2 message at or before index ${localLastMessage.index}`);
|
|
527
|
+
for await (const msg of this.store.iterateL1ToL2Messages({
|
|
528
|
+
reverse: true,
|
|
529
|
+
end: localLastMessage.index
|
|
530
|
+
})){
|
|
531
|
+
const remoteMsg = await this.retrieveL1ToL2Message(msg.leaf);
|
|
532
|
+
const logCtx = {
|
|
533
|
+
remoteMsg,
|
|
534
|
+
localMsg: msg
|
|
535
|
+
};
|
|
536
|
+
if (remoteMsg && remoteMsg.rollingHash.equals(msg.rollingHash)) {
|
|
537
|
+
this.log.verbose(`Found most recent common L1 to L2 message at index ${msg.index} on L1 block ${msg.l1BlockNumber}`, logCtx);
|
|
538
|
+
commonMsg = remoteMsg;
|
|
539
|
+
break;
|
|
540
|
+
} else if (remoteMsg) {
|
|
541
|
+
this.log.debug(`Local L1 to L2 message with index ${msg.index} has different rolling hash`, logCtx);
|
|
542
|
+
} else {
|
|
543
|
+
this.log.debug(`Local L1 to L2 message with index ${msg.index} not found on L1`, logCtx);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
// Delete everything after the common message we found.
|
|
547
|
+
const lastGoodIndex = commonMsg?.index;
|
|
548
|
+
this.log.warn(`Deleting all local L1 to L2 messages after index ${lastGoodIndex ?? 'undefined'}`);
|
|
549
|
+
await this.store.removeL1ToL2Messages(lastGoodIndex !== undefined ? lastGoodIndex + 1n : 0n);
|
|
550
|
+
// Update the syncpoint so the loop below reprocesses the changed messages. We go to the block before
|
|
551
|
+
// the last common one, so we force reprocessing it, in case new messages were added on that same L1 block
|
|
552
|
+
// after the last common message.
|
|
553
|
+
const syncPointL1BlockNumber = commonMsg ? commonMsg.l1BlockNumber - 1n : this.l1constants.l1StartBlock;
|
|
554
|
+
const syncPointL1BlockHash = await this.getL1BlockHash(syncPointL1BlockNumber);
|
|
555
|
+
messagesSyncPoint = {
|
|
556
|
+
l1BlockNumber: syncPointL1BlockNumber,
|
|
557
|
+
l1BlockHash: syncPointL1BlockHash
|
|
558
|
+
};
|
|
559
|
+
await this.store.setMessageSynchedL1Block(messagesSyncPoint);
|
|
560
|
+
return messagesSyncPoint;
|
|
271
561
|
}
|
|
272
|
-
async
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
562
|
+
async getL1BlockHash(l1BlockNumber) {
|
|
563
|
+
const block = await this.publicClient.getBlock({
|
|
564
|
+
blockNumber: l1BlockNumber,
|
|
565
|
+
includeTransactions: false
|
|
566
|
+
});
|
|
567
|
+
if (!block) {
|
|
568
|
+
throw new Error(`Missing L1 block ${l1BlockNumber}`);
|
|
569
|
+
}
|
|
570
|
+
return Buffer32.fromString(block.hash);
|
|
571
|
+
}
|
|
572
|
+
async handleCheckpoints(blocksSynchedTo, currentL1BlockNumber) {
|
|
573
|
+
const localPendingCheckpointNumber = await this.getSynchedCheckpointNumber();
|
|
574
|
+
const initialValidationResult = await this.store.getPendingChainValidationStatus();
|
|
575
|
+
const [rollupProvenCheckpointNumber, provenArchive, rollupPendingCheckpointNumber, pendingArchive, archiveForLocalPendingCheckpointNumber] = await this.rollup.status(localPendingCheckpointNumber, {
|
|
277
576
|
blockNumber: currentL1BlockNumber
|
|
278
577
|
});
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
578
|
+
const provenCheckpointNumber = CheckpointNumber.fromBigInt(rollupProvenCheckpointNumber);
|
|
579
|
+
const pendingCheckpointNumber = CheckpointNumber.fromBigInt(rollupPendingCheckpointNumber);
|
|
580
|
+
const rollupStatus = {
|
|
581
|
+
provenCheckpointNumber,
|
|
582
|
+
provenArchive,
|
|
583
|
+
pendingCheckpointNumber,
|
|
584
|
+
pendingArchive,
|
|
585
|
+
validationResult: initialValidationResult
|
|
586
|
+
};
|
|
587
|
+
this.log.trace(`Retrieved rollup status at current L1 block ${currentL1BlockNumber}.`, {
|
|
588
|
+
localPendingCheckpointNumber,
|
|
589
|
+
blocksSynchedTo,
|
|
590
|
+
currentL1BlockNumber,
|
|
591
|
+
archiveForLocalPendingCheckpointNumber,
|
|
592
|
+
...rollupStatus
|
|
593
|
+
});
|
|
594
|
+
const updateProvenCheckpoint = async ()=>{
|
|
595
|
+
// Annoying edge case: if proven checkpoint is moved back to 0 due to a reorg at the beginning of the chain,
|
|
596
|
+
// we need to set it to zero. This is an edge case because we dont have a checkpoint zero (initial checkpoint is one),
|
|
597
|
+
// so localCheckpointForDestinationProvenCheckpointNumber would not be found below.
|
|
598
|
+
if (provenCheckpointNumber === 0) {
|
|
599
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
600
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
601
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
602
|
+
this.log.info(`Rolled back proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
603
|
+
provenCheckpointNumber
|
|
604
|
+
});
|
|
605
|
+
}
|
|
286
606
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
607
|
+
const localCheckpointForDestinationProvenCheckpointNumber = await this.store.getCheckpointData(provenCheckpointNumber);
|
|
608
|
+
// Sanity check. I've hit what seems to be a state where the proven checkpoint is set to a value greater than the latest
|
|
609
|
+
// synched checkpoint when requesting L2Tips from the archiver. This is the only place where the proven checkpoint is set.
|
|
610
|
+
const synched = await this.getSynchedCheckpointNumber();
|
|
611
|
+
if (localCheckpointForDestinationProvenCheckpointNumber && synched < localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber) {
|
|
612
|
+
this.log.error(`Hit local checkpoint greater than last synched checkpoint: ${localCheckpointForDestinationProvenCheckpointNumber.checkpointNumber} > ${synched}`);
|
|
613
|
+
}
|
|
614
|
+
this.log.trace(`Local checkpoint for remote proven checkpoint ${provenCheckpointNumber} is ${localCheckpointForDestinationProvenCheckpointNumber?.archive.root.toString() ?? 'undefined'}`);
|
|
615
|
+
if (localCheckpointForDestinationProvenCheckpointNumber && provenArchive === localCheckpointForDestinationProvenCheckpointNumber.archive.root.toString()) {
|
|
616
|
+
const localProvenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
617
|
+
if (localProvenCheckpointNumber !== provenCheckpointNumber) {
|
|
618
|
+
await this.setProvenCheckpointNumber(provenCheckpointNumber);
|
|
619
|
+
this.log.info(`Updated proven chain to checkpoint ${provenCheckpointNumber}`, {
|
|
620
|
+
provenCheckpointNumber
|
|
621
|
+
});
|
|
622
|
+
const provenSlotNumber = localCheckpointForDestinationProvenCheckpointNumber.header.slotNumber;
|
|
623
|
+
const provenEpochNumber = getEpochAtSlot(provenSlotNumber, this.l1constants);
|
|
624
|
+
const lastBlockNumberInCheckpoint = localCheckpointForDestinationProvenCheckpointNumber.startBlock + localCheckpointForDestinationProvenCheckpointNumber.numBlocks - 1;
|
|
625
|
+
this.emit(L2BlockSourceEvents.L2BlockProven, {
|
|
626
|
+
type: L2BlockSourceEvents.L2BlockProven,
|
|
627
|
+
blockNumber: BlockNumber(lastBlockNumberInCheckpoint),
|
|
628
|
+
slotNumber: provenSlotNumber,
|
|
629
|
+
epochNumber: provenEpochNumber
|
|
293
630
|
});
|
|
631
|
+
this.instrumentation.updateLastProvenBlock(lastBlockNumberInCheckpoint);
|
|
632
|
+
} else {
|
|
633
|
+
this.log.trace(`Proven checkpoint ${provenCheckpointNumber} already stored.`);
|
|
294
634
|
}
|
|
295
635
|
}
|
|
296
|
-
this.instrumentation.updateLastProvenBlock(Number(provenBlockNumber));
|
|
297
636
|
};
|
|
298
|
-
// This is an edge case that we only hit if there are no proposed
|
|
299
|
-
// If we have 0
|
|
300
|
-
const
|
|
301
|
-
if (
|
|
302
|
-
await this.store.
|
|
303
|
-
this.log.debug(`No
|
|
304
|
-
return
|
|
305
|
-
provenBlockNumber
|
|
306
|
-
};
|
|
637
|
+
// This is an edge case that we only hit if there are no proposed checkpoints.
|
|
638
|
+
// If we have 0 checkpoints locally and there are no checkpoints onchain there is nothing to do.
|
|
639
|
+
const noCheckpoints = localPendingCheckpointNumber === 0 && pendingCheckpointNumber === 0;
|
|
640
|
+
if (noCheckpoints) {
|
|
641
|
+
await this.store.setCheckpointSynchedL1BlockNumber(currentL1BlockNumber);
|
|
642
|
+
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}, no checkpoints on chain`);
|
|
643
|
+
return rollupStatus;
|
|
307
644
|
}
|
|
308
|
-
await
|
|
645
|
+
await updateProvenCheckpoint();
|
|
309
646
|
// Related to the L2 reorgs of the pending chain. We are only interested in actually addressing a reorg if there
|
|
310
|
-
// are any state that could be impacted by it. If we have no
|
|
311
|
-
if (
|
|
312
|
-
const
|
|
313
|
-
if (
|
|
314
|
-
throw new Error(`Missing
|
|
647
|
+
// are any state that could be impacted by it. If we have no checkpoints, there is no impact.
|
|
648
|
+
if (localPendingCheckpointNumber > 0) {
|
|
649
|
+
const localPendingCheckpoint = await this.store.getCheckpointData(localPendingCheckpointNumber);
|
|
650
|
+
if (localPendingCheckpoint === undefined) {
|
|
651
|
+
throw new Error(`Missing checkpoint ${localPendingCheckpointNumber}`);
|
|
315
652
|
}
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
653
|
+
const localPendingArchiveRoot = localPendingCheckpoint.archive.root.toString();
|
|
654
|
+
const noCheckpointSinceLast = localPendingCheckpoint && pendingArchive === localPendingArchiveRoot;
|
|
655
|
+
if (noCheckpointSinceLast) {
|
|
656
|
+
// We believe the following line causes a problem when we encounter L1 re-orgs.
|
|
657
|
+
// Basically, by setting the synched L1 block number here, we are saying that we have
|
|
658
|
+
// processed all checkpoints up to the current L1 block number and we will not attempt to retrieve logs from
|
|
659
|
+
// this block again (or any blocks before).
|
|
660
|
+
// However, in the re-org scenario, our L1 node is temporarily lying to us and we end up potentially missing checkpoints.
|
|
661
|
+
// We must only set this block number based on actually retrieved logs.
|
|
662
|
+
// TODO(#8621): Tackle this properly when we handle L1 Re-orgs.
|
|
663
|
+
// await this.store.setBlockSynchedL1BlockNumber(currentL1BlockNumber);
|
|
664
|
+
this.log.debug(`No checkpoints to retrieve from ${blocksSynchedTo + 1n} to ${currentL1BlockNumber}`);
|
|
665
|
+
return rollupStatus;
|
|
323
666
|
}
|
|
324
|
-
const
|
|
325
|
-
if (!
|
|
326
|
-
// If our local pending
|
|
667
|
+
const localPendingCheckpointInChain = archiveForLocalPendingCheckpointNumber === localPendingArchiveRoot;
|
|
668
|
+
if (!localPendingCheckpointInChain) {
|
|
669
|
+
// If our local pending checkpoint tip is not in the chain on L1 a "prune" must have happened
|
|
327
670
|
// or the L1 have reorged.
|
|
328
671
|
// In any case, we have to figure out how far into the past the action will take us.
|
|
329
|
-
// For simplicity here, we will simply rewind until we end in a
|
|
330
|
-
this.log.debug(`L2 prune has been detected
|
|
331
|
-
|
|
672
|
+
// For simplicity here, we will simply rewind until we end in a checkpoint that is also on the chain on L1.
|
|
673
|
+
this.log.debug(`L2 prune has been detected due to local pending checkpoint ${localPendingCheckpointNumber} not in chain`, {
|
|
674
|
+
localPendingCheckpointNumber,
|
|
675
|
+
localPendingArchiveRoot,
|
|
676
|
+
archiveForLocalPendingCheckpointNumber
|
|
677
|
+
});
|
|
678
|
+
let tipAfterUnwind = localPendingCheckpointNumber;
|
|
332
679
|
while(true){
|
|
333
|
-
const
|
|
334
|
-
if (
|
|
680
|
+
const candidateCheckpoint = await this.store.getCheckpointData(tipAfterUnwind);
|
|
681
|
+
if (candidateCheckpoint === undefined) {
|
|
335
682
|
break;
|
|
336
683
|
}
|
|
337
|
-
const archiveAtContract = await this.rollup.
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
684
|
+
const archiveAtContract = await this.rollup.archiveAt(candidateCheckpoint.checkpointNumber);
|
|
685
|
+
this.log.trace(`Checking local checkpoint ${candidateCheckpoint.checkpointNumber} with archive ${candidateCheckpoint.archive.root}`, {
|
|
686
|
+
archiveAtContract,
|
|
687
|
+
archiveLocal: candidateCheckpoint.archive.root.toString()
|
|
688
|
+
});
|
|
689
|
+
if (archiveAtContract === candidateCheckpoint.archive.root.toString()) {
|
|
341
690
|
break;
|
|
342
691
|
}
|
|
343
692
|
tipAfterUnwind--;
|
|
344
693
|
}
|
|
345
|
-
const
|
|
346
|
-
await this.
|
|
347
|
-
this.log.warn(`Unwound ${count(
|
|
694
|
+
const checkpointsToUnwind = localPendingCheckpointNumber - tipAfterUnwind;
|
|
695
|
+
await this.unwindCheckpoints(localPendingCheckpointNumber, checkpointsToUnwind);
|
|
696
|
+
this.log.warn(`Unwound ${count(checkpointsToUnwind, 'checkpoint')} from checkpoint ${localPendingCheckpointNumber} ` + `due to mismatched checkpoint hashes at L1 block ${currentL1BlockNumber}. ` + `Updated L2 latest checkpoint is ${await this.getSynchedCheckpointNumber()}.`);
|
|
348
697
|
}
|
|
349
698
|
}
|
|
350
|
-
// Retrieve
|
|
699
|
+
// Retrieve checkpoints in batches. Each batch is estimated to accommodate up to 'blockBatchSize' L1 blocks,
|
|
351
700
|
// computed using the L2 block time vs the L1 block time.
|
|
352
701
|
let searchStartBlock = blocksSynchedTo;
|
|
353
702
|
let searchEndBlock = blocksSynchedTo;
|
|
703
|
+
let lastRetrievedCheckpoint;
|
|
704
|
+
let lastL1BlockWithCheckpoint = undefined;
|
|
354
705
|
do {
|
|
355
706
|
[searchStartBlock, searchEndBlock] = this.nextRange(searchEndBlock, currentL1BlockNumber);
|
|
356
|
-
this.log.trace(`Retrieving
|
|
357
|
-
// TODO(md):
|
|
358
|
-
const
|
|
359
|
-
if (
|
|
707
|
+
this.log.trace(`Retrieving checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
708
|
+
// TODO(md): Retrieve from blob client then from consensus client, then from peers
|
|
709
|
+
const retrievedCheckpoints = await retrieveCheckpointsFromRollup(this.rollup.getContract(), this.publicClient, this.debugClient, this.blobClient, searchStartBlock, searchEndBlock, this.l1Addresses, this.instrumentation, this.log, !this.initialSyncComplete);
|
|
710
|
+
if (retrievedCheckpoints.length === 0) {
|
|
360
711
|
// We are not calling `setBlockSynchedL1BlockNumber` because it may cause sync issues if based off infura.
|
|
361
712
|
// See further details in earlier comments.
|
|
362
|
-
this.log.trace(`Retrieved no new
|
|
713
|
+
this.log.trace(`Retrieved no new checkpoints from L1 block ${searchStartBlock} to ${searchEndBlock}`);
|
|
363
714
|
continue;
|
|
364
715
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
716
|
+
this.log.debug(`Retrieved ${retrievedCheckpoints.length} new checkpoints between L1 blocks ${searchStartBlock} and ${searchEndBlock}`, {
|
|
717
|
+
lastProcessedCheckpoint: retrievedCheckpoints[retrievedCheckpoints.length - 1].l1,
|
|
718
|
+
searchStartBlock,
|
|
719
|
+
searchEndBlock
|
|
720
|
+
});
|
|
721
|
+
const publishedCheckpoints = await Promise.all(retrievedCheckpoints.map((b)=>retrievedToPublishedCheckpoint(b)));
|
|
722
|
+
const validCheckpoints = [];
|
|
723
|
+
for (const published of publishedCheckpoints){
|
|
724
|
+
const validationResult = this.config.skipValidateBlockAttestations ? {
|
|
725
|
+
valid: true
|
|
726
|
+
} : await validateCheckpointAttestations(published, this.epochCache, this.l1constants, this.log);
|
|
727
|
+
// Only update the validation result if it has changed, so we can keep track of the first invalid checkpoint
|
|
728
|
+
// in case there is a sequence of more than one invalid checkpoint, as we need to invalidate the first one.
|
|
729
|
+
// There is an exception though: if a checkpoint is invalidated and replaced with another invalid checkpoint,
|
|
730
|
+
// we need to update the validation result, since we need to be able to invalidate the new one.
|
|
731
|
+
// See test 'chain progresses if an invalid checkpoint is invalidated with an invalid one' for more info.
|
|
732
|
+
if (rollupStatus.validationResult?.valid !== validationResult.valid || !rollupStatus.validationResult.valid && !validationResult.valid && rollupStatus.validationResult.block.blockNumber === validationResult.block.blockNumber) {
|
|
733
|
+
rollupStatus.validationResult = validationResult;
|
|
734
|
+
}
|
|
735
|
+
if (!validationResult.valid) {
|
|
736
|
+
this.log.warn(`Skipping checkpoint ${published.checkpoint.number} due to invalid attestations`, {
|
|
737
|
+
checkpointHash: published.checkpoint.hash(),
|
|
738
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
739
|
+
...pick(validationResult, 'reason')
|
|
740
|
+
});
|
|
741
|
+
// Emit event for invalid block detection
|
|
742
|
+
this.emit(L2BlockSourceEvents.InvalidAttestationsBlockDetected, {
|
|
743
|
+
type: L2BlockSourceEvents.InvalidAttestationsBlockDetected,
|
|
744
|
+
validationResult
|
|
745
|
+
});
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
// Check the inHash of the checkpoint against the l1->l2 messages.
|
|
749
|
+
// The messages should've been synced up to the currentL1BlockNumber and must be available for the published
|
|
750
|
+
// checkpoints we just retrieved.
|
|
751
|
+
const l1ToL2Messages = await this.getL1ToL2Messages(published.checkpoint.number);
|
|
752
|
+
const computedInHash = computeInHashFromL1ToL2Messages(l1ToL2Messages);
|
|
753
|
+
const publishedInHash = published.checkpoint.header.contentCommitment.inHash;
|
|
754
|
+
if (!computedInHash.equals(publishedInHash)) {
|
|
755
|
+
this.log.fatal(`Mismatch inHash for checkpoint ${published.checkpoint.number}`, {
|
|
756
|
+
checkpointHash: published.checkpoint.hash(),
|
|
757
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
758
|
+
computedInHash,
|
|
759
|
+
publishedInHash
|
|
760
|
+
});
|
|
761
|
+
// Throwing an error since this is most likely caused by a bug.
|
|
762
|
+
throw new Error(`Mismatch inHash for checkpoint ${published.checkpoint.number}. Expected ${computedInHash} but got ${publishedInHash}`);
|
|
763
|
+
}
|
|
764
|
+
validCheckpoints.push(published);
|
|
765
|
+
this.log.debug(`Ingesting new checkpoint ${published.checkpoint.number} with ${published.checkpoint.blocks.length} blocks`, {
|
|
766
|
+
checkpointHash: published.checkpoint.hash(),
|
|
767
|
+
l1BlockNumber: published.l1.blockNumber,
|
|
768
|
+
...published.checkpoint.header.toInspect(),
|
|
769
|
+
blocks: published.checkpoint.blocks.map((b)=>b.getStats())
|
|
373
770
|
});
|
|
374
771
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
this.
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
772
|
+
try {
|
|
773
|
+
const updatedValidationResult = rollupStatus.validationResult === initialValidationResult ? undefined : rollupStatus.validationResult;
|
|
774
|
+
const [processDuration] = await elapsed(()=>this.addCheckpoints(validCheckpoints, updatedValidationResult));
|
|
775
|
+
this.instrumentation.processNewBlocks(processDuration / validCheckpoints.length, validCheckpoints.flatMap((c)=>c.checkpoint.blocks));
|
|
776
|
+
} catch (err) {
|
|
777
|
+
if (err instanceof InitialCheckpointNumberNotSequentialError) {
|
|
778
|
+
const { previousCheckpointNumber, newCheckpointNumber } = err;
|
|
779
|
+
const previousCheckpoint = previousCheckpointNumber ? await this.store.getCheckpointData(CheckpointNumber(previousCheckpointNumber)) : undefined;
|
|
780
|
+
const updatedL1SyncPoint = previousCheckpoint?.l1.blockNumber ?? this.l1constants.l1StartBlock;
|
|
781
|
+
await this.store.setBlockSynchedL1BlockNumber(updatedL1SyncPoint);
|
|
782
|
+
this.log.warn(`Attempting to insert checkpoint ${newCheckpointNumber} with previous block ${previousCheckpointNumber}. Rolling back L1 sync point to ${updatedL1SyncPoint} to try and fetch the missing blocks.`, {
|
|
783
|
+
previousCheckpointNumber,
|
|
784
|
+
newCheckpointNumber,
|
|
785
|
+
updatedL1SyncPoint
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
throw err;
|
|
789
|
+
}
|
|
790
|
+
for (const checkpoint of validCheckpoints){
|
|
791
|
+
this.log.info(`Downloaded checkpoint ${checkpoint.checkpoint.number}`, {
|
|
792
|
+
checkpointHash: checkpoint.checkpoint.hash(),
|
|
793
|
+
checkpointNumber: checkpoint.checkpoint.number,
|
|
794
|
+
blockCount: checkpoint.checkpoint.blocks.length,
|
|
795
|
+
txCount: checkpoint.checkpoint.blocks.reduce((acc, b)=>acc + b.body.txEffects.length, 0),
|
|
796
|
+
header: checkpoint.checkpoint.header.toInspect(),
|
|
797
|
+
archiveRoot: checkpoint.checkpoint.archive.root.toString(),
|
|
798
|
+
archiveNextLeafIndex: checkpoint.checkpoint.archive.nextAvailableLeafIndex
|
|
383
799
|
});
|
|
384
800
|
}
|
|
801
|
+
lastRetrievedCheckpoint = validCheckpoints.at(-1) ?? lastRetrievedCheckpoint;
|
|
802
|
+
lastL1BlockWithCheckpoint = retrievedCheckpoints.at(-1)?.l1.blockNumber ?? lastL1BlockWithCheckpoint;
|
|
385
803
|
}while (searchEndBlock < currentL1BlockNumber)
|
|
386
804
|
// Important that we update AFTER inserting the blocks.
|
|
387
|
-
await
|
|
805
|
+
await updateProvenCheckpoint();
|
|
388
806
|
return {
|
|
389
|
-
|
|
807
|
+
...rollupStatus,
|
|
808
|
+
lastRetrievedCheckpoint,
|
|
809
|
+
lastL1BlockWithCheckpoint
|
|
390
810
|
};
|
|
391
811
|
}
|
|
812
|
+
async checkForNewCheckpointsBeforeL1SyncPoint(status, blocksSynchedTo, currentL1BlockNumber) {
|
|
813
|
+
const { lastRetrievedCheckpoint, pendingCheckpointNumber } = status;
|
|
814
|
+
// Compare the last checkpoint we have (either retrieved in this round or loaded from store) with what the
|
|
815
|
+
// rollup contract told us was the latest one (pinned at the currentL1BlockNumber).
|
|
816
|
+
const latestLocalCheckpointNumber = lastRetrievedCheckpoint?.checkpoint.number ?? await this.getSynchedCheckpointNumber();
|
|
817
|
+
if (latestLocalCheckpointNumber < pendingCheckpointNumber) {
|
|
818
|
+
// Here we have consumed all logs until the `currentL1Block` we pinned at the beginning of the archiver loop,
|
|
819
|
+
// but still haven't reached the pending checkpoint according to the call to the rollup contract.
|
|
820
|
+
// We suspect an L1 reorg that added checkpoints *behind* us. If that is the case, it must have happened between
|
|
821
|
+
// the last checkpoint we saw and the current one, so we reset the last synched L1 block number. In the edge case
|
|
822
|
+
// we don't have one, we go back 2 L1 epochs, which is the deepest possible reorg (assuming Casper is working).
|
|
823
|
+
let latestLocalCheckpointArchive = undefined;
|
|
824
|
+
let targetL1BlockNumber = maxBigint(currentL1BlockNumber - 64n, 0n);
|
|
825
|
+
if (lastRetrievedCheckpoint) {
|
|
826
|
+
latestLocalCheckpointArchive = lastRetrievedCheckpoint.checkpoint.archive.root.toString();
|
|
827
|
+
targetL1BlockNumber = lastRetrievedCheckpoint.l1.blockNumber;
|
|
828
|
+
} else if (latestLocalCheckpointNumber > 0) {
|
|
829
|
+
const checkpoint = await this.store.getRangeOfCheckpoints(latestLocalCheckpointNumber, 1).then(([c])=>c);
|
|
830
|
+
latestLocalCheckpointArchive = checkpoint.archive.root.toString();
|
|
831
|
+
targetL1BlockNumber = checkpoint.l1.blockNumber;
|
|
832
|
+
}
|
|
833
|
+
this.log.warn(`Failed to reach checkpoint ${pendingCheckpointNumber} at ${currentL1BlockNumber} (latest is ${latestLocalCheckpointNumber}). ` + `Rolling back last synched L1 block number to ${targetL1BlockNumber}.`, {
|
|
834
|
+
latestLocalCheckpointNumber,
|
|
835
|
+
latestLocalCheckpointArchive,
|
|
836
|
+
blocksSynchedTo,
|
|
837
|
+
currentL1BlockNumber,
|
|
838
|
+
...status
|
|
839
|
+
});
|
|
840
|
+
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
841
|
+
} else {
|
|
842
|
+
this.log.trace(`No new checkpoints behind L1 sync point to retrieve.`, {
|
|
843
|
+
latestLocalCheckpointNumber,
|
|
844
|
+
pendingCheckpointNumber
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
/** Resumes the archiver after a stop. */ resume() {
|
|
849
|
+
if (this.runningPromise.isRunning()) {
|
|
850
|
+
this.log.warn(`Archiver already running`);
|
|
851
|
+
}
|
|
852
|
+
this.log.info(`Restarting archiver`);
|
|
853
|
+
this.runningPromise.start();
|
|
854
|
+
}
|
|
392
855
|
/**
|
|
393
856
|
* Stops the archiver.
|
|
394
857
|
* @returns A promise signalling completion of the stop process.
|
|
395
858
|
*/ async stop() {
|
|
396
859
|
this.log.debug('Stopping...');
|
|
397
|
-
await this.runningPromise
|
|
860
|
+
await this.runningPromise.stop();
|
|
398
861
|
this.log.info('Stopped.');
|
|
399
862
|
return Promise.resolve();
|
|
400
863
|
}
|
|
864
|
+
backupTo(destPath) {
|
|
865
|
+
return this.dataStore.backupTo(destPath);
|
|
866
|
+
}
|
|
401
867
|
getL1Constants() {
|
|
402
868
|
return Promise.resolve(this.l1constants);
|
|
403
869
|
}
|
|
870
|
+
getGenesisValues() {
|
|
871
|
+
return Promise.resolve({
|
|
872
|
+
genesisArchiveRoot: this.l1constants.genesisArchiveRoot
|
|
873
|
+
});
|
|
874
|
+
}
|
|
404
875
|
getRollupAddress() {
|
|
405
876
|
return Promise.resolve(this.l1Addresses.rollupAddress);
|
|
406
877
|
}
|
|
@@ -408,44 +879,65 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
408
879
|
return Promise.resolve(this.l1Addresses.registryAddress);
|
|
409
880
|
}
|
|
410
881
|
getL1BlockNumber() {
|
|
411
|
-
|
|
412
|
-
if (!l1BlockNumber) {
|
|
413
|
-
throw new Error('L1 block number not yet available. Complete an initial sync first.');
|
|
414
|
-
}
|
|
415
|
-
return l1BlockNumber;
|
|
882
|
+
return this.l1BlockNumber;
|
|
416
883
|
}
|
|
417
884
|
getL1Timestamp() {
|
|
418
|
-
|
|
419
|
-
if (!l1Timestamp) {
|
|
420
|
-
throw new Error('L1 timestamp not yet available. Complete an initial sync first.');
|
|
421
|
-
}
|
|
422
|
-
return l1Timestamp;
|
|
885
|
+
return Promise.resolve(this.l1Timestamp);
|
|
423
886
|
}
|
|
424
887
|
getL2SlotNumber() {
|
|
425
|
-
return Promise.resolve(getSlotAtTimestamp(this.
|
|
888
|
+
return Promise.resolve(this.l1Timestamp === undefined ? undefined : getSlotAtTimestamp(this.l1Timestamp, this.l1constants));
|
|
426
889
|
}
|
|
427
890
|
getL2EpochNumber() {
|
|
428
|
-
return Promise.resolve(getEpochNumberAtTimestamp(this.
|
|
891
|
+
return Promise.resolve(this.l1Timestamp === undefined ? undefined : getEpochNumberAtTimestamp(this.l1Timestamp, this.l1constants));
|
|
429
892
|
}
|
|
430
893
|
async getBlocksForEpoch(epochNumber) {
|
|
431
894
|
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
432
895
|
const blocks = [];
|
|
433
|
-
// Walk the list of
|
|
434
|
-
// We'll typically ask for
|
|
435
|
-
let
|
|
436
|
-
const slot = (b)=>b.header.
|
|
437
|
-
while(
|
|
438
|
-
if (slot(
|
|
439
|
-
blocks
|
|
896
|
+
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
897
|
+
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
898
|
+
let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
899
|
+
const slot = (b)=>b.header.slotNumber;
|
|
900
|
+
while(checkpoint && slot(checkpoint) >= start){
|
|
901
|
+
if (slot(checkpoint) <= end) {
|
|
902
|
+
// push the blocks on backwards
|
|
903
|
+
const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
904
|
+
for(let i = endBlock; i >= checkpoint.startBlock; i--){
|
|
905
|
+
const block = await this.getBlock(BlockNumber(i));
|
|
906
|
+
if (block) {
|
|
907
|
+
blocks.push(block);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
440
910
|
}
|
|
441
|
-
|
|
911
|
+
checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
|
|
912
|
+
}
|
|
913
|
+
return blocks.reverse();
|
|
914
|
+
}
|
|
915
|
+
async getBlockHeadersForEpoch(epochNumber) {
|
|
916
|
+
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
917
|
+
const blocks = [];
|
|
918
|
+
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
919
|
+
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
920
|
+
let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
921
|
+
const slot = (b)=>b.header.slotNumber;
|
|
922
|
+
while(checkpoint && slot(checkpoint) >= start){
|
|
923
|
+
if (slot(checkpoint) <= end) {
|
|
924
|
+
// push the blocks on backwards
|
|
925
|
+
const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
|
|
926
|
+
for(let i = endBlock; i >= checkpoint.startBlock; i--){
|
|
927
|
+
const block = await this.getBlockHeader(BlockNumber(i));
|
|
928
|
+
if (block) {
|
|
929
|
+
blocks.push(block);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
|
|
442
934
|
}
|
|
443
935
|
return blocks.reverse();
|
|
444
936
|
}
|
|
445
937
|
async isEpochComplete(epochNumber) {
|
|
446
938
|
// The epoch is complete if the current L2 block is the last one in the epoch (or later)
|
|
447
939
|
const header = await this.getBlockHeader('latest');
|
|
448
|
-
const slot = header
|
|
940
|
+
const slot = header ? header.globalVariables.slotNumber : undefined;
|
|
449
941
|
const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
450
942
|
if (slot && slot >= endSlot) {
|
|
451
943
|
return true;
|
|
@@ -466,30 +958,67 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
466
958
|
const leeway = 1n;
|
|
467
959
|
return l1Timestamp + leeway >= endTimestamp;
|
|
468
960
|
}
|
|
469
|
-
/**
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
961
|
+
/** Returns whether the archiver has completed an initial sync run successfully. */ isInitialSyncComplete() {
|
|
962
|
+
return this.initialSyncComplete;
|
|
963
|
+
}
|
|
964
|
+
async getCheckpointHeader(number) {
|
|
965
|
+
if (number === 'latest') {
|
|
966
|
+
number = await this.getSynchedCheckpointNumber();
|
|
967
|
+
}
|
|
968
|
+
if (number === 0) {
|
|
969
|
+
return undefined;
|
|
970
|
+
}
|
|
971
|
+
const checkpoint = await this.store.getCheckpointData(number);
|
|
972
|
+
if (!checkpoint) {
|
|
973
|
+
return undefined;
|
|
974
|
+
}
|
|
975
|
+
return checkpoint.header;
|
|
976
|
+
}
|
|
977
|
+
getCheckpointNumber() {
|
|
978
|
+
return this.getSynchedCheckpointNumber();
|
|
979
|
+
}
|
|
980
|
+
getSynchedCheckpointNumber() {
|
|
981
|
+
return this.store.getSynchedCheckpointNumber();
|
|
982
|
+
}
|
|
983
|
+
getProvenCheckpointNumber() {
|
|
984
|
+
return this.store.getProvenCheckpointNumber();
|
|
985
|
+
}
|
|
986
|
+
setProvenCheckpointNumber(checkpointNumber) {
|
|
987
|
+
return this.store.setProvenCheckpointNumber(checkpointNumber);
|
|
988
|
+
}
|
|
989
|
+
unwindCheckpoints(from, checkpointsToUnwind) {
|
|
990
|
+
return this.store.unwindCheckpoints(from, checkpointsToUnwind);
|
|
991
|
+
}
|
|
992
|
+
async getLastBlockNumberInCheckpoint(checkpointNumber) {
|
|
993
|
+
const checkpointData = await this.store.getCheckpointData(checkpointNumber);
|
|
994
|
+
if (!checkpointData) {
|
|
995
|
+
return undefined;
|
|
996
|
+
}
|
|
997
|
+
return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
|
|
998
|
+
}
|
|
999
|
+
addCheckpoints(checkpoints, pendingChainValidationStatus) {
|
|
1000
|
+
return this.store.addCheckpoints(checkpoints, pendingChainValidationStatus);
|
|
1001
|
+
}
|
|
1002
|
+
getBlockHeaderByHash(blockHash) {
|
|
1003
|
+
return this.store.getBlockHeaderByHash(blockHash);
|
|
1004
|
+
}
|
|
1005
|
+
getBlockHeaderByArchive(archive) {
|
|
1006
|
+
return this.store.getBlockHeaderByArchive(archive);
|
|
478
1007
|
}
|
|
479
1008
|
/**
|
|
480
1009
|
* Gets an l2 block.
|
|
481
1010
|
* @param number - The block number to return.
|
|
482
1011
|
* @returns The requested L2 block.
|
|
483
|
-
*/ async
|
|
1012
|
+
*/ async getL2BlockNew(number) {
|
|
484
1013
|
// If the number provided is -ve, then return the latest block.
|
|
485
1014
|
if (number < 0) {
|
|
486
1015
|
number = await this.store.getSynchedL2BlockNumber();
|
|
487
1016
|
}
|
|
488
|
-
if (number
|
|
1017
|
+
if (number === 0) {
|
|
489
1018
|
return undefined;
|
|
490
1019
|
}
|
|
491
|
-
const
|
|
492
|
-
return
|
|
1020
|
+
const publishedBlock = await this.store.store.getBlock(number);
|
|
1021
|
+
return publishedBlock;
|
|
493
1022
|
}
|
|
494
1023
|
async getBlockHeader(number) {
|
|
495
1024
|
if (number === 'latest') {
|
|
@@ -501,52 +1030,29 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
501
1030
|
const headers = await this.store.getBlockHeaders(number, 1);
|
|
502
1031
|
return headers.length === 0 ? undefined : headers[0];
|
|
503
1032
|
}
|
|
1033
|
+
getCheckpointedBlock(number) {
|
|
1034
|
+
return this.store.getCheckpointedBlock(number);
|
|
1035
|
+
}
|
|
1036
|
+
getCheckpointedBlockByHash(blockHash) {
|
|
1037
|
+
return this.store.getCheckpointedBlockByHash(blockHash);
|
|
1038
|
+
}
|
|
1039
|
+
getProvenBlockNumber() {
|
|
1040
|
+
return this.store.getProvenBlockNumber();
|
|
1041
|
+
}
|
|
1042
|
+
getCheckpointedBlockByArchive(archive) {
|
|
1043
|
+
return this.store.getCheckpointedBlockByArchive(archive);
|
|
1044
|
+
}
|
|
504
1045
|
getTxEffect(txHash) {
|
|
505
1046
|
return this.store.getTxEffect(txHash);
|
|
506
1047
|
}
|
|
507
1048
|
getSettledTxReceipt(txHash) {
|
|
508
1049
|
return this.store.getSettledTxReceipt(txHash);
|
|
509
1050
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
* @param address - The contract address containing the function to fetch.
|
|
513
|
-
* @param selector - The function selector of the function to fetch.
|
|
514
|
-
* @returns The public function data (if found).
|
|
515
|
-
*/ async getPublicFunction(address, selector) {
|
|
516
|
-
const instance = await this.getContract(address);
|
|
517
|
-
if (!instance) {
|
|
518
|
-
throw new Error(`Contract ${address.toString()} not found`);
|
|
519
|
-
}
|
|
520
|
-
const contractClass = await this.getContractClass(instance.currentContractClassId);
|
|
521
|
-
if (!contractClass) {
|
|
522
|
-
throw new Error(`Contract class ${instance.currentContractClassId.toString()} for ${address.toString()} not found`);
|
|
523
|
-
}
|
|
524
|
-
return contractClass.publicFunctions.find((f)=>f.selector.equals(selector));
|
|
525
|
-
}
|
|
526
|
-
/**
|
|
527
|
-
* Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
|
|
528
|
-
* @param from - The block number from which to begin retrieving logs.
|
|
529
|
-
* @param limit - The maximum number of blocks to retrieve logs from.
|
|
530
|
-
* @returns An array of private logs from the specified range of blocks.
|
|
531
|
-
*/ getPrivateLogs(from, limit) {
|
|
532
|
-
return this.store.getPrivateLogs(from, limit);
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
|
|
536
|
-
* @param tags - The tags to filter the logs by.
|
|
537
|
-
* @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
|
|
538
|
-
* that tag.
|
|
539
|
-
*/ getLogsByTags(tags) {
|
|
540
|
-
return this.store.getLogsByTags(tags);
|
|
1051
|
+
getPrivateLogsByTags(tags) {
|
|
1052
|
+
return this.store.getPrivateLogsByTags(tags);
|
|
541
1053
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
* they were first included in, or undefined if they're not present in the tree
|
|
545
|
-
* @param blockNumber Max block number to search for the nullifiers
|
|
546
|
-
* @param nullifiers Nullifiers to get
|
|
547
|
-
* @returns The block scoped indexes of the provided nullifiers, or undefined if the nullifier doesn't exist in the tree
|
|
548
|
-
*/ findNullifiersIndexesWithBlock(blockNumber, nullifiers) {
|
|
549
|
-
return this.store.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
|
|
1054
|
+
getPublicLogsByTagsFromContract(contractAddress, tags) {
|
|
1055
|
+
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
|
|
550
1056
|
}
|
|
551
1057
|
/**
|
|
552
1058
|
* Gets public logs based on the provided filter.
|
|
@@ -564,15 +1070,10 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
564
1070
|
}
|
|
565
1071
|
/**
|
|
566
1072
|
* Gets the number of the latest L2 block processed by the block source implementation.
|
|
1073
|
+
* This includes both checkpointed and uncheckpointed blocks.
|
|
567
1074
|
* @returns The number of the latest L2 block processed by the block source implementation.
|
|
568
1075
|
*/ getBlockNumber() {
|
|
569
|
-
return this.store.
|
|
570
|
-
}
|
|
571
|
-
getProvenBlockNumber() {
|
|
572
|
-
return this.store.getProvenL2BlockNumber();
|
|
573
|
-
}
|
|
574
|
-
/** Forcefully updates the last proven block number. Use for testing. */ setProvenBlockNumber(blockNumber) {
|
|
575
|
-
return this.store.setProvenL2BlockNumber(blockNumber);
|
|
1076
|
+
return this.store.getLatestBlockNumber();
|
|
576
1077
|
}
|
|
577
1078
|
getContractClass(id) {
|
|
578
1079
|
return this.store.getContractClass(id);
|
|
@@ -580,15 +1081,23 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
580
1081
|
getBytecodeCommitment(id) {
|
|
581
1082
|
return this.store.getBytecodeCommitment(id);
|
|
582
1083
|
}
|
|
583
|
-
getContract(address) {
|
|
584
|
-
|
|
1084
|
+
async getContract(address, maybeTimestamp) {
|
|
1085
|
+
let timestamp;
|
|
1086
|
+
if (maybeTimestamp === undefined) {
|
|
1087
|
+
const latestBlockHeader = await this.getBlockHeader('latest');
|
|
1088
|
+
// If we get undefined block header, it means that the archiver has not yet synced any block so we default to 0.
|
|
1089
|
+
timestamp = latestBlockHeader ? latestBlockHeader.globalVariables.timestamp : 0n;
|
|
1090
|
+
} else {
|
|
1091
|
+
timestamp = maybeTimestamp;
|
|
1092
|
+
}
|
|
1093
|
+
return this.store.getContractInstance(address, timestamp);
|
|
585
1094
|
}
|
|
586
1095
|
/**
|
|
587
|
-
* Gets L1 to L2 message (to be) included in a given
|
|
588
|
-
* @param
|
|
1096
|
+
* Gets L1 to L2 message (to be) included in a given checkpoint.
|
|
1097
|
+
* @param checkpointNumber - Checkpoint number to get messages for.
|
|
589
1098
|
* @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
|
|
590
|
-
*/ getL1ToL2Messages(
|
|
591
|
-
return this.store.getL1ToL2Messages(
|
|
1099
|
+
*/ getL1ToL2Messages(checkpointNumber) {
|
|
1100
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
592
1101
|
}
|
|
593
1102
|
/**
|
|
594
1103
|
* Gets the L1 to L2 message index in the L1 to L2 message tree.
|
|
@@ -600,29 +1109,33 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
600
1109
|
getContractClassIds() {
|
|
601
1110
|
return this.store.getContractClassIds();
|
|
602
1111
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
await this.store.addContractClasses([
|
|
606
|
-
contractClass
|
|
607
|
-
], [
|
|
608
|
-
await computePublicBytecodeCommitment(contractClass.packedBytecode)
|
|
609
|
-
], 0);
|
|
610
|
-
return;
|
|
1112
|
+
registerContractFunctionSignatures(signatures) {
|
|
1113
|
+
return this.store.registerContractFunctionSignatures(signatures);
|
|
611
1114
|
}
|
|
612
|
-
|
|
613
|
-
return this.store.
|
|
1115
|
+
getDebugFunctionName(address, selector) {
|
|
1116
|
+
return this.store.getDebugFunctionName(address, selector);
|
|
1117
|
+
}
|
|
1118
|
+
async getPendingChainValidationStatus() {
|
|
1119
|
+
return await this.store.getPendingChainValidationStatus() ?? {
|
|
1120
|
+
valid: true
|
|
1121
|
+
};
|
|
614
1122
|
}
|
|
615
|
-
|
|
616
|
-
return this.
|
|
1123
|
+
isPendingChainInvalid() {
|
|
1124
|
+
return this.getPendingChainValidationStatus().then((status)=>!status.valid);
|
|
617
1125
|
}
|
|
618
1126
|
async getL2Tips() {
|
|
619
1127
|
const [latestBlockNumber, provenBlockNumber] = await Promise.all([
|
|
620
1128
|
this.getBlockNumber(),
|
|
621
1129
|
this.getProvenBlockNumber()
|
|
622
1130
|
]);
|
|
623
|
-
|
|
1131
|
+
// TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
1132
|
+
// We just force it 2 epochs worth of proven data for now.
|
|
1133
|
+
// NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
|
|
1134
|
+
const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1constants.epochDuration * 2, 0));
|
|
1135
|
+
const [latestBlockHeader, provenBlockHeader, finalizedBlockHeader] = await Promise.all([
|
|
624
1136
|
latestBlockNumber > 0 ? this.getBlockHeader(latestBlockNumber) : undefined,
|
|
625
|
-
provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined
|
|
1137
|
+
provenBlockNumber > 0 ? this.getBlockHeader(provenBlockNumber) : undefined,
|
|
1138
|
+
finalizedBlockNumber > 0 ? this.getBlockHeader(finalizedBlockNumber) : undefined
|
|
626
1139
|
]);
|
|
627
1140
|
if (latestBlockNumber > 0 && !latestBlockHeader) {
|
|
628
1141
|
throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
|
|
@@ -630,29 +1143,163 @@ import { ArchiverInstrumentation } from './instrumentation.js';
|
|
|
630
1143
|
if (provenBlockNumber > 0 && !provenBlockHeader) {
|
|
631
1144
|
throw new Error(`Failed to retrieve proven block header for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`);
|
|
632
1145
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
1146
|
+
if (finalizedBlockNumber > 0 && !finalizedBlockHeader) {
|
|
1147
|
+
throw new Error(`Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`);
|
|
1148
|
+
}
|
|
1149
|
+
const latestBlockHeaderHash = await latestBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1150
|
+
const provenBlockHeaderHash = await provenBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
1151
|
+
const finalizedBlockHeaderHash = await finalizedBlockHeader?.hash() ?? GENESIS_BLOCK_HEADER_HASH;
|
|
636
1152
|
return {
|
|
637
1153
|
latest: {
|
|
638
1154
|
number: latestBlockNumber,
|
|
639
|
-
hash: latestBlockHeaderHash
|
|
1155
|
+
hash: latestBlockHeaderHash.toString()
|
|
640
1156
|
},
|
|
641
1157
|
proven: {
|
|
642
1158
|
number: provenBlockNumber,
|
|
643
|
-
hash: provenBlockHeaderHash
|
|
1159
|
+
hash: provenBlockHeaderHash.toString()
|
|
644
1160
|
},
|
|
645
1161
|
finalized: {
|
|
646
|
-
number:
|
|
647
|
-
hash: finalizedBlockHeaderHash
|
|
1162
|
+
number: finalizedBlockNumber,
|
|
1163
|
+
hash: finalizedBlockHeaderHash.toString()
|
|
648
1164
|
}
|
|
649
1165
|
};
|
|
650
1166
|
}
|
|
1167
|
+
async rollbackTo(targetL2BlockNumber) {
|
|
1168
|
+
// TODO(pw/mbps): This still assumes 1 block per checkpoint
|
|
1169
|
+
const currentBlocks = await this.getL2Tips();
|
|
1170
|
+
const currentL2Block = currentBlocks.latest.number;
|
|
1171
|
+
const currentProvenBlock = currentBlocks.proven.number;
|
|
1172
|
+
if (targetL2BlockNumber >= currentL2Block) {
|
|
1173
|
+
throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
|
|
1174
|
+
}
|
|
1175
|
+
const blocksToUnwind = currentL2Block - targetL2BlockNumber;
|
|
1176
|
+
const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
|
|
1177
|
+
if (!targetL2Block) {
|
|
1178
|
+
throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
|
|
1179
|
+
}
|
|
1180
|
+
const targetL1BlockNumber = targetL2Block.l1.blockNumber;
|
|
1181
|
+
const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
|
|
1182
|
+
const targetL1BlockHash = await this.getL1BlockHash(targetL1BlockNumber);
|
|
1183
|
+
this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
|
|
1184
|
+
await this.store.unwindCheckpoints(CheckpointNumber(currentL2Block), blocksToUnwind);
|
|
1185
|
+
this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
|
|
1186
|
+
await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1187
|
+
this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
|
|
1188
|
+
await this.store.setBlockSynchedL1BlockNumber(targetL1BlockNumber);
|
|
1189
|
+
await this.store.setMessageSynchedL1Block({
|
|
1190
|
+
l1BlockNumber: targetL1BlockNumber,
|
|
1191
|
+
l1BlockHash: targetL1BlockHash
|
|
1192
|
+
});
|
|
1193
|
+
if (targetL2BlockNumber < currentProvenBlock) {
|
|
1194
|
+
this.log.info(`Clearing proven L2 block number`);
|
|
1195
|
+
await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
|
|
1196
|
+
}
|
|
1197
|
+
// TODO(palla/reorg): Set the finalized block when we add support for it.
|
|
1198
|
+
// if (targetL2BlockNumber < currentFinalizedBlock) {
|
|
1199
|
+
// this.log.info(`Clearing finalized L2 block number`);
|
|
1200
|
+
// await this.store.setFinalizedL2BlockNumber(0);
|
|
1201
|
+
// }
|
|
1202
|
+
}
|
|
1203
|
+
async getPublishedCheckpoints(checkpointNumber, limit) {
|
|
1204
|
+
const checkpoints = await this.store.getRangeOfCheckpoints(checkpointNumber, limit);
|
|
1205
|
+
const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
|
|
1206
|
+
const fullCheckpoints = [];
|
|
1207
|
+
for(let i = 0; i < checkpoints.length; i++){
|
|
1208
|
+
const blocksForCheckpoint = blocks[i];
|
|
1209
|
+
const checkpoint = checkpoints[i];
|
|
1210
|
+
const fullCheckpoint = new Checkpoint(checkpoint.archive, checkpoint.header, blocksForCheckpoint, checkpoint.checkpointNumber);
|
|
1211
|
+
const publishedCheckpoint = new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
|
|
1212
|
+
fullCheckpoints.push(publishedCheckpoint);
|
|
1213
|
+
}
|
|
1214
|
+
return fullCheckpoints;
|
|
1215
|
+
}
|
|
1216
|
+
async getCheckpointsForEpoch(epochNumber) {
|
|
1217
|
+
const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1constants);
|
|
1218
|
+
const checkpoints = [];
|
|
1219
|
+
// Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
|
|
1220
|
+
// We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
|
|
1221
|
+
let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
|
|
1222
|
+
const slot = (b)=>b.header.slotNumber;
|
|
1223
|
+
while(checkpointData && slot(checkpointData) >= start){
|
|
1224
|
+
if (slot(checkpointData) <= end) {
|
|
1225
|
+
// push the checkpoints on backwards
|
|
1226
|
+
const [checkpoint] = await this.getPublishedCheckpoints(checkpointData.checkpointNumber, 1);
|
|
1227
|
+
checkpoints.push(checkpoint.checkpoint);
|
|
1228
|
+
}
|
|
1229
|
+
checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
|
|
1230
|
+
}
|
|
1231
|
+
return checkpoints.reverse();
|
|
1232
|
+
}
|
|
1233
|
+
/* Legacy APIs */ async getPublishedBlockByHash(blockHash) {
|
|
1234
|
+
const checkpointedBlock = await this.store.getCheckpointedBlockByHash(blockHash);
|
|
1235
|
+
return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
|
|
1236
|
+
}
|
|
1237
|
+
async getPublishedBlockByArchive(archive) {
|
|
1238
|
+
const checkpointedBlock = await this.store.getCheckpointedBlockByArchive(archive);
|
|
1239
|
+
return this.buildOldBlockFromCheckpointedBlock(checkpointedBlock);
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
1243
|
+
* @param from - Number of the first block to return (inclusive).
|
|
1244
|
+
* @param limit - The number of blocks to return.
|
|
1245
|
+
* @param proven - If true, only return blocks that have been proven.
|
|
1246
|
+
* @returns The requested L2 blocks.
|
|
1247
|
+
*/ async getBlocks(from, limit, proven) {
|
|
1248
|
+
const publishedBlocks = await this.getPublishedBlocks(from, limit, proven);
|
|
1249
|
+
return publishedBlocks.map((x)=>x.block);
|
|
1250
|
+
}
|
|
1251
|
+
async getPublishedBlocks(from, limit, proven) {
|
|
1252
|
+
const checkpoints = await this.store.getRangeOfCheckpoints(CheckpointNumber(from), limit);
|
|
1253
|
+
const provenCheckpointNumber = await this.getProvenCheckpointNumber();
|
|
1254
|
+
const blocks = (await Promise.all(checkpoints.map((ch)=>this.store.getBlocksForCheckpoint(ch.checkpointNumber)))).filter(isDefined);
|
|
1255
|
+
const olbBlocks = [];
|
|
1256
|
+
for(let i = 0; i < checkpoints.length; i++){
|
|
1257
|
+
const blockForCheckpoint = blocks[i][0];
|
|
1258
|
+
const checkpoint = checkpoints[i];
|
|
1259
|
+
if (checkpoint.checkpointNumber > provenCheckpointNumber && proven === true) {
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1262
|
+
const oldCheckpoint = new Checkpoint(blockForCheckpoint.archive, checkpoint.header, [
|
|
1263
|
+
blockForCheckpoint
|
|
1264
|
+
], checkpoint.checkpointNumber);
|
|
1265
|
+
const oldBlock = L2Block.fromCheckpoint(oldCheckpoint);
|
|
1266
|
+
const publishedBlock = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
|
|
1267
|
+
olbBlocks.push(publishedBlock);
|
|
1268
|
+
}
|
|
1269
|
+
return olbBlocks;
|
|
1270
|
+
}
|
|
1271
|
+
async buildOldBlockFromCheckpointedBlock(checkpointedBlock) {
|
|
1272
|
+
if (!checkpointedBlock) {
|
|
1273
|
+
return undefined;
|
|
1274
|
+
}
|
|
1275
|
+
const checkpoint = await this.store.getCheckpointData(checkpointedBlock.checkpointNumber);
|
|
1276
|
+
if (!checkpoint) {
|
|
1277
|
+
return checkpoint;
|
|
1278
|
+
}
|
|
1279
|
+
const fullCheckpoint = new Checkpoint(checkpointedBlock?.block.archive, checkpoint?.header, [
|
|
1280
|
+
checkpointedBlock.block
|
|
1281
|
+
], checkpoint.checkpointNumber);
|
|
1282
|
+
const oldBlock = L2Block.fromCheckpoint(fullCheckpoint);
|
|
1283
|
+
const published = new PublishedL2Block(oldBlock, checkpoint.l1, checkpoint.attestations.map((x)=>CommitteeAttestation.fromBuffer(x)));
|
|
1284
|
+
return published;
|
|
1285
|
+
}
|
|
1286
|
+
async getBlock(number) {
|
|
1287
|
+
// If the number provided is -ve, then return the latest block.
|
|
1288
|
+
if (number < 0) {
|
|
1289
|
+
number = await this.store.getSynchedL2BlockNumber();
|
|
1290
|
+
}
|
|
1291
|
+
if (number === 0) {
|
|
1292
|
+
return undefined;
|
|
1293
|
+
}
|
|
1294
|
+
const publishedBlocks = await this.getPublishedBlocks(number, 1);
|
|
1295
|
+
if (publishedBlocks.length === 0) {
|
|
1296
|
+
return undefined;
|
|
1297
|
+
}
|
|
1298
|
+
return publishedBlocks[0].block;
|
|
1299
|
+
}
|
|
651
1300
|
}
|
|
652
1301
|
_ts_decorate([
|
|
653
|
-
trackSpan('Archiver.sync'
|
|
654
|
-
[Attributes.INITIAL_SYNC]: initialRun
|
|
655
|
-
}))
|
|
1302
|
+
trackSpan('Archiver.sync')
|
|
656
1303
|
], Archiver.prototype, "sync", null);
|
|
657
1304
|
var Operation = /*#__PURE__*/ function(Operation) {
|
|
658
1305
|
Operation[Operation["Store"] = 0] = "Store";
|
|
@@ -664,23 +1311,19 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
664
1311
|
*
|
|
665
1312
|
* I would have preferred to not have this type. But it is useful for handling the logic that any
|
|
666
1313
|
* store would need to include otherwise while exposing fewer functions and logic directly to the archiver.
|
|
667
|
-
*/ class ArchiverStoreHelper {
|
|
1314
|
+
*/ export class ArchiverStoreHelper {
|
|
668
1315
|
store;
|
|
669
1316
|
#log;
|
|
670
1317
|
constructor(store){
|
|
671
1318
|
this.store = store;
|
|
672
1319
|
this.#log = createLogger('archiver:block-helper');
|
|
673
1320
|
}
|
|
674
|
-
// TODO(#10007): Remove this method
|
|
675
|
-
addContractClasses(contractClasses, bytecodeCommitments, blockNum) {
|
|
676
|
-
return this.store.addContractClasses(contractClasses, bytecodeCommitments, blockNum);
|
|
677
|
-
}
|
|
678
1321
|
/**
|
|
679
|
-
* Extracts and stores contract classes out of
|
|
1322
|
+
* Extracts and stores contract classes out of ContractClassPublished events emitted by the class registry contract.
|
|
680
1323
|
* @param allLogs - All logs emitted in a bunch of blocks.
|
|
681
|
-
*/ async #
|
|
682
|
-
const
|
|
683
|
-
const contractClasses = await Promise.all(
|
|
1324
|
+
*/ async #updatePublishedContractClasses(allLogs, blockNum, operation) {
|
|
1325
|
+
const contractClassPublishedEvents = allLogs.filter((log)=>ContractClassPublishedEvent.isContractClassPublishedEvent(log)).map((log)=>ContractClassPublishedEvent.fromLog(log));
|
|
1326
|
+
const contractClasses = await Promise.all(contractClassPublishedEvents.map((e)=>e.toContractClassPublic()));
|
|
684
1327
|
if (contractClasses.length > 0) {
|
|
685
1328
|
contractClasses.forEach((c)=>this.#log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
|
|
686
1329
|
if (operation == 0) {
|
|
@@ -694,10 +1337,10 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
694
1337
|
return true;
|
|
695
1338
|
}
|
|
696
1339
|
/**
|
|
697
|
-
* Extracts and stores contract instances out of
|
|
1340
|
+
* Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
|
|
698
1341
|
* @param allLogs - All logs emitted in a bunch of blocks.
|
|
699
1342
|
*/ async #updateDeployedContractInstances(allLogs, blockNum, operation) {
|
|
700
|
-
const contractInstances = allLogs.filter((log)=>
|
|
1343
|
+
const contractInstances = allLogs.filter((log)=>ContractInstancePublishedEvent.isContractInstancePublishedEvent(log)).map((log)=>ContractInstancePublishedEvent.fromLog(log)).map((e)=>e.toContractInstance());
|
|
701
1344
|
if (contractInstances.length > 0) {
|
|
702
1345
|
contractInstances.forEach((c)=>this.#log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`));
|
|
703
1346
|
if (operation == 0) {
|
|
@@ -709,22 +1352,24 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
709
1352
|
return true;
|
|
710
1353
|
}
|
|
711
1354
|
/**
|
|
712
|
-
* Extracts and stores contract instances out of
|
|
1355
|
+
* Extracts and stores contract instances out of ContractInstancePublished events emitted by the canonical deployer contract.
|
|
713
1356
|
* @param allLogs - All logs emitted in a bunch of blocks.
|
|
714
|
-
|
|
1357
|
+
* @param timestamp - Timestamp at which the updates were scheduled.
|
|
1358
|
+
* @param operation - The operation to perform on the contract instance updates (Store or Delete).
|
|
1359
|
+
*/ async #updateUpdatedContractInstances(allLogs, timestamp, operation) {
|
|
715
1360
|
const contractUpdates = allLogs.filter((log)=>ContractInstanceUpdatedEvent.isContractInstanceUpdatedEvent(log)).map((log)=>ContractInstanceUpdatedEvent.fromLog(log)).map((e)=>e.toContractInstanceUpdate());
|
|
716
1361
|
if (contractUpdates.length > 0) {
|
|
717
1362
|
contractUpdates.forEach((c)=>this.#log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`));
|
|
718
1363
|
if (operation == 0) {
|
|
719
|
-
return await this.store.addContractInstanceUpdates(contractUpdates,
|
|
1364
|
+
return await this.store.addContractInstanceUpdates(contractUpdates, timestamp);
|
|
720
1365
|
} else if (operation == 1) {
|
|
721
|
-
return await this.store.deleteContractInstanceUpdates(contractUpdates,
|
|
1366
|
+
return await this.store.deleteContractInstanceUpdates(contractUpdates, timestamp);
|
|
722
1367
|
}
|
|
723
1368
|
}
|
|
724
1369
|
return true;
|
|
725
1370
|
}
|
|
726
1371
|
/**
|
|
727
|
-
* Stores the functions that
|
|
1372
|
+
* Stores the functions that were broadcasted individually
|
|
728
1373
|
*
|
|
729
1374
|
* @dev Beware that there is not a delete variant of this, since they are added to contract classes
|
|
730
1375
|
* and will be deleted as part of the class if needed.
|
|
@@ -733,13 +1378,13 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
733
1378
|
* @param _blockNum - The block number
|
|
734
1379
|
* @returns
|
|
735
1380
|
*/ async #storeBroadcastedIndividualFunctions(allLogs, _blockNum) {
|
|
736
|
-
// Filter out private and
|
|
1381
|
+
// Filter out private and utility function broadcast events
|
|
737
1382
|
const privateFnEvents = allLogs.filter((log)=>PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log)).map((log)=>PrivateFunctionBroadcastedEvent.fromLog(log));
|
|
738
|
-
const
|
|
1383
|
+
const utilityFnEvents = allLogs.filter((log)=>UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log)).map((log)=>UtilityFunctionBroadcastedEvent.fromLog(log));
|
|
739
1384
|
// Group all events by contract class id
|
|
740
1385
|
for (const [classIdString, classEvents] of Object.entries(groupBy([
|
|
741
1386
|
...privateFnEvents,
|
|
742
|
-
...
|
|
1387
|
+
...utilityFnEvents
|
|
743
1388
|
], (e)=>e.contractClassId.toString()))){
|
|
744
1389
|
const contractClassId = Fr.fromHexString(classIdString);
|
|
745
1390
|
const contractClass = await this.getContractClass(contractClassId);
|
|
@@ -747,21 +1392,21 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
747
1392
|
this.#log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
|
|
748
1393
|
continue;
|
|
749
1394
|
}
|
|
750
|
-
// Split private and
|
|
1395
|
+
// Split private and utility functions, and filter out invalid ones
|
|
751
1396
|
const allFns = classEvents.map((e)=>e.toFunctionWithMembershipProof());
|
|
752
|
-
const privateFns = allFns.filter((fn)=>'
|
|
753
|
-
const
|
|
1397
|
+
const privateFns = allFns.filter((fn)=>'utilityFunctionsTreeRoot' in fn);
|
|
1398
|
+
const utilityFns = allFns.filter((fn)=>'privateFunctionsArtifactTreeRoot' in fn);
|
|
754
1399
|
const privateFunctionsWithValidity = await Promise.all(privateFns.map(async (fn)=>({
|
|
755
1400
|
fn,
|
|
756
1401
|
valid: await isValidPrivateFunctionMembershipProof(fn, contractClass)
|
|
757
1402
|
})));
|
|
758
1403
|
const validPrivateFns = privateFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
|
|
759
|
-
const
|
|
1404
|
+
const utilityFunctionsWithValidity = await Promise.all(utilityFns.map(async (fn)=>({
|
|
760
1405
|
fn,
|
|
761
|
-
valid: await
|
|
1406
|
+
valid: await isValidUtilityFunctionMembershipProof(fn, contractClass)
|
|
762
1407
|
})));
|
|
763
|
-
const
|
|
764
|
-
const validFnCount = validPrivateFns.length +
|
|
1408
|
+
const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid })=>valid).map(({ fn })=>fn);
|
|
1409
|
+
const validFnCount = validPrivateFns.length + validUtilityFns.length;
|
|
765
1410
|
if (validFnCount !== allFns.length) {
|
|
766
1411
|
this.#log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
|
|
767
1412
|
}
|
|
@@ -769,62 +1414,143 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
769
1414
|
if (validFnCount > 0) {
|
|
770
1415
|
this.#log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
|
|
771
1416
|
}
|
|
772
|
-
return await this.store.addFunctions(contractClassId, validPrivateFns,
|
|
1417
|
+
return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
|
|
773
1418
|
}
|
|
774
1419
|
return true;
|
|
775
1420
|
}
|
|
776
|
-
async
|
|
777
|
-
const
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
this.store.
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
1421
|
+
async addBlockDataToDB(block) {
|
|
1422
|
+
const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
|
|
1423
|
+
// ContractInstancePublished event logs are broadcast in privateLogs.
|
|
1424
|
+
const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
|
|
1425
|
+
const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
|
|
1426
|
+
return (await Promise.all([
|
|
1427
|
+
this.#updatePublishedContractClasses(contractClassLogs, block.number, 0),
|
|
1428
|
+
this.#updateDeployedContractInstances(privateLogs, block.number, 0),
|
|
1429
|
+
this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 0),
|
|
1430
|
+
this.#storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
|
|
1431
|
+
])).every(Boolean);
|
|
1432
|
+
}
|
|
1433
|
+
addBlocks(blocks, pendingChainValidationStatus) {
|
|
1434
|
+
// Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
|
|
1435
|
+
// or if the previous block is not in the store.
|
|
1436
|
+
return this.store.transactionAsync(async ()=>{
|
|
1437
|
+
await this.store.addBlocks(blocks);
|
|
1438
|
+
const opResults = await Promise.all([
|
|
1439
|
+
// Update the pending chain validation status if provided
|
|
1440
|
+
pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
1441
|
+
// Add any logs emitted during the retrieved blocks
|
|
1442
|
+
this.store.addLogs(blocks),
|
|
1443
|
+
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
1444
|
+
...blocks.map((block)=>{
|
|
1445
|
+
return this.addBlockDataToDB(block);
|
|
1446
|
+
})
|
|
1447
|
+
]);
|
|
1448
|
+
return opResults.every(Boolean);
|
|
1449
|
+
});
|
|
796
1450
|
}
|
|
797
|
-
|
|
798
|
-
|
|
1451
|
+
addCheckpoints(checkpoints, pendingChainValidationStatus) {
|
|
1452
|
+
// Add the blocks to the store. Store will throw if the blocks are not in order, there are gaps,
|
|
1453
|
+
// or if the previous block is not in the store.
|
|
1454
|
+
return this.store.transactionAsync(async ()=>{
|
|
1455
|
+
await this.store.addCheckpoints(checkpoints);
|
|
1456
|
+
const allBlocks = checkpoints.flatMap((ch)=>ch.checkpoint.blocks);
|
|
1457
|
+
const opResults = await Promise.all([
|
|
1458
|
+
// Update the pending chain validation status if provided
|
|
1459
|
+
pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
|
|
1460
|
+
// Add any logs emitted during the retrieved blocks
|
|
1461
|
+
this.store.addLogs(allBlocks),
|
|
1462
|
+
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
1463
|
+
...allBlocks.map((block)=>{
|
|
1464
|
+
return this.addBlockDataToDB(block);
|
|
1465
|
+
})
|
|
1466
|
+
]);
|
|
1467
|
+
return opResults.every(Boolean);
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
async unwindCheckpoints(from, checkpointsToUnwind) {
|
|
1471
|
+
if (checkpointsToUnwind <= 0) {
|
|
1472
|
+
throw new Error(`Cannot unwind ${checkpointsToUnwind} blocks`);
|
|
1473
|
+
}
|
|
1474
|
+
const last = await this.getSynchedCheckpointNumber();
|
|
799
1475
|
if (from != last) {
|
|
800
|
-
throw new Error(`
|
|
1476
|
+
throw new Error(`Cannot unwind checkpoints from checkpoint ${from} when the last checkpoint is ${last}`);
|
|
1477
|
+
}
|
|
1478
|
+
const blocks = [];
|
|
1479
|
+
const lastCheckpointNumber = from + checkpointsToUnwind - 1;
|
|
1480
|
+
for(let checkpointNumber = from; checkpointNumber <= lastCheckpointNumber; checkpointNumber++){
|
|
1481
|
+
const blocksForCheckpoint = await this.store.getBlocksForCheckpoint(checkpointNumber);
|
|
1482
|
+
if (!blocksForCheckpoint) {
|
|
1483
|
+
continue;
|
|
1484
|
+
}
|
|
1485
|
+
blocks.push(...blocksForCheckpoint);
|
|
801
1486
|
}
|
|
802
|
-
// from - blocksToUnwind = the new head, so + 1 for what we need to remove
|
|
803
|
-
const blocks = await this.getBlocks(from - blocksToUnwind + 1, blocksToUnwind);
|
|
804
1487
|
const opResults = await Promise.all([
|
|
1488
|
+
// Prune rolls back to the last proven block, which is by definition valid
|
|
1489
|
+
this.store.setPendingChainValidationStatus({
|
|
1490
|
+
valid: true
|
|
1491
|
+
}),
|
|
805
1492
|
// Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
|
|
806
1493
|
...blocks.map(async (block)=>{
|
|
807
|
-
const contractClassLogs = block.
|
|
808
|
-
//
|
|
809
|
-
const privateLogs = block.
|
|
810
|
-
const publicLogs = block.
|
|
1494
|
+
const contractClassLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.contractClassLogs);
|
|
1495
|
+
// ContractInstancePublished event logs are broadcast in privateLogs.
|
|
1496
|
+
const privateLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.privateLogs);
|
|
1497
|
+
const publicLogs = block.body.txEffects.flatMap((txEffect)=>txEffect.publicLogs);
|
|
811
1498
|
return (await Promise.all([
|
|
812
|
-
this.#
|
|
813
|
-
this.#updateDeployedContractInstances(privateLogs, block.
|
|
814
|
-
this.#updateUpdatedContractInstances(publicLogs, block.
|
|
1499
|
+
this.#updatePublishedContractClasses(contractClassLogs, block.number, 1),
|
|
1500
|
+
this.#updateDeployedContractInstances(privateLogs, block.number, 1),
|
|
1501
|
+
this.#updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, 1)
|
|
815
1502
|
])).every(Boolean);
|
|
816
1503
|
}),
|
|
817
|
-
this.store.deleteLogs(blocks
|
|
818
|
-
this.store.
|
|
1504
|
+
this.store.deleteLogs(blocks),
|
|
1505
|
+
this.store.unwindCheckpoints(from, checkpointsToUnwind)
|
|
819
1506
|
]);
|
|
820
1507
|
return opResults.every(Boolean);
|
|
821
1508
|
}
|
|
822
|
-
|
|
823
|
-
return this.store.
|
|
1509
|
+
getCheckpointData(checkpointNumber) {
|
|
1510
|
+
return this.store.getCheckpointData(checkpointNumber);
|
|
1511
|
+
}
|
|
1512
|
+
getRangeOfCheckpoints(from, limit) {
|
|
1513
|
+
return this.store.getRangeOfCheckpoints(from, limit);
|
|
1514
|
+
}
|
|
1515
|
+
getCheckpointedL2BlockNumber() {
|
|
1516
|
+
return this.store.getCheckpointedL2BlockNumber();
|
|
1517
|
+
}
|
|
1518
|
+
getSynchedCheckpointNumber() {
|
|
1519
|
+
return this.store.getSynchedCheckpointNumber();
|
|
1520
|
+
}
|
|
1521
|
+
setCheckpointSynchedL1BlockNumber(l1BlockNumber) {
|
|
1522
|
+
return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
|
|
1523
|
+
}
|
|
1524
|
+
getCheckpointedBlock(number) {
|
|
1525
|
+
return this.store.getCheckpointedBlock(number);
|
|
1526
|
+
}
|
|
1527
|
+
getCheckpointedBlockByHash(blockHash) {
|
|
1528
|
+
return this.store.getCheckpointedBlockByHash(blockHash);
|
|
1529
|
+
}
|
|
1530
|
+
getCheckpointedBlockByArchive(archive) {
|
|
1531
|
+
return this.store.getCheckpointedBlockByArchive(archive);
|
|
824
1532
|
}
|
|
825
1533
|
getBlockHeaders(from, limit) {
|
|
826
1534
|
return this.store.getBlockHeaders(from, limit);
|
|
827
1535
|
}
|
|
1536
|
+
getBlockHeaderByHash(blockHash) {
|
|
1537
|
+
return this.store.getBlockHeaderByHash(blockHash);
|
|
1538
|
+
}
|
|
1539
|
+
getBlockHeaderByArchive(archive) {
|
|
1540
|
+
return this.store.getBlockHeaderByArchive(archive);
|
|
1541
|
+
}
|
|
1542
|
+
getBlockByHash(blockHash) {
|
|
1543
|
+
return this.store.getBlockByHash(blockHash);
|
|
1544
|
+
}
|
|
1545
|
+
getBlockByArchive(archive) {
|
|
1546
|
+
return this.store.getBlockByArchive(archive);
|
|
1547
|
+
}
|
|
1548
|
+
getLatestBlockNumber() {
|
|
1549
|
+
return this.store.getLatestBlockNumber();
|
|
1550
|
+
}
|
|
1551
|
+
getBlocksForCheckpoint(checkpointNumber) {
|
|
1552
|
+
return this.store.getBlocksForCheckpoint(checkpointNumber);
|
|
1553
|
+
}
|
|
828
1554
|
getTxEffect(txHash) {
|
|
829
1555
|
return this.store.getTxEffect(txHash);
|
|
830
1556
|
}
|
|
@@ -834,20 +1560,17 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
834
1560
|
addL1ToL2Messages(messages) {
|
|
835
1561
|
return this.store.addL1ToL2Messages(messages);
|
|
836
1562
|
}
|
|
837
|
-
getL1ToL2Messages(
|
|
838
|
-
return this.store.getL1ToL2Messages(
|
|
1563
|
+
getL1ToL2Messages(checkpointNumber) {
|
|
1564
|
+
return this.store.getL1ToL2Messages(checkpointNumber);
|
|
839
1565
|
}
|
|
840
1566
|
getL1ToL2MessageIndex(l1ToL2Message) {
|
|
841
1567
|
return this.store.getL1ToL2MessageIndex(l1ToL2Message);
|
|
842
1568
|
}
|
|
843
|
-
|
|
844
|
-
return this.store.
|
|
1569
|
+
getPrivateLogsByTags(tags) {
|
|
1570
|
+
return this.store.getPrivateLogsByTags(tags);
|
|
845
1571
|
}
|
|
846
|
-
|
|
847
|
-
return this.store.
|
|
848
|
-
}
|
|
849
|
-
findNullifiersIndexesWithBlock(blockNumber, nullifiers) {
|
|
850
|
-
return this.store.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
|
|
1572
|
+
getPublicLogsByTagsFromContract(contractAddress, tags) {
|
|
1573
|
+
return this.store.getPublicLogsByTagsFromContract(contractAddress, tags);
|
|
851
1574
|
}
|
|
852
1575
|
getPublicLogs(filter) {
|
|
853
1576
|
return this.store.getPublicLogs(filter);
|
|
@@ -856,19 +1579,22 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
856
1579
|
return this.store.getContractClassLogs(filter);
|
|
857
1580
|
}
|
|
858
1581
|
getSynchedL2BlockNumber() {
|
|
859
|
-
return this.store.
|
|
1582
|
+
return this.store.getCheckpointedL2BlockNumber();
|
|
860
1583
|
}
|
|
861
|
-
|
|
862
|
-
return this.store.
|
|
1584
|
+
getProvenCheckpointNumber() {
|
|
1585
|
+
return this.store.getProvenCheckpointNumber();
|
|
863
1586
|
}
|
|
864
|
-
|
|
865
|
-
return this.store.
|
|
1587
|
+
getProvenBlockNumber() {
|
|
1588
|
+
return this.store.getProvenBlockNumber();
|
|
1589
|
+
}
|
|
1590
|
+
setProvenCheckpointNumber(checkpointNumber) {
|
|
1591
|
+
return this.store.setProvenCheckpointNumber(checkpointNumber);
|
|
866
1592
|
}
|
|
867
1593
|
setBlockSynchedL1BlockNumber(l1BlockNumber) {
|
|
868
|
-
return this.store.
|
|
1594
|
+
return this.store.setCheckpointSynchedL1BlockNumber(l1BlockNumber);
|
|
869
1595
|
}
|
|
870
|
-
|
|
871
|
-
return this.store.
|
|
1596
|
+
setMessageSynchedL1Block(l1Block) {
|
|
1597
|
+
return this.store.setMessageSynchedL1Block(l1Block);
|
|
872
1598
|
}
|
|
873
1599
|
getSynchPoint() {
|
|
874
1600
|
return this.store.getSynchPoint();
|
|
@@ -879,17 +1605,17 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
879
1605
|
getBytecodeCommitment(contractClassId) {
|
|
880
1606
|
return this.store.getBytecodeCommitment(contractClassId);
|
|
881
1607
|
}
|
|
882
|
-
getContractInstance(address) {
|
|
883
|
-
return this.store.getContractInstance(address);
|
|
1608
|
+
getContractInstance(address, timestamp) {
|
|
1609
|
+
return this.store.getContractInstance(address, timestamp);
|
|
884
1610
|
}
|
|
885
1611
|
getContractClassIds() {
|
|
886
1612
|
return this.store.getContractClassIds();
|
|
887
1613
|
}
|
|
888
|
-
registerContractFunctionSignatures(
|
|
889
|
-
return this.store.registerContractFunctionSignatures(
|
|
1614
|
+
registerContractFunctionSignatures(signatures) {
|
|
1615
|
+
return this.store.registerContractFunctionSignatures(signatures);
|
|
890
1616
|
}
|
|
891
|
-
|
|
892
|
-
return this.store.
|
|
1617
|
+
getDebugFunctionName(address, selector) {
|
|
1618
|
+
return this.store.getDebugFunctionName(address, selector);
|
|
893
1619
|
}
|
|
894
1620
|
getTotalL1ToL2MessageCount() {
|
|
895
1621
|
return this.store.getTotalL1ToL2MessageCount();
|
|
@@ -897,4 +1623,23 @@ var Operation = /*#__PURE__*/ function(Operation) {
|
|
|
897
1623
|
estimateSize() {
|
|
898
1624
|
return this.store.estimateSize();
|
|
899
1625
|
}
|
|
1626
|
+
rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber) {
|
|
1627
|
+
return this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
1628
|
+
}
|
|
1629
|
+
iterateL1ToL2Messages(range = {}) {
|
|
1630
|
+
return this.store.iterateL1ToL2Messages(range);
|
|
1631
|
+
}
|
|
1632
|
+
removeL1ToL2Messages(startIndex) {
|
|
1633
|
+
return this.store.removeL1ToL2Messages(startIndex);
|
|
1634
|
+
}
|
|
1635
|
+
getLastL1ToL2Message() {
|
|
1636
|
+
return this.store.getLastL1ToL2Message();
|
|
1637
|
+
}
|
|
1638
|
+
getPendingChainValidationStatus() {
|
|
1639
|
+
return this.store.getPendingChainValidationStatus();
|
|
1640
|
+
}
|
|
1641
|
+
setPendingChainValidationStatus(status) {
|
|
1642
|
+
this.#log.debug(`Setting pending chain validation status to valid ${status?.valid}`, status);
|
|
1643
|
+
return this.store.setPendingChainValidationStatus(status);
|
|
1644
|
+
}
|
|
900
1645
|
}
|