@@ -46,13 +46,8 @@ func (p *proxy) UpdateDelegationBalance(addr *common.Address, valID *hexutil.Big
4646 return err
4747 }
4848
49- // check if we need to update it
50- if ! p .stakedAmounts .Update (addr , valID , val ) {
51- return nil
52- }
53-
5449 // do the update
55- err = p .db . UpdateDelegationBalance (addr , valID , ( * hexutil . Big )( val ) )
50+ err = p .updateDelegationBalance (addr , valID , val )
5651 if err == nil {
5752 return nil
5853 }
@@ -65,10 +60,41 @@ func (p *proxy) UpdateDelegationBalance(addr *common.Address, valID *hexutil.Big
6560 return err
6661}
6762
63+ // updateDelegationBalance performs delegation balance update if needed.
64+ func (p * proxy ) updateDelegationBalance (addr * common.Address , valID * hexutil.Big , amo * big.Int ) error {
65+ // get the delegation detail
66+ dlg , err := p .Delegation (addr , valID )
67+ if err != nil {
68+ return err
69+ }
70+
71+ // do we need to update?
72+ if dlg .AmountStaked .ToInt ().Cmp (amo ) == 0 {
73+ return nil
74+ }
75+
76+ // update the delegation in cache
77+ dlg .AmountDelegated = (* hexutil .Big )(amo )
78+
79+ // perform the update
80+ return p .db .UpdateDelegationBalance (addr , valID , dlg .AmountDelegated )
81+ }
82+
6883// Delegation returns a detail of delegation for the given address.
6984func (p * proxy ) Delegation (addr * common.Address , valID * hexutil.Big ) (* types.Delegation , error ) {
85+ // log what we do
7086 p .log .Debugf ("loading delegation of %s to #%d" , addr .String (), valID .ToInt ().Uint64 ())
71- return p .db .Delegation (addr , valID )
87+
88+ // try cache first
89+
90+ // pull from DB instead
91+ dlg , err := p .db .Delegation (addr , valID )
92+ if err != nil {
93+ return nil , err
94+ }
95+
96+ // store to cache for future reference
97+ return dlg , nil
7298}
7399
74100// DelegationAmountStaked returns the current amount of staked tokens for the given delegation.
0 commit comments