github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/rpc/types.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  //版权所有2015 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package rpc
    26  
    27  import (
    28  	"fmt"
    29  	"math"
    30  	"reflect"
    31  	"strings"
    32  	"sync"
    33  
    34  	mapset "github.com/deckarep/golang-set"
    35  	"github.com/ethereum/go-ethereum/common/hexutil"
    36  )
    37  
    38  //API描述了通过RPC接口提供的一组方法
    39  type API struct {
    40  Namespace string      //暴露服务的RPC方法的命名空间
    41  Version   string      //DAPP的API版本
    42  Service   interface{} //保存方法的接收器实例
    43  Public    bool        //是否必须将这些方法视为公共使用安全的指示
    44  }
    45  
    46  //回调是在服务器中注册的方法回调
    47  type callback struct {
    48  rcvr        reflect.Value  //方法接受者
    49  method      reflect.Method //回调
    50  argTypes    []reflect.Type //输入参数类型
    51  hasCtx      bool           //方法的第一个参数是上下文(不包括在argtype中)
    52  errPos      int            //当方法无法返回错误时,错误返回IDX,共-1个
    53  isSubscribe bool           //指示回调是否为订阅
    54  }
    55  
    56  //服务表示已注册的对象
    57  type service struct {
    58  name          string        //服务的名称
    59  typ           reflect.Type  //接收机类型
    60  callbacks     callbacks     //已注册的处理程序
    61  subscriptions subscriptions //可用订阅/通知
    62  }
    63  
    64  //ServerRequest是一个传入请求
    65  type serverRequest struct {
    66  	id            interface{}
    67  	svcname       string
    68  	callb         *callback
    69  	args          []reflect.Value
    70  	isUnsubscribe bool
    71  	err           Error
    72  }
    73  
    74  type serviceRegistry map[string]*service //服务收集
    75  type callbacks map[string]*callback      //RPC回调集合
    76  type subscriptions map[string]*callback  //认购回拨集合
    77  
    78  //服务器表示RPC服务器
    79  type Server struct {
    80  	services serviceRegistry
    81  
    82  	run      int32
    83  	codecsMu sync.Mutex
    84  	codecs   mapset.Set
    85  }
    86  
    87  //rpc request表示原始传入的rpc请求
    88  type rpcRequest struct {
    89  	service  string
    90  	method   string
    91  	id       interface{}
    92  	isPubSub bool
    93  	params   interface{}
    94  err      Error //批元素无效
    95  }
    96  
    97  //错误包装了RPC错误,其中除消息外还包含错误代码。
    98  type Error interface {
    99  Error() string  //返回消息
   100  ErrorCode() int //返回代码
   101  }
   102  
   103  //ServerCodec实现对服务器端的RPC消息的读取、分析和写入
   104  //一个RPC会话。由于可以调用编解码器,因此实现必须是安全的执行例程。
   105  //同时执行多个go例程。
   106  type ServerCodec interface {
   107  //阅读下一个请求
   108  	ReadRequestHeaders() ([]rpcRequest, bool, Error)
   109  //将请求参数解析为给定类型
   110  	ParseRequestArguments(argTypes []reflect.Type, params interface{}) ([]reflect.Value, Error)
   111  //组装成功响应,期望响应ID和有效负载
   112  	CreateResponse(id interface{}, reply interface{}) interface{}
   113  //组装错误响应,需要响应ID和错误
   114  	CreateErrorResponse(id interface{}, err Error) interface{}
   115  //使用有关错误的额外信息通过信息组装错误响应
   116  	CreateErrorResponseWithInfo(id interface{}, err Error, info interface{}) interface{}
   117  //创建通知响应
   118  	CreateNotification(id, namespace string, event interface{}) interface{}
   119  //将消息写入客户端。
   120  	Write(msg interface{}) error
   121  //关闭基础数据流
   122  	Close()
   123  //当基础连接关闭时关闭
   124  	Closed() <-chan interface{}
   125  }
   126  
   127  type BlockNumber int64
   128  
   129  const (
   130  	PendingBlockNumber  = BlockNumber(-2)
   131  	LatestBlockNumber   = BlockNumber(-1)
   132  	EarliestBlockNumber = BlockNumber(0)
   133  )
   134  
   135  //unmashaljson将给定的json片段解析为一个blocknumber。它支持:
   136  //-“最新”、“最早”或“挂起”作为字符串参数
   137  //-区块编号
   138  //返回的错误:
   139  //-当给定参数不是已知字符串时出现无效的块号错误
   140  //-当给定的块号太小或太大时出现超出范围的错误。
   141  func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
   142  	input := strings.TrimSpace(string(data))
   143  	if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
   144  		input = input[1 : len(input)-1]
   145  	}
   146  
   147  	switch input {
   148  	case "earliest":
   149  		*bn = EarliestBlockNumber
   150  		return nil
   151  	case "latest":
   152  		*bn = LatestBlockNumber
   153  		return nil
   154  	case "pending":
   155  		*bn = PendingBlockNumber
   156  		return nil
   157  	}
   158  
   159  	blckNum, err := hexutil.DecodeUint64(input)
   160  	if err != nil {
   161  		return err
   162  	}
   163  	if blckNum > math.MaxInt64 {
   164  		return fmt.Errorf("Blocknumber too high")
   165  	}
   166  
   167  	*bn = BlockNumber(blckNum)
   168  	return nil
   169  }
   170  
   171  func (bn BlockNumber) Int64() int64 {
   172  	return (int64)(bn)
   173  }