@@ -65,6 +65,7 @@ export async function nodeDeposit(node, bondAmount = '4'.ether, useExpressTicket
6565 rocketDepositPool ,
6666 rocketDAOProtocolSettingsDeposit ,
6767 rocketMegapoolManager ,
68+ linkedListStorage ,
6869 ] = await Promise . all ( [
6970 RocketNodeDeposit . deployed ( ) ,
7071 RocketNodeManager . deployed ( ) ,
@@ -73,6 +74,7 @@ export async function nodeDeposit(node, bondAmount = '4'.ether, useExpressTicket
7374 RocketDepositPool . deployed ( ) ,
7475 RocketDAOProtocolSettingsDeposit . deployed ( ) ,
7576 RocketMegapoolManager . deployed ( ) ,
77+ LinkedListStorage . deployed ( ) ,
7678 ] ) ;
7779
7880 // Construct deposit data for prestake
@@ -139,15 +141,67 @@ export async function nodeDeposit(node, bondAmount = '4'.ether, useExpressTicket
139141 return data ;
140142 }
141143
144+ const megapool = await getMegapoolForNode ( node ) ;
145+
142146 const data1 = await getData ( ) ;
143147
144- const expressQueueLengthAfterDeposit = useExpressTicket ? data1 . expressQueueLength + 1n : data1 . expressQueueLength ;
145- const nextAssignmentIsExpress = ( queueIndex % ( expressQueueRate + 1n ) !== expressQueueRate ) && expressQueueLengthAfterDeposit !== 0n ;
148+ const nextAssignmentIsExpress =
149+ ( queueIndex % ( expressQueueRate + 1n ) !== expressQueueRate ) &&
150+ (
151+ ( data1 . expressQueueLength === 0n && useExpressTicket ) ||
152+ ( data1 . expressQueueLength !== 0n )
153+ ) ;
146154
147155 const assignmentsEnabled = await rocketDAOProtocolSettingsDeposit . getAssignDepositsEnabled ( ) ;
148- const depositPoolCapacity = await rocketDepositPool . getBalance ( ) ;
149- const amountRequired = '32' . ether - bondAmount ;
156+ const depositPoolCapacity = await rocketDepositPool . getBalance ( ) + bondAmount ;
157+
158+ const expressQueueNamespace = ethers . solidityPackedKeccak256 ( [ 'string' ] , [ 'deposit.queue.express' ] ) ;
159+ const standardQueueNamespace = ethers . solidityPackedKeccak256 ( [ 'string' ] , [ 'deposit.queue.standard' ] ) ;
160+ const queueLength = await linkedListStorage . getLength ( nextAssignmentIsExpress ? expressQueueNamespace : standardQueueNamespace ) ;
161+
150162 let expectedNodeBalanceChange = bondAmount ;
163+ let expectedUserQueuedCapitalChange = '32' . ether - bondAmount ;
164+ let expectedNodeQueuedBondChange = bondAmount ;
165+ let expectSelfAssignment = false ;
166+ let expectImmediateAssignment = false ;
167+ let expectStandardQueueChange = useExpressTicket ? 0n : 1n ;
168+ let expectExpressQueueChange = useExpressTicket ? 1n : 0n ;
169+ let expectExpressTicketsChange = useExpressTicket ? - 1n : 0n ;
170+ let amountRequired = '32' . ether ;
171+
172+ if ( assignmentsEnabled ) {
173+ if ( queueLength > 0n ) {
174+ const queueHead = await linkedListStorage . peekItem ( nextAssignmentIsExpress ? expressQueueNamespace : standardQueueNamespace ) ;
175+ if ( depositPoolCapacity >= amountRequired ) {
176+ expectedNodeBalanceChange -= queueHead [ 2 ] * milliToWei ;
177+ if ( nextAssignmentIsExpress ) {
178+ expectExpressQueueChange -= 1n ;
179+ } else {
180+ expectStandardQueueChange -= 1n ;
181+ }
182+
183+ if ( queueHead [ 0 ] === megapool . target ) {
184+ expectedUserQueuedCapitalChange -= '32' . ether - ( queueHead [ 2 ] * milliToWei ) ;
185+ expectedNodeQueuedBondChange -= queueHead [ 2 ] * milliToWei ;
186+ expectSelfAssignment = true ;
187+ }
188+ }
189+ } else {
190+ if ( depositPoolCapacity >= amountRequired ) {
191+ expectSelfAssignment = true ;
192+ expectImmediateAssignment = true ;
193+ expectedUserQueuedCapitalChange = 0n ;
194+ expectedNodeQueuedBondChange = 0n ;
195+ expectedNodeBalanceChange = 0n ;
196+
197+ if ( nextAssignmentIsExpress ) {
198+ expectExpressQueueChange -= 1n ;
199+ } else {
200+ expectStandardQueueChange -= 1n ;
201+ }
202+ }
203+ }
204+ }
151205
152206 if ( ! usingCredit ) {
153207 const tx = await rocketNodeDeposit . connect ( node ) . deposit ( bondAmount , useExpressTicket , depositData . pubkey , depositData . signature , depositDataRoot , { value : bondAmount } ) ;
@@ -157,8 +211,6 @@ export async function nodeDeposit(node, bondAmount = '4'.ether, useExpressTicket
157211 await tx . wait ( ) ;
158212 }
159213
160- const megapool = await getMegapoolForNode ( node ) ;
161-
162214 const data2 = await getData ( ) ;
163215
164216 if ( ! data1 . deployed ) {
@@ -195,41 +247,12 @@ export async function nodeDeposit(node, bondAmount = '4'.ether, useExpressTicket
195247 assignmentsEnabled &&
196248 depositPoolCapacity >= amountRequired ;
197249
198- let expectSelfAssignment =
199- expectAssignment &&
200- (
201- ( useExpressTicket && data1 . expressQueueLength === 0n && nextAssignmentIsExpress ) ||
202- ( ! useExpressTicket && data1 . standardQueueLength === 0n )
203- ) ;
204-
205250 const queueTop = await rocketDepositPool . getQueueTop ( ) ;
206251 let expectMegapoolAssignment = expectSelfAssignment || ( expectAssignment && ( queueTop [ 0 ] === megapool . target ) ) ;
207252
208- if ( useExpressTicket ) {
209- assertBN . equal ( numExpressTicketsDelta , - 1n , 'Did not consume express ticket' ) ;
210- if ( expectAssignment ) {
211- if ( nextAssignmentIsExpress ) {
212- assertBN . equal ( expressQueueLengthDelta , 0n , 'Express queue changed' ) ;
213- assertBN . equal ( standardQueueLengthDelta , 0n , 'Standard queue changed' ) ;
214- } else {
215- assertBN . equal ( expressQueueLengthDelta , 1n , 'Express queue did not grow by 1' ) ;
216- assertBN . equal ( standardQueueLengthDelta , - 1n , 'Standard queue did not shrink by 1' ) ;
217- }
218- }
219- } else {
220- assertBN . equal ( numExpressTicketsDelta , 0n , 'Express ticket count incorrect' ) ;
221- if ( expectAssignment ) {
222- if ( nextAssignmentIsExpress ) {
223- console . log ( data1 ) ;
224- console . log ( data2 ) ;
225- assertBN . equal ( expressQueueLengthDelta , - 1n , 'Express queue did not shrink by 1' ) ;
226- assertBN . equal ( standardQueueLengthDelta , 1n , 'Standard queue did not grow by 1' ) ;
227- } else {
228- assertBN . equal ( expressQueueLengthDelta , 0n , 'Express queue changed' ) ;
229- assertBN . equal ( standardQueueLengthDelta , 0n , 'Standard queue changed' ) ;
230- }
231- }
232- }
253+ assertBN . equal ( numExpressTicketsDelta , expectExpressTicketsChange , 'Did not consume express ticket' ) ;
254+ assertBN . equal ( expressQueueLengthDelta , expectExpressQueueChange , 'Express queue length incorrect' ) ;
255+ assertBN . equal ( standardQueueLengthDelta , expectStandardQueueChange , 'Standard queue length incorrect' ) ;
233256
234257 // Confirm state of new validator
235258 const validatorInfo = await getValidatorInfo ( megapool , data1 . numValidators ) ;
@@ -244,33 +267,24 @@ export async function nodeDeposit(node, bondAmount = '4'.ether, useExpressTicket
244267 assert . equal ( validatorInfo . inQueue , true , 'Incorrect validator status' ) ;
245268 assert . equal ( validatorInfo . inPrestake , false , 'Incorrect validator status' ) ;
246269 assertBN . equal ( assignedValueDelta , 0n , 'Incorrect assigned value' ) ;
247- assertBN . equal ( userQueuedCapitalDelta , launchValue - bondAmount ) ;
248- assertBN . equal ( nodeQueuedBondDelta , bondAmount ) ;
249- } else if ( expectSelfAssignment ) {
270+ } else if ( expectImmediateAssignment ) {
250271 assert . equal ( validatorInfo . inQueue , false , 'Incorrect validator status' ) ;
251272 assert . equal ( validatorInfo . inPrestake , true , 'Incorrect validator status' ) ;
252273 assertBN . equal ( assignedValueDelta , '31' . ether , 'Incorrect assigned value' ) ;
253- assertBN . equal ( nodeBalanceDelta , 0n , 'Incorrect node balance value' ) ;
254- // If validator is assigned immediately, then there should be no change in queued capital balances
255- assertBN . equal ( nodeQueuedBondDelta , 0n ) ;
256- assertBN . equal ( userQueuedCapitalDelta , 0n ) ;
257274 } else {
258275 assert . equal ( validatorInfo . inQueue , true , 'Incorrect validator status' ) ;
259276 assert . equal ( validatorInfo . inPrestake , false , 'Incorrect validator status' ) ;
277+ }
260278
261- if ( expectMegapoolAssignment ) {
262- assertBN . equal ( assignedValueDelta , '31' . ether , 'Incorrect assigned value' ) ;
263- assertBN . equal ( nodeBalanceDelta , 0n , 'Incorrect node balance value' ) ;
264- assertBN . equal ( userQueuedCapitalDelta , 0n ) ;
265- assertBN . equal ( nodeQueuedBondDelta , 0n ) ;
266- } else {
267- assertBN . equal ( assignedValueDelta , 0n , 'Incorrect assigned value' ) ;
268- assertBN . equal ( nodeBalanceDelta , expectedNodeBalanceChange , 'Incorrect node balance value' ) ;
269- assertBN . equal ( userQueuedCapitalDelta , launchValue - bondAmount ) ;
270- assertBN . equal ( nodeQueuedBondDelta , bondAmount ) ;
271- }
279+ if ( expectSelfAssignment ) {
280+ assertBN . equal ( assignedValueDelta , '31' . ether , 'Incorrect assigned value' ) ;
281+ } else {
282+ assertBN . equal ( assignedValueDelta , 0n , 'Incorrect assigned value' ) ;
272283 }
273284
285+ assertBN . equal ( userQueuedCapitalDelta , expectedUserQueuedCapitalChange , 'Incorrect user queued capital' ) ;
286+ assertBN . equal ( nodeQueuedBondDelta , expectedNodeQueuedBondChange , 'Incorrect node queued bond' ) ;
287+ assertBN . equal ( nodeBalanceDelta , expectedNodeBalanceChange , 'Incorrect node balance value' ) ;
274288 assertBN . equal ( validatorInfo . lastRequestedValue , '32' . ether / milliToWei , 'Incorrect validator lastRequestedValue' ) ;
275289 assertBN . equal ( validatorInfo . lastRequestedBond , bondAmount / milliToWei , 'Incorrect validator lastRequestedBond' ) ;
276290
@@ -497,7 +511,7 @@ export async function getMegapoolForNodeAddress(nodeAddress) {
497511 const megapoolAddress = await rocketMegapoolFactory . getExpectedAddress ( nodeAddress ) ;
498512
499513 if ( ! await rocketMegapoolFactory . getMegapoolDeployed ( nodeAddress ) ) {
500- return null
514+ return null ;
501515 }
502516
503517 const delegateAbi = artifacts . require ( 'RocketMegapoolDelegate' ) . abi ;
0 commit comments