github.com/ethersphere/bee/v2@v2.2.0/pkg/postage/mock/service.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mock
     6  
     7  import (
     8  	"context"
     9  	"math/big"
    10  	"sync"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/postage"
    13  )
    14  
    15  type optionFunc func(*mockPostage)
    16  
    17  // Option is an option passed to a mock postage Service.
    18  type Option interface {
    19  	apply(*mockPostage)
    20  }
    21  
    22  func (f optionFunc) apply(r *mockPostage) { f(r) }
    23  
    24  // New creates a new mock postage service.
    25  func New(o ...Option) postage.Service {
    26  	m := &mockPostage{
    27  		issuersMap: make(map[string]*postage.StampIssuer),
    28  	}
    29  	for _, v := range o {
    30  		v.apply(m)
    31  	}
    32  
    33  	return m
    34  }
    35  
    36  // WithAcceptAll sets the mock to return a new BatchIssuer on every
    37  // call to GetStampIssuer.
    38  func WithAcceptAll() Option {
    39  	return optionFunc(func(m *mockPostage) { m.acceptAll = true })
    40  }
    41  
    42  func WithIssuer(s *postage.StampIssuer) Option {
    43  	return optionFunc(func(m *mockPostage) {
    44  		m.issuersMap = map[string]*postage.StampIssuer{string(s.ID()): s}
    45  	})
    46  }
    47  
    48  type mockPostage struct {
    49  	issuersMap map[string]*postage.StampIssuer
    50  	issuerLock sync.Mutex
    51  	acceptAll  bool
    52  }
    53  
    54  func (m *mockPostage) HandleStampExpiry(ctx context.Context, id []byte) error {
    55  	m.issuerLock.Lock()
    56  	defer m.issuerLock.Unlock()
    57  	delete(m.issuersMap, string(id))
    58  	return nil
    59  }
    60  
    61  func (m *mockPostage) Add(s *postage.StampIssuer) error {
    62  	m.issuerLock.Lock()
    63  	defer m.issuerLock.Unlock()
    64  
    65  	m.issuersMap[string(s.ID())] = s
    66  	return nil
    67  }
    68  
    69  func (m *mockPostage) StampIssuers() []*postage.StampIssuer {
    70  	m.issuerLock.Lock()
    71  	defer m.issuerLock.Unlock()
    72  
    73  	issuers := make([]*postage.StampIssuer, 0)
    74  	for _, v := range m.issuersMap {
    75  		issuers = append(issuers, v)
    76  	}
    77  	return issuers
    78  }
    79  
    80  func (m *mockPostage) GetStampIssuer(id []byte) (*postage.StampIssuer, func() error, error) {
    81  	if m.acceptAll {
    82  		return postage.NewStampIssuer("test fallback", "test identity", id, big.NewInt(3), 24, 6, 1000, false), func() error { return nil }, nil
    83  	}
    84  
    85  	m.issuerLock.Lock()
    86  	defer m.issuerLock.Unlock()
    87  
    88  	i, exists := m.issuersMap[string(id)]
    89  	if !exists {
    90  		return nil, nil, postage.ErrNotFound
    91  	}
    92  
    93  	return i, func() error {
    94  		return nil
    95  	}, nil
    96  }
    97  
    98  func (m *mockPostage) IssuerUsable(_ *postage.StampIssuer) bool {
    99  	return true
   100  }
   101  
   102  func (m *mockPostage) HandleCreate(_ *postage.Batch, _ *big.Int) error { return nil }
   103  
   104  func (m *mockPostage) HandleTopUp(_ []byte, _ *big.Int) {}
   105  
   106  func (m *mockPostage) HandleDepthIncrease(_ []byte, _ uint8) {}
   107  
   108  func (m *mockPostage) Close() error {
   109  	return nil
   110  }
   111  
   112  var _ postage.BatchExpiryHandler = (*mockPostage)(nil)