aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/swap/swap.go
blob: 60ffdbb871bf1a3dace689400dc3d337b57dad6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package swap

import (
    "errors"
    "fmt"
    "strconv"
    "sync"

    "github.com/dexon-foundation/dexon/p2p/enode"
    "github.com/dexon-foundation/dexon/p2p/protocols"
    "github.com/dexon-foundation/dexon/swarm/log"
    "github.com/dexon-foundation/dexon/swarm/state"
)

// SwAP Swarm Accounting Protocol
// a peer to peer micropayment system
// A node maintains an individual balance with every peer
// Only messages which have a price will be accounted for
type Swap struct {
    stateStore state.Store        //stateStore is needed in order to keep balances across sessions
    lock       sync.RWMutex       //lock the balances
    balances   map[enode.ID]int64 //map of balances for each peer
}

// New - swap constructor
func New(stateStore state.Store) (swap *Swap) {
    swap = &Swap{
        stateStore: stateStore,
        balances:   make(map[enode.ID]int64),
    }
    return
}

//Swap implements the protocols.Balance interface
//Add is the (sole) accounting function
func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) {
    s.lock.Lock()
    defer s.lock.Unlock()

    //load existing balances from the state store
    err = s.loadState(peer)
    if err != nil && err != state.ErrNotFound {
        return
    }
    //adjust the balance
    //if amount is negative, it will decrease, otherwise increase
    s.balances[peer.ID()] += amount
    //save the new balance to the state store
    peerBalance := s.balances[peer.ID()]
    err = s.stateStore.Put(peer.ID().String(), &peerBalance)

    log.Debug(fmt.Sprintf("balance for peer %s: %s", peer.ID().String(), strconv.FormatInt(peerBalance, 10)))
    return err
}

//GetPeerBalance returns the balance for a given peer
func (swap *Swap) GetPeerBalance(peer enode.ID) (int64, error) {
    swap.lock.RLock()
    defer swap.lock.RUnlock()
    if p, ok := swap.balances[peer]; ok {
        return p, nil
    }
    return 0, errors.New("Peer not found")
}

//load balances from the state store (persisted)
func (s *Swap) loadState(peer *protocols.Peer) (err error) {
    var peerBalance int64
    peerID := peer.ID()
    //only load if the current instance doesn't already have this peer's
    //balance in memory
    if _, ok := s.balances[peerID]; !ok {
        err = s.stateStore.Get(peerID.String(), &peerBalance)
        s.balances[peerID] = peerBalance
    }
    return
}

//Clean up Swap
func (swap *Swap) Close() {
    swap.stateStore.Close()
}