Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
laocheng.cheng committed Sep 24, 2021
1 parent 982cbf9 commit f54baef
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 204 deletions.
25 changes: 11 additions & 14 deletions accounting/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (

"github.com/TRON-US/go-btfs/transaction/logging"
"github.com/TRON-US/go-btfs/transaction/storage"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/pricing"
"github.com/ethersphere/bee/pkg/swarm"
//"github.com/ethersphere/bee/pkg/p2p"
//"github.com/ethersphere/bee/pkg/pricing"
//"github.com/ethersphere/bee/pkg/swarm"
)


// PayFunc is the function used for async monetary settlement
type PayFunc func(context.Context, swarm.Address, *big.Int)
type PayFunc func(context.Context, string, *big.Int)

// accountingPeer holds all in-memory accounting information for one peer.
type accountingPeer struct {
Expand All @@ -44,25 +44,22 @@ type Accounting struct {

// lower bound for the value of issued cheques
minimumPayment *big.Int
pricing pricing.Interface
wg sync.WaitGroup
p2p p2p.Service
//p2p p2p.Service
timeNow func() time.Time
}

// NewAccounting creates a new Accounting instance with the provided options.
func NewAccounting(
Logger logging.Logger,
Store storage.StateStorer,
Pricing pricing.Interface,

) (*Accounting, error) {
return &Accounting{
accountingPeers: make(map[string]*accountingPeer),
logger: Logger,
store: Store,
minimumPayment: big.NewInt(0),
pricing: Pricing,
timeNow: time.Now,
}, nil
}
Expand All @@ -78,7 +75,7 @@ func (a *Accounting) Close() error {
}

// Settle to a peer. The lock on the accountingPeer must be held when called.
func (a *Accounting) Settle(toPeer swarm.Address, paymentAmount *big.Int) error {
func (a *Accounting) Settle(toPeer string, paymentAmount *big.Int) error {
if paymentAmount.Cmp(a.minimumPayment) >= 0 {
a.wg.Add(1)
go a.payFunction(context.Background(), toPeer, paymentAmount)
Expand All @@ -89,23 +86,23 @@ func (a *Accounting) Settle(toPeer swarm.Address, paymentAmount *big.Int) error

// getAccountingPeer returns the accountingPeer for a given swarm address.
// If not found in memory it will initialize it.
func (a *Accounting) getAccountingPeer(peer swarm.Address) *accountingPeer {
func (a *Accounting) getAccountingPeer(peer string) *accountingPeer {
a.accountingPeersMu.Lock()
defer a.accountingPeersMu.Unlock()

peerData, ok := a.accountingPeers[peer.String()]
peerData, ok := a.accountingPeers[peer]
if !ok {
peerData = &accountingPeer{
connected: false,
}
a.accountingPeers[peer.String()] = peerData
a.accountingPeers[peer] = peerData
}

return peerData
}

// NotifyPaymentSent is triggered by async monetary settlement to update our balance and remove it's price from the shadow reserve
func (a *Accounting) NotifyPaymentSent(peer swarm.Address, amount *big.Int, receivedError error) {
func (a *Accounting) NotifyPaymentSent(peer string, amount *big.Int, receivedError error) {
defer a.wg.Done()
accountingPeer := a.getAccountingPeer(peer)

Expand All @@ -121,7 +118,7 @@ func (a *Accounting) NotifyPaymentSent(peer swarm.Address, amount *big.Int, rece


// NotifyPayment is called by Settlement when we receive a payment.
func (a *Accounting) NotifyPaymentReceived(peer swarm.Address, amount *big.Int) error {
func (a *Accounting) NotifyPaymentReceived(peer string, amount *big.Int) error {
accountingPeer := a.getAccountingPeer(peer)

accountingPeer.lock.Lock()
Expand Down
25 changes: 10 additions & 15 deletions accounting/accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
"errors"
"io/ioutil"
"math/big"
"strings"
"testing"
"time"

"github.com/TRON-US/go-btfs/accounting"
"github.com/TRON-US/go-btfs/statestore/mock"
"github.com/TRON-US/go-btfs/transaction/logging"

"github.com/ethersphere/bee/pkg/swarm"
peer "github.com/libp2p/go-libp2p-core/peer"
//"github.com/ethersphere/bee/pkg/swarm"
)

type paymentCall struct {
peer swarm.Address
peer string
amount *big.Int
}

Expand All @@ -32,22 +34,18 @@ func TestAccountingCallSettlementTooSoon(t *testing.T) {
store := mock.NewStateStore()
defer store.Close()

acc, err := accounting.NewAccounting(logger, store, nil)
acc, err := accounting.NewAccounting(logger, store)
if err != nil {
t.Fatal(err)
}

paychan := make(chan paymentCall, 1)

acc.SetPayFunc(func(ctx context.Context, peer swarm.Address, amount *big.Int) {
acc.SetPayFunc(func(ctx context.Context, peer string, amount *big.Int) {
paychan <- paymentCall{peer: peer, amount: amount}
})

peer1Addr, err := swarm.ParseHexAddress("00112233")
if err != nil {
t.Fatal(err)
}

peer1Addr := peer.ID("00112233").String()

requestPriceTmp := 1000

Expand All @@ -56,7 +54,7 @@ func TestAccountingCallSettlementTooSoon(t *testing.T) {
if call.amount.Cmp(big.NewInt(int64(requestPriceTmp))) != 0 {
t.Fatalf("paid wrong amount. got %d wanted %d", call.amount, requestPriceTmp)
}
if !call.peer.Equal(peer1Addr) {
if strings.Compare(call.peer, peer1Addr) != 0 {
t.Fatalf("wrong peer address got %v wanted %v", call.peer, peer1Addr)
}
case <-time.After(1 * time.Second):
Expand All @@ -74,15 +72,12 @@ func TestAccountingNotifyPaymentReceived(t *testing.T) {
store := mock.NewStateStore()
defer store.Close()

acc, err := accounting.NewAccounting(logger, store, nil)
acc, err := accounting.NewAccounting(logger, store)
if err != nil {
t.Fatal(err)
}

peer1Addr, err := swarm.ParseHexAddress("00112233")
if err != nil {
t.Fatal(err)
}
peer1Addr := peer.ID("00112233").String()

var amoutTmp uint64 = 5000

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.4 h1:EBfaK0SWSwk+fgk6efYFWdzl8MwRWoOO1gkmiaTXPW4=
github.com/klauspost/cpuid v1.2.4/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid/v2 v2.0.4 h1:g0I61F2K2DjRHz1cnxlkNSBIaePVoJIjjnHui8QHbiw=
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.8 h1:bhR2mgIlno/Sfk4oUbH4sPlc83z1yGrN9bvqiq3C33I=
Expand Down Expand Up @@ -1518,6 +1519,7 @@ github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB8
github.com/prometheus/common v0.14.0 h1:RHRyE8UocrbjU+6UvRzwi6HjiDfxrrBU91TtbKzkGp4=
github.com/prometheus/common v0.14.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/common v0.18.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/common v0.26.0 h1:iMAkS2TDoNWnKM+Kopnx/8tnEStIfpYA0ur0xQzzhMQ=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/common v0.29.0 h1:3jqPBvKT4OHAbje2Ql7KeaaSicDBCxMYwEJU1zRJceE=
github.com/prometheus/common v0.29.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
Expand All @@ -1533,6 +1535,7 @@ github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.2.0 h1:wH4vA7pcjKuZzjF7lM8awk4fnuJO6idemZXoKnULUx4=
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.0 h1:OQZ41sZU9XkRpzrz8/TD0EldH/Rwbddkdu5wDyUwzfE=
github.com/prometheus/procfs v0.7.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
Expand Down
18 changes: 9 additions & 9 deletions settlement/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"math/big"

"github.com/ethersphere/bee/pkg/swarm"
//"github.com/ethersphere/bee/pkg/swarm"
)

var (
Expand All @@ -18,20 +18,20 @@ var (
// Interface is the interface used by Accounting to trigger settlement
type Interface interface {
// TotalSent returns the total amount sent to a peer
TotalSent(peer swarm.Address) (totalSent *big.Int, err error)
TotalSent(peer string) (totalSent *big.Int, err error)
// TotalReceived returns the total amount received from a peer
TotalReceived(peer swarm.Address) (totalSent *big.Int, err error)
TotalReceived(peer string) (totalSent *big.Int, err error)
// SettlementsSent returns sent settlements for each individual known peer
SettlementsSent() (map[string]*big.Int, error)
// SettlementsReceived returns received settlements for each individual known peer
SettlementsReceived() (map[string]*big.Int, error)
}

type Accounting interface {
PeerDebt(peer swarm.Address) (*big.Int, error)
NotifyPaymentReceived(peer swarm.Address, amount *big.Int) error
NotifyPaymentSent(peer swarm.Address, amount *big.Int, receivedError error)
NotifyRefreshmentReceived(peer swarm.Address, amount *big.Int) error
Connect(peer swarm.Address)
Disconnect(peer swarm.Address)
PeerDebt(peer string) (*big.Int, error)
NotifyPaymentReceived(peer string, amount *big.Int) error
NotifyPaymentSent(peer string, amount *big.Int, receivedError error)
NotifyRefreshmentReceived(peer string, amount *big.Int) error
Connect(peer string)
Disconnect(peer string)
}
52 changes: 26 additions & 26 deletions settlement/swap/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/TRON-US/go-btfs/transaction/storage"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethereum/go-ethereum/common"
//"github.com/ethersphere/bee/pkg/swarm"
)

var (
Expand All @@ -25,19 +25,19 @@ var (
// Addressbook maps peers to beneficaries, chequebooks and in reverse.
type Addressbook interface {
// Beneficiary returns the beneficiary for the given peer.
Beneficiary(peer swarm.Address) (beneficiary common.Address, known bool, err error)
Beneficiary(peer string) (beneficiary common.Address, known bool, err error)
// Chequebook returns the chequebook for the given peer.
Chequebook(peer swarm.Address) (chequebookAddress common.Address, known bool, err error)
Chequebook(peer string) (chequebookAddress common.Address, known bool, err error)
// BeneficiaryPeer returns the peer for a beneficiary.
BeneficiaryPeer(beneficiary common.Address) (peer swarm.Address, known bool, err error)
BeneficiaryPeer(beneficiary common.Address) (peer string, known bool, err error)
// ChequebookPeer returns the peer for a beneficiary.
ChequebookPeer(chequebook common.Address) (peer swarm.Address, known bool, err error)
ChequebookPeer(chequebook common.Address) (peer string, known bool, err error)
// PutBeneficiary stores the beneficiary for the given peer.
PutBeneficiary(peer swarm.Address, beneficiary common.Address) error
PutBeneficiary(peer string, beneficiary common.Address) error
// PutChequebook stores the chequebook for the given peer.
PutChequebook(peer swarm.Address, chequebook common.Address) error
PutChequebook(peer string, chequebook common.Address) error
// MigratePeer returns whether a peer have already received a cheque that has been deducted
MigratePeer(oldPeer, newPeer swarm.Address) error
MigratePeer(oldPeer, newPeer string) error
}

type addressbook struct {
Expand All @@ -51,7 +51,7 @@ func NewAddressbook(store storage.StateStorer) Addressbook {
}
}

func (a *addressbook) MigratePeer(oldPeer, newPeer swarm.Address) error {
func (a *addressbook) MigratePeer(oldPeer, newPeer string) error {
ba, known, err := a.Beneficiary(oldPeer)
if err != nil {
return err
Expand Down Expand Up @@ -86,7 +86,7 @@ func (a *addressbook) MigratePeer(oldPeer, newPeer swarm.Address) error {
}

// Beneficiary returns the beneficiary for the given peer.
func (a *addressbook) Beneficiary(peer swarm.Address) (beneficiary common.Address, known bool, err error) {
func (a *addressbook) Beneficiary(peer string) (beneficiary common.Address, known bool, err error) {
err = a.store.Get(peerBeneficiaryKey(peer), &beneficiary)
if err != nil {
if err != storage.ErrNotFound {
Expand All @@ -98,19 +98,19 @@ func (a *addressbook) Beneficiary(peer swarm.Address) (beneficiary common.Addres
}

// BeneficiaryPeer returns the peer for a beneficiary.
func (a *addressbook) BeneficiaryPeer(beneficiary common.Address) (peer swarm.Address, known bool, err error) {
func (a *addressbook) BeneficiaryPeer(beneficiary common.Address) (peer string, known bool, err error) {
err = a.store.Get(beneficiaryPeerKey(beneficiary), &peer)
if err != nil {
if err != storage.ErrNotFound {
return swarm.Address{}, false, err
return "", false, err
}
return swarm.Address{}, false, nil
return "", false, nil
}
return peer, true, nil
}

// Chequebook returns the chequebook for the given peer.
func (a *addressbook) Chequebook(peer swarm.Address) (chequebookAddress common.Address, known bool, err error) {
func (a *addressbook) Chequebook(peer string) (chequebookAddress common.Address, known bool, err error) {
err = a.store.Get(peerKey(peer), &chequebookAddress)
if err != nil {
if err != storage.ErrNotFound {
Expand All @@ -122,19 +122,19 @@ func (a *addressbook) Chequebook(peer swarm.Address) (chequebookAddress common.A
}

// ChequebookPeer returns the peer for a beneficiary.
func (a *addressbook) ChequebookPeer(chequebook common.Address) (peer swarm.Address, known bool, err error) {
func (a *addressbook) ChequebookPeer(chequebook common.Address) (peer string, known bool, err error) {
err = a.store.Get(chequebookPeerKey(chequebook), &peer)
if err != nil {
if err != storage.ErrNotFound {
return swarm.Address{}, false, err
return "", false, err
}
return swarm.Address{}, false, nil
return "", false, nil
}
return peer, true, nil
}

// PutBeneficiary stores the beneficiary for the given peer.
func (a *addressbook) PutBeneficiary(peer swarm.Address, beneficiary common.Address) error {
func (a *addressbook) PutBeneficiary(peer string, beneficiary common.Address) error {
err := a.store.Put(peerBeneficiaryKey(peer), beneficiary)
if err != nil {
return err
Expand All @@ -143,7 +143,7 @@ func (a *addressbook) PutBeneficiary(peer swarm.Address, beneficiary common.Addr
}

// PutChequebook stores the chequebook for the given peer.
func (a *addressbook) PutChequebook(peer swarm.Address, chequebook common.Address) error {
func (a *addressbook) PutChequebook(peer string, chequebook common.Address) error {
err := a.store.Put(peerKey(peer), chequebook)
if err != nil {
return err
Expand All @@ -152,7 +152,7 @@ func (a *addressbook) PutChequebook(peer swarm.Address, chequebook common.Addres
}

// peerKey computes the key where to store the chequebook from a peer.
func peerKey(peer swarm.Address) string {
func peerKey(peer string) string {
return fmt.Sprintf("%s%s", peerPrefix, peer)
}

Expand All @@ -162,7 +162,7 @@ func chequebookPeerKey(chequebook common.Address) string {
}

// peerBeneficiaryKey computes the key where to store the beneficiary for a peer.
func peerBeneficiaryKey(peer swarm.Address) string {
func peerBeneficiaryKey(peer string) string {
return fmt.Sprintf("%s%s", peerBeneficiaryPrefix, peer)
}

Expand All @@ -171,10 +171,10 @@ func beneficiaryPeerKey(peer common.Address) string {
return fmt.Sprintf("%s%x", beneficiaryPeerPrefix, peer)
}

func peerDeductedByKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", deductedByPeerPrefix, peer.String())
func peerDeductedByKey(peer string) string {
return fmt.Sprintf("%s%s", deductedByPeerPrefix, peer)
}

func peerDeductedForKey(peer swarm.Address) string {
return fmt.Sprintf("%s%s", deductedForPeerPrefix, peer.String())
func peerDeductedForKey(peer string) string {
return fmt.Sprintf("%s%s", deductedForPeerPrefix, peer)
}
Loading

0 comments on commit f54baef

Please sign in to comment.