Skip to content

Commit 0bf7197

Browse files
author
Jiri Malek
committed
Use in-memory to store known delegations.
1 parent 7aa9e11 commit 0bf7197

4 files changed

Lines changed: 45 additions & 71 deletions

File tree

internal/repository/db/account.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ type AccountRow struct {
4848
ScHash *common.Hash `bson:"-"`
4949
}
5050

51+
// initAccountsCollection initializes the account collection with
52+
// indexes and additional parameters needed by the app.
53+
func (db *MongoDbBridge) initAccountsCollection() {
54+
db.log.Debugf("accounts collection initialized")
55+
}
56+
5157
// Account tries to load an account identified by the address given from
5258
// the off-chain database.
5359
func (db *MongoDbBridge) Account(addr *common.Address) (*types.Account, error) {
@@ -91,12 +97,6 @@ func (db *MongoDbBridge) Account(addr *common.Address) (*types.Account, error) {
9197
}, nil
9298
}
9399

94-
// initAccountsCollection initializes the account collection with
95-
// indexes and additional parameters needed by the app.
96-
func (db *MongoDbBridge) initAccountsCollection() {
97-
db.log.Debugf("accounts collection initialized")
98-
}
99-
100100
// AddAccount stores an account in the blockchain if not exists.
101101
func (db *MongoDbBridge) AddAccount(acc *types.Account) error {
102102
// do we have account data?

internal/repository/sfc_delegation.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
6984
func (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.

internal/repository/stake_map.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

internal/types/delegation.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ type Delegation struct {
3131
Address common.Address `json:"address"`
3232
ToStakerId *hexutil.Big `json:"toStakerID"`
3333
ToStakerAddress common.Address `json:"toStakerAddr"`
34-
AmountStaked *hexutil.Big `json:"amountStaked"`
35-
AmountDelegated *hexutil.Big `json:"amountDelegated"`
3634
CreatedTime hexutil.Uint64 `json:"createdTime"`
35+
36+
// AmountStaked represents the current staked amount
37+
AmountStaked *hexutil.Big `json:"amountStaked"`
38+
39+
// AmountDelegated is the original amount delegated
40+
AmountDelegated *hexutil.Big `json:"amountDelegated"`
3741
}
3842

3943
// BsonDelegation represents the BSON i/o struct for a delegation.

0 commit comments

Comments
 (0)