github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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/intfoundation/intchain/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 // A DataError contains some data in addition to the error message. 43 type DataError interface { 44 Error() string // returns the message 45 ErrorData() interface{} // returns the error data 46 } 47 48 // ServerCodec implements reading, parsing and writing RPC messages for the server side of 49 // a RPC session. Implementations must be go-routine safe since the codec can be called in 50 // multiple go-routines concurrently. 51 type ServerCodec interface { 52 Read() (msgs []*jsonrpcMessage, isBatch bool, err error) 53 Close() 54 jsonWriter 55 } 56 57 // jsonWriter can write JSON messages to its underlying connection. 58 // Implementations must be safe for concurrent use. 59 type jsonWriter interface { 60 Write(context.Context, interface{}) error 61 // Closed returns a channel which is closed when the connection is closed. 62 Closed() <-chan interface{} 63 // RemoteAddr returns the peer address of the connection. 64 RemoteAddr() string 65 } 66 67 type BlockNumber int64 68 69 const ( 70 PendingBlockNumber = BlockNumber(-2) 71 LatestBlockNumber = BlockNumber(-1) 72 EarliestBlockNumber = BlockNumber(0) 73 ) 74 75 // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: 76 // - "latest", "earliest" or "pending" as string arguments 77 // - the block number 78 // Returned errors: 79 // - an invalid block number error when the given argument isn't a known strings 80 // - an out of range error when the given block number is either too little or too large 81 func (bn *BlockNumber) UnmarshalJSON(data []byte) error { 82 input := strings.TrimSpace(string(data)) 83 if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' { 84 input = input[1 : len(input)-1] 85 } 86 87 switch input { 88 case "earliest": 89 *bn = EarliestBlockNumber 90 return nil 91 case "latest": 92 *bn = LatestBlockNumber 93 return nil 94 case "pending": 95 *bn = PendingBlockNumber 96 return nil 97 } 98 99 blckNum, err := hexutil.DecodeUint64(input) 100 if err != nil { 101 return err 102 } 103 if blckNum > math.MaxInt64 { 104 return fmt.Errorf("Blocknumber too high") 105 } 106 107 *bn = BlockNumber(blckNum) 108 return nil 109 } 110 111 func (bn BlockNumber) Int64() int64 { 112 return (int64)(bn) 113 }