@@ -37,6 +37,7 @@ import (
3737
3838 "github.com/ethereum/go-ethereum/accounts/abi/bind"
3939 "github.com/ethereum/go-ethereum/common"
40+ "github.com/ethereum/go-ethereum/common/hexutil"
4041 "github.com/ethereum/go-ethereum/contracts/chequebook/contract"
4142 "github.com/ethereum/go-ethereum/core/types"
4243 "github.com/ethereum/go-ethereum/crypto"
@@ -104,6 +105,8 @@ type Chequebook struct {
104105 txhash string // tx hash of last deposit tx
105106 threshold * big.Int // threshold that triggers autodeposit if not nil
106107 buffer * big.Int // buffer to keep on top of balance for fork protection
108+
109+ log log.Logger // contextual logger with the contrac address embedded
107110}
108111
109112func (self * Chequebook ) String () string {
@@ -135,19 +138,20 @@ func NewChequebook(path string, contractAddr common.Address, prvKey *ecdsa.Priva
135138 owner : transactOpts .From ,
136139 contract : chbook ,
137140 session : session ,
141+ log : log .New ("contract" , contractAddr ),
138142 }
139143
140144 if (contractAddr != common.Address {}) {
141145 self .setBalanceFromBlockChain ()
142- log .Trace (fmt . Sprintf ( "new chequebook initialised from %s (owner: %v, balance: %s)" , contractAddr . Hex (), self .owner . Hex (), self .balance . String ()) )
146+ self . log .Trace ("New chequebook initialised" , "owner" , self .owner , "balance" , self .balance )
143147 }
144148 return
145149}
146150
147151func (self * Chequebook ) setBalanceFromBlockChain () {
148152 balance , err := self .backend .BalanceAt (context .TODO (), self .contractAddr , nil )
149153 if err != nil {
150- log .Error (fmt . Sprintf ( "can't get balance: %v" , err ) )
154+ log .Error ("Failed to retrieve chequebook balance" , "err" , err )
151155 } else {
152156 self .balance .Set (balance )
153157 }
@@ -160,7 +164,6 @@ func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend, chec
160164 if err != nil {
161165 return
162166 }
163-
164167 self , _ = NewChequebook (path , common.Address {}, prvKey , backend )
165168
166169 err = json .Unmarshal (data , self )
@@ -170,8 +173,7 @@ func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend, chec
170173 if checkBalance {
171174 self .setBalanceFromBlockChain ()
172175 }
173-
174- log .Trace (fmt .Sprintf ("loaded chequebook (%s, owner: %v, balance: %v) initialised from %v" , self .contractAddr .Hex (), self .owner .Hex (), self .balance , path ))
176+ log .Trace ("Loaded chequebook from disk" , "path" , path )
175177
176178 return
177179}
@@ -226,7 +228,7 @@ func (self *Chequebook) Save() (err error) {
226228 if err != nil {
227229 return err
228230 }
229- log .Trace (fmt . Sprintf ( "saving chequebook (%s) to %v " , self .contractAddr . Hex (), self . path ) )
231+ self . log .Trace ("Saving chequebook to disk " , self .path )
230232
231233 return ioutil .WriteFile (self .path , data , os .ModePerm )
232234}
@@ -339,12 +341,12 @@ func (self *Chequebook) deposit(amount *big.Int) (string, error) {
339341 chbookRaw := & contract.ChequebookRaw {Contract : self .contract }
340342 tx , err := chbookRaw .Transfer (depositTransactor )
341343 if err != nil {
342- log .Warn (fmt . Sprintf ( "error depositing %d wei to chequebook (%s, balance: %v, target: %v): %v" , amount , self . contractAddr . Hex () , self .balance , self .buffer , err ) )
344+ self . log .Warn ("Failed to fund chequebook" , "amount" , amount , "balance" , self .balance , "target" , self .buffer , " err" , err )
343345 return "" , err
344346 }
345347 // assume that transaction is actually successful, we add the amount to balance right away
346348 self .balance .Add (self .balance , amount )
347- log .Trace (fmt . Sprintf ( "deposited %d wei to chequebook (%s, balance: %v, target: %v)" , amount , self . contractAddr . Hex () , self .balance , self .buffer ) )
349+ self . log .Trace ("Deposited funds to chequebook" , "amount" , amount , "balance" , self .balance , "target" , self .buffer )
348350 return tx .Hash ().Hex (), nil
349351}
350352
@@ -440,6 +442,7 @@ type Inbox struct {
440442 maxUncashed * big.Int // threshold that triggers autocashing
441443 cashed * big.Int // cumulative amount cashed
442444 cheque * Cheque // last cheque, nil if none yet received
445+ log log.Logger // contextual logger with the contrac address embedded
443446}
444447
445448// NewInbox creates an Inbox. An Inboxes is not persisted, the cumulative sum is updated
@@ -467,8 +470,9 @@ func NewInbox(prvKey *ecdsa.PrivateKey, contractAddr, beneficiary common.Address
467470 signer : signer ,
468471 session : session ,
469472 cashed : new (big.Int ).Set (common .Big0 ),
473+ log : log .New ("contract" , contractAddr ),
470474 }
471- log .Trace (fmt . Sprintf ( "initialised inbox (%s -> %s) expected signer: %x " , self . contract . Hex () , self .beneficiary . Hex (), crypto .FromECDSAPub (signer )))
475+ self . log .Trace ("New chequebook inbox initialized " , "beneficiary" , self .beneficiary , "signer" , hexutil . Bytes ( crypto .FromECDSAPub (signer )))
472476 return
473477}
474478
@@ -490,7 +494,7 @@ func (self *Inbox) Stop() {
490494func (self * Inbox ) Cash () (txhash string , err error ) {
491495 if self .cheque != nil {
492496 txhash , err = self .cheque .Cash (self .session )
493- log .Trace (fmt . Sprintf ( "cashing cheque (total: %v) on chequebook (%s) sending to %v" , self .cheque .Amount , self . contract . Hex () , self .beneficiary . Hex ()) )
497+ self . log .Trace ("Cashing in chequebook cheque" , "amount" , self .cheque .Amount , "beneficiary" , self .beneficiary )
494498 self .cashed = self .cheque .Amount
495499 }
496500 return
@@ -574,15 +578,15 @@ func (self *Inbox) Receive(promise swap.Promise) (*big.Int, error) {
574578 self .Cash ()
575579 }
576580 }
577- log .Trace (fmt . Sprintf ( "received cheque of %v wei in inbox (%s, uncashed: %v)" , amount , self . contract . Hex () , uncashed ) )
581+ self . log .Trace ("Received cheque in chequebook inbox" , "amount" , amount , "uncashed" , uncashed )
578582 }
579583
580584 return amount , err
581585}
582586
583587// Verify verifies cheque for signer, contract, beneficiary, amount, valid signature.
584588func (self * Cheque ) Verify (signerKey * ecdsa.PublicKey , contract , beneficiary common.Address , sum * big.Int ) (* big.Int , error ) {
585- log .Trace (fmt . Sprintf ( "verify cheque: %v - sum: %v" , self , sum ) )
589+ log .Trace ("Verifying chequebook cheque" , "cheque" , self , " sum" , sum )
586590 if sum == nil {
587591 return nil , fmt .Errorf ("invalid amount" )
588592 }
0 commit comments