github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/mock/rpc/rpc.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  //
    26  //
    27  //
    28  //
    29  //
    30  //
    31  //
    32  package rpc
    33  
    34  import (
    35  	"fmt"
    36  
    37  	"github.com/ethereum/go-ethereum/common"
    38  	"github.com/ethereum/go-ethereum/rpc"
    39  	"github.com/ethereum/go-ethereum/swarm/log"
    40  	"github.com/ethereum/go-ethereum/swarm/storage/mock"
    41  )
    42  
    43  //
    44  //
    45  type GlobalStore struct {
    46  	client *rpc.Client
    47  }
    48  
    49  //
    50  func NewGlobalStore(client *rpc.Client) *GlobalStore {
    51  	return &GlobalStore{
    52  		client: client,
    53  	}
    54  }
    55  
    56  //
    57  func (s *GlobalStore) Close() error {
    58  	s.client.Close()
    59  	return nil
    60  }
    61  
    62  //
    63  //
    64  func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore {
    65  	return mock.NewNodeStore(addr, s)
    66  }
    67  
    68  //
    69  func (s *GlobalStore) Get(addr common.Address, key []byte) (data []byte, err error) {
    70  	err = s.client.Call(&data, "mockStore_get", addr, key)
    71  	if err != nil && err.Error() == "not found" {
    72  //
    73  		return data, mock.ErrNotFound
    74  	}
    75  	return data, err
    76  }
    77  
    78  //
    79  func (s *GlobalStore) Put(addr common.Address, key []byte, data []byte) error {
    80  	err := s.client.Call(nil, "mockStore_put", addr, key, data)
    81  	return err
    82  }
    83  
    84  //
    85  func (s *GlobalStore) HasKey(addr common.Address, key []byte) bool {
    86  	var has bool
    87  	if err := s.client.Call(&has, "mockStore_hasKey", addr, key); err != nil {
    88  		log.Error(fmt.Sprintf("mock store HasKey: addr %s, key %064x: %v", addr, key, err))
    89  		return false
    90  	}
    91  	return has
    92  }