aboutsummaryrefslogtreecommitdiff
path: root/rpc
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-10-01 13:28:55 -0400
committerDeterminant <[email protected]>2019-10-01 13:28:55 -0400
commitbc43122f54ed8de21f74ee6393549c9554d732e8 (patch)
tree549fffb8771dd280b29eec1e8691e67eb4b24895 /rpc
parent841b2b7225a9318718c3c856a9debdf01bc4f061 (diff)
support "accepted" as block number in JSON-RPC
Diffstat (limited to 'rpc')
-rw-r--r--rpc/types.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/rpc/types.go b/rpc/types.go
new file mode 100644
index 0000000..7312397
--- /dev/null
+++ b/rpc/types.go
@@ -0,0 +1,77 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package rpc
+
+import (
+ "fmt"
+ "math"
+ "strings"
+
+ "github.com/ava-labs/go-ethereum/common/hexutil"
+)
+
+type BlockNumber int64
+
+const (
+ AcceptedBlockNumber = BlockNumber(-3)
+ PendingBlockNumber = BlockNumber(-2)
+ LatestBlockNumber = BlockNumber(-1)
+ EarliestBlockNumber = BlockNumber(0)
+)
+
+// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
+// - "latest", "earliest" or "pending" as string arguments
+// - the block number
+// Returned errors:
+// - an invalid block number error when the given argument isn't a known strings
+// - an out of range error when the given block number is either too little or too large
+func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
+ input := strings.TrimSpace(string(data))
+ if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
+ input = input[1 : len(input)-1]
+ }
+
+ switch input {
+ case "earliest":
+ *bn = EarliestBlockNumber
+ return nil
+ case "latest":
+ *bn = LatestBlockNumber
+ return nil
+ case "pending":
+ *bn = PendingBlockNumber
+ return nil
+ case "accepted":
+ *bn = AcceptedBlockNumber
+ return nil
+ }
+
+ blckNum, err := hexutil.DecodeUint64(input)
+ if err != nil {
+ return err
+ }
+ if blckNum > math.MaxInt64 {
+ return fmt.Errorf("Blocknumber too high")
+ }
+
+ *bn = BlockNumber(blckNum)
+ return nil
+}
+
+func (bn BlockNumber) Int64() int64 {
+ return (int64)(bn)
+}