github.com/zecrey-Labs/bor@v0.1.4/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  	"fmt"
    22  	"math"
    23  	"strings"
    24  
    25  	"github.com/maticnetwork/bor/common/hexutil"
    26  )
    27  
    28  // API describes the set of methods offered over the RPC interface
    29  type API struct {
    30  	Namespace string      // namespace under which the rpc methods of Service are exposed
    31  	Version   string      // api version for DApp's
    32  	Service   interface{} // receiver instance which holds the methods
    33  	Public    bool        // indication if the methods must be considered safe for public use
    34  }
    35  
    36  // Error wraps RPC errors, which contain an error code in addition to the message.
    37  type Error interface {
    38  	Error() string  // returns the message
    39  	ErrorCode() int // returns the code
    40  }
    41  
    42  // ServerCodec implements reading, parsing and writing RPC messages for the server side of
    43  // a RPC session. Implementations must be go-routine safe since the codec can be called in
    44  // multiple go-routines concurrently.
    45  type ServerCodec interface {
    46  	Read() (msgs []*jsonrpcMessage, isBatch bool, err error)
    47  	Close()
    48  	jsonWriter
    49  }
    50  
    51  // jsonWriter can write JSON messages to its underlying connection.
    52  // Implementations must be safe for concurrent use.
    53  type jsonWriter interface {
    54  	Write(context.Context, interface{}) error
    55  	// Closed returns a channel which is closed when the connection is closed.
    56  	Closed() <-chan interface{}
    57  	// RemoteAddr returns the peer address of the connection.
    58  	RemoteAddr() string
    59  }
    60  
    61  type BlockNumber int64
    62  
    63  const (
    64  	PendingBlockNumber  = BlockNumber(-2)
    65  	LatestBlockNumber   = BlockNumber(-1)
    66  	EarliestBlockNumber = BlockNumber(0)
    67  )
    68  
    69  // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
    70  // - "latest", "earliest" or "pending" as string arguments
    71  // - the block number
    72  // Returned errors:
    73  // - an invalid block number error when the given argument isn't a known strings
    74  // - an out of range error when the given block number is either too little or too large
    75  func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
    76  	input := strings.TrimSpace(string(data))
    77  	if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
    78  		input = input[1 : len(input)-1]
    79  	}
    80  
    81  	switch input {
    82  	case "earliest":
    83  		*bn = EarliestBlockNumber
    84  		return nil
    85  	case "latest":
    86  		*bn = LatestBlockNumber
    87  		return nil
    88  	case "pending":
    89  		*bn = PendingBlockNumber
    90  		return nil
    91  	}
    92  
    93  	blckNum, err := hexutil.DecodeUint64(input)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	if blckNum > math.MaxInt64 {
    98  		return fmt.Errorf("Blocknumber too high")
    99  	}
   100  
   101  	*bn = BlockNumber(blckNum)
   102  	return nil
   103  }
   104  
   105  func (bn BlockNumber) Int64() int64 {
   106  	return (int64)(bn)
   107  }