Skip to content

Commit 1687d5f

Browse files
Eknirzelig
authored andcommitted
swarm, swap: pass chequebook address at start-up (ethersphere#1718)
swarm, swap: use passed --chequebook param, swap: don't use chequebookAddress on swap.new, create function NewInstanceAt - updated call to swap.New in tests - swapEnabled err checking - updated comment at NewInstanceAt - error handling and renaming - rename InstanceAt to BindToContract - refactor => chequebook setup in swap, remove verifyContract - err returning and err handling
1 parent f8e4dc6 commit 1687d5f

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

swap/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (s *Swap) verifyHandshake(msg interface{}) error {
9797
return ErrEmptyAddressInSignature
9898
}
9999

100-
return s.verifyContract(context.Background(), handshake.ContractAddress)
100+
return contract.ValidateCode(context.Background(), s.backend, handshake.ContractAddress)
101101
}
102102

103103
// run is the actual swap protocol run method

swap/swap.go

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ func NewParams() *Params {
8686
}
8787

8888
// New - swap constructor
89-
func New(stateStore state.Store, prvkey *ecdsa.PrivateKey, contract common.Address, backend contract.Backend) *Swap {
89+
func New(stateStore state.Store, prvkey *ecdsa.PrivateKey, backend contract.Backend) *Swap {
9090
return &Swap{
9191
store: stateStore,
9292
balances: make(map[enode.ID]int64),
9393
cheques: make(map[enode.ID]*Cheque),
9494
peers: make(map[enode.ID]*Peer),
9595
backend: backend,
96-
owner: createOwner(prvkey, contract),
96+
owner: createOwner(prvkey),
9797
params: NewParams(),
9898
paymentThreshold: DefaultPaymentThreshold,
9999
disconnectThreshold: DefaultDisconnectThreshold,
@@ -127,13 +127,12 @@ func keyToID(key string, prefix string) enode.ID {
127127
}
128128

129129
// createOwner assings keys and addresses
130-
func createOwner(prvkey *ecdsa.PrivateKey, contract common.Address) *Owner {
130+
func createOwner(prvkey *ecdsa.PrivateKey) *Owner {
131131
pubkey := &prvkey.PublicKey
132132
return &Owner{
133+
address: crypto.PubkeyToAddress(*pubkey),
133134
privateKey: prvkey,
134135
publicKey: pubkey,
135-
Contract: contract,
136-
address: crypto.PubkeyToAddress(*pubkey),
137136
}
138137
}
139138

@@ -522,9 +521,9 @@ func (s *Swap) GetParams() *swap.Params {
522521
return s.contract.ContractParams()
523522
}
524523

525-
// verifyContract checks if the bytecode found at address matches the expected bytecode
526-
func (s *Swap) verifyContract(ctx context.Context, address common.Address) error {
527-
return contract.ValidateCode(ctx, s.backend, address)
524+
// setChequebookAddr sets the chequebook address
525+
func (s *Swap) setChequebookAddr(chequebookAddr common.Address) {
526+
s.owner.Contract = chequebookAddr
528527
}
529528

530529
// getContractOwner retrieve the owner of the chequebook at address from the blockchain
@@ -537,8 +536,38 @@ func (s *Swap) getContractOwner(ctx context.Context, address common.Address) (co
537536
return contr.Issuer(nil)
538537
}
539538

540-
// Deploy deploys the Swap contract
541-
func (s *Swap) Deploy(ctx context.Context, backend swap.Backend, path string) error {
539+
// StartChequebook deploys a new instance of a chequebook if chequebookAddr is empty, otherwise it wil bind to an existing instance
540+
func (s *Swap) StartChequebook(chequebookAddr common.Address) error {
541+
if chequebookAddr != (common.Address{}) {
542+
if err := s.BindToContractAt(chequebookAddr); err != nil {
543+
return err
544+
}
545+
log.Info("Using the provided chequebook", "chequebookAddr", chequebookAddr)
546+
} else {
547+
if err := s.Deploy(context.Background(), s.backend); err != nil {
548+
return err
549+
}
550+
log.Info("New SWAP contract deployed", "contract info", s.DeploySuccess())
551+
}
552+
return nil
553+
}
554+
555+
// BindToContractAt binds an instance of an already existing chequebook contract at address and sets chequebookAddr
556+
func (s *Swap) BindToContractAt(address common.Address) (err error) {
557+
558+
if err := contract.ValidateCode(context.Background(), s.backend, address); err != nil {
559+
return fmt.Errorf("contract validation for %v failed: %v", address, err)
560+
}
561+
s.contract, err = contract.InstanceAt(address, s.backend)
562+
if err != nil {
563+
return err
564+
}
565+
s.setChequebookAddr(address)
566+
return nil
567+
}
568+
569+
// Deploy deploys the Swap contract and sets the contract address
570+
func (s *Swap) Deploy(ctx context.Context, backend swap.Backend) error {
542571
opts := bind.NewKeyedTransactor(s.owner.privateKey)
543572
// initial topup value
544573
opts.Value = big.NewInt(int64(s.params.InitialDepositAmount))
@@ -550,7 +579,7 @@ func (s *Swap) Deploy(ctx context.Context, backend swap.Backend, path string) er
550579
log.Error("unable to deploy swap", "error", err)
551580
return err
552581
}
553-
s.owner.Contract = address
582+
s.setChequebookAddr(address)
554583
log.Info("swap deployed", "address", address.Hex(), "owner", opts.From.Hex())
555584

556585
return err

swap/swap_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func newBaseTestSwap(t *testing.T, key *ecdsa.PrivateKey) (*Swap, string) {
502502
}
503503
log.Debug("creating simulated backend")
504504

505-
swap := New(stateStore, key, common.Address{}, testBackend)
505+
swap := New(stateStore, key, testBackend)
506506
return swap, dir
507507
}
508508

@@ -644,8 +644,8 @@ func TestVerifyChequeInvalidSignature(t *testing.T) {
644644
}
645645
}
646646

647-
// tests if verifyContract accepts an address with the correct bytecode
648-
func TestVerifyContract(t *testing.T) {
647+
// tests if TestValidateCode accepts an address with the correct bytecode
648+
func TestValidateCode(t *testing.T) {
649649
swap, clean := newTestSwap(t, ownerKey)
650650
defer clean()
651651

@@ -658,13 +658,13 @@ func TestVerifyContract(t *testing.T) {
658658

659659
testBackend.Commit()
660660

661-
if err = swap.verifyContract(context.TODO(), addr); err != nil {
661+
if err = cswap.ValidateCode(context.TODO(), swap.backend, addr); err != nil {
662662
t.Fatalf("Contract verification failed: %v", err)
663663
}
664664
}
665665

666-
// tests if verifyContract rejects an address with different bytecode
667-
func TestVerifyContractWrongContract(t *testing.T) {
666+
// tests if ValidateCode rejects an address with different bytecode
667+
func TestValidateWrongCode(t *testing.T) {
668668
swap, clean := newTestSwap(t, ownerKey)
669669
defer clean()
670670

@@ -679,7 +679,7 @@ func TestVerifyContractWrongContract(t *testing.T) {
679679
testBackend.Commit()
680680

681681
// since the bytecode is different this should throw an error
682-
if err = swap.verifyContract(context.TODO(), addr); err != cswap.ErrNotASwapContract {
682+
if err = cswap.ValidateCode(context.TODO(), swap.backend, addr); err != cswap.ErrNotASwapContract {
683683
t.Fatalf("Contract verification verified wrong contract: %v", err)
684684
}
685685
}

swarm.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
133133
return nil, err
134134
}
135135
// create the accounting objects
136-
self.swap = swap.New(swapStore, self.privateKey, self.config.Contract, self.backend)
136+
self.swap = swap.New(swapStore, self.privateKey, self.backend)
137137
// start anonymous metrics collection
138138
self.accountingMetrics = protocols.SetupAccountingMetrics(10*time.Second, filepath.Join(config.Path, "metrics.db"))
139139
}
@@ -374,11 +374,9 @@ func (s *Swarm) Start(srv *p2p.Server) error {
374374
log.Info("Updated bzz local addr", "oaddr", fmt.Sprintf("%x", newaddr.OAddr), "uaddr", fmt.Sprintf("%s", newaddr.UAddr))
375375

376376
if s.config.SwapEnabled {
377-
err := s.swap.Deploy(context.Background(), s.backend, s.config.Path)
378-
if err != nil {
379-
return fmt.Errorf("Unable to deploy swap contract: %v", err)
377+
if err := s.swap.StartChequebook(s.config.Contract); err != nil {
378+
return err
380379
}
381-
log.Info("SWAP contract deployed", "contract info", s.swap.DeploySuccess())
382380
} else {
383381
log.Info("SWAP disabled: no chequebook set")
384382
}

0 commit comments

Comments
 (0)