swarm/swap: ignore ErrNotFound on stateStore.Get()

This commit is contained in:
Fabio Barone 2018-11-08 08:44:55 -05:00
parent 7750577184
commit 9e8f2c470f
2 changed files with 7 additions and 4 deletions

View file

@ -54,7 +54,10 @@ func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) {
defer s.lock.Unlock() defer s.lock.Unlock()
//load existing balances from the state store //load existing balances from the state store
s.loadState(peer) err = s.loadState(peer)
if err != nil && err != state.ErrNotFound {
return
}
//adjust the balance //adjust the balance
//if amount is negative, it will decrease, otherwise increase //if amount is negative, it will decrease, otherwise increase
s.balances[peer.ID()] += amount s.balances[peer.ID()] += amount
@ -77,13 +80,13 @@ func (swap *Swap) GetPeerBalance(peer enode.ID) (int64, error) {
} }
//load balances from the state store (persisted) //load balances from the state store (persisted)
func (s *Swap) loadState(peer *protocols.Peer) { func (s *Swap) loadState(peer *protocols.Peer) (err error) {
var peerBalance int64 var peerBalance int64
peerID := peer.ID() peerID := peer.ID()
//only load if the current instance doesn't already have this peer's //only load if the current instance doesn't already have this peer's
//balance in memory //balance in memory
if _, ok := s.balances[peerID]; !ok { if _, ok := s.balances[peerID]; !ok {
s.stateStore.Get(peerID.String(), &peerBalance) err = s.stateStore.Get(peerID.String(), &peerBalance)
s.balances[peerID] = peerBalance s.balances[peerID] = peerBalance
} }
return return

View file

@ -176,7 +176,7 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
if config.SwapEnabled { if config.SwapEnabled {
balancesStore, err := state.NewDBStore(filepath.Join(config.Path, "balances.db")) balancesStore, err := state.NewDBStore(filepath.Join(config.Path, "balances.db"))
if err != nil { if err != nil {
return return nil, err
} }
self.swap = swap.New(balancesStore) self.swap = swap.New(balancesStore)
} }