| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // Copyright 2016 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 ( | 
					
						
							| 
									
										
										
										
											2017-03-22 18:20:33 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"crypto/ecdsa" | 
					
						
							| 
									
										
										
										
											2018-06-12 15:26:08 +02:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"math/big" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/accounts/abi/bind" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/contracts/chequebook" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/contracts/chequebook/contract" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/swarm/log" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/swarm/services/swap/swap" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SwAP       Swarm Accounting Protocol with | 
					
						
							|  |  |  | // SWAP^2     Strategies of Withholding Automatic Payments | 
					
						
							|  |  |  | // SWAP^3     Accreditation: payment via credit SWAP | 
					
						
							|  |  |  | // using chequebook pkg for delayed payments | 
					
						
							|  |  |  | // default parameters | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	autoCashInterval     = 300 * time.Second           // default interval for autocash | 
					
						
							|  |  |  | 	autoCashThreshold    = big.NewInt(50000000000000)  // threshold that triggers autocash (wei) | 
					
						
							|  |  |  | 	autoDepositInterval  = 300 * time.Second           // default interval for autocash | 
					
						
							|  |  |  | 	autoDepositThreshold = big.NewInt(50000000000000)  // threshold that triggers autodeposit (wei) | 
					
						
							|  |  |  | 	autoDepositBuffer    = big.NewInt(100000000000000) // buffer that is surplus for fork protection etc (wei) | 
					
						
							|  |  |  | 	buyAt                = big.NewInt(20000000000)     // maximum chunk price host is willing to pay (wei) | 
					
						
							|  |  |  | 	sellAt               = big.NewInt(20000000000)     // minimum chunk price host requires (wei) | 
					
						
							|  |  |  | 	payAt                = 100                         // threshold that triggers payment {request} (units) | 
					
						
							|  |  |  | 	dropAt               = 10000                       // threshold that triggers disconnect (units) | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	chequebookDeployRetries = 5 | 
					
						
							|  |  |  | 	chequebookDeployDelay   = 1 * time.Second // delay between retries | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // LocalProfile combines a PayProfile with *swap.Params | 
					
						
							|  |  |  | type LocalProfile struct { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	*swap.Params | 
					
						
							|  |  |  | 	*PayProfile | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // RemoteProfile combines a PayProfile with *swap.Profile | 
					
						
							|  |  |  | type RemoteProfile struct { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	*swap.Profile | 
					
						
							|  |  |  | 	*PayProfile | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // PayProfile is a container for relevant chequebook and beneficiary options | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | type PayProfile struct { | 
					
						
							|  |  |  | 	PublicKey   string         // check against signature of promise | 
					
						
							|  |  |  | 	Contract    common.Address // address of chequebook contract | 
					
						
							|  |  |  | 	Beneficiary common.Address // recipient address for swarm sales revenue | 
					
						
							|  |  |  | 	privateKey  *ecdsa.PrivateKey | 
					
						
							|  |  |  | 	publicKey   *ecdsa.PublicKey | 
					
						
							|  |  |  | 	owner       common.Address | 
					
						
							|  |  |  | 	chbook      *chequebook.Chequebook | 
					
						
							|  |  |  | 	lock        sync.RWMutex | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // NewDefaultSwapParams create params with default values | 
					
						
							|  |  |  | func NewDefaultSwapParams() *LocalProfile { | 
					
						
							|  |  |  | 	return &LocalProfile{ | 
					
						
							| 
									
										
										
										
											2017-12-11 16:56:06 -05:00
										 |  |  | 		PayProfile: &PayProfile{}, | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		Params: &swap.Params{ | 
					
						
							|  |  |  | 			Profile: &swap.Profile{ | 
					
						
							|  |  |  | 				BuyAt:  buyAt, | 
					
						
							|  |  |  | 				SellAt: sellAt, | 
					
						
							|  |  |  | 				PayAt:  uint(payAt), | 
					
						
							|  |  |  | 				DropAt: uint(dropAt), | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Strategy: &swap.Strategy{ | 
					
						
							|  |  |  | 				AutoCashInterval:     autoCashInterval, | 
					
						
							|  |  |  | 				AutoCashThreshold:    autoCashThreshold, | 
					
						
							|  |  |  | 				AutoDepositInterval:  autoDepositInterval, | 
					
						
							|  |  |  | 				AutoDepositThreshold: autoDepositThreshold, | 
					
						
							|  |  |  | 				AutoDepositBuffer:    autoDepositBuffer, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // Init this can only finally be set after all config options (file, cmd line, env vars) | 
					
						
							|  |  |  | // have been evaluated | 
					
						
							|  |  |  | func (lp *LocalProfile) Init(contract common.Address, prvkey *ecdsa.PrivateKey) { | 
					
						
							| 
									
										
										
										
											2017-12-11 16:56:06 -05:00
										 |  |  | 	pubkey := &prvkey.PublicKey | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	lp.PayProfile = &PayProfile{ | 
					
						
							| 
									
										
										
										
											2017-12-11 16:56:06 -05:00
										 |  |  | 		PublicKey:   common.ToHex(crypto.FromECDSAPub(pubkey)), | 
					
						
							|  |  |  | 		Contract:    contract, | 
					
						
							|  |  |  | 		Beneficiary: crypto.PubkeyToAddress(*pubkey), | 
					
						
							|  |  |  | 		privateKey:  prvkey, | 
					
						
							|  |  |  | 		publicKey:   pubkey, | 
					
						
							|  |  |  | 		owner:       crypto.PubkeyToAddress(*pubkey), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // NewSwap constructor, parameters | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // * global chequebook, assume deployed service and | 
					
						
							|  |  |  | // * the balance is at buffer. | 
					
						
							|  |  |  | // swap.Add(n) called in netstore | 
					
						
							|  |  |  | // n > 0 called when sending chunks = receiving retrieve requests | 
					
						
							|  |  |  | //                 OR sending cheques. | 
					
						
							|  |  |  | // n < 0  called when receiving chunks = receiving delivery responses | 
					
						
							|  |  |  | //                 OR receiving cheques. | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func NewSwap(localProfile *LocalProfile, remoteProfile *RemoteProfile, backend chequebook.Backend, proto swap.Protocol) (swapInstance *swap.Swap, err error) { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	var ( | 
					
						
							|  |  |  | 		ctx = context.TODO() | 
					
						
							|  |  |  | 		ok  bool | 
					
						
							|  |  |  | 		in  *chequebook.Inbox | 
					
						
							|  |  |  | 		out *chequebook.Outbox | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	remotekey, err := crypto.UnmarshalPubkey(common.FromHex(remoteProfile.PublicKey)) | 
					
						
							| 
									
										
										
										
											2018-06-12 15:26:08 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, errors.New("invalid remote public key") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	// check if remoteProfile chequebook is valid | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	// insolvent chequebooks suicide so will signal as invalid | 
					
						
							|  |  |  | 	// TODO: monitoring a chequebooks events | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	ok, err = chequebook.ValidateCode(ctx, backend, remoteProfile.Contract) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		log.Info(fmt.Sprintf("invalid contract %v for peer %v: %v)", remoteProfile.Contract.Hex()[:8], proto, err)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		// remoteProfile contract valid, create inbox | 
					
						
							|  |  |  | 		in, err = chequebook.NewInbox(localProfile.privateKey, remoteProfile.Contract, localProfile.Beneficiary, remotekey, backend) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			log.Warn(fmt.Sprintf("unable to set up inbox for chequebook contract %v for peer %v: %v)", remoteProfile.Contract.Hex()[:8], proto, err)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	// check if localProfile chequebook contract is valid | 
					
						
							|  |  |  | 	ok, err = chequebook.ValidateCode(ctx, backend, localProfile.Contract) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		log.Warn(fmt.Sprintf("unable to set up outbox for peer %v:  chequebook contract (owner: %v): %v)", proto, localProfile.owner.Hex(), err)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		out = chequebook.NewOutbox(localProfile.Chequebook(), remoteProfile.Beneficiary) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pm := swap.Payment{ | 
					
						
							|  |  |  | 		In:    in, | 
					
						
							|  |  |  | 		Out:   out, | 
					
						
							|  |  |  | 		Buys:  out != nil, | 
					
						
							|  |  |  | 		Sells: in != nil, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	swapInstance, err = swap.New(localProfile.Params, pm, proto) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	// remoteProfile profile given (first) in handshake | 
					
						
							|  |  |  | 	swapInstance.SetRemote(remoteProfile.Profile) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	var buy, sell string | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	if swapInstance.Buys { | 
					
						
							|  |  |  | 		buy = "purchase from peer enabled at " + remoteProfile.SellAt.String() + " wei/chunk" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		buy = "purchase from peer disabled" | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	if swapInstance.Sells { | 
					
						
							|  |  |  | 		sell = "selling to peer enabled at " + localProfile.SellAt.String() + " wei/chunk" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		sell = "selling to peer disabled" | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	log.Warn(fmt.Sprintf("SWAP arrangement with <%v>: %v; %v)", proto, buy, sell)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // Chequebook get's chequebook from the localProfile | 
					
						
							|  |  |  | func (lp *LocalProfile) Chequebook() *chequebook.Chequebook { | 
					
						
							|  |  |  | 	defer lp.lock.Unlock() | 
					
						
							|  |  |  | 	lp.lock.Lock() | 
					
						
							|  |  |  | 	return lp.chbook | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // PrivateKey accessor | 
					
						
							|  |  |  | func (lp *LocalProfile) PrivateKey() *ecdsa.PrivateKey { | 
					
						
							|  |  |  | 	return lp.privateKey | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // func (self *LocalProfile) PublicKey() *ecdsa.PublicKey { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // 	return self.publicKey | 
					
						
							|  |  |  | // } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // SetKey set's private and public key on localProfile | 
					
						
							|  |  |  | func (lp *LocalProfile) SetKey(prvkey *ecdsa.PrivateKey) { | 
					
						
							|  |  |  | 	lp.privateKey = prvkey | 
					
						
							|  |  |  | 	lp.publicKey = &prvkey.PublicKey | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // SetChequebook wraps the chequebook initialiser and sets up autoDeposit to cover spending. | 
					
						
							|  |  |  | func (lp *LocalProfile) SetChequebook(ctx context.Context, backend chequebook.Backend, path string) error { | 
					
						
							|  |  |  | 	lp.lock.Lock() | 
					
						
							|  |  |  | 	swapContract := lp.Contract | 
					
						
							|  |  |  | 	lp.lock.Unlock() | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	valid, err := chequebook.ValidateCode(ctx, backend, swapContract) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if valid { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		return lp.newChequebookFromContract(path, backend) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	return lp.deployChequebook(ctx, backend, path) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // deployChequebook deploys the localProfile Chequebook | 
					
						
							|  |  |  | func (lp *LocalProfile) deployChequebook(ctx context.Context, backend chequebook.Backend, path string) error { | 
					
						
							|  |  |  | 	opts := bind.NewKeyedTransactor(lp.privateKey) | 
					
						
							|  |  |  | 	opts.Value = lp.AutoDepositBuffer | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	opts.Context = ctx | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	log.Info(fmt.Sprintf("Deploying new chequebook (owner: %v)", opts.From.Hex())) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	address, err := deployChequebookLoop(opts, backend) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		log.Error(fmt.Sprintf("unable to deploy new chequebook: %v", err)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	log.Info(fmt.Sprintf("new chequebook deployed at %v (owner: %v)", address.Hex(), opts.From.Hex())) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// need to save config at this point | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	lp.lock.Lock() | 
					
						
							|  |  |  | 	lp.Contract = address | 
					
						
							|  |  |  | 	err = lp.newChequebookFromContract(path, backend) | 
					
						
							|  |  |  | 	lp.lock.Unlock() | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		log.Warn(fmt.Sprintf("error initialising cheque book (owner: %v): %v", opts.From.Hex(), err)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // deployChequebookLoop repeatedly tries to deploy a chequebook. | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | func deployChequebookLoop(opts *bind.TransactOpts, backend chequebook.Backend) (addr common.Address, err error) { | 
					
						
							|  |  |  | 	var tx *types.Transaction | 
					
						
							|  |  |  | 	for try := 0; try < chequebookDeployRetries; try++ { | 
					
						
							|  |  |  | 		if try > 0 { | 
					
						
							|  |  |  | 			time.Sleep(chequebookDeployDelay) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if _, tx, _, err = contract.DeployChequebook(opts, backend); err != nil { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 			log.Warn(fmt.Sprintf("can't send chequebook deploy tx (try %d): %v", try, err)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if addr, err = bind.WaitDeployed(opts.Context, backend, tx); err != nil { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 			log.Warn(fmt.Sprintf("chequebook deploy error (try %d): %v", try, err)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return addr, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return addr, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // newChequebookFromContract - initialise the chequebook from a persisted json file or create a new one | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // caller holds the lock | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func (lp *LocalProfile) newChequebookFromContract(path string, backend chequebook.Backend) error { | 
					
						
							|  |  |  | 	hexkey := common.Bytes2Hex(lp.Contract.Bytes()) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	err := os.MkdirAll(filepath.Join(path, "chequebooks"), os.ModePerm) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("unable to create directory for chequebooks: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	chbookpath := filepath.Join(path, "chequebooks", hexkey+".json") | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	lp.chbook, err = chequebook.LoadChequebook(chbookpath, lp.privateKey, backend, true) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		lp.chbook, err = chequebook.NewChequebook(chbookpath, lp.Contract, lp.privateKey, backend) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			log.Warn(fmt.Sprintf("unable to initialise chequebook (owner: %v): %v", lp.owner.Hex(), err)) | 
					
						
							|  |  |  | 			return fmt.Errorf("unable to initialise chequebook (owner: %v): %v", lp.owner.Hex(), err) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	lp.chbook.AutoDeposit(lp.AutoDepositInterval, lp.AutoDepositThreshold, lp.AutoDepositBuffer) | 
					
						
							|  |  |  | 	log.Info(fmt.Sprintf("auto deposit ON for %v -> %v: interval = %v, threshold = %v, buffer = %v)", crypto.PubkeyToAddress(*(lp.publicKey)).Hex()[:8], lp.Contract.Hex()[:8], lp.AutoDepositInterval, lp.AutoDepositThreshold, lp.AutoDepositBuffer)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |