@@ -70,56 +70,78 @@ export async function launch(kernelStateDBDir, mailboxStorage, vatsDir, argv) {
70
70
) ;
71
71
72
72
let mailboxLastData = djson . stringify ( mbs . exportToData ( ) ) ;
73
- // save the initial state immediately
74
- commit ( ) ;
75
-
76
- // then arrange for inbound messages to be processed, after which we save
77
- async function turnCrank ( ) {
73
+ function saveState ( runTime = undefined ) {
78
74
let start = Date . now ( ) ;
79
- await controller . run ( ) ;
80
- const runTime = Date . now ( ) - start ;
75
+
81
76
// now check mbs
82
- start = Date . now ( ) ;
83
77
const newState = mbs . exportToData ( ) ;
84
78
const newData = djson . stringify ( newState ) ;
85
79
if ( newData !== mailboxLastData ) {
86
80
console . log ( `outbox changed` ) ;
87
- for ( const peer of Object . getOwnPropertyNames ( newState ) ) {
88
- const data = {
89
- outbox : newState [ peer ] . outbox ,
90
- ack : newState [ peer ] . inboundAck ,
91
- } ;
92
- mailboxStorage . set ( `mailbox.${ peer } ` , djson . stringify ( data ) ) ;
93
- }
94
- mailboxStorage . set ( `mailbox` , newData ) ;
95
- mailboxLastData = newData ;
96
81
}
82
+
83
+ for ( const peer of Object . getOwnPropertyNames ( newState ) ) {
84
+ const data = {
85
+ outbox : newState [ peer ] . outbox ,
86
+ ack : newState [ peer ] . inboundAck ,
87
+ } ;
88
+ mailboxStorage . set ( `mailbox.${ peer } ` , djson . stringify ( data ) ) ;
89
+ }
90
+ mailboxStorage . set ( 'mailbox' , newData ) ;
91
+ mailboxLastData = newData ;
92
+
97
93
const mbTime = Date . now ( ) - start ;
94
+
95
+ // Save the rest of the kernel state.
98
96
start = Date . now ( ) ;
99
- const mailboxSize = mailboxLastData . length ;
97
+ commit ( ) ;
100
98
const saveTime = Date . now ( ) - start ;
99
+
100
+ const mailboxSize = mailboxLastData . length ;
101
+ const runTimeStr = runTime === undefined ? '' : `run=${ runTime } ms, ` ;
101
102
console . log (
102
- `wrote SwingSet checkpoint (mailbox=${ mailboxSize } ), [run= ${ runTime } ms, mb=${ mbTime } ms, save=${ saveTime } ms]` ,
103
+ `wrote SwingSet checkpoint (mailbox=${ mailboxSize } ), [${ runTimeStr } mb=${ mbTime } ms, save=${ saveTime } ms]` ,
103
104
) ;
104
105
}
105
106
106
- async function deliverInbound ( sender , messages , ack ) {
107
+ // save the initial state immediately
108
+ saveState ( ) ;
109
+
110
+ // then arrange for inbound messages to be processed, after which we save
111
+ async function turnCrank ( committed ) {
112
+ const start = Date . now ( ) ;
113
+ await controller . run ( ) ;
114
+ const runTime = Date . now ( ) - start ;
115
+ if ( committed ) {
116
+ saveState ( runTime ) ;
117
+ } else {
118
+ console . log ( `proposed SwingSet transaction [run=${ runTime } ms]` ) ;
119
+ }
120
+ }
121
+
122
+ async function deliverInbound ( sender , messages , ack , committed ) {
107
123
if ( ! ( messages instanceof Array ) ) {
108
124
throw new Error ( `inbound given non-Array: ${ messages } ` ) ;
109
125
}
110
126
if ( mb . deliverInbound ( sender , messages , ack ) ) {
111
127
console . log ( `mboxDeliver: ADDED messages` ) ;
112
- await turnCrank ( ) ;
128
+ await turnCrank ( committed ) ;
129
+ } else if ( committed ) {
130
+ // We need to save our state on every commitment.
131
+ saveState ( ) ;
113
132
}
114
133
}
115
134
116
- async function deliverStartBlock ( blockHeight , blockTime ) {
135
+ async function deliverStartBlock ( blockHeight , blockTime , committed ) {
117
136
const addedToQueue = timer . poll ( blockTime ) ;
118
137
console . log (
119
138
`polled; blockTime:${ blockTime } , h:${ blockHeight } ADDED: ${ addedToQueue } ` ,
120
139
) ;
121
140
if ( addedToQueue ) {
122
- await turnCrank ( ) ;
141
+ await turnCrank ( committed ) ;
142
+ } else if ( committed ) {
143
+ // We need to save our state on every commitment.
144
+ saveState ( ) ;
123
145
}
124
146
}
125
147
0 commit comments