1
- import type { StateRecord } from "../BatchProducerModule" ;
2
1
import { Bool , Field , Poseidon } from "o1js" ;
3
2
import { RollupMerkleTree } from "@proto-kit/common" ;
4
3
import {
@@ -10,16 +9,21 @@ import {
10
9
Protocol ,
11
10
ProtocolModulesRecord ,
12
11
ProvableBlockHook ,
13
- reduceStateTransitions ,
14
- RuntimeMethodExecutionContext ,
15
12
RuntimeTransaction ,
16
13
} from "@proto-kit/protocol" ;
17
14
import { inject , injectable , Lifecycle , scoped } from "tsyringe" ;
18
15
19
- import { Block , BlockResult } from "../../../storage/model/Block" ;
16
+ import {
17
+ Block ,
18
+ BlockResult ,
19
+ TransactionExecutionResult ,
20
+ } from "../../../storage/model/Block" ;
20
21
import { AsyncMerkleTreeStore } from "../../../state/async/AsyncMerkleTreeStore" ;
21
22
import { CachedMerkleTreeStore } from "../../../state/merkle/CachedMerkleTreeStore" ;
22
23
import { UntypedStateTransition } from "../helpers/UntypedStateTransition" ;
24
+ import type { StateRecord } from "../BatchProducerModule" ;
25
+
26
+ import { executeWithExecutionContext } from "./TransactionExecutionService" ;
23
27
24
28
function collectStateDiff (
25
29
stateTransitions : UntypedStateTransition [ ]
@@ -35,13 +39,27 @@ function collectStateDiff(
35
39
) ;
36
40
}
37
41
42
+ function createCombinedStateDiff ( transactions : TransactionExecutionResult [ ] ) {
43
+ // Flatten diff list into a single diff by applying them over each other
44
+ return transactions
45
+ . map ( ( tx ) => {
46
+ const transitions = tx . protocolTransitions . concat (
47
+ tx . status . toBoolean ( ) ? tx . stateTransitions : [ ]
48
+ ) ;
49
+ return collectStateDiff ( transitions ) ;
50
+ } )
51
+ . reduce < StateRecord > ( ( accumulator , diff ) => {
52
+ // accumulator properties will be overwritten by diff's values
53
+ return Object . assign ( accumulator , diff ) ;
54
+ } , { } ) ;
55
+ }
56
+
38
57
@injectable ( )
39
58
@scoped ( Lifecycle . ContainerScoped )
40
59
export class BlockResultService {
41
60
private readonly blockHooks : ProvableBlockHook < unknown > [ ] ;
42
61
43
62
public constructor (
44
- private readonly executionContext : RuntimeMethodExecutionContext ,
45
63
@inject ( "Protocol" )
46
64
protocol : Protocol < MandatoryProtocolModulesRecord & ProtocolModulesRecord >
47
65
) {
@@ -55,18 +73,7 @@ export class BlockResultService {
55
73
blockHashTreeStore : AsyncMerkleTreeStore ,
56
74
modifyTreeStore = true
57
75
) : Promise < BlockResult > {
58
- // Flatten diff list into a single diff by applying them over each other
59
- const combinedDiff = block . transactions
60
- . map ( ( tx ) => {
61
- const transitions = tx . protocolTransitions . concat (
62
- tx . status . toBoolean ( ) ? tx . stateTransitions : [ ]
63
- ) ;
64
- return collectStateDiff ( transitions ) ;
65
- } )
66
- . reduce < StateRecord > ( ( accumulator , diff ) => {
67
- // accumulator properties will be overwritten by diff's values
68
- return Object . assign ( accumulator , diff ) ;
69
- } , { } ) ;
76
+ const combinedDiff = createCombinedStateDiff ( block . transactions ) ;
70
77
71
78
const inMemoryStore = new CachedMerkleTreeStore ( merkleTreeStore ) ;
72
79
const tree = new RollupMerkleTree ( inMemoryStore ) ;
@@ -104,23 +111,22 @@ export class BlockResultService {
104
111
} ;
105
112
106
113
// TODO Set StateProvider for @state access to state
107
- this . executionContext . clear ( ) ;
108
- this . executionContext . setup ( {
114
+ const context = {
109
115
networkState : block . networkState . during ,
110
116
transaction : RuntimeTransaction . dummyTransaction ( ) ,
111
- } ) ;
117
+ } ;
112
118
113
- const resultingNetworkState = await this . blockHooks . reduce <
114
- Promise < NetworkState >
115
- > (
116
- async ( networkState , hook ) =>
117
- await hook . afterBlock ( await networkState , state ) ,
118
- Promise . resolve ( block . networkState . during )
119
+ const executionResult = await executeWithExecutionContext (
120
+ async ( ) =>
121
+ await this . blockHooks . reduce < Promise < NetworkState > > (
122
+ async ( networkState , hook ) =>
123
+ await hook . afterBlock ( await networkState , state ) ,
124
+ Promise . resolve ( block . networkState . during )
125
+ ) ,
126
+ context
119
127
) ;
120
128
121
- const { stateTransitions } = this . executionContext . result ;
122
- this . executionContext . clear ( ) ;
123
- const reducedStateTransitions = reduceStateTransitions ( stateTransitions ) ;
129
+ const { stateTransitions, methodResult } = executionResult ;
124
130
125
131
// Update the block hash tree with this block
126
132
blockHashTree . setLeaf (
@@ -139,12 +145,12 @@ export class BlockResultService {
139
145
}
140
146
141
147
return {
142
- afterNetworkState : resultingNetworkState ,
148
+ afterNetworkState : methodResult ,
143
149
stateRoot : stateRoot . toBigInt ( ) ,
144
150
blockHashRoot : newBlockHashRoot . toBigInt ( ) ,
145
151
blockHashWitness,
146
152
147
- blockStateTransitions : reducedStateTransitions . map ( ( st ) =>
153
+ blockStateTransitions : stateTransitions . map ( ( st ) =>
148
154
UntypedStateTransition . fromStateTransition ( st )
149
155
) ,
150
156
blockHash : block . hash . toBigInt ( ) ,
0 commit comments