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

Commit 5122fcc

Browse files
Swap logger (#1754)
* api,debug,log,main,swap,swarm: adds swap specific logger and saves to disk if --swap-audit-logpath is specified
1 parent afd1d9f commit 5122fcc

File tree

12 files changed

+198
-32
lines changed

12 files changed

+198
-32
lines changed

api/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Config struct {
5757
SwapEnabled bool // whether SWAP incentives are enabled
5858
SwapPaymentThreshold uint64 // honey amount at which a payment is triggered
5959
SwapDisconnectThreshold uint64 // honey amount at which a peer disconnects
60+
SwapLogPath string // dir to swap related audit logs
6061
Contract common.Address // address of the chequebook contract
6162
// end of Swap configs
6263

@@ -94,6 +95,7 @@ func NewConfig() (c *Config) {
9495
SwapEnabled: false,
9596
SwapPaymentThreshold: swap.DefaultPaymentThreshold,
9697
SwapDisconnectThreshold: swap.DefaultDisconnectThreshold,
98+
SwapLogPath: "",
9799
HiveParams: network.NewHiveParams(),
98100
Pss: pss.NewParams(),
99101
EnsRoot: ens.TestNetAddress,

cmd/swarm/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const (
6969
SwarmEnvSwapBackendURL = "SWARM_SWAP_BACKEND_URL"
7070
SwarmEnvSwapPaymentThreshold = "SWARM_SWAP_PAYMENT_THRESHOLD"
7171
SwarmEnvSwapDisconnectThreshold = "SWARM_SWAP_DISCONNECT_THRESHOLD"
72+
SwarmEnvSwapLogPath = "SWARM_SWAP_LOG_PATH"
7273
SwarmEnvSyncDisable = "SWARM_SYNC_DISABLE"
7374
SwarmEnvSyncUpdateDelay = "SWARM_ENV_SYNC_UPDATE_DELAY"
7475
SwarmEnvMaxStreamPeerServers = "SWARM_ENV_MAX_STREAM_PEER_SERVERS"
@@ -204,6 +205,9 @@ func flagsOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Confi
204205
if swapBackendURL := ctx.GlobalString(SwarmSwapBackendURLFlag.Name); swapBackendURL != "" {
205206
currentConfig.SwapBackendURL = swapBackendURL
206207
}
208+
if swapLogPath := ctx.GlobalString(SwarmSwapLogPathFlag.Name); currentConfig.SwapEnabled && swapLogPath != "" {
209+
currentConfig.SwapLogPath = swapLogPath
210+
}
207211
if paymentThreshold := ctx.GlobalUint64(SwarmSwapPaymentThresholdFlag.Name); paymentThreshold != 0 {
208212
currentConfig.SwapPaymentThreshold = paymentThreshold
209213
}

cmd/swarm/flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ var (
7979
Usage: "honey amount at which a peer disconnects",
8080
EnvVar: SwarmEnvSwapDisconnectThreshold,
8181
}
82+
SwarmSwapLogPathFlag = cli.StringFlag{
83+
Name: "swap-audit-logpath",
84+
Usage: "Write execution logs of swap audit to the given directory",
85+
EnvVar: SwarmEnvSwapLogPath,
86+
}
8287
SwarmSyncDisabledFlag = cli.BoolTFlag{
8388
Name: "nosync",
8489
Usage: "Disable swarm syncing",

cmd/swarm/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func init() {
181181
SwarmSwapBackendURLFlag,
182182
SwarmSwapDisconnectThresholdFlag,
183183
SwarmSwapPaymentThresholdFlag,
184+
SwarmSwapLogPathFlag,
184185
SwarmSwapChequebookAddrFlag,
185186
// end of swap flags
186187
SwarmSyncDisabledFlag,

internal/debug/flags.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,23 @@ func init() {
110110
glogger = log.NewGlogHandler(ostream)
111111
}
112112

113+
// rotatingFileHandler returns a RotatingFileHandler this will split the logs into multiple files.
114+
// the files are split based on the limit parameter expressed in bytes
115+
func rotatingFileHandler(logdir string) (log.Handler, error) {
116+
return log.RotatingFileHandler(
117+
logdir,
118+
262144,
119+
log.JSONFormatOrderedEx(false, true),
120+
)
121+
}
122+
113123
// Setup initializes profiling and logging based on the CLI flags.
114124
// It should be called as early as possible in the program.
115125
func Setup(ctx *cli.Context, logdir string) error {
116126
// logging
117127
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
118128
if logdir != "" {
119-
rfh, err := log.RotatingFileHandler(
120-
logdir,
121-
262144,
122-
log.JSONFormatOrderedEx(false, true),
123-
)
129+
rfh, err := rotatingFileHandler(logdir)
124130
if err != nil {
125131
return err
126132
}

log/log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ func Trace(msg string, ctx ...interface{}) {
4646
metrics.GetOrRegisterCounter("trace", nil).Inc(1)
4747
l.Output(msg, l.LvlTrace, CallDepth, ctx...)
4848
}
49+
50+
// GetHandler return the Handler assigned to root
51+
func GetHandler() l.Handler {
52+
return l.Root().GetHandler()
53+
}

swap/peer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"sync"
2525

2626
"github.com/ethereum/go-ethereum/common"
27-
"github.com/ethersphere/swarm/log"
2827
"github.com/ethersphere/swarm/p2p/protocols"
2928
)
3029

@@ -109,7 +108,7 @@ func (p *Peer) updateBalance(amount int64) error {
109108
if err := p.setBalance(newBalance); err != nil {
110109
return err
111110
}
112-
log.Debug("balance for peer after accounting", "peer", p.ID().String(), "balance", strconv.FormatInt(newBalance, 10))
111+
auditLog.Debug("balance for peer after accounting", "peer", p.ID().String(), "balance", strconv.FormatInt(newBalance, 10))
113112
return nil
114113
}
115114

@@ -165,7 +164,7 @@ func (p *Peer) sendCheque() error {
165164
return err
166165
}
167166

168-
log.Info("sending cheque", "honey", cheque.Honey, "cumulativePayout", cheque.ChequeParams.CumulativePayout, "beneficiary", cheque.Beneficiary, "contract", cheque.Contract)
167+
auditLog.Info("sending cheque", "honey", cheque.Honey, "cumulativePayout", cheque.ChequeParams.CumulativePayout, "beneficiary", cheque.Beneficiary, "contract", cheque.Contract)
169168
return p.Send(context.Background(), &EmitChequeMsg{
170169
Cheque: cheque,
171170
})

swap/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
"errors"
2222

2323
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/log"
2425
"github.com/ethereum/go-ethereum/p2p/enode"
2526

2627
"github.com/ethereum/go-ethereum/p2p"
2728
"github.com/ethereum/go-ethereum/rpc"
2829
contract "github.com/ethersphere/swarm/contracts/swap"
29-
"github.com/ethersphere/swarm/log"
3030
"github.com/ethersphere/swarm/p2p/protocols"
3131
)
3232

swap/protocol_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ import (
3636
contract "github.com/ethersphere/swarm/contracts/swap"
3737
"github.com/ethersphere/swarm/p2p/protocols"
3838
p2ptest "github.com/ethersphere/swarm/p2p/testing"
39+
colorable "github.com/mattn/go-colorable"
3940
)
4041

42+
func init() {
43+
log.PrintOrigins(true)
44+
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
45+
}
46+
4147
/*
4248
TestHandshake creates two mock nodes and initiates an exchange;
4349
it expects a handshake to take place between the two nodes

swap/swap.go

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,22 @@ import (
3232
"github.com/ethereum/go-ethereum/core/types"
3333
"github.com/ethereum/go-ethereum/crypto"
3434
"github.com/ethereum/go-ethereum/ethclient"
35+
"github.com/ethereum/go-ethereum/log"
3536
"github.com/ethereum/go-ethereum/p2p/enode"
3637
"github.com/ethersphere/swarm/contracts/swap"
3738
contract "github.com/ethersphere/swarm/contracts/swap"
38-
"github.com/ethersphere/swarm/log"
3939
"github.com/ethersphere/swarm/p2p/protocols"
4040
"github.com/ethersphere/swarm/state"
4141
)
4242

4343
// ErrInvalidChequeSignature indicates the signature on the cheque was invalid
4444
var ErrInvalidChequeSignature = errors.New("invalid cheque signature")
4545

46+
var auditLog log.Logger // logger for Swap related messages and audit trail
47+
48+
// swapLogLevel indicates filter level of log messages
49+
const swapLogLevel = 3
50+
4651
// Swap represents the Swarm Accounting Protocol
4752
// a peer to peer micropayment system
4853
// A node maintains an individual balance with every peer
@@ -80,8 +85,42 @@ func NewParams() *Params {
8085
}
8186
}
8287

88+
// newLogger returns a new logger
89+
func newLogger(logpath string) log.Logger {
90+
swapLogger := log.New("swaplog", "*")
91+
92+
lh := log.Root().GetHandler()
93+
rfh, err := swapRotatingFileHandler(logpath)
94+
95+
if err != nil {
96+
log.Warn("RotatingFileHandler was not initialized", "logdir", logpath, "err", err)
97+
//sets a fallback logger, it will use the swarm logger.
98+
swapLogger.SetHandler(lh)
99+
return swapLogger
100+
}
101+
102+
//Filters messages with the correct logLevel for swap
103+
rfh = log.LvlFilterHandler(log.Lvl(swapLogLevel), rfh)
104+
105+
//Dispatches the logs to the default swarm log and also the filtered swap file logger.
106+
swapLogger.SetHandler(log.MultiHandler(lh, rfh))
107+
108+
return swapLogger
109+
}
110+
111+
// swapRotatingFileHandler returns a RotatingFileHandler this will split the logs into multiple files.
112+
// the files are split based on the limit parameter expressed in bytes
113+
func swapRotatingFileHandler(logdir string) (log.Handler, error) {
114+
return log.RotatingFileHandler(
115+
logdir,
116+
262144,
117+
log.JSONFormatOrderedEx(false, true),
118+
)
119+
}
120+
83121
// new - swap constructor without integrity check
84-
func new(stateStore state.Store, prvkey *ecdsa.PrivateKey, backend contract.Backend, disconnectThreshold uint64, paymentThreshold uint64) *Swap {
122+
func new(logpath string, stateStore state.Store, prvkey *ecdsa.PrivateKey, backend contract.Backend, disconnectThreshold uint64, paymentThreshold uint64) *Swap {
123+
auditLog = newLogger(logpath)
85124
return &Swap{
86125
store: stateStore,
87126
peers: make(map[enode.ID]*Peer),
@@ -95,7 +134,7 @@ func new(stateStore state.Store, prvkey *ecdsa.PrivateKey, backend contract.Back
95134
}
96135

97136
// New - swap constructor with integrity checks
98-
func New(dbPath string, prvkey *ecdsa.PrivateKey, backendURL string, disconnectThreshold uint64, paymentThreshold uint64) (*Swap, error) {
137+
func New(logpath string, dbPath string, prvkey *ecdsa.PrivateKey, backendURL string, disconnectThreshold uint64, paymentThreshold uint64) (*Swap, error) {
99138
// we MUST have a backend
100139
if backendURL == "" {
101140
return nil, errors.New("swap init error: no backend URL given")
@@ -114,6 +153,7 @@ func New(dbPath string, prvkey *ecdsa.PrivateKey, backendURL string, disconnectT
114153
return nil, fmt.Errorf("swap init error: error connecting to Ethereum API %s: %s", backendURL, err)
115154
}
116155
return new(
156+
logpath,
117157
stateStore,
118158
prvkey,
119159
backend,
@@ -185,7 +225,7 @@ func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) {
185225
// It is the peer with a negative balance who sends a cheque, thus we check
186226
// that the balance is *below* the threshold
187227
if swapPeer.getBalance() <= -s.paymentThreshold {
188-
log.Warn("balance for peer went over the payment threshold, sending cheque", "peer", peer.ID().String(), "payment threshold", s.paymentThreshold)
228+
auditLog.Info("balance for peer went over the payment threshold, sending cheque", "peer", peer.ID().String(), "payment threshold", s.paymentThreshold)
189229
return swapPeer.sendCheque()
190230
}
191231

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

214254
cheque := msg.Cheque
215-
log.Info("received cheque from peer", "peer", p.ID().String(), "honey", cheque.Honey)
255+
auditLog.Info("received cheque from peer", "peer", p.ID().String(), "honey", cheque.Honey)
216256
_, err := s.processAndVerifyCheque(cheque, p)
217257
if err != nil {
218258
return err
219259
}
220260

221-
log.Debug("received cheque processed and verified", "peer", p.ID().String())
261+
auditLog.Debug("received cheque processed and verified", "peer", p.ID().String())
222262

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

256296
if result.Bounced {
257-
log.Error("cheque bounced", "tx", receipt.TxHash)
297+
auditLog.Warn("cheque bounced", "tx", receipt.TxHash)
258298
return
259299
// TODO: do something here
260300
}
261301

262-
log.Debug("cash tx mined", "receipt", receipt)
302+
auditLog.Debug("cash tx mined", "receipt", receipt)
263303
}
264304

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

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

@@ -432,13 +472,13 @@ func (s *Swap) Deploy(ctx context.Context) error {
432472
opts.Value = big.NewInt(int64(s.params.InitialDepositAmount))
433473
opts.Context = ctx
434474

435-
log.Info("deploying new swap", "owner", opts.From.Hex())
475+
auditLog.Info("deploying new swap", "owner", opts.From.Hex())
436476
address, err := s.deployLoop(opts, s.owner.address, defaultHarddepositTimeoutDuration)
437477
if err != nil {
438-
log.Error("unable to deploy swap", "error", err)
478+
auditLog.Error("unable to deploy swap", "error", err)
439479
return err
440480
}
441-
log.Info("swap deployed", "address", address.Hex(), "owner", opts.From.Hex())
481+
auditLog.Info("swap deployed", "address", address.Hex(), "owner", opts.From.Hex())
442482

443483
return err
444484
}
@@ -452,11 +492,11 @@ func (s *Swap) deployLoop(opts *bind.TransactOpts, owner common.Address, default
452492
}
453493

454494
if s.contract, tx, err = contract.Deploy(opts, s.backend, owner, defaultHarddepositTimeoutDuration); err != nil {
455-
log.Warn("can't send chequebook deploy tx", "try", try, "error", err)
495+
auditLog.Warn("can't send chequebook deploy tx", "try", try, "error", err)
456496
continue
457497
}
458498
if addr, err = bind.WaitDeployed(opts.Context, s.backend, tx); err != nil {
459-
log.Warn("chequebook deploy error", "try", try, "error", err)
499+
auditLog.Warn("chequebook deploy error", "try", try, "error", err)
460500
continue
461501
}
462502
return addr, nil

0 commit comments

Comments
 (0)