@@ -38,7 +38,7 @@ func (p *proxy) StoreDelegation(dl *types.Delegation) error {
3838}
3939
4040// UpdateDelegationBalance updates active balance of the given delegation.
41- func (p * proxy ) UpdateDelegationBalance (addr * common.Address , valID * hexutil.Big , unknown func (* big.Int ) error ) error {
41+ func (p * proxy ) UpdateDelegationBalance (addr * common.Address , valID * hexutil.Big , unknownDelegation func (* big.Int ) error ) error {
4242 // pull the current value
4343 val , err := p .DelegationAmountStaked (addr , valID )
4444 if err != nil {
@@ -54,8 +54,11 @@ func (p *proxy) UpdateDelegationBalance(addr *common.Address, valID *hexutil.Big
5454
5555 // unknown delegation detected?
5656 if err == db .ErrUnknownDelegation {
57- return unknown (val )
57+ p .log .Errorf ("delegation %s to %d not known yet" , addr .String (), valID .ToInt ().Uint64 ())
58+ return unknownDelegation (val )
5859 }
60+
61+ // some other error
5962 p .log .Errorf ("delegation %s to %d update failed; %s" , addr .String (), valID .ToInt ().Uint64 (), err .Error ())
6063 return err
6164}
@@ -68,32 +71,39 @@ func (p *proxy) updateDelegationBalance(addr *common.Address, valID *hexutil.Big
6871 return err
6972 }
7073
71- // do we need to update?
74+ // do we need to update? if the amount did not change, skip the update
7275 if dlg .AmountStaked .ToInt ().Cmp (amo ) == 0 {
7376 return nil
7477 }
7578
76- // update the delegation in cache
79+ // update the delegation in DB and memory
7780 dlg .AmountDelegated = (* hexutil .Big )(amo )
78-
79- // perform the update
80- return p .db .UpdateDelegationBalance (addr , valID , dlg .AmountDelegated )
81+ err = p .db .UpdateDelegationBalance (addr , valID , dlg .AmountDelegated )
82+ if nil == err {
83+ p .cache .PushDelegation (dlg )
84+ }
85+ return err
8186}
8287
8388// Delegation returns a detail of delegation for the given address.
84- func (p * proxy ) Delegation (addr * common.Address , valID * hexutil.Big ) (* types.Delegation , error ) {
89+ func (p * proxy ) Delegation (adr * common.Address , valID * hexutil.Big ) (* types.Delegation , error ) {
8590 // log what we do
86- p .log .Debugf ("loading delegation of %s to #%d" , addr .String (), valID .ToInt ().Uint64 ())
91+ p .log .Debugf ("accessing delegation of %s to #%d" , adr .String (), valID .ToInt ().Uint64 ())
8792
8893 // try cache first
94+ dlg := p .cache .PullDelegation (* adr , valID )
95+ if dlg != nil {
96+ return dlg , nil
97+ }
8998
90- // pull from DB instead
91- dlg , err := p .db .Delegation (addr , valID )
99+ // pull from DB instead; do we actually have it?
100+ dlg , err := p .db .Delegation (adr , valID )
92101 if err != nil {
93102 return nil , err
94103 }
95104
96105 // store to cache for future reference
106+ p .cache .PushDelegation (dlg )
97107 return dlg , nil
98108}
99109
0 commit comments