@@ -19,9 +19,10 @@ import (
1919
2020type Client struct {
2121 // local temporary repository for testing ui features
22- publicKeys []client.PublicKey
23- accounts []client.Account
24- links []client.Link
22+ publicKeys []client.PublicKey
23+ accounts []client.Account
24+ links []client.Link
25+ remoteStates map [client.AccountId ]string
2526
2627 // id counter to simulate serial
2728 publicKeyIdCounter client.PublicKeyId
@@ -52,7 +53,7 @@ func (c *Client) accountDeployCache(account client.Account, deployCache string)
5253// --- Lifecycle & Initialization ---
5354
5455func NewClient () * Client {
55- return & Client {}
56+ return & Client {remoteStates : make ( map [client. AccountId ] string ) }
5657}
5758
5859func (c * Client ) Close (ctx context.Context ) error {
@@ -369,10 +370,10 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
369370 go func () {
370371 defer close (deployProgressChan )
371372
372- AccountLoop :
373+ accountLoop :
373374 for i , account := range accounts {
374375 if checkContextCanceled (account .Id , deployProgress ) {
375- continue AccountLoop
376+ continue accountLoop
376377 }
377378
378379 deployProgress .Accounts [account .Id ].Status = "deploying"
@@ -382,7 +383,7 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
382383 for _i := range 5 {
383384 time .Sleep (time .Millisecond * 100 )
384385 if checkContextCanceled (account .Id , deployProgress ) {
385- continue AccountLoop
386+ continue accountLoop
386387 }
387388 deployProgress .Accounts [account .Id ].Progress = float64 (_i + 1 ) / 10
388389 deployProgressChan <- deployProgress
@@ -401,13 +402,16 @@ func (c *Client) DeployAccounts(ctx context.Context, accountIds ...client.Accoun
401402 for _i := range 5 {
402403 time .Sleep (time .Millisecond * 100 )
403404 if checkContextCanceled (account .Id , deployProgress ) {
404- continue AccountLoop
405+ continue accountLoop
405406 }
406407 deployProgress .Accounts [account .Id ].Progress = float64 (_i + 6 ) / 10
407408 deployProgressChan <- deployProgress
408409 }
409410
410- c .accounts [_i ].DeployCache = c .accountDeployCache (account , deployDatas [i ])
411+ // simulate deploying data to remote
412+ c .remoteStates [account .Id ] = c .accountDeployCache (account , deployDatas [i ])
413+
414+ c .accounts [_i ].DeployCache = c .remoteStates [account .Id ]
411415 deployProgress .Accounts [account .Id ].Status = "finished"
412416 deployProgress .Accounts [account .Id ].Progress = 1
413417 deployProgressChan <- deployProgress
@@ -457,16 +461,39 @@ func (c *Client) VerifyAccounts(ctx context.Context, accountIds ...client.Accoun
457461 }),
458462 }
459463
464+ checkContextCanceled := func (accountId client.AccountId , deployProgress client.DeployProgressAccounts ) bool {
465+ if ctx .Err () != nil {
466+ deployProgress .Accounts [accountId ].Progress = 1
467+ if errors .Is (ctx .Err (), context .Canceled ) {
468+ deployProgress .Accounts [accountId ].Status = "canceled"
469+ deployProgress .Accounts [accountId ].Err = errors .New ("canceled" )
470+ } else {
471+ deployProgress .Accounts [accountId ].Status = "error"
472+ deployProgress .Accounts [accountId ].Err = ctx .Err ()
473+ }
474+ return true
475+ }
476+ return false
477+ }
478+
460479 go func () {
461480 defer close (verifyProgressChan )
462481
482+ accountLoop:
463483 for i , account := range accounts {
484+ if checkContextCanceled (account .Id , verifyProgress ) {
485+ continue accountLoop
486+ }
487+
464488 verifyProgress .Accounts [account .Id ].Status = "verifing"
465489 verifyProgressChan <- verifyProgress
466490
467491 // simulate deplay
468492 for _i := range 5 {
469493 time .Sleep (time .Millisecond * 100 )
494+ if checkContextCanceled (account .Id , verifyProgress ) {
495+ continue accountLoop
496+ }
470497 verifyProgress .Accounts [account .Id ].Progress = float64 (_i + 1 ) / 10
471498 verifyProgressChan <- verifyProgress
472499 }
@@ -483,15 +510,18 @@ func (c *Client) VerifyAccounts(ctx context.Context, accountIds ...client.Accoun
483510 // simulate deplay
484511 for _i := range 5 {
485512 time .Sleep (time .Millisecond * 100 )
513+ if checkContextCanceled (account .Id , verifyProgress ) {
514+ continue accountLoop
515+ }
486516 verifyProgress .Accounts [account .Id ].Progress = float64 (_i + 6 ) / 10
487517 verifyProgressChan <- verifyProgress
488518 }
489519
490- // simulate getting deployCache from remote
491- deployCache := c .accountDeployCache ( account , deployDatas [ i ])
520+ // simulate getting remoteState from remote
521+ remoteState , hasState := c .remoteStates [ account . Id ]
492522
493- if c .accountDeployCache (account , deployDatas [i ]) != deployCache {
494- c .accounts [_i ].DeployCache = deployCache // update local DeployCache to reflect the remotes state
523+ if ! hasState || c .accountDeployCache (account , deployDatas [i ]) != remoteState {
524+ c .accounts [_i ].DeployCache = remoteState // update local DeployCache to reflect the remotes state
495525 verifyProgress .Accounts [account .Id ].Status = "error"
496526 verifyProgress .Accounts [account .Id ].Err = errors .New ("account is out of sync" )
497527 } else {
@@ -501,6 +531,7 @@ func (c *Client) VerifyAccounts(ctx context.Context, accountIds ...client.Accoun
501531 verifyProgress .Accounts [account .Id ].Progress = 1
502532 verifyProgressChan <- verifyProgress
503533 }
534+ verifyProgressChan <- verifyProgress
504535 }()
505536
506537 return verifyProgressChan , nil
0 commit comments