git.gammaspectra.live/P2Pool/consensus@v0.0.0-20240403173234-a039820b20c9/p2pool/sidechain/fake_server.go (about)

     1  package sidechain
     2  
     3  import (
     4  	"context"
     5  	mainblock "git.gammaspectra.live/P2Pool/consensus/monero/block"
     6  	"git.gammaspectra.live/P2Pool/consensus/monero/client"
     7  	p2pooltypes "git.gammaspectra.live/P2Pool/consensus/p2pool/types"
     8  	"git.gammaspectra.live/P2Pool/consensus/types"
     9  	"sync"
    10  )
    11  
    12  type FakeServer struct {
    13  	consensus   *Consensus
    14  	headersLock sync.Mutex
    15  	headers     map[uint64]*mainblock.Header
    16  }
    17  
    18  func (s *FakeServer) Context() context.Context {
    19  	return context.Background()
    20  }
    21  
    22  func (s *FakeServer) Consensus() *Consensus {
    23  	return s.consensus
    24  }
    25  
    26  func (s *FakeServer) GetBlob(key []byte) (blob []byte, err error) {
    27  	return nil, nil
    28  }
    29  
    30  func (s *FakeServer) SetBlob(key, blob []byte) (err error) {
    31  	return nil
    32  }
    33  
    34  func (s *FakeServer) RemoveBlob(key []byte) (err error) {
    35  	return nil
    36  }
    37  
    38  func (s *FakeServer) UpdateTip(tip *PoolBlock) {
    39  
    40  }
    41  func (s *FakeServer) Broadcast(block *PoolBlock) {
    42  
    43  }
    44  func (s *FakeServer) ClientRPC() *client.Client {
    45  	return client.GetDefaultClient()
    46  }
    47  func (s *FakeServer) GetChainMainByHeight(height uint64) *ChainMain {
    48  	return nil
    49  }
    50  func (s *FakeServer) GetChainMainByHash(hash types.Hash) *ChainMain {
    51  	return nil
    52  }
    53  func (s *FakeServer) GetMinimalBlockHeaderByHeight(height uint64) *mainblock.Header {
    54  	s.headersLock.Lock()
    55  	defer s.headersLock.Unlock()
    56  	if h, ok := s.headers[height]; ok {
    57  		return h
    58  	}
    59  	if h, err := s.ClientRPC().GetBlockHeaderByHeight(height, context.Background()); err != nil {
    60  		return nil
    61  	} else {
    62  		header := &mainblock.Header{
    63  			MajorVersion: uint8(h.BlockHeader.MajorVersion),
    64  			MinorVersion: uint8(h.BlockHeader.MinorVersion),
    65  			Timestamp:    uint64(h.BlockHeader.Timestamp),
    66  			PreviousId:   types.MustHashFromString(h.BlockHeader.PrevHash),
    67  			Height:       h.BlockHeader.Height,
    68  			Nonce:        uint32(h.BlockHeader.Nonce),
    69  			Reward:       h.BlockHeader.Reward,
    70  			Difficulty:   types.DifficultyFrom64(h.BlockHeader.Difficulty),
    71  			Id:           types.MustHashFromString(h.BlockHeader.Hash),
    72  		}
    73  		s.headers[height] = header
    74  		return header
    75  	}
    76  }
    77  func (s *FakeServer) GetMinimalBlockHeaderByHash(hash types.Hash) *mainblock.Header {
    78  	return nil
    79  }
    80  func (s *FakeServer) GetDifficultyByHeight(height uint64) types.Difficulty {
    81  	return s.GetMinimalBlockHeaderByHeight(height).Difficulty
    82  }
    83  func (s *FakeServer) UpdateBlockFound(data *ChainMain, block *PoolBlock) {
    84  
    85  }
    86  func (s *FakeServer) SubmitBlock(block *mainblock.Block) {
    87  
    88  }
    89  func (s *FakeServer) GetChainMainTip() *ChainMain {
    90  	return nil
    91  }
    92  func (s *FakeServer) GetMinerDataTip() *p2pooltypes.MinerData {
    93  	return nil
    94  }
    95  func (s *FakeServer) Store(block *PoolBlock) {
    96  
    97  }
    98  func (s *FakeServer) ClearCachedBlocks() {
    99  
   100  }
   101  
   102  func GetFakeTestServer(consensus *Consensus) *FakeServer {
   103  	return &FakeServer{
   104  		consensus: consensus,
   105  		headers:   make(map[uint64]*mainblock.Header),
   106  	}
   107  }