github.com/phillinzzz/newBsc@v1.1.6/rpc/types.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"math"
    24  	"strings"
    25  
    26  	"github.com/phillinzzz/newBsc/common"
    27  	"github.com/phillinzzz/newBsc/common/hexutil"
    28  )
    29  
    30  // API describes the set of methods offered over the RPC interface
    31  type API struct {
    32  	Namespace string      // namespace under which the rpc methods of Service are exposed
    33  	Version   string      // api version for DApp's
    34  	Service   interface{} // receiver instance which holds the methods
    35  	Public    bool        // indication if the methods must be considered safe for public use
    36  }
    37  
    38  // ServerCodec implements reading, parsing and writing RPC messages for the server side of
    39  // a RPC session. Implementations must be go-routine safe since the codec can be called in
    40  // multiple go-routines concurrently.
    41  type ServerCodec interface {
    42  	readBatch() (msgs []*jsonrpcMessage, isBatch bool, err error)
    43  	close()
    44  	jsonWriter
    45  }
    46  
    47  // jsonWriter can write JSON messages to its underlying connection.
    48  // Implementations must be safe for concurrent use.
    49  type jsonWriter interface {
    50  	writeJSON(context.Context, interface{}) error
    51  	// Closed returns a channel which is closed when the connection is closed.
    52  	closed() <-chan interface{}
    53  	// RemoteAddr returns the peer address of the connection.
    54  	remoteAddr() string
    55  }
    56  
    57  type BlockNumber int64
    58  
    59  const (
    60  	PendingBlockNumber  = BlockNumber(-2)
    61  	LatestBlockNumber   = BlockNumber(-1)
    62  	EarliestBlockNumber = BlockNumber(0)
    63  )
    64  
    65  // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
    66  // - "latest", "earliest" or "pending" as string arguments
    67  // - the block number
    68  // Returned errors:
    69  // - an invalid block number error when the given argument isn't a known strings
    70  // - an out of range error when the given block number is either too little or too large
    71  func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
    72  	input := strings.TrimSpace(string(data))
    73  	if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
    74  		input = input[1 : len(input)-1]
    75  	}
    76  
    77  	switch input {
    78  	case "earliest":
    79  		*bn = EarliestBlockNumber
    80  		return nil
    81  	case "latest":
    82  		*bn = LatestBlockNumber
    83  		return nil
    84  	case "pending":
    85  		*bn = PendingBlockNumber
    86  		return nil
    87  	}
    88  
    89  	blckNum, err := hexutil.DecodeUint64(input)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	if blckNum > math.MaxInt64 {
    94  		return fmt.Errorf("block number larger than int64")
    95  	}
    96  	*bn = BlockNumber(blckNum)
    97  	return nil
    98  }
    99  
   100  func (bn BlockNumber) Int64() int64 {
   101  	return (int64)(bn)
   102  }
   103  
   104  type BlockNumberOrHash struct {
   105  	BlockNumber      *BlockNumber `json:"blockNumber,omitempty"`
   106  	BlockHash        *common.Hash `json:"blockHash,omitempty"`
   107  	RequireCanonical bool         `json:"requireCanonical,omitempty"`
   108  }
   109  
   110  func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
   111  	type erased BlockNumberOrHash
   112  	e := erased{}
   113  	err := json.Unmarshal(data, &e)
   114  	if err == nil {
   115  		if e.BlockNumber != nil && e.BlockHash != nil {
   116  			return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other")
   117  		}
   118  		bnh.BlockNumber = e.BlockNumber
   119  		bnh.BlockHash = e.BlockHash
   120  		bnh.RequireCanonical = e.RequireCanonical
   121  		return nil
   122  	}
   123  	var input string
   124  	err = json.Unmarshal(data, &input)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	switch input {
   129  	case "earliest":
   130  		bn := EarliestBlockNumber
   131  		bnh.BlockNumber = &bn
   132  		return nil
   133  	case "latest":
   134  		bn := LatestBlockNumber
   135  		bnh.BlockNumber = &bn
   136  		return nil
   137  	case "pending":
   138  		bn := PendingBlockNumber
   139  		bnh.BlockNumber = &bn
   140  		return nil
   141  	default:
   142  		if len(input) == 66 {
   143  			hash := common.Hash{}
   144  			err := hash.UnmarshalText([]byte(input))
   145  			if err != nil {
   146  				return err
   147  			}
   148  			bnh.BlockHash = &hash
   149  			return nil
   150  		} else {
   151  			blckNum, err := hexutil.DecodeUint64(input)
   152  			if err != nil {
   153  				return err
   154  			}
   155  			if blckNum > math.MaxInt64 {
   156  				return fmt.Errorf("blocknumber too high")
   157  			}
   158  			bn := BlockNumber(blckNum)
   159  			bnh.BlockNumber = &bn
   160  			return nil
   161  		}
   162  	}
   163  }
   164  
   165  func (bnh *BlockNumberOrHash) Number() (BlockNumber, bool) {
   166  	if bnh.BlockNumber != nil {
   167  		return *bnh.BlockNumber, true
   168  	}
   169  	return BlockNumber(0), false
   170  }
   171  
   172  func (bnh *BlockNumberOrHash) Hash() (common.Hash, bool) {
   173  	if bnh.BlockHash != nil {
   174  		return *bnh.BlockHash, true
   175  	}
   176  	return common.Hash{}, false
   177  }
   178  
   179  func BlockNumberOrHashWithNumber(blockNr BlockNumber) BlockNumberOrHash {
   180  	return BlockNumberOrHash{
   181  		BlockNumber:      &blockNr,
   182  		BlockHash:        nil,
   183  		RequireCanonical: false,
   184  	}
   185  }
   186  
   187  func BlockNumberOrHashWithHash(hash common.Hash, canonical bool) BlockNumberOrHash {
   188  	return BlockNumberOrHash{
   189  		BlockNumber:      nil,
   190  		BlockHash:        &hash,
   191  		RequireCanonical: canonical,
   192  	}
   193  }