Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

swarm, swap: add addresses to logs #1806

Merged
merged 40 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ed9f561
swap: add selfAddress to swap logger context
mortelli Sep 20, 2019
7295408
swap: minor refactor in new function
mortelli Sep 20, 2019
f91233a
swap: update comments in newLogger function to comply with current co…
mortelli Sep 20, 2019
02ef574
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Sep 23, 2019
c10beae
swap: add peer ID to log entry in peer.sendCheque function
mortelli Sep 23, 2019
86bb6e7
swap: create separate logger variables for Swap and Peer structs
mortelli Sep 23, 2019
1632fdb
swap: reformat log context in peer calls
mortelli Sep 24, 2019
9d6eb39
swap: remove print, add missing error message in TestSwapLogToFile test
mortelli Sep 24, 2019
f1c3fc4
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Sep 24, 2019
b6086a1
swap: create different functions for swap and swap/peer loggers
mortelli Sep 24, 2019
c151daa
swap: remove auditLog variable
mortelli Sep 24, 2019
ecfec4c
swap: change logger functions to receive Swap struct as parameter
mortelli Sep 24, 2019
518ccf1
swap: add comments to logger vars and funcs
mortelli Sep 24, 2019
083cb4e
swap: update comments for logger vars and funcs
mortelli Sep 24, 2019
0746650
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Sep 25, 2019
0399110
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Sep 25, 2019
6a6c293
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Sep 25, 2019
e35dc65
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Sep 26, 2019
1100486
swap: refactor setLoggerHandler function
mortelli Sep 26, 2019
7be01a1
swap: minor comment expansion
mortelli Sep 26, 2019
272db7f
swarm, swap: add overlay address to swap params
mortelli Sep 26, 2019
f771aa2
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Sep 30, 2019
e424853
swap: add comment to new function
mortelli Sep 30, 2019
0d892a3
swap: expand swap log messages when retrying to deploy the chequebook…
mortelli Sep 30, 2019
a0238b7
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 1, 2019
423ff3d
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 2, 2019
f4e5156
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 2, 2019
8c42e64
swap: minor change to swap cheque log
mortelli Oct 2, 2019
7989efc
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 3, 2019
346edeb
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 3, 2019
2d25f1f
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 7, 2019
1f58276
swap: add t.Helper call to newDefaultParams function
mortelli Oct 7, 2019
edc9db0
swap: rename new function to newSwapInstance
mortelli Oct 7, 2019
481f5e0
swap: rename new function to newSwapInstance
mortelli Oct 7, 2019
d91d4a6
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 8, 2019
43d8d45
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 9, 2019
32af153
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 11, 2019
48dc81b
swap: change the swap logger back to a global variable
mortelli Oct 11, 2019
c49d7cb
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 11, 2019
6ebda39
Merge remote-tracking branch 'origin/master' into swap-logger-addresses
mortelli Oct 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions swap/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethersphere/swarm/p2p/protocols"
)

Expand All @@ -40,6 +41,7 @@ type Peer struct {
lastReceivedCheque *Cheque
lastSentCheque *Cheque
balance int64
logger log.Logger // logger for swap related messages and audit trail with peer identifier
}

// NewPeer creates a new swap Peer instance
Expand All @@ -49,6 +51,7 @@ func NewPeer(p *protocols.Peer, s *Swap, beneficiary common.Address, contractAdd
swap: s,
beneficiary: beneficiary,
contractAddress: contractAddress,
logger: newPeerLogger(s, p.ID()),
mortelli marked this conversation as resolved.
Show resolved Hide resolved
}

if peer.lastReceivedCheque, err = s.loadLastReceivedCheque(p.ID()); err != nil {
Expand Down Expand Up @@ -108,7 +111,7 @@ func (p *Peer) updateBalance(amount int64) error {
if err := p.setBalance(newBalance); err != nil {
return err
}
auditLog.Debug("balance for peer after accounting", "peer", p.ID().String(), "balance", strconv.FormatInt(newBalance, 10))
p.logger.Debug("updated balance", "balance", strconv.FormatInt(newBalance, 10))
return nil
}

Expand Down Expand Up @@ -164,7 +167,7 @@ func (p *Peer) sendCheque() error {
return err
}

auditLog.Info("sending cheque", "honey", cheque.Honey, "cumulativePayout", cheque.ChequeParams.CumulativePayout, "beneficiary", cheque.Beneficiary, "contract", cheque.Contract)
p.logger.Info("sending cheque to peer", "honey", cheque.Honey, "cumulativePayout", cheque.ChequeParams.CumulativePayout, "beneficiary", cheque.Beneficiary, "contract", cheque.Contract)
return p.Send(context.Background(), &EmitChequeMsg{
Cheque: cheque,
})
Expand Down
72 changes: 42 additions & 30 deletions swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ import (
// ErrInvalidChequeSignature indicates the signature on the cheque was invalid
var ErrInvalidChequeSignature = errors.New("invalid cheque signature")

var auditLog log.Logger // logger for Swap related messages and audit trail

// swapLogLevel indicates filter level of log messages
const swapLogLevel = 3

Expand All @@ -64,6 +62,8 @@ type Swap struct {
honeyPriceOracle HoneyOracle // oracle which resolves the price of honey (in Wei)
paymentThreshold int64 // honey amount at which a payment is triggered
disconnectThreshold int64 // honey amount at which a peer disconnects
logpath string // path to log files in case they are persisted to disk
logger log.Logger // logger for swap related messages and audit trail
}

// Owner encapsulates information related to accessing the contract
Expand All @@ -85,27 +85,37 @@ func NewParams() *Params {
}
}

// newLogger returns a new logger
func newLogger(logpath string) log.Logger {
swapLogger := log.New("swaplog", "*")
// newSwapLogger returns a new logger for standard swap logs
func newSwapLogger(s *Swap) log.Logger {
swapLogger := log.New("swaplog", "*", "selfAddress", s.owner.address)
santicomp2014 marked this conversation as resolved.
Show resolved Hide resolved
setLoggerHandler(s.logpath, swapLogger)
return swapLogger
}

// newPeerLogger returns a new logger for swap logs with peer info
func newPeerLogger(s *Swap, peerID enode.ID) log.Logger {
peerLogger := log.New("swaplog", "*", "selfAddress", s.owner.address, "peer", peerID)
setLoggerHandler(s.logpath, peerLogger)
return peerLogger
}

// setLoggerHandler will set the logger handle to write logs to the specified path
// or use the default swarm logger in case this isn't specified or an error occurs
func setLoggerHandler(logpath string, logger log.Logger) {
lh := log.Root().GetHandler()
rfh, err := swapRotatingFileHandler(logpath)

if err != nil {
log.Warn("RotatingFileHandler was not initialized", "logdir", logpath, "err", err)
//sets a fallback logger, it will use the swarm logger.
swapLogger.SetHandler(lh)
return swapLogger
}

//Filters messages with the correct logLevel for swap
rfh = log.LvlFilterHandler(log.Lvl(swapLogLevel), rfh)

//Dispatches the logs to the default swarm log and also the filtered swap file logger.
swapLogger.SetHandler(log.MultiHandler(lh, rfh))
// use the default swarm logger as a fallback
logger.SetHandler(lh)
} else {
santicomp2014 marked this conversation as resolved.
Show resolved Hide resolved
// filter messages with the correct log level for swap
rfh = log.LvlFilterHandler(log.Lvl(swapLogLevel), rfh)

return swapLogger
// dispatch the logs to the default swarm log and also the filtered swap logger
logger.SetHandler(log.MultiHandler(lh, rfh))
}
}

// swapRotatingFileHandler returns a RotatingFileHandler this will split the logs into multiple files.
Expand All @@ -120,8 +130,7 @@ func swapRotatingFileHandler(logdir string) (log.Handler, error) {

// new - swap constructor without integrity check
func new(logpath string, stateStore state.Store, prvkey *ecdsa.PrivateKey, backend contract.Backend, disconnectThreshold uint64, paymentThreshold uint64) *Swap {
auditLog = newLogger(logpath)
return &Swap{
s := &Swap{
store: stateStore,
peers: make(map[enode.ID]*Peer),
backend: backend,
Expand All @@ -130,7 +139,10 @@ func new(logpath string, stateStore state.Store, prvkey *ecdsa.PrivateKey, backe
disconnectThreshold: int64(disconnectThreshold),
paymentThreshold: int64(paymentThreshold),
honeyPriceOracle: NewHoneyPriceOracle(),
logpath: logpath,
santicomp2014 marked this conversation as resolved.
Show resolved Hide resolved
}
s.logger = newSwapLogger(s)
return s
}

// New - swap constructor with integrity checks
Expand Down Expand Up @@ -225,7 +237,7 @@ func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) {
// It is the peer with a negative balance who sends a cheque, thus we check
// that the balance is *below* the threshold
if swapPeer.getBalance() <= -s.paymentThreshold {
auditLog.Info("balance for peer went over the payment threshold, sending cheque", "peer", peer.ID().String(), "payment threshold", s.paymentThreshold)
swapPeer.logger.Info("balance for peer went over the payment threshold, sending cheque", "payment threshold", s.paymentThreshold)
return swapPeer.sendCheque()
}

Expand All @@ -252,13 +264,13 @@ func (s *Swap) handleEmitChequeMsg(ctx context.Context, p *Peer, msg *EmitCheque
defer p.lock.Unlock()

cheque := msg.Cheque
auditLog.Info("received cheque from peer", "peer", p.ID().String(), "honey", cheque.Honey)
p.logger.Info("received cheque from peer", "honey", cheque.Honey)
_, err := s.processAndVerifyCheque(cheque, p)
if err != nil {
return err
}

auditLog.Debug("received cheque processed and verified", "peer", p.ID().String())
p.logger.Debug("received cheque, processed and verified")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be good to log which cheque was processed and verified? without any identifying details this logline doesn't say much

Copy link
Contributor Author

@mortelli mortelli Sep 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add honey as in line 307, but other than that, i'm not sure which field could identify a cheque in a way that is useful for logs.

i am certain that knowing a cheque has arrived/been sent is useful already.

tagging @ralph-pichler and @Eknir for insight

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CumulativePayout and Contract fields should be enough to uniquely identify which cheque it is. Instead of Contract p.beneficiary might also be useful (this would be the signer of the cheque here).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added Beneficiary and CumulativePayout to this Debug log entry

thanks guys


// reset balance by amount
// as this is done by the creditor, receiving the cheque, the amount should be negative,
Expand Down Expand Up @@ -289,17 +301,17 @@ func cashCheque(s *Swap, otherSwap contract.Contract, opts *bind.TransactOpts, c
if err != nil {
// TODO: do something with the error
// and we actually need to log this error as we are in an async routine; nobody is handling this error for now
auditLog.Error("error cashing cheque", "err", err)
s.logger.Error("error cashing cheque", "err", err)
return
}

if result.Bounced {
auditLog.Warn("cheque bounced", "tx", receipt.TxHash)
s.logger.Warn("cheque bounced", "tx", receipt.TxHash)
return
// TODO: do something here
}

auditLog.Debug("cash tx mined", "receipt", receipt)
s.logger.Debug("cash tx mined", "receipt", receipt)
}

// processAndVerifyCheque verifies the cheque and compares it with the last received cheque
Expand All @@ -323,7 +335,7 @@ func (s *Swap) processAndVerifyCheque(cheque *Cheque, p *Peer) (uint64, error) {
}

if err := p.setLastReceivedCheque(cheque); err != nil {
auditLog.Error("error while saving last received cheque", "peer", p.ID().String(), "err", err.Error())
p.logger.Error("error while saving last received cheque", "err", err.Error())
// TODO: what do we do here? Related issue: https://github.com/ethersphere/swarm/issues/1515
}

Expand Down Expand Up @@ -472,13 +484,13 @@ func (s *Swap) Deploy(ctx context.Context) error {
opts.Value = big.NewInt(int64(s.params.InitialDepositAmount))
opts.Context = ctx

auditLog.Info("deploying new swap", "owner", opts.From.Hex())
s.logger.Info("deploying new swap", "owner", opts.From.Hex())
address, err := s.deployLoop(opts, s.owner.address, defaultHarddepositTimeoutDuration)
if err != nil {
auditLog.Error("unable to deploy swap", "error", err)
s.logger.Error("unable to deploy swap", "error", err)
return err
}
auditLog.Info("swap deployed", "address", address.Hex(), "owner", opts.From.Hex())
s.logger.Info("swap deployed", "address", address.Hex(), "owner", opts.From.Hex())

return err
}
Expand All @@ -492,11 +504,11 @@ func (s *Swap) deployLoop(opts *bind.TransactOpts, owner common.Address, default
}

if s.contract, tx, err = contract.Deploy(opts, s.backend, owner, defaultHarddepositTimeoutDuration); err != nil {
auditLog.Warn("can't send chequebook deploy tx", "try", try, "error", err)
s.logger.Warn("can't send chequebook deploy tx", "try", try, "error", err)
acud marked this conversation as resolved.
Show resolved Hide resolved
continue
}
if addr, err = bind.WaitDeployed(opts.Context, s.backend, tx); err != nil {
auditLog.Warn("chequebook deploy error", "try", try, "error", err)
s.logger.Warn("chequebook deploy error", "try", try, "error", err)
continue
}
return addr, nil
Expand Down
7 changes: 5 additions & 2 deletions swap/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ func TestSwapLogToFile(t *testing.T) {
// so creditor is the model of the remote mode for the debitor! (and vice versa)
cPeer := newDummyPeerWithSpec(Spec)
dPeer := newDummyPeerWithSpec(Spec)
fmt.Println(1)

creditor, err := debitorSwap.addPeer(cPeer.Peer, creditorSwap.owner.address, debitorSwap.GetParams().ContractAddress)
if err != nil {
t.Fatal(err)
Expand All @@ -1313,9 +1313,12 @@ func TestSwapLogToFile(t *testing.T) {
}

files, err := ioutil.ReadDir(logDirDebitor)
if err != nil || len(files) == 0 {
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatalf("expected at least 1 file in the log directory, found none")
}

logFile := path.Join(logDirDebitor, files[0].Name())

Expand Down