-
Notifications
You must be signed in to change notification settings - Fork 20.8k
Swarm accounting #18050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Swarm accounting #18050
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d014421
swarm: completed 1st phase of swap accounting
holisticode 7eeb79e
swarm: swap accounting for swarm with p2p accounting
holisticode 7750577
swarm/swap: addressed PR comments
holisticode 9e8f2c4
swarm/swap: ignore ErrNotFound on stateStore.Get()
holisticode a42ce93
swarm/swap: GetPeerBalance test; add TODO for chequebook API check
holisticode 0a7b7bd
swarm/network/stream: fix NewRegistry calls with new arguments
janos bacb6a4
swarm/swap: address @justelad's PR comments
holisticode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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/ethereum/go-ethereum/p2p/enode" | ||
"github.com/ethereum/go-ethereum/p2p/protocols" | ||
"github.com/ethereum/go-ethereum/swarm/log" | ||
"github.com/ethereum/go-ethereum/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) { | ||
holisticode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we please have this as
setupProtocolSpec
? and the correspondingcreateSpec
ascreateProtocolSpec
? these function names are over-generalisedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any specific reason for that? I have had many "obvious" wording in previous PRs required to be edited away, The variable was called
Spec
before and inp2p/protocols/protocol.go
it is still just calledSpec
. So I don't see a over-generalization here?