From 2c1a5a2ccbc3f0cdc54968fcd0b360ceb231dc91 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Isabel=20Sch=C3=B6ps=20Thiel=20=40IsabelSchoepd?=
<155141998+IST-Github@users.noreply.github.com>
Date: Thu, 4 Jan 2024 04:26:01 +0100
Subject: [PATCH] Delete common directory
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Isabel Schöps Thiel @IsabelSchoepd <155141998+IST-Github@users.noreply.github.com>
---
common/big.go | 30 --
common/bitutil/bitutil.go | 188 ---------
common/bitutil/bitutil_test.go | 221 ----------
common/bitutil/compress.go | 170 --------
common/bitutil/compress_test.go | 223 -----------
common/bytes.go | 151 -------
common/bytes_test.go | 126 ------
common/compiler/helpers.go | 45 ---
common/compiler/solidity.go | 132 ------
common/debug.go | 52 ---
common/fdlimit/fdlimit_bsd.go | 68 ----
common/fdlimit/fdlimit_darwin.go | 71 ----
common/fdlimit/fdlimit_test.go | 44 --
common/fdlimit/fdlimit_unix.go | 66 ---
common/fdlimit/fdlimit_windows.go | 50 ---
common/format.go | 82 ----
common/hexutil/hexutil.go | 241 -----------
common/hexutil/hexutil_test.go | 215 ----------
common/hexutil/json.go | 421 --------------------
common/hexutil/json_example_test.go | 45 ---
common/hexutil/json_test.go | 434 --------------------
common/lru/basiclru.go | 223 -----------
common/lru/basiclru_test.go | 255 ------------
common/lru/blob_lru.go | 84 ----
common/lru/blob_lru_test.go | 155 --------
common/lru/lru.go | 95 -----
common/math/big.go | 270 -------------
common/math/big_test.go | 324 ---------------
common/math/integer.go | 109 -----
common/math/integer_test.go | 116 ------
common/mclock/alarm.go | 106 -----
common/mclock/alarm_test.go | 116 ------
common/mclock/mclock.go | 127 ------
common/mclock/mclock.s | 1 -
common/mclock/simclock.go | 209 ----------
common/mclock/simclock_test.go | 162 --------
common/path.go | 40 --
common/prque/lazyqueue.go | 195 ---------
common/prque/lazyqueue_test.go | 123 ------
common/prque/prque.go | 77 ----
common/prque/prque_test.go | 133 -------
common/prque/sstack.go | 113 ------
common/prque/sstack_test.go | 100 -----
common/size.go | 56 ---
common/size_test.go | 59 ---
common/test_utils.go | 53 ---
common/types.go | 477 ----------------------
common/types_test.go | 597 ----------------------------
48 files changed, 7450 deletions(-)
delete mode 100644 common/big.go
delete mode 100644 common/bitutil/bitutil.go
delete mode 100644 common/bitutil/bitutil_test.go
delete mode 100644 common/bitutil/compress.go
delete mode 100644 common/bitutil/compress_test.go
delete mode 100644 common/bytes.go
delete mode 100644 common/bytes_test.go
delete mode 100644 common/compiler/helpers.go
delete mode 100644 common/compiler/solidity.go
delete mode 100644 common/debug.go
delete mode 100644 common/fdlimit/fdlimit_bsd.go
delete mode 100644 common/fdlimit/fdlimit_darwin.go
delete mode 100644 common/fdlimit/fdlimit_test.go
delete mode 100644 common/fdlimit/fdlimit_unix.go
delete mode 100644 common/fdlimit/fdlimit_windows.go
delete mode 100644 common/format.go
delete mode 100644 common/hexutil/hexutil.go
delete mode 100644 common/hexutil/hexutil_test.go
delete mode 100644 common/hexutil/json.go
delete mode 100644 common/hexutil/json_example_test.go
delete mode 100644 common/hexutil/json_test.go
delete mode 100644 common/lru/basiclru.go
delete mode 100644 common/lru/basiclru_test.go
delete mode 100644 common/lru/blob_lru.go
delete mode 100644 common/lru/blob_lru_test.go
delete mode 100644 common/lru/lru.go
delete mode 100644 common/math/big.go
delete mode 100644 common/math/big_test.go
delete mode 100644 common/math/integer.go
delete mode 100644 common/math/integer_test.go
delete mode 100644 common/mclock/alarm.go
delete mode 100644 common/mclock/alarm_test.go
delete mode 100644 common/mclock/mclock.go
delete mode 100644 common/mclock/mclock.s
delete mode 100644 common/mclock/simclock.go
delete mode 100644 common/mclock/simclock_test.go
delete mode 100644 common/path.go
delete mode 100644 common/prque/lazyqueue.go
delete mode 100644 common/prque/lazyqueue_test.go
delete mode 100755 common/prque/prque.go
delete mode 100644 common/prque/prque_test.go
delete mode 100755 common/prque/sstack.go
delete mode 100644 common/prque/sstack_test.go
delete mode 100644 common/size.go
delete mode 100644 common/size_test.go
delete mode 100644 common/test_utils.go
delete mode 100644 common/types.go
delete mode 100644 common/types_test.go
diff --git a/common/big.go b/common/big.go
deleted file mode 100644
index 65d4377bf7..0000000000
--- a/common/big.go
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2014 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 .
-
-package common
-
-import "math/big"
-
-// Common big integers often used
-var (
- Big1 = big.NewInt(1)
- Big2 = big.NewInt(2)
- Big3 = big.NewInt(3)
- Big0 = big.NewInt(0)
- Big32 = big.NewInt(32)
- Big256 = big.NewInt(256)
- Big257 = big.NewInt(257)
-)
diff --git a/common/bitutil/bitutil.go b/common/bitutil/bitutil.go
deleted file mode 100644
index cd3e72169f..0000000000
--- a/common/bitutil/bitutil.go
+++ /dev/null
@@ -1,188 +0,0 @@
-// Copyright 2013 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Adapted from: https://golang.org/src/crypto/cipher/xor.go
-
-// Package bitutil implements fast bitwise operations.
-package bitutil
-
-import (
- "runtime"
- "unsafe"
-)
-
-const wordSize = int(unsafe.Sizeof(uintptr(0)))
-const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "amd64" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "s390x"
-
-// XORBytes xors the bytes in a and b. The destination is assumed to have enough
-// space. Returns the number of bytes xor'd.
-func XORBytes(dst, a, b []byte) int {
- if supportsUnaligned {
- return fastXORBytes(dst, a, b)
- }
- return safeXORBytes(dst, a, b)
-}
-
-// fastXORBytes xors in bulk. It only works on architectures that support
-// unaligned read/writes.
-func fastXORBytes(dst, a, b []byte) int {
- n := len(a)
- if len(b) < n {
- n = len(b)
- }
- w := n / wordSize
- if w > 0 {
- dw := *(*[]uintptr)(unsafe.Pointer(&dst))
- aw := *(*[]uintptr)(unsafe.Pointer(&a))
- bw := *(*[]uintptr)(unsafe.Pointer(&b))
- for i := 0; i < w; i++ {
- dw[i] = aw[i] ^ bw[i]
- }
- }
- for i := n - n%wordSize; i < n; i++ {
- dst[i] = a[i] ^ b[i]
- }
- return n
-}
-
-// safeXORBytes xors one by one. It works on all architectures, independent if
-// it supports unaligned read/writes or not.
-func safeXORBytes(dst, a, b []byte) int {
- n := len(a)
- if len(b) < n {
- n = len(b)
- }
- for i := 0; i < n; i++ {
- dst[i] = a[i] ^ b[i]
- }
- return n
-}
-
-// ANDBytes ands the bytes in a and b. The destination is assumed to have enough
-// space. Returns the number of bytes and'd.
-func ANDBytes(dst, a, b []byte) int {
- if supportsUnaligned {
- return fastANDBytes(dst, a, b)
- }
- return safeANDBytes(dst, a, b)
-}
-
-// fastANDBytes ands in bulk. It only works on architectures that support
-// unaligned read/writes.
-func fastANDBytes(dst, a, b []byte) int {
- n := len(a)
- if len(b) < n {
- n = len(b)
- }
- w := n / wordSize
- if w > 0 {
- dw := *(*[]uintptr)(unsafe.Pointer(&dst))
- aw := *(*[]uintptr)(unsafe.Pointer(&a))
- bw := *(*[]uintptr)(unsafe.Pointer(&b))
- for i := 0; i < w; i++ {
- dw[i] = aw[i] & bw[i]
- }
- }
- for i := n - n%wordSize; i < n; i++ {
- dst[i] = a[i] & b[i]
- }
- return n
-}
-
-// safeANDBytes ands one by one. It works on all architectures, independent if
-// it supports unaligned read/writes or not.
-func safeANDBytes(dst, a, b []byte) int {
- n := len(a)
- if len(b) < n {
- n = len(b)
- }
- for i := 0; i < n; i++ {
- dst[i] = a[i] & b[i]
- }
- return n
-}
-
-// ORBytes ors the bytes in a and b. The destination is assumed to have enough
-// space. Returns the number of bytes or'd.
-func ORBytes(dst, a, b []byte) int {
- if supportsUnaligned {
- return fastORBytes(dst, a, b)
- }
- return safeORBytes(dst, a, b)
-}
-
-// fastORBytes ors in bulk. It only works on architectures that support
-// unaligned read/writes.
-func fastORBytes(dst, a, b []byte) int {
- n := len(a)
- if len(b) < n {
- n = len(b)
- }
- w := n / wordSize
- if w > 0 {
- dw := *(*[]uintptr)(unsafe.Pointer(&dst))
- aw := *(*[]uintptr)(unsafe.Pointer(&a))
- bw := *(*[]uintptr)(unsafe.Pointer(&b))
- for i := 0; i < w; i++ {
- dw[i] = aw[i] | bw[i]
- }
- }
- for i := n - n%wordSize; i < n; i++ {
- dst[i] = a[i] | b[i]
- }
- return n
-}
-
-// safeORBytes ors one by one. It works on all architectures, independent if
-// it supports unaligned read/writes or not.
-func safeORBytes(dst, a, b []byte) int {
- n := len(a)
- if len(b) < n {
- n = len(b)
- }
- for i := 0; i < n; i++ {
- dst[i] = a[i] | b[i]
- }
- return n
-}
-
-// TestBytes tests whether any bit is set in the input byte slice.
-func TestBytes(p []byte) bool {
- if supportsUnaligned {
- return fastTestBytes(p)
- }
- return safeTestBytes(p)
-}
-
-// fastTestBytes tests for set bits in bulk. It only works on architectures that
-// support unaligned read/writes.
-func fastTestBytes(p []byte) bool {
- n := len(p)
- w := n / wordSize
- if w > 0 {
- pw := *(*[]uintptr)(unsafe.Pointer(&p))
- for i := 0; i < w; i++ {
- if pw[i] != 0 {
- return true
- }
- }
- }
- for i := n - n%wordSize; i < n; i++ {
- if p[i] != 0 {
- return true
- }
- }
- return false
-}
-
-// safeTestBytes tests for set bits one byte at a time. It works on all
-// architectures, independent if it supports unaligned read/writes or not.
-func safeTestBytes(p []byte) bool {
- for i := 0; i < len(p); i++ {
- if p[i] != 0 {
- return true
- }
- }
- return false
-}
diff --git a/common/bitutil/bitutil_test.go b/common/bitutil/bitutil_test.go
deleted file mode 100644
index 307bf731f7..0000000000
--- a/common/bitutil/bitutil_test.go
+++ /dev/null
@@ -1,221 +0,0 @@
-// Copyright 2013 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Adapted from: https://golang.org/src/crypto/cipher/xor_test.go
-
-package bitutil
-
-import (
- "bytes"
- "testing"
-)
-
-// Tests that bitwise XOR works for various alignments.
-func TestXOR(t *testing.T) {
- for alignP := 0; alignP < 2; alignP++ {
- for alignQ := 0; alignQ < 2; alignQ++ {
- for alignD := 0; alignD < 2; alignD++ {
- p := make([]byte, 1023)[alignP:]
- q := make([]byte, 1023)[alignQ:]
-
- for i := 0; i < len(p); i++ {
- p[i] = byte(i)
- }
- for i := 0; i < len(q); i++ {
- q[i] = byte(len(q) - i)
- }
- d1 := make([]byte, 1023+alignD)[alignD:]
- d2 := make([]byte, 1023+alignD)[alignD:]
-
- XORBytes(d1, p, q)
- safeXORBytes(d2, p, q)
- if !bytes.Equal(d1, d2) {
- t.Error("not equal", d1, d2)
- }
- }
- }
- }
-}
-
-// Tests that bitwise AND works for various alignments.
-func TestAND(t *testing.T) {
- for alignP := 0; alignP < 2; alignP++ {
- for alignQ := 0; alignQ < 2; alignQ++ {
- for alignD := 0; alignD < 2; alignD++ {
- p := make([]byte, 1023)[alignP:]
- q := make([]byte, 1023)[alignQ:]
-
- for i := 0; i < len(p); i++ {
- p[i] = byte(i)
- }
- for i := 0; i < len(q); i++ {
- q[i] = byte(len(q) - i)
- }
- d1 := make([]byte, 1023+alignD)[alignD:]
- d2 := make([]byte, 1023+alignD)[alignD:]
-
- ANDBytes(d1, p, q)
- safeANDBytes(d2, p, q)
- if !bytes.Equal(d1, d2) {
- t.Error("not equal")
- }
- }
- }
- }
-}
-
-// Tests that bitwise OR works for various alignments.
-func TestOR(t *testing.T) {
- for alignP := 0; alignP < 2; alignP++ {
- for alignQ := 0; alignQ < 2; alignQ++ {
- for alignD := 0; alignD < 2; alignD++ {
- p := make([]byte, 1023)[alignP:]
- q := make([]byte, 1023)[alignQ:]
-
- for i := 0; i < len(p); i++ {
- p[i] = byte(i)
- }
- for i := 0; i < len(q); i++ {
- q[i] = byte(len(q) - i)
- }
- d1 := make([]byte, 1023+alignD)[alignD:]
- d2 := make([]byte, 1023+alignD)[alignD:]
-
- ORBytes(d1, p, q)
- safeORBytes(d2, p, q)
- if !bytes.Equal(d1, d2) {
- t.Error("not equal")
- }
- }
- }
- }
-}
-
-// Tests that bit testing works for various alignments.
-func TestTest(t *testing.T) {
- for align := 0; align < 2; align++ {
- // Test for bits set in the bulk part
- p := make([]byte, 1023)[align:]
- p[100] = 1
-
- if TestBytes(p) != safeTestBytes(p) {
- t.Error("not equal")
- }
- // Test for bits set in the tail part
- q := make([]byte, 1023)[align:]
- q[len(q)-1] = 1
-
- if TestBytes(q) != safeTestBytes(q) {
- t.Error("not equal")
- }
- }
-}
-
-// Benchmarks the potentially optimized XOR performance.
-func BenchmarkFastXOR1KB(b *testing.B) { benchmarkFastXOR(b, 1024) }
-func BenchmarkFastXOR2KB(b *testing.B) { benchmarkFastXOR(b, 2048) }
-func BenchmarkFastXOR4KB(b *testing.B) { benchmarkFastXOR(b, 4096) }
-
-func benchmarkFastXOR(b *testing.B, size int) {
- p, q := make([]byte, size), make([]byte, size)
-
- for i := 0; i < b.N; i++ {
- XORBytes(p, p, q)
- }
-}
-
-// Benchmarks the baseline XOR performance.
-func BenchmarkBaseXOR1KB(b *testing.B) { benchmarkBaseXOR(b, 1024) }
-func BenchmarkBaseXOR2KB(b *testing.B) { benchmarkBaseXOR(b, 2048) }
-func BenchmarkBaseXOR4KB(b *testing.B) { benchmarkBaseXOR(b, 4096) }
-
-func benchmarkBaseXOR(b *testing.B, size int) {
- p, q := make([]byte, size), make([]byte, size)
-
- for i := 0; i < b.N; i++ {
- safeXORBytes(p, p, q)
- }
-}
-
-// Benchmarks the potentially optimized AND performance.
-func BenchmarkFastAND1KB(b *testing.B) { benchmarkFastAND(b, 1024) }
-func BenchmarkFastAND2KB(b *testing.B) { benchmarkFastAND(b, 2048) }
-func BenchmarkFastAND4KB(b *testing.B) { benchmarkFastAND(b, 4096) }
-
-func benchmarkFastAND(b *testing.B, size int) {
- p, q := make([]byte, size), make([]byte, size)
-
- for i := 0; i < b.N; i++ {
- ANDBytes(p, p, q)
- }
-}
-
-// Benchmarks the baseline AND performance.
-func BenchmarkBaseAND1KB(b *testing.B) { benchmarkBaseAND(b, 1024) }
-func BenchmarkBaseAND2KB(b *testing.B) { benchmarkBaseAND(b, 2048) }
-func BenchmarkBaseAND4KB(b *testing.B) { benchmarkBaseAND(b, 4096) }
-
-func benchmarkBaseAND(b *testing.B, size int) {
- p, q := make([]byte, size), make([]byte, size)
-
- for i := 0; i < b.N; i++ {
- safeANDBytes(p, p, q)
- }
-}
-
-// Benchmarks the potentially optimized OR performance.
-func BenchmarkFastOR1KB(b *testing.B) { benchmarkFastOR(b, 1024) }
-func BenchmarkFastOR2KB(b *testing.B) { benchmarkFastOR(b, 2048) }
-func BenchmarkFastOR4KB(b *testing.B) { benchmarkFastOR(b, 4096) }
-
-func benchmarkFastOR(b *testing.B, size int) {
- p, q := make([]byte, size), make([]byte, size)
-
- for i := 0; i < b.N; i++ {
- ORBytes(p, p, q)
- }
-}
-
-// Benchmarks the baseline OR performance.
-func BenchmarkBaseOR1KB(b *testing.B) { benchmarkBaseOR(b, 1024) }
-func BenchmarkBaseOR2KB(b *testing.B) { benchmarkBaseOR(b, 2048) }
-func BenchmarkBaseOR4KB(b *testing.B) { benchmarkBaseOR(b, 4096) }
-
-func benchmarkBaseOR(b *testing.B, size int) {
- p, q := make([]byte, size), make([]byte, size)
-
- for i := 0; i < b.N; i++ {
- safeORBytes(p, p, q)
- }
-}
-
-var GloBool bool // Exported global will not be dead-code eliminated, at least not yet.
-
-// Benchmarks the potentially optimized bit testing performance.
-func BenchmarkFastTest1KB(b *testing.B) { benchmarkFastTest(b, 1024) }
-func BenchmarkFastTest2KB(b *testing.B) { benchmarkFastTest(b, 2048) }
-func BenchmarkFastTest4KB(b *testing.B) { benchmarkFastTest(b, 4096) }
-
-func benchmarkFastTest(b *testing.B, size int) {
- p := make([]byte, size)
- a := false
- for i := 0; i < b.N; i++ {
- a = a != TestBytes(p)
- }
- GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
-}
-
-// Benchmarks the baseline bit testing performance.
-func BenchmarkBaseTest1KB(b *testing.B) { benchmarkBaseTest(b, 1024) }
-func BenchmarkBaseTest2KB(b *testing.B) { benchmarkBaseTest(b, 2048) }
-func BenchmarkBaseTest4KB(b *testing.B) { benchmarkBaseTest(b, 4096) }
-
-func benchmarkBaseTest(b *testing.B, size int) {
- p := make([]byte, size)
- a := false
- for i := 0; i < b.N; i++ {
- a = a != safeTestBytes(p)
- }
- GloBool = a // Use of benchmark "result" to prevent total dead code elimination.
-}
diff --git a/common/bitutil/compress.go b/common/bitutil/compress.go
deleted file mode 100644
index c057cee4a6..0000000000
--- a/common/bitutil/compress.go
+++ /dev/null
@@ -1,170 +0,0 @@
-// Copyright 2017 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 .
-
-package bitutil
-
-import "errors"
-
-var (
- // errMissingData is returned from decompression if the byte referenced by
- // the bitset header overflows the input data.
- errMissingData = errors.New("missing bytes on input")
-
- // errUnreferencedData is returned from decompression if not all bytes were used
- // up from the input data after decompressing it.
- errUnreferencedData = errors.New("extra bytes on input")
-
- // errExceededTarget is returned from decompression if the bitset header has
- // more bits defined than the number of target buffer space available.
- errExceededTarget = errors.New("target data size exceeded")
-
- // errZeroContent is returned from decompression if a data byte referenced in
- // the bitset header is actually a zero byte.
- errZeroContent = errors.New("zero byte in input content")
-)
-
-// The compression algorithm implemented by CompressBytes and DecompressBytes is
-// optimized for sparse input data which contains a lot of zero bytes. Decompression
-// requires knowledge of the decompressed data length.
-//
-// Compression works as follows:
-//
-// if data only contains zeroes,
-// CompressBytes(data) == nil
-// otherwise if len(data) <= 1,
-// CompressBytes(data) == data
-// otherwise:
-// CompressBytes(data) == append(CompressBytes(nonZeroBitset(data)), nonZeroBytes(data)...)
-// where
-// nonZeroBitset(data) is a bit vector with len(data) bits (MSB first):
-// nonZeroBitset(data)[i/8] && (1 << (7-i%8)) != 0 if data[i] != 0
-// len(nonZeroBitset(data)) == (len(data)+7)/8
-// nonZeroBytes(data) contains the non-zero bytes of data in the same order
-
-// CompressBytes compresses the input byte slice according to the sparse bitset
-// representation algorithm. If the result is bigger than the original input, no
-// compression is done.
-func CompressBytes(data []byte) []byte {
- if out := bitsetEncodeBytes(data); len(out) < len(data) {
- return out
- }
- cpy := make([]byte, len(data))
- copy(cpy, data)
- return cpy
-}
-
-// bitsetEncodeBytes compresses the input byte slice according to the sparse
-// bitset representation algorithm.
-func bitsetEncodeBytes(data []byte) []byte {
- // Empty slices get compressed to nil
- if len(data) == 0 {
- return nil
- }
- // One byte slices compress to nil or retain the single byte
- if len(data) == 1 {
- if data[0] == 0 {
- return nil
- }
- return data
- }
- // Calculate the bitset of set bytes, and gather the non-zero bytes
- nonZeroBitset := make([]byte, (len(data)+7)/8)
- nonZeroBytes := make([]byte, 0, len(data))
-
- for i, b := range data {
- if b != 0 {
- nonZeroBytes = append(nonZeroBytes, b)
- nonZeroBitset[i/8] |= 1 << byte(7-i%8)
- }
- }
- if len(nonZeroBytes) == 0 {
- return nil
- }
- return append(bitsetEncodeBytes(nonZeroBitset), nonZeroBytes...)
-}
-
-// DecompressBytes decompresses data with a known target size. If the input data
-// matches the size of the target, it means no compression was done in the first
-// place.
-func DecompressBytes(data []byte, target int) ([]byte, error) {
- if len(data) > target {
- return nil, errExceededTarget
- }
- if len(data) == target {
- cpy := make([]byte, len(data))
- copy(cpy, data)
- return cpy, nil
- }
- return bitsetDecodeBytes(data, target)
-}
-
-// bitsetDecodeBytes decompresses data with a known target size.
-func bitsetDecodeBytes(data []byte, target int) ([]byte, error) {
- out, size, err := bitsetDecodePartialBytes(data, target)
- if err != nil {
- return nil, err
- }
- if size != len(data) {
- return nil, errUnreferencedData
- }
- return out, nil
-}
-
-// bitsetDecodePartialBytes decompresses data with a known target size, but does
-// not enforce consuming all the input bytes. In addition to the decompressed
-// output, the function returns the length of compressed input data corresponding
-// to the output as the input slice may be longer.
-func bitsetDecodePartialBytes(data []byte, target int) ([]byte, int, error) {
- // Sanity check 0 targets to avoid infinite recursion
- if target == 0 {
- return nil, 0, nil
- }
- // Handle the zero and single byte corner cases
- decomp := make([]byte, target)
- if len(data) == 0 {
- return decomp, 0, nil
- }
- if target == 1 {
- decomp[0] = data[0] // copy to avoid referencing the input slice
- if data[0] != 0 {
- return decomp, 1, nil
- }
- return decomp, 0, nil
- }
- // Decompress the bitset of set bytes and distribute the non zero bytes
- nonZeroBitset, ptr, err := bitsetDecodePartialBytes(data, (target+7)/8)
- if err != nil {
- return nil, ptr, err
- }
- for i := 0; i < 8*len(nonZeroBitset); i++ {
- if nonZeroBitset[i/8]&(1<= len(data) {
- return nil, 0, errMissingData
- }
- if i >= len(decomp) {
- return nil, 0, errExceededTarget
- }
- // Make sure the data is valid and push into the slot
- if data[ptr] == 0 {
- return nil, 0, errZeroContent
- }
- decomp[i] = data[ptr]
- ptr++
- }
- }
- return decomp, ptr, nil
-}
diff --git a/common/bitutil/compress_test.go b/common/bitutil/compress_test.go
deleted file mode 100644
index c6f6fe8bcf..0000000000
--- a/common/bitutil/compress_test.go
+++ /dev/null
@@ -1,223 +0,0 @@
-// Copyright 2017 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 .
-
-package bitutil
-
-import (
- "bytes"
- "fmt"
- "math/rand"
- "testing"
-
- "github.com/ethereum/go-ethereum/common/hexutil"
-)
-
-// Tests that data bitset encoding and decoding works and is bijective.
-func TestEncodingCycle(t *testing.T) {
- tests := []string{
- // Tests generated by go-fuzz to maximize code coverage
- "0x000000000000000000",
- "0xef0400",
- "0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb",
- "0x7b64000000",
- "0x000034000000000000",
- "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000000000000000",
- "0x4912385c0e7b64000000",
- "0x000034000000000000000000000000000000",
- "0x00",
- "0x000003e834ff7f0000",
- "0x0000",
- "0x0000000000000000000000000000000000000000000000000000000000ff00",
- "0x895f0c6a020f850c6a020f85f88df88d",
- "0xdf7070533534333636313639343638373432313536346c1bc3315aac2f65fefb",
- "0x0000000000",
- "0xdf70706336346c65fefb",
- "0x00006d643634000000",
- "0xdf7070533534333636313639343638373532313536346c1bc333393438373130707063363430353639343638373532313536346c1bc333393438336336346c65fe",
- }
- for i, tt := range tests {
- if err := testEncodingCycle(hexutil.MustDecode(tt)); err != nil {
- t.Errorf("test %d: %v", i, err)
- }
- }
-}
-
-func testEncodingCycle(data []byte) error {
- proc, err := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
- if err != nil {
- return fmt.Errorf("failed to decompress compressed data: %v", err)
- }
- if !bytes.Equal(data, proc) {
- return fmt.Errorf("compress/decompress mismatch: have %x, want %x", proc, data)
- }
- return nil
-}
-
-// Tests that data bitset decoding and rencoding works and is bijective.
-func TestDecodingCycle(t *testing.T) {
- tests := []struct {
- size int
- input string
- fail error
- }{
- {size: 0, input: "0x"},
-
- // Crashers generated by go-fuzz
- {size: 0, input: "0x0020", fail: errUnreferencedData},
- {size: 0, input: "0x30", fail: errUnreferencedData},
- {size: 1, input: "0x00", fail: errUnreferencedData},
- {size: 2, input: "0x07", fail: errMissingData},
- {size: 1024, input: "0x8000", fail: errZeroContent},
-
- // Tests generated by go-fuzz to maximize code coverage
- {size: 29490, input: "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e", fail: errMissingData},
- {size: 59395, input: "0x00", fail: errUnreferencedData},
- {size: 52574, input: "0x70706336346c65c0de", fail: errExceededTarget},
- {size: 42264, input: "0x07", fail: errMissingData},
- {size: 52, input: "0xa5045bad48f4", fail: errExceededTarget},
- {size: 52574, input: "0xc0de", fail: errMissingData},
- {size: 52574, input: "0x"},
- {size: 29490, input: "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e", fail: errMissingData},
- {size: 29491, input: "0x3973333930783e30783e", fail: errMissingData},
-
- {size: 1024, input: "0x808080608080"},
- {size: 1024, input: "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80"},
- {size: 1024, input: "0x8080808070"},
- {size: 1024, input: "0x808070705e36346c6580ef46806380635a80"},
- {size: 1024, input: "0x80808046802680"},
- {size: 1024, input: "0x4040404035"},
- {size: 1024, input: "0x4040bf3ba2b3f684402d353234373438373934409fe5b1e7ada94ebfd7d0505e27be4035"},
- {size: 1024, input: "0x404040bf3ba2b3f6844035"},
- {size: 1024, input: "0x40402d35323437343837393440bfd7d0505e27be4035"},
- }
- for i, tt := range tests {
- data := hexutil.MustDecode(tt.input)
-
- orig, err := bitsetDecodeBytes(data, tt.size)
- if err != tt.fail {
- t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.fail)
- }
- if err != nil {
- continue
- }
- if comp := bitsetEncodeBytes(orig); !bytes.Equal(comp, data) {
- t.Errorf("test %d: decompress/compress mismatch: have %x, want %x", i, comp, data)
- }
- }
-}
-
-// TestCompression tests that compression works by returning either the bitset
-// encoded input, or the actual input if the bitset version is longer.
-func TestCompression(t *testing.T) {
- // Check the compression returns the bitset encoding is shorter
- in := hexutil.MustDecode("0x4912385c0e7b64000000")
- out := hexutil.MustDecode("0x80fe4912385c0e7b64")
-
- if data := CompressBytes(in); !bytes.Equal(data, out) {
- t.Errorf("encoding mismatch for sparse data: have %x, want %x", data, out)
- }
- if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) {
- t.Errorf("decoding mismatch for sparse data: have %x, want %x, error %v", data, in, err)
- }
- // Check the compression returns the input if the bitset encoding is longer
- in = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
- out = hexutil.MustDecode("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb")
-
- if data := CompressBytes(in); !bytes.Equal(data, out) {
- t.Errorf("encoding mismatch for dense data: have %x, want %x", data, out)
- }
- if data, err := DecompressBytes(out, len(in)); err != nil || !bytes.Equal(data, in) {
- t.Errorf("decoding mismatch for dense data: have %x, want %x, error %v", data, in, err)
- }
- // Check that decompressing a longer input than the target fails
- if _, err := DecompressBytes([]byte{0xc0, 0x01, 0x01}, 2); err != errExceededTarget {
- t.Errorf("decoding error mismatch for long data: have %v, want %v", err, errExceededTarget)
- }
-}
-
-// Crude benchmark for compressing random slices of bytes.
-func BenchmarkEncoding1KBVerySparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.0001) }
-func BenchmarkEncoding2KBVerySparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.0001) }
-func BenchmarkEncoding4KBVerySparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.0001) }
-
-func BenchmarkEncoding1KBSparse(b *testing.B) { benchmarkEncoding(b, 1024, 0.001) }
-func BenchmarkEncoding2KBSparse(b *testing.B) { benchmarkEncoding(b, 2048, 0.001) }
-func BenchmarkEncoding4KBSparse(b *testing.B) { benchmarkEncoding(b, 4096, 0.001) }
-
-func BenchmarkEncoding1KBDense(b *testing.B) { benchmarkEncoding(b, 1024, 0.1) }
-func BenchmarkEncoding2KBDense(b *testing.B) { benchmarkEncoding(b, 2048, 0.1) }
-func BenchmarkEncoding4KBDense(b *testing.B) { benchmarkEncoding(b, 4096, 0.1) }
-
-func BenchmarkEncoding1KBSaturated(b *testing.B) { benchmarkEncoding(b, 1024, 0.5) }
-func BenchmarkEncoding2KBSaturated(b *testing.B) { benchmarkEncoding(b, 2048, 0.5) }
-func BenchmarkEncoding4KBSaturated(b *testing.B) { benchmarkEncoding(b, 4096, 0.5) }
-
-func benchmarkEncoding(b *testing.B, bytes int, fill float64) {
- // Generate a random slice of bytes to compress
- random := rand.NewSource(0) // reproducible and comparable
-
- data := make([]byte, bytes)
- bits := int(float64(bytes) * 8 * fill)
-
- for i := 0; i < bits; i++ {
- idx := random.Int63() % int64(len(data))
- bit := uint(random.Int63() % 8)
- data[idx] |= 1 << bit
- }
- // Reset the benchmark and measure encoding/decoding
- b.ResetTimer()
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
- }
-}
-
-func FuzzEncoder(f *testing.F) {
- f.Fuzz(func(t *testing.T, data []byte) {
- if err := testEncodingCycle(data); err != nil {
- t.Fatal(err)
- }
- })
-}
-func FuzzDecoder(f *testing.F) {
- f.Fuzz(func(t *testing.T, data []byte) {
- fuzzDecode(data)
- })
-}
-
-// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
-// reencoding algorithm.
-func fuzzDecode(data []byte) {
- blob, err := DecompressBytes(data, 1024)
- if err != nil {
- return
- }
- // re-compress it (it's OK if the re-compressed differs from the
- // original - the first input may not have been compressed at all)
- comp := CompressBytes(blob)
- if len(comp) > len(blob) {
- // After compression, it must be smaller or equal
- panic("bad compression")
- }
- // But decompressing it once again should work
- decomp, err := DecompressBytes(data, 1024)
- if err != nil {
- panic(err)
- }
- if !bytes.Equal(decomp, blob) {
- panic("content mismatch")
- }
-}
diff --git a/common/bytes.go b/common/bytes.go
deleted file mode 100644
index d1f5c6c995..0000000000
--- a/common/bytes.go
+++ /dev/null
@@ -1,151 +0,0 @@
-// Copyright 2014 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 .
-
-// Package common contains various helper functions.
-package common
-
-import (
- "encoding/hex"
- "errors"
-
- "github.com/ethereum/go-ethereum/common/hexutil"
-)
-
-// FromHex returns the bytes represented by the hexadecimal string s.
-// s may be prefixed with "0x".
-func FromHex(s string) []byte {
- if has0xPrefix(s) {
- s = s[2:]
- }
- if len(s)%2 == 1 {
- s = "0" + s
- }
- return Hex2Bytes(s)
-}
-
-// CopyBytes returns an exact copy of the provided bytes.
-func CopyBytes(b []byte) (copiedBytes []byte) {
- if b == nil {
- return nil
- }
- copiedBytes = make([]byte, len(b))
- copy(copiedBytes, b)
-
- return
-}
-
-// has0xPrefix validates str begins with '0x' or '0X'.
-func has0xPrefix(str string) bool {
- return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
-}
-
-// isHexCharacter returns bool of c being a valid hexadecimal.
-func isHexCharacter(c byte) bool {
- return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
-}
-
-// isHex validates whether each byte is valid hexadecimal string.
-func isHex(str string) bool {
- if len(str)%2 != 0 {
- return false
- }
- for _, c := range []byte(str) {
- if !isHexCharacter(c) {
- return false
- }
- }
- return true
-}
-
-// Bytes2Hex returns the hexadecimal encoding of d.
-func Bytes2Hex(d []byte) string {
- return hex.EncodeToString(d)
-}
-
-// Hex2Bytes returns the bytes represented by the hexadecimal string str.
-func Hex2Bytes(str string) []byte {
- h, _ := hex.DecodeString(str)
- return h
-}
-
-// Hex2BytesFixed returns bytes of a specified fixed length flen.
-func Hex2BytesFixed(str string, flen int) []byte {
- h, _ := hex.DecodeString(str)
- if len(h) == flen {
- return h
- }
- if len(h) > flen {
- return h[len(h)-flen:]
- }
- hh := make([]byte, flen)
- copy(hh[flen-len(h):flen], h)
- return hh
-}
-
-// ParseHexOrString tries to hexdecode b, but if the prefix is missing, it instead just returns the raw bytes
-func ParseHexOrString(str string) ([]byte, error) {
- b, err := hexutil.Decode(str)
- if errors.Is(err, hexutil.ErrMissingPrefix) {
- return []byte(str), nil
- }
- return b, err
-}
-
-// RightPadBytes zero-pads slice to the right up to length l.
-func RightPadBytes(slice []byte, l int) []byte {
- if l <= len(slice) {
- return slice
- }
-
- padded := make([]byte, l)
- copy(padded, slice)
-
- return padded
-}
-
-// LeftPadBytes zero-pads slice to the left up to length l.
-func LeftPadBytes(slice []byte, l int) []byte {
- if l <= len(slice) {
- return slice
- }
-
- padded := make([]byte, l)
- copy(padded[l-len(slice):], slice)
-
- return padded
-}
-
-// TrimLeftZeroes returns a subslice of s without leading zeroes
-func TrimLeftZeroes(s []byte) []byte {
- idx := 0
- for ; idx < len(s); idx++ {
- if s[idx] != 0 {
- break
- }
- }
- return s[idx:]
-}
-
-// TrimRightZeroes returns a subslice of s without trailing zeroes
-func TrimRightZeroes(s []byte) []byte {
- idx := len(s)
- for ; idx > 0; idx-- {
- if s[idx-1] != 0 {
- break
- }
- }
- return s[:idx]
-}
diff --git a/common/bytes_test.go b/common/bytes_test.go
deleted file mode 100644
index 0e3ec974ee..0000000000
--- a/common/bytes_test.go
+++ /dev/null
@@ -1,126 +0,0 @@
-// Copyright 2014 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 .
-
-package common
-
-import (
- "bytes"
- "testing"
-)
-
-func TestCopyBytes(t *testing.T) {
- input := []byte{1, 2, 3, 4}
-
- v := CopyBytes(input)
- if !bytes.Equal(v, []byte{1, 2, 3, 4}) {
- t.Fatal("not equal after copy")
- }
- v[0] = 99
- if bytes.Equal(v, input) {
- t.Fatal("result is not a copy")
- }
-}
-
-func TestLeftPadBytes(t *testing.T) {
- val := []byte{1, 2, 3, 4}
- padded := []byte{0, 0, 0, 0, 1, 2, 3, 4}
-
- if r := LeftPadBytes(val, 8); !bytes.Equal(r, padded) {
- t.Fatalf("LeftPadBytes(%v, 8) == %v", val, r)
- }
- if r := LeftPadBytes(val, 2); !bytes.Equal(r, val) {
- t.Fatalf("LeftPadBytes(%v, 2) == %v", val, r)
- }
-}
-
-func TestRightPadBytes(t *testing.T) {
- val := []byte{1, 2, 3, 4}
- padded := []byte{1, 2, 3, 4, 0, 0, 0, 0}
-
- if r := RightPadBytes(val, 8); !bytes.Equal(r, padded) {
- t.Fatalf("RightPadBytes(%v, 8) == %v", val, r)
- }
- if r := RightPadBytes(val, 2); !bytes.Equal(r, val) {
- t.Fatalf("RightPadBytes(%v, 2) == %v", val, r)
- }
-}
-
-func TestFromHex(t *testing.T) {
- input := "0x01"
- expected := []byte{1}
- result := FromHex(input)
- if !bytes.Equal(expected, result) {
- t.Errorf("Expected %x got %x", expected, result)
- }
-}
-
-func TestIsHex(t *testing.T) {
- tests := []struct {
- input string
- ok bool
- }{
- {"", true},
- {"0", false},
- {"00", true},
- {"a9e67e", true},
- {"A9E67E", true},
- {"0xa9e67e", false},
- {"a9e67e001", false},
- {"0xHELLO_MY_NAME_IS_STEVEN_@#$^&*", false},
- }
- for _, test := range tests {
- if ok := isHex(test.input); ok != test.ok {
- t.Errorf("isHex(%q) = %v, want %v", test.input, ok, test.ok)
- }
- }
-}
-
-func TestFromHexOddLength(t *testing.T) {
- input := "0x1"
- expected := []byte{1}
- result := FromHex(input)
- if !bytes.Equal(expected, result) {
- t.Errorf("Expected %x got %x", expected, result)
- }
-}
-
-func TestNoPrefixShortHexOddLength(t *testing.T) {
- input := "1"
- expected := []byte{1}
- result := FromHex(input)
- if !bytes.Equal(expected, result) {
- t.Errorf("Expected %x got %x", expected, result)
- }
-}
-
-func TestTrimRightZeroes(t *testing.T) {
- tests := []struct {
- arr []byte
- exp []byte
- }{
- {FromHex("0x00ffff00ff0000"), FromHex("0x00ffff00ff")},
- {FromHex("0x00000000000000"), []byte{}},
- {FromHex("0xff"), FromHex("0xff")},
- {[]byte{}, []byte{}},
- {FromHex("0x00ffffffffffff"), FromHex("0x00ffffffffffff")},
- }
- for i, test := range tests {
- got := TrimRightZeroes(test.arr)
- if !bytes.Equal(got, test.exp) {
- t.Errorf("test %d, got %x exp %x", i, got, test.exp)
- }
- }
-}
diff --git a/common/compiler/helpers.go b/common/compiler/helpers.go
deleted file mode 100644
index 063fc10811..0000000000
--- a/common/compiler/helpers.go
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright 2019 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 .
-
-// Package compiler wraps the Solidity and Vyper compiler executables (solc; vyper).
-package compiler
-
-// Contract contains information about a compiled contract, alongside its code and runtime code.
-type Contract struct {
- Code string `json:"code"`
- RuntimeCode string `json:"runtime-code"`
- Info ContractInfo `json:"info"`
- Hashes map[string]string `json:"hashes"`
-}
-
-// ContractInfo contains information about a compiled contract, including access
-// to the ABI definition, source mapping, user and developer docs, and metadata.
-//
-// Depending on the source, language version, compiler version, and compiler
-// options will provide information about how the contract was compiled.
-type ContractInfo struct {
- Source string `json:"source"`
- Language string `json:"language"`
- LanguageVersion string `json:"languageVersion"`
- CompilerVersion string `json:"compilerVersion"`
- CompilerOptions string `json:"compilerOptions"`
- SrcMap interface{} `json:"srcMap"`
- SrcMapRuntime string `json:"srcMapRuntime"`
- AbiDefinition interface{} `json:"abiDefinition"`
- UserDoc interface{} `json:"userDoc"`
- DeveloperDoc interface{} `json:"developerDoc"`
- Metadata string `json:"metadata"`
-}
diff --git a/common/compiler/solidity.go b/common/compiler/solidity.go
deleted file mode 100644
index 9de94017c2..0000000000
--- a/common/compiler/solidity.go
+++ /dev/null
@@ -1,132 +0,0 @@
-// 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 .
-
-// Package compiler wraps the ABI compilation outputs.
-package compiler
-
-import (
- "encoding/json"
- "fmt"
-)
-
-// --combined-output format
-type solcOutput struct {
- Contracts map[string]struct {
- BinRuntime string `json:"bin-runtime"`
- SrcMapRuntime string `json:"srcmap-runtime"`
- Bin, SrcMap, Abi, Devdoc, Userdoc, Metadata string
- Hashes map[string]string
- }
- Version string
-}
-
-// solidity v.0.8 changes the way ABI, Devdoc and Userdoc are serialized
-type solcOutputV8 struct {
- Contracts map[string]struct {
- BinRuntime string `json:"bin-runtime"`
- SrcMapRuntime string `json:"srcmap-runtime"`
- Bin, SrcMap, Metadata string
- Abi interface{}
- Devdoc interface{}
- Userdoc interface{}
- Hashes map[string]string
- }
- Version string
-}
-
-// ParseCombinedJSON takes the direct output of a solc --combined-output run and
-// parses it into a map of string contract name to Contract structs. The
-// provided source, language and compiler version, and compiler options are all
-// passed through into the Contract structs.
-//
-// The solc output is expected to contain ABI, source mapping, user docs, and dev docs.
-//
-// Returns an error if the JSON is malformed or missing data, or if the JSON
-// embedded within the JSON is malformed.
-func ParseCombinedJSON(combinedJSON []byte, source string, languageVersion string, compilerVersion string, compilerOptions string) (map[string]*Contract, error) {
- var output solcOutput
- if err := json.Unmarshal(combinedJSON, &output); err != nil {
- // Try to parse the output with the new solidity v.0.8.0 rules
- return parseCombinedJSONV8(combinedJSON, source, languageVersion, compilerVersion, compilerOptions)
- }
- // Compilation succeeded, assemble and return the contracts.
- contracts := make(map[string]*Contract)
- for name, info := range output.Contracts {
- // Parse the individual compilation results.
- var abi, userdoc, devdoc interface{}
- if err := json.Unmarshal([]byte(info.Abi), &abi); err != nil {
- return nil, fmt.Errorf("solc: error reading abi definition (%v)", err)
- }
- if err := json.Unmarshal([]byte(info.Userdoc), &userdoc); err != nil {
- return nil, fmt.Errorf("solc: error reading userdoc definition (%v)", err)
- }
- if err := json.Unmarshal([]byte(info.Devdoc), &devdoc); err != nil {
- return nil, fmt.Errorf("solc: error reading devdoc definition (%v)", err)
- }
-
- contracts[name] = &Contract{
- Code: "0x" + info.Bin,
- RuntimeCode: "0x" + info.BinRuntime,
- Hashes: info.Hashes,
- Info: ContractInfo{
- Source: source,
- Language: "Solidity",
- LanguageVersion: languageVersion,
- CompilerVersion: compilerVersion,
- CompilerOptions: compilerOptions,
- SrcMap: info.SrcMap,
- SrcMapRuntime: info.SrcMapRuntime,
- AbiDefinition: abi,
- UserDoc: userdoc,
- DeveloperDoc: devdoc,
- Metadata: info.Metadata,
- },
- }
- }
- return contracts, nil
-}
-
-// parseCombinedJSONV8 parses the direct output of solc --combined-output
-// and parses it using the rules from solidity v.0.8.0 and later.
-func parseCombinedJSONV8(combinedJSON []byte, source string, languageVersion string, compilerVersion string, compilerOptions string) (map[string]*Contract, error) {
- var output solcOutputV8
- if err := json.Unmarshal(combinedJSON, &output); err != nil {
- return nil, err
- }
- // Compilation succeeded, assemble and return the contracts.
- contracts := make(map[string]*Contract)
- for name, info := range output.Contracts {
- contracts[name] = &Contract{
- Code: "0x" + info.Bin,
- RuntimeCode: "0x" + info.BinRuntime,
- Hashes: info.Hashes,
- Info: ContractInfo{
- Source: source,
- Language: "Solidity",
- LanguageVersion: languageVersion,
- CompilerVersion: compilerVersion,
- CompilerOptions: compilerOptions,
- SrcMap: info.SrcMap,
- SrcMapRuntime: info.SrcMapRuntime,
- AbiDefinition: info.Abi,
- UserDoc: info.Userdoc,
- DeveloperDoc: info.Devdoc,
- Metadata: info.Metadata,
- },
- }
- }
- return contracts, nil
-}
diff --git a/common/debug.go b/common/debug.go
deleted file mode 100644
index 28c52b4a9c..0000000000
--- a/common/debug.go
+++ /dev/null
@@ -1,52 +0,0 @@
-// 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 .
-
-package common
-
-import (
- "fmt"
- "os"
- "runtime"
- "runtime/debug"
- "strings"
-)
-
-// Report gives off a warning requesting the user to submit an issue to the github tracker.
-func Report(extra ...interface{}) {
- fmt.Fprintln(os.Stderr, "You've encountered a sought after, hard to reproduce bug. Please report this to the developers <3 https://github.com/ethereum/go-ethereum/issues")
- fmt.Fprintln(os.Stderr, extra...)
-
- _, file, line, _ := runtime.Caller(1)
- fmt.Fprintf(os.Stderr, "%v:%v\n", file, line)
-
- debug.PrintStack()
-
- fmt.Fprintln(os.Stderr, "#### BUG! PLEASE REPORT ####")
-}
-
-// PrintDeprecationWarning prints the given string in a box using fmt.Println.
-func PrintDeprecationWarning(str string) {
- line := strings.Repeat("#", len(str)+4)
- emptyLine := strings.Repeat(" ", len(str))
- fmt.Printf(`
-%s
-# %s #
-# %s #
-# %s #
-%s
-
-`, line, emptyLine, str, emptyLine, line)
-}
diff --git a/common/fdlimit/fdlimit_bsd.go b/common/fdlimit/fdlimit_bsd.go
deleted file mode 100644
index a3a6902c09..0000000000
--- a/common/fdlimit/fdlimit_bsd.go
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright 2016 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 .
-
-//go:build freebsd || dragonfly
-// +build freebsd dragonfly
-
-package fdlimit
-
-import "syscall"
-
-// This file is largely identical to fdlimit_unix.go,
-// but Rlimit fields have type int64 on *BSD so it needs
-// an extra conversion.
-
-// Raise tries to maximize the file descriptor allowance of this process
-// to the maximum hard-limit allowed by the OS.
-func Raise(max uint64) (uint64, error) {
- // Get the current limit
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- // Try to update the limit to the max allowance
- limit.Cur = limit.Max
- if limit.Cur > int64(max) {
- limit.Cur = int64(max)
- }
- if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return uint64(limit.Cur), nil
-}
-
-// Current retrieves the number of file descriptors allowed to be opened by this
-// process.
-func Current() (int, error) {
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return int(limit.Cur), nil
-}
-
-// Maximum retrieves the maximum number of file descriptors this process is
-// allowed to request for itself.
-func Maximum() (int, error) {
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return int(limit.Max), nil
-}
diff --git a/common/fdlimit/fdlimit_darwin.go b/common/fdlimit/fdlimit_darwin.go
deleted file mode 100644
index 6b26fa00f1..0000000000
--- a/common/fdlimit/fdlimit_darwin.go
+++ /dev/null
@@ -1,71 +0,0 @@
-// Copyright 2016 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 .
-
-package fdlimit
-
-import "syscall"
-
-// hardlimit is the number of file descriptors allowed at max by the kernel.
-const hardlimit = 10240
-
-// Raise tries to maximize the file descriptor allowance of this process
-// to the maximum hard-limit allowed by the OS.
-// Returns the size it was set to (may differ from the desired 'max')
-func Raise(max uint64) (uint64, error) {
- // Get the current limit
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- // Try to update the limit to the max allowance
- limit.Cur = limit.Max
- if limit.Cur > max {
- limit.Cur = max
- }
- if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- // MacOS can silently apply further caps, so retrieve the actually set limit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return limit.Cur, nil
-}
-
-// Current retrieves the number of file descriptors allowed to be opened by this
-// process.
-func Current() (int, error) {
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return int(limit.Cur), nil
-}
-
-// Maximum retrieves the maximum number of file descriptors this process is
-// allowed to request for itself.
-func Maximum() (int, error) {
- // Retrieve the maximum allowed by dynamic OS limits
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- // Cap it to OPEN_MAX (10240) because macos is a special snowflake
- if limit.Max > hardlimit {
- limit.Max = hardlimit
- }
- return int(limit.Max), nil
-}
diff --git a/common/fdlimit/fdlimit_test.go b/common/fdlimit/fdlimit_test.go
deleted file mode 100644
index 9fd5e9fc3c..0000000000
--- a/common/fdlimit/fdlimit_test.go
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright 2016 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 .
-
-package fdlimit
-
-import (
- "testing"
-)
-
-// TestFileDescriptorLimits simply tests whether the file descriptor allowance
-// per this process can be retrieved.
-func TestFileDescriptorLimits(t *testing.T) {
- target := 4096
- hardlimit, err := Maximum()
- if err != nil {
- t.Fatal(err)
- }
- if hardlimit < target {
- t.Skipf("system limit is less than desired test target: %d < %d", hardlimit, target)
- }
-
- if limit, err := Current(); err != nil || limit <= 0 {
- t.Fatalf("failed to retrieve file descriptor limit (%d): %v", limit, err)
- }
- if _, err := Raise(uint64(target)); err != nil {
- t.Fatalf("failed to raise file allowance")
- }
- if limit, err := Current(); err != nil || limit < target {
- t.Fatalf("failed to retrieve raised descriptor limit (have %v, want %v): %v", limit, target, err)
- }
-}
diff --git a/common/fdlimit/fdlimit_unix.go b/common/fdlimit/fdlimit_unix.go
deleted file mode 100644
index a1f388ebb7..0000000000
--- a/common/fdlimit/fdlimit_unix.go
+++ /dev/null
@@ -1,66 +0,0 @@
-// Copyright 2016 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 .
-
-//go:build linux || netbsd || openbsd || solaris
-// +build linux netbsd openbsd solaris
-
-package fdlimit
-
-import "syscall"
-
-// Raise tries to maximize the file descriptor allowance of this process
-// to the maximum hard-limit allowed by the OS.
-// Returns the size it was set to (may differ from the desired 'max')
-func Raise(max uint64) (uint64, error) {
- // Get the current limit
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- // Try to update the limit to the max allowance
- limit.Cur = limit.Max
- if limit.Cur > max {
- limit.Cur = max
- }
- if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- // MacOS can silently apply further caps, so retrieve the actually set limit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return limit.Cur, nil
-}
-
-// Current retrieves the number of file descriptors allowed to be opened by this
-// process.
-func Current() (int, error) {
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return int(limit.Cur), nil
-}
-
-// Maximum retrieves the maximum number of file descriptors this process is
-// allowed to request for itself.
-func Maximum() (int, error) {
- var limit syscall.Rlimit
- if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
- return 0, err
- }
- return int(limit.Max), nil
-}
diff --git a/common/fdlimit/fdlimit_windows.go b/common/fdlimit/fdlimit_windows.go
deleted file mode 100644
index f472153662..0000000000
--- a/common/fdlimit/fdlimit_windows.go
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2018 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 .
-
-package fdlimit
-
-import "fmt"
-
-// hardlimit is the number of file descriptors allowed at max by the kernel.
-const hardlimit = 16384
-
-// Raise tries to maximize the file descriptor allowance of this process
-// to the maximum hard-limit allowed by the OS.
-func Raise(max uint64) (uint64, error) {
- // This method is NOP by design:
- // * Linux/Darwin counterparts need to manually increase per process limits
- // * On Windows Go uses the CreateFile API, which is limited to 16K files, non
- // changeable from within a running process
- // This way we can always "request" raising the limits, which will either have
- // or not have effect based on the platform we're running on.
- if max > hardlimit {
- return hardlimit, fmt.Errorf("file descriptor limit (%d) reached", hardlimit)
- }
- return max, nil
-}
-
-// Current retrieves the number of file descriptors allowed to be opened by this
-// process.
-func Current() (int, error) {
- // Please see Raise for the reason why we use hard coded 16K as the limit
- return hardlimit, nil
-}
-
-// Maximum retrieves the maximum number of file descriptors this process is
-// allowed to request for itself.
-func Maximum() (int, error) {
- return Current()
-}
diff --git a/common/format.go b/common/format.go
deleted file mode 100644
index 7af41f52d5..0000000000
--- a/common/format.go
+++ /dev/null
@@ -1,82 +0,0 @@
-// Copyright 2016 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 .
-
-package common
-
-import (
- "fmt"
- "regexp"
- "strings"
- "time"
-)
-
-// PrettyDuration is a pretty printed version of a time.Duration value that cuts
-// the unnecessary precision off from the formatted textual representation.
-type PrettyDuration time.Duration
-
-var prettyDurationRe = regexp.MustCompile(`\.[0-9]{4,}`)
-
-// String implements the Stringer interface, allowing pretty printing of duration
-// values rounded to three decimals.
-func (d PrettyDuration) String() string {
- label := time.Duration(d).String()
- if match := prettyDurationRe.FindString(label); len(match) > 4 {
- label = strings.Replace(label, match, match[:4], 1)
- }
- return label
-}
-
-// PrettyAge is a pretty printed version of a time.Duration value that rounds
-// the values up to a single most significant unit, days/weeks/years included.
-type PrettyAge time.Time
-
-// ageUnits is a list of units the age pretty printing uses.
-var ageUnits = []struct {
- Size time.Duration
- Symbol string
-}{
- {12 * 30 * 24 * time.Hour, "y"},
- {30 * 24 * time.Hour, "mo"},
- {7 * 24 * time.Hour, "w"},
- {24 * time.Hour, "d"},
- {time.Hour, "h"},
- {time.Minute, "m"},
- {time.Second, "s"},
-}
-
-// String implements the Stringer interface, allowing pretty printing of duration
-// values rounded to the most significant time unit.
-func (t PrettyAge) String() string {
- // Calculate the time difference and handle the 0 cornercase
- diff := time.Since(time.Time(t))
- if diff < time.Second {
- return "0"
- }
- // Accumulate a precision of 3 components before returning
- result, prec := "", 0
-
- for _, unit := range ageUnits {
- if diff > unit.Size {
- result = fmt.Sprintf("%s%d%s", result, diff/unit.Size, unit.Symbol)
- diff %= unit.Size
-
- if prec += 1; prec >= 3 {
- break
- }
- }
- }
- return result
-}
diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go
deleted file mode 100644
index d3201850a8..0000000000
--- a/common/hexutil/hexutil.go
+++ /dev/null
@@ -1,241 +0,0 @@
-// Copyright 2016 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 .
-
-/*
-Package hexutil implements hex encoding with 0x prefix.
-This encoding is used by the Ethereum RPC API to transport binary data in JSON payloads.
-
-# Encoding Rules
-
-All hex data must have prefix "0x".
-
-For byte slices, the hex data must be of even length. An empty byte slice
-encodes as "0x".
-
-Integers are encoded using the least amount of digits (no leading zero digits). Their
-encoding may be of uneven length. The number zero encodes as "0x0".
-*/
-package hexutil
-
-import (
- "encoding/hex"
- "fmt"
- "math/big"
- "strconv"
-)
-
-const uintBits = 32 << (uint64(^uint(0)) >> 63)
-
-// Errors
-var (
- ErrEmptyString = &decError{"empty hex string"}
- ErrSyntax = &decError{"invalid hex string"}
- ErrMissingPrefix = &decError{"hex string without 0x prefix"}
- ErrOddLength = &decError{"hex string of odd length"}
- ErrEmptyNumber = &decError{"hex string \"0x\""}
- ErrLeadingZero = &decError{"hex number with leading zero digits"}
- ErrUint64Range = &decError{"hex number > 64 bits"}
- ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", uintBits)}
- ErrBig256Range = &decError{"hex number > 256 bits"}
-)
-
-type decError struct{ msg string }
-
-func (err decError) Error() string { return err.msg }
-
-// Decode decodes a hex string with 0x prefix.
-func Decode(input string) ([]byte, error) {
- if len(input) == 0 {
- return nil, ErrEmptyString
- }
- if !has0xPrefix(input) {
- return nil, ErrMissingPrefix
- }
- b, err := hex.DecodeString(input[2:])
- if err != nil {
- err = mapError(err)
- }
- return b, err
-}
-
-// MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
-func MustDecode(input string) []byte {
- dec, err := Decode(input)
- if err != nil {
- panic(err)
- }
- return dec
-}
-
-// Encode encodes b as a hex string with 0x prefix.
-func Encode(b []byte) string {
- enc := make([]byte, len(b)*2+2)
- copy(enc, "0x")
- hex.Encode(enc[2:], b)
- return string(enc)
-}
-
-// DecodeUint64 decodes a hex string with 0x prefix as a quantity.
-func DecodeUint64(input string) (uint64, error) {
- raw, err := checkNumber(input)
- if err != nil {
- return 0, err
- }
- dec, err := strconv.ParseUint(raw, 16, 64)
- if err != nil {
- err = mapError(err)
- }
- return dec, err
-}
-
-// MustDecodeUint64 decodes a hex string with 0x prefix as a quantity.
-// It panics for invalid input.
-func MustDecodeUint64(input string) uint64 {
- dec, err := DecodeUint64(input)
- if err != nil {
- panic(err)
- }
- return dec
-}
-
-// EncodeUint64 encodes i as a hex string with 0x prefix.
-func EncodeUint64(i uint64) string {
- enc := make([]byte, 2, 10)
- copy(enc, "0x")
- return string(strconv.AppendUint(enc, i, 16))
-}
-
-var bigWordNibbles int
-
-func init() {
- // This is a weird way to compute the number of nibbles required for big.Word.
- // The usual way would be to use constant arithmetic but go vet can't handle that.
- b, _ := new(big.Int).SetString("FFFFFFFFFF", 16)
- switch len(b.Bits()) {
- case 1:
- bigWordNibbles = 16
- case 2:
- bigWordNibbles = 8
- default:
- panic("weird big.Word size")
- }
-}
-
-// DecodeBig decodes a hex string with 0x prefix as a quantity.
-// Numbers larger than 256 bits are not accepted.
-func DecodeBig(input string) (*big.Int, error) {
- raw, err := checkNumber(input)
- if err != nil {
- return nil, err
- }
- if len(raw) > 64 {
- return nil, ErrBig256Range
- }
- words := make([]big.Word, len(raw)/bigWordNibbles+1)
- end := len(raw)
- for i := range words {
- start := end - bigWordNibbles
- if start < 0 {
- start = 0
- }
- for ri := start; ri < end; ri++ {
- nib := decodeNibble(raw[ri])
- if nib == badNibble {
- return nil, ErrSyntax
- }
- words[i] *= 16
- words[i] += big.Word(nib)
- }
- end = start
- }
- dec := new(big.Int).SetBits(words)
- return dec, nil
-}
-
-// MustDecodeBig decodes a hex string with 0x prefix as a quantity.
-// It panics for invalid input.
-func MustDecodeBig(input string) *big.Int {
- dec, err := DecodeBig(input)
- if err != nil {
- panic(err)
- }
- return dec
-}
-
-// EncodeBig encodes bigint as a hex string with 0x prefix.
-func EncodeBig(bigint *big.Int) string {
- if sign := bigint.Sign(); sign == 0 {
- return "0x0"
- } else if sign > 0 {
- return "0x" + bigint.Text(16)
- } else {
- return "-0x" + bigint.Text(16)[1:]
- }
-}
-
-func has0xPrefix(input string) bool {
- return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
-}
-
-func checkNumber(input string) (raw string, err error) {
- if len(input) == 0 {
- return "", ErrEmptyString
- }
- if !has0xPrefix(input) {
- return "", ErrMissingPrefix
- }
- input = input[2:]
- if len(input) == 0 {
- return "", ErrEmptyNumber
- }
- if len(input) > 1 && input[0] == '0' {
- return "", ErrLeadingZero
- }
- return input, nil
-}
-
-const badNibble = ^uint64(0)
-
-func decodeNibble(in byte) uint64 {
- switch {
- case in >= '0' && in <= '9':
- return uint64(in - '0')
- case in >= 'A' && in <= 'F':
- return uint64(in - 'A' + 10)
- case in >= 'a' && in <= 'f':
- return uint64(in - 'a' + 10)
- default:
- return badNibble
- }
-}
-
-func mapError(err error) error {
- if err, ok := err.(*strconv.NumError); ok {
- switch err.Err {
- case strconv.ErrRange:
- return ErrUint64Range
- case strconv.ErrSyntax:
- return ErrSyntax
- }
- }
- if _, ok := err.(hex.InvalidByteError); ok {
- return ErrSyntax
- }
- if err == hex.ErrLength {
- return ErrOddLength
- }
- return err
-}
diff --git a/common/hexutil/hexutil_test.go b/common/hexutil/hexutil_test.go
deleted file mode 100644
index f2b800d82c..0000000000
--- a/common/hexutil/hexutil_test.go
+++ /dev/null
@@ -1,215 +0,0 @@
-// Copyright 2016 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 .
-
-package hexutil
-
-import (
- "bytes"
- "math/big"
- "testing"
-)
-
-type marshalTest struct {
- input interface{}
- want string
-}
-
-type unmarshalTest struct {
- input string
- want interface{}
- wantErr error // if set, decoding must fail on any platform
- wantErr32bit error // if set, decoding must fail on 32bit platforms (used for Uint tests)
-}
-
-var (
- encodeBytesTests = []marshalTest{
- {[]byte{}, "0x"},
- {[]byte{0}, "0x00"},
- {[]byte{0, 0, 1, 2}, "0x00000102"},
- }
-
- encodeBigTests = []marshalTest{
- {referenceBig("0"), "0x0"},
- {referenceBig("1"), "0x1"},
- {referenceBig("ff"), "0xff"},
- {referenceBig("112233445566778899aabbccddeeff"), "0x112233445566778899aabbccddeeff"},
- {referenceBig("80a7f2c1bcc396c00"), "0x80a7f2c1bcc396c00"},
- {referenceBig("-80a7f2c1bcc396c00"), "-0x80a7f2c1bcc396c00"},
- }
-
- encodeUint64Tests = []marshalTest{
- {uint64(0), "0x0"},
- {uint64(1), "0x1"},
- {uint64(0xff), "0xff"},
- {uint64(0x1122334455667788), "0x1122334455667788"},
- }
-
- encodeUintTests = []marshalTest{
- {uint(0), "0x0"},
- {uint(1), "0x1"},
- {uint(0xff), "0xff"},
- {uint(0x11223344), "0x11223344"},
- }
-
- decodeBytesTests = []unmarshalTest{
- // invalid
- {input: ``, wantErr: ErrEmptyString},
- {input: `0`, wantErr: ErrMissingPrefix},
- {input: `0x0`, wantErr: ErrOddLength},
- {input: `0x023`, wantErr: ErrOddLength},
- {input: `0xxx`, wantErr: ErrSyntax},
- {input: `0x01zz01`, wantErr: ErrSyntax},
- // valid
- {input: `0x`, want: []byte{}},
- {input: `0X`, want: []byte{}},
- {input: `0x02`, want: []byte{0x02}},
- {input: `0X02`, want: []byte{0x02}},
- {input: `0xffffffffff`, want: []byte{0xff, 0xff, 0xff, 0xff, 0xff}},
- {
- input: `0xffffffffffffffffffffffffffffffffffff`,
- want: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
- },
- }
-
- decodeBigTests = []unmarshalTest{
- // invalid
- {input: `0`, wantErr: ErrMissingPrefix},
- {input: `0x`, wantErr: ErrEmptyNumber},
- {input: `0x01`, wantErr: ErrLeadingZero},
- {input: `0xx`, wantErr: ErrSyntax},
- {input: `0x1zz01`, wantErr: ErrSyntax},
- {
- input: `0x10000000000000000000000000000000000000000000000000000000000000000`,
- wantErr: ErrBig256Range,
- },
- // valid
- {input: `0x0`, want: big.NewInt(0)},
- {input: `0x2`, want: big.NewInt(0x2)},
- {input: `0x2F2`, want: big.NewInt(0x2f2)},
- {input: `0X2F2`, want: big.NewInt(0x2f2)},
- {input: `0x1122aaff`, want: big.NewInt(0x1122aaff)},
- {input: `0xbBb`, want: big.NewInt(0xbbb)},
- {input: `0xfffffffff`, want: big.NewInt(0xfffffffff)},
- {
- input: `0x112233445566778899aabbccddeeff`,
- want: referenceBig("112233445566778899aabbccddeeff"),
- },
- {
- input: `0xffffffffffffffffffffffffffffffffffff`,
- want: referenceBig("ffffffffffffffffffffffffffffffffffff"),
- },
- {
- input: `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`,
- want: referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
- },
- }
-
- decodeUint64Tests = []unmarshalTest{
- // invalid
- {input: `0`, wantErr: ErrMissingPrefix},
- {input: `0x`, wantErr: ErrEmptyNumber},
- {input: `0x01`, wantErr: ErrLeadingZero},
- {input: `0xfffffffffffffffff`, wantErr: ErrUint64Range},
- {input: `0xx`, wantErr: ErrSyntax},
- {input: `0x1zz01`, wantErr: ErrSyntax},
- // valid
- {input: `0x0`, want: uint64(0)},
- {input: `0x2`, want: uint64(0x2)},
- {input: `0x2F2`, want: uint64(0x2f2)},
- {input: `0X2F2`, want: uint64(0x2f2)},
- {input: `0x1122aaff`, want: uint64(0x1122aaff)},
- {input: `0xbbb`, want: uint64(0xbbb)},
- {input: `0xffffffffffffffff`, want: uint64(0xffffffffffffffff)},
- }
-)
-
-func TestEncode(t *testing.T) {
- for _, test := range encodeBytesTests {
- enc := Encode(test.input.([]byte))
- if enc != test.want {
- t.Errorf("input %x: wrong encoding %s", test.input, enc)
- }
- }
-}
-
-func TestDecode(t *testing.T) {
- for _, test := range decodeBytesTests {
- dec, err := Decode(test.input)
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if !bytes.Equal(test.want.([]byte), dec) {
- t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
- continue
- }
- }
-}
-
-func TestEncodeBig(t *testing.T) {
- for _, test := range encodeBigTests {
- enc := EncodeBig(test.input.(*big.Int))
- if enc != test.want {
- t.Errorf("input %x: wrong encoding %s", test.input, enc)
- }
- }
-}
-
-func TestDecodeBig(t *testing.T) {
- for _, test := range decodeBigTests {
- dec, err := DecodeBig(test.input)
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if dec.Cmp(test.want.(*big.Int)) != 0 {
- t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
- continue
- }
- }
-}
-
-func TestEncodeUint64(t *testing.T) {
- for _, test := range encodeUint64Tests {
- enc := EncodeUint64(test.input.(uint64))
- if enc != test.want {
- t.Errorf("input %x: wrong encoding %s", test.input, enc)
- }
- }
-}
-
-func TestDecodeUint64(t *testing.T) {
- for _, test := range decodeUint64Tests {
- dec, err := DecodeUint64(test.input)
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if dec != test.want.(uint64) {
- t.Errorf("input %s: value mismatch: got %x, want %x", test.input, dec, test.want)
- continue
- }
- }
-}
-
-func BenchmarkEncodeBig(b *testing.B) {
- for _, bench := range encodeBigTests {
- b.Run(bench.want, func(b *testing.B) {
- b.ReportAllocs()
- bigint := bench.input.(*big.Int)
- for i := 0; i < b.N; i++ {
- EncodeBig(bigint)
- }
- })
- }
-}
diff --git a/common/hexutil/json.go b/common/hexutil/json.go
deleted file mode 100644
index e0ac98f52d..0000000000
--- a/common/hexutil/json.go
+++ /dev/null
@@ -1,421 +0,0 @@
-// Copyright 2016 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 .
-
-package hexutil
-
-import (
- "encoding/hex"
- "encoding/json"
- "fmt"
- "math/big"
- "reflect"
- "strconv"
-
- "github.com/holiman/uint256"
-)
-
-var (
- bytesT = reflect.TypeOf(Bytes(nil))
- bigT = reflect.TypeOf((*Big)(nil))
- uintT = reflect.TypeOf(Uint(0))
- uint64T = reflect.TypeOf(Uint64(0))
- u256T = reflect.TypeOf((*uint256.Int)(nil))
-)
-
-// Bytes marshals/unmarshals as a JSON string with 0x prefix.
-// The empty slice marshals as "0x".
-type Bytes []byte
-
-// MarshalText implements encoding.TextMarshaler
-func (b Bytes) MarshalText() ([]byte, error) {
- result := make([]byte, len(b)*2+2)
- copy(result, `0x`)
- hex.Encode(result[2:], b)
- return result, nil
-}
-
-// UnmarshalJSON implements json.Unmarshaler.
-func (b *Bytes) UnmarshalJSON(input []byte) error {
- if !isString(input) {
- return errNonString(bytesT)
- }
- return wrapTypeError(b.UnmarshalText(input[1:len(input)-1]), bytesT)
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler.
-func (b *Bytes) UnmarshalText(input []byte) error {
- raw, err := checkText(input, true)
- if err != nil {
- return err
- }
- dec := make([]byte, len(raw)/2)
- if _, err = hex.Decode(dec, raw); err != nil {
- err = mapError(err)
- } else {
- *b = dec
- }
- return err
-}
-
-// String returns the hex encoding of b.
-func (b Bytes) String() string {
- return Encode(b)
-}
-
-// ImplementsGraphQLType returns true if Bytes implements the specified GraphQL type.
-func (b Bytes) ImplementsGraphQLType(name string) bool { return name == "Bytes" }
-
-// UnmarshalGraphQL unmarshals the provided GraphQL query data.
-func (b *Bytes) UnmarshalGraphQL(input interface{}) error {
- var err error
- switch input := input.(type) {
- case string:
- data, err := Decode(input)
- if err != nil {
- return err
- }
- *b = data
- default:
- err = fmt.Errorf("unexpected type %T for Bytes", input)
- }
- return err
-}
-
-// UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out
-// determines the required input length. This function is commonly used to implement the
-// UnmarshalJSON method for fixed-size types.
-func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error {
- if !isString(input) {
- return errNonString(typ)
- }
- return wrapTypeError(UnmarshalFixedText(typ.String(), input[1:len(input)-1], out), typ)
-}
-
-// UnmarshalFixedText decodes the input as a string with 0x prefix. The length of out
-// determines the required input length. This function is commonly used to implement the
-// UnmarshalText method for fixed-size types.
-func UnmarshalFixedText(typname string, input, out []byte) error {
- raw, err := checkText(input, true)
- if err != nil {
- return err
- }
- if len(raw)/2 != len(out) {
- return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(out)*2, typname)
- }
- // Pre-verify syntax before modifying out.
- for _, b := range raw {
- if decodeNibble(b) == badNibble {
- return ErrSyntax
- }
- }
- hex.Decode(out, raw)
- return nil
-}
-
-// UnmarshalFixedUnprefixedText decodes the input as a string with optional 0x prefix. The
-// length of out determines the required input length. This function is commonly used to
-// implement the UnmarshalText method for fixed-size types.
-func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error {
- raw, err := checkText(input, false)
- if err != nil {
- return err
- }
- if len(raw)/2 != len(out) {
- return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(out)*2, typname)
- }
- // Pre-verify syntax before modifying out.
- for _, b := range raw {
- if decodeNibble(b) == badNibble {
- return ErrSyntax
- }
- }
- hex.Decode(out, raw)
- return nil
-}
-
-// Big marshals/unmarshals as a JSON string with 0x prefix.
-// The zero value marshals as "0x0".
-//
-// Negative integers are not supported at this time. Attempting to marshal them will
-// return an error. Values larger than 256bits are rejected by Unmarshal but will be
-// marshaled without error.
-type Big big.Int
-
-// MarshalText implements encoding.TextMarshaler
-func (b Big) MarshalText() ([]byte, error) {
- return []byte(EncodeBig((*big.Int)(&b))), nil
-}
-
-// UnmarshalJSON implements json.Unmarshaler.
-func (b *Big) UnmarshalJSON(input []byte) error {
- if !isString(input) {
- return errNonString(bigT)
- }
- return wrapTypeError(b.UnmarshalText(input[1:len(input)-1]), bigT)
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler
-func (b *Big) UnmarshalText(input []byte) error {
- raw, err := checkNumberText(input)
- if err != nil {
- return err
- }
- if len(raw) > 64 {
- return ErrBig256Range
- }
- words := make([]big.Word, len(raw)/bigWordNibbles+1)
- end := len(raw)
- for i := range words {
- start := end - bigWordNibbles
- if start < 0 {
- start = 0
- }
- for ri := start; ri < end; ri++ {
- nib := decodeNibble(raw[ri])
- if nib == badNibble {
- return ErrSyntax
- }
- words[i] *= 16
- words[i] += big.Word(nib)
- }
- end = start
- }
- var dec big.Int
- dec.SetBits(words)
- *b = (Big)(dec)
- return nil
-}
-
-// ToInt converts b to a big.Int.
-func (b *Big) ToInt() *big.Int {
- return (*big.Int)(b)
-}
-
-// String returns the hex encoding of b.
-func (b *Big) String() string {
- return EncodeBig(b.ToInt())
-}
-
-// ImplementsGraphQLType returns true if Big implements the provided GraphQL type.
-func (b Big) ImplementsGraphQLType(name string) bool { return name == "BigInt" }
-
-// UnmarshalGraphQL unmarshals the provided GraphQL query data.
-func (b *Big) UnmarshalGraphQL(input interface{}) error {
- var err error
- switch input := input.(type) {
- case string:
- return b.UnmarshalText([]byte(input))
- case int32:
- var num big.Int
- num.SetInt64(int64(input))
- *b = Big(num)
- default:
- err = fmt.Errorf("unexpected type %T for BigInt", input)
- }
- return err
-}
-
-// U256 marshals/unmarshals as a JSON string with 0x prefix.
-// The zero value marshals as "0x0".
-type U256 uint256.Int
-
-// MarshalText implements encoding.TextMarshaler
-func (b U256) MarshalText() ([]byte, error) {
- u256 := (*uint256.Int)(&b)
- return []byte(u256.Hex()), nil
-}
-
-// UnmarshalJSON implements json.Unmarshaler.
-func (b *U256) UnmarshalJSON(input []byte) error {
- // The uint256.Int.UnmarshalJSON method accepts "dec", "0xhex"; we must be
- // more strict, hence we check string and invoke SetFromHex directly.
- if !isString(input) {
- return errNonString(u256T)
- }
- // The hex decoder needs to accept empty string ("") as '0', which uint256.Int
- // would reject.
- if len(input) == 2 {
- (*uint256.Int)(b).Clear()
- return nil
- }
- err := (*uint256.Int)(b).SetFromHex(string(input[1 : len(input)-1]))
- if err != nil {
- return &json.UnmarshalTypeError{Value: err.Error(), Type: u256T}
- }
- return nil
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler
-func (b *U256) UnmarshalText(input []byte) error {
- // The uint256.Int.UnmarshalText method accepts "dec", "0xhex"; we must be
- // more strict, hence we check string and invoke SetFromHex directly.
- return (*uint256.Int)(b).SetFromHex(string(input))
-}
-
-// String returns the hex encoding of b.
-func (b *U256) String() string {
- return (*uint256.Int)(b).Hex()
-}
-
-// Uint64 marshals/unmarshals as a JSON string with 0x prefix.
-// The zero value marshals as "0x0".
-type Uint64 uint64
-
-// MarshalText implements encoding.TextMarshaler.
-func (b Uint64) MarshalText() ([]byte, error) {
- buf := make([]byte, 2, 10)
- copy(buf, `0x`)
- buf = strconv.AppendUint(buf, uint64(b), 16)
- return buf, nil
-}
-
-// UnmarshalJSON implements json.Unmarshaler.
-func (b *Uint64) UnmarshalJSON(input []byte) error {
- if !isString(input) {
- return errNonString(uint64T)
- }
- return wrapTypeError(b.UnmarshalText(input[1:len(input)-1]), uint64T)
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler
-func (b *Uint64) UnmarshalText(input []byte) error {
- raw, err := checkNumberText(input)
- if err != nil {
- return err
- }
- if len(raw) > 16 {
- return ErrUint64Range
- }
- var dec uint64
- for _, byte := range raw {
- nib := decodeNibble(byte)
- if nib == badNibble {
- return ErrSyntax
- }
- dec *= 16
- dec += nib
- }
- *b = Uint64(dec)
- return nil
-}
-
-// String returns the hex encoding of b.
-func (b Uint64) String() string {
- return EncodeUint64(uint64(b))
-}
-
-// ImplementsGraphQLType returns true if Uint64 implements the provided GraphQL type.
-func (b Uint64) ImplementsGraphQLType(name string) bool { return name == "Long" }
-
-// UnmarshalGraphQL unmarshals the provided GraphQL query data.
-func (b *Uint64) UnmarshalGraphQL(input interface{}) error {
- var err error
- switch input := input.(type) {
- case string:
- return b.UnmarshalText([]byte(input))
- case int32:
- *b = Uint64(input)
- default:
- err = fmt.Errorf("unexpected type %T for Long", input)
- }
- return err
-}
-
-// Uint marshals/unmarshals as a JSON string with 0x prefix.
-// The zero value marshals as "0x0".
-type Uint uint
-
-// MarshalText implements encoding.TextMarshaler.
-func (b Uint) MarshalText() ([]byte, error) {
- return Uint64(b).MarshalText()
-}
-
-// UnmarshalJSON implements json.Unmarshaler.
-func (b *Uint) UnmarshalJSON(input []byte) error {
- if !isString(input) {
- return errNonString(uintT)
- }
- return wrapTypeError(b.UnmarshalText(input[1:len(input)-1]), uintT)
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler.
-func (b *Uint) UnmarshalText(input []byte) error {
- var u64 Uint64
- err := u64.UnmarshalText(input)
- if u64 > Uint64(^uint(0)) || err == ErrUint64Range {
- return ErrUintRange
- } else if err != nil {
- return err
- }
- *b = Uint(u64)
- return nil
-}
-
-// String returns the hex encoding of b.
-func (b Uint) String() string {
- return EncodeUint64(uint64(b))
-}
-
-func isString(input []byte) bool {
- return len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"'
-}
-
-func bytesHave0xPrefix(input []byte) bool {
- return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
-}
-
-func checkText(input []byte, wantPrefix bool) ([]byte, error) {
- if len(input) == 0 {
- return nil, nil // empty strings are allowed
- }
- if bytesHave0xPrefix(input) {
- input = input[2:]
- } else if wantPrefix {
- return nil, ErrMissingPrefix
- }
- if len(input)%2 != 0 {
- return nil, ErrOddLength
- }
- return input, nil
-}
-
-func checkNumberText(input []byte) (raw []byte, err error) {
- if len(input) == 0 {
- return nil, nil // empty strings are allowed
- }
- if !bytesHave0xPrefix(input) {
- return nil, ErrMissingPrefix
- }
- input = input[2:]
- if len(input) == 0 {
- return nil, ErrEmptyNumber
- }
- if len(input) > 1 && input[0] == '0' {
- return nil, ErrLeadingZero
- }
- return input, nil
-}
-
-func wrapTypeError(err error, typ reflect.Type) error {
- if _, ok := err.(*decError); ok {
- return &json.UnmarshalTypeError{Value: err.Error(), Type: typ}
- }
- return err
-}
-
-func errNonString(typ reflect.Type) error {
- return &json.UnmarshalTypeError{Value: "non-string", Type: typ}
-}
diff --git a/common/hexutil/json_example_test.go b/common/hexutil/json_example_test.go
deleted file mode 100644
index 80180d9186..0000000000
--- a/common/hexutil/json_example_test.go
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright 2017 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 .
-
-package hexutil_test
-
-import (
- "encoding/json"
- "fmt"
-
- "github.com/ethereum/go-ethereum/common/hexutil"
-)
-
-type MyType [5]byte
-
-func (v *MyType) UnmarshalText(input []byte) error {
- return hexutil.UnmarshalFixedText("MyType", input, v[:])
-}
-
-func (v MyType) String() string {
- return hexutil.Bytes(v[:]).String()
-}
-
-func ExampleUnmarshalFixedText() {
- var v1, v2 MyType
- fmt.Println("v1 error:", json.Unmarshal([]byte(`"0x01"`), &v1))
- fmt.Println("v2 error:", json.Unmarshal([]byte(`"0x0101010101"`), &v2))
- fmt.Println("v2:", v2)
- // Output:
- // v1 error: hex string has length 2, want 10 for MyType
- // v2 error:
- // v2: 0x0101010101
-}
diff --git a/common/hexutil/json_test.go b/common/hexutil/json_test.go
deleted file mode 100644
index 7cca300951..0000000000
--- a/common/hexutil/json_test.go
+++ /dev/null
@@ -1,434 +0,0 @@
-// Copyright 2016 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 .
-
-package hexutil
-
-import (
- "bytes"
- "encoding/hex"
- "encoding/json"
- "errors"
- "math/big"
- "testing"
-
- "github.com/holiman/uint256"
-)
-
-func checkError(t *testing.T, input string, got, want error) bool {
- if got == nil {
- if want != nil {
- t.Errorf("input %s: got no error, want %q", input, want)
- return false
- }
- return true
- }
- if want == nil {
- t.Errorf("input %s: unexpected error %q", input, got)
- } else if got.Error() != want.Error() {
- t.Errorf("input %s: got error %q, want %q", input, got, want)
- }
- return false
-}
-
-func referenceBig(s string) *big.Int {
- b, ok := new(big.Int).SetString(s, 16)
- if !ok {
- panic("invalid")
- }
- return b
-}
-
-func referenceBytes(s string) []byte {
- b, err := hex.DecodeString(s)
- if err != nil {
- panic(err)
- }
- return b
-}
-
-var errJSONEOF = errors.New("unexpected end of JSON input")
-
-var unmarshalBytesTests = []unmarshalTest{
- // invalid encoding
- {input: "", wantErr: errJSONEOF},
- {input: "null", wantErr: errNonString(bytesT)},
- {input: "10", wantErr: errNonString(bytesT)},
- {input: `"0"`, wantErr: wrapTypeError(ErrMissingPrefix, bytesT)},
- {input: `"0x0"`, wantErr: wrapTypeError(ErrOddLength, bytesT)},
- {input: `"0xxx"`, wantErr: wrapTypeError(ErrSyntax, bytesT)},
- {input: `"0x01zz01"`, wantErr: wrapTypeError(ErrSyntax, bytesT)},
-
- // valid encoding
- {input: `""`, want: referenceBytes("")},
- {input: `"0x"`, want: referenceBytes("")},
- {input: `"0x02"`, want: referenceBytes("02")},
- {input: `"0X02"`, want: referenceBytes("02")},
- {input: `"0xffffffffff"`, want: referenceBytes("ffffffffff")},
- {
- input: `"0xffffffffffffffffffffffffffffffffffff"`,
- want: referenceBytes("ffffffffffffffffffffffffffffffffffff"),
- },
-}
-
-func TestUnmarshalBytes(t *testing.T) {
- for _, test := range unmarshalBytesTests {
- var v Bytes
- err := json.Unmarshal([]byte(test.input), &v)
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if !bytes.Equal(test.want.([]byte), v) {
- t.Errorf("input %s: value mismatch: got %x, want %x", test.input, &v, test.want)
- continue
- }
- }
-}
-
-func BenchmarkUnmarshalBytes(b *testing.B) {
- input := []byte(`"0x123456789abcdef123456789abcdef"`)
- for i := 0; i < b.N; i++ {
- var v Bytes
- if err := v.UnmarshalJSON(input); err != nil {
- b.Fatal(err)
- }
- }
-}
-
-func TestMarshalBytes(t *testing.T) {
- for _, test := range encodeBytesTests {
- in := test.input.([]byte)
- out, err := json.Marshal(Bytes(in))
- if err != nil {
- t.Errorf("%x: %v", in, err)
- continue
- }
- if want := `"` + test.want + `"`; string(out) != want {
- t.Errorf("%x: MarshalJSON output mismatch: got %q, want %q", in, out, want)
- continue
- }
- if out := Bytes(in).String(); out != test.want {
- t.Errorf("%x: String mismatch: got %q, want %q", in, out, test.want)
- continue
- }
- }
-}
-
-var unmarshalBigTests = []unmarshalTest{
- // invalid encoding
- {input: "", wantErr: errJSONEOF},
- {input: "null", wantErr: errNonString(bigT)},
- {input: "10", wantErr: errNonString(bigT)},
- {input: `"0"`, wantErr: wrapTypeError(ErrMissingPrefix, bigT)},
- {input: `"0x"`, wantErr: wrapTypeError(ErrEmptyNumber, bigT)},
- {input: `"0x01"`, wantErr: wrapTypeError(ErrLeadingZero, bigT)},
- {input: `"0xx"`, wantErr: wrapTypeError(ErrSyntax, bigT)},
- {input: `"0x1zz01"`, wantErr: wrapTypeError(ErrSyntax, bigT)},
- {
- input: `"0x10000000000000000000000000000000000000000000000000000000000000000"`,
- wantErr: wrapTypeError(ErrBig256Range, bigT),
- },
-
- // valid encoding
- {input: `""`, want: big.NewInt(0)},
- {input: `"0x0"`, want: big.NewInt(0)},
- {input: `"0x2"`, want: big.NewInt(0x2)},
- {input: `"0x2F2"`, want: big.NewInt(0x2f2)},
- {input: `"0X2F2"`, want: big.NewInt(0x2f2)},
- {input: `"0x1122aaff"`, want: big.NewInt(0x1122aaff)},
- {input: `"0xbBb"`, want: big.NewInt(0xbbb)},
- {input: `"0xfffffffff"`, want: big.NewInt(0xfffffffff)},
- {
- input: `"0x112233445566778899aabbccddeeff"`,
- want: referenceBig("112233445566778899aabbccddeeff"),
- },
- {
- input: `"0xffffffffffffffffffffffffffffffffffff"`,
- want: referenceBig("ffffffffffffffffffffffffffffffffffff"),
- },
- {
- input: `"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"`,
- want: referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
- },
-}
-
-func TestUnmarshalBig(t *testing.T) {
- for _, test := range unmarshalBigTests {
- var v Big
- err := json.Unmarshal([]byte(test.input), &v)
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if test.want != nil && test.want.(*big.Int).Cmp((*big.Int)(&v)) != 0 {
- t.Errorf("input %s: value mismatch: got %x, want %x", test.input, (*big.Int)(&v), test.want)
- continue
- }
- }
-}
-
-var unmarshalU256Tests = []unmarshalTest{
- // invalid encoding
- {input: "", wantErr: errJSONEOF},
- {input: "null", wantErr: errNonString(u256T)},
- {input: "10", wantErr: errNonString(u256T)},
- {input: `"0"`, wantErr: wrapTypeError(ErrMissingPrefix, u256T)},
- {input: `"0x"`, wantErr: wrapTypeError(ErrEmptyNumber, u256T)},
- {input: `"0x01"`, wantErr: wrapTypeError(ErrLeadingZero, u256T)},
- {input: `"0xx"`, wantErr: wrapTypeError(ErrSyntax, u256T)},
- {input: `"0x1zz01"`, wantErr: wrapTypeError(ErrSyntax, u256T)},
- {
- input: `"0x10000000000000000000000000000000000000000000000000000000000000000"`,
- wantErr: wrapTypeError(ErrBig256Range, u256T),
- },
-
- // valid encoding
- {input: `""`, want: big.NewInt(0)},
- {input: `"0x0"`, want: big.NewInt(0)},
- {input: `"0x2"`, want: big.NewInt(0x2)},
- {input: `"0x2F2"`, want: big.NewInt(0x2f2)},
- {input: `"0X2F2"`, want: big.NewInt(0x2f2)},
- {input: `"0x1122aaff"`, want: big.NewInt(0x1122aaff)},
- {input: `"0xbBb"`, want: big.NewInt(0xbbb)},
- {input: `"0xfffffffff"`, want: big.NewInt(0xfffffffff)},
- {
- input: `"0x112233445566778899aabbccddeeff"`,
- want: referenceBig("112233445566778899aabbccddeeff"),
- },
- {
- input: `"0xffffffffffffffffffffffffffffffffffff"`,
- want: referenceBig("ffffffffffffffffffffffffffffffffffff"),
- },
- {
- input: `"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"`,
- want: referenceBig("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
- },
-}
-
-func TestUnmarshalU256(t *testing.T) {
- for _, test := range unmarshalU256Tests {
- var v U256
- err := json.Unmarshal([]byte(test.input), &v)
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if test.want == nil {
- continue
- }
- want := new(uint256.Int)
- want.SetFromBig(test.want.(*big.Int))
- have := (*uint256.Int)(&v)
- if want.Cmp(have) != 0 {
- t.Errorf("input %s: value mismatch: have %x, want %x", test.input, have, want)
- continue
- }
- }
-}
-
-func BenchmarkUnmarshalBig(b *testing.B) {
- input := []byte(`"0x123456789abcdef123456789abcdef"`)
- for i := 0; i < b.N; i++ {
- var v Big
- if err := v.UnmarshalJSON(input); err != nil {
- b.Fatal(err)
- }
- }
-}
-
-func TestMarshalBig(t *testing.T) {
- for _, test := range encodeBigTests {
- in := test.input.(*big.Int)
- out, err := json.Marshal((*Big)(in))
- if err != nil {
- t.Errorf("%d: %v", in, err)
- continue
- }
- if want := `"` + test.want + `"`; string(out) != want {
- t.Errorf("%d: MarshalJSON output mismatch: got %q, want %q", in, out, want)
- continue
- }
- if out := (*Big)(in).String(); out != test.want {
- t.Errorf("%x: String mismatch: got %q, want %q", in, out, test.want)
- continue
- }
- }
-}
-
-var unmarshalUint64Tests = []unmarshalTest{
- // invalid encoding
- {input: "", wantErr: errJSONEOF},
- {input: "null", wantErr: errNonString(uint64T)},
- {input: "10", wantErr: errNonString(uint64T)},
- {input: `"0"`, wantErr: wrapTypeError(ErrMissingPrefix, uint64T)},
- {input: `"0x"`, wantErr: wrapTypeError(ErrEmptyNumber, uint64T)},
- {input: `"0x01"`, wantErr: wrapTypeError(ErrLeadingZero, uint64T)},
- {input: `"0xfffffffffffffffff"`, wantErr: wrapTypeError(ErrUint64Range, uint64T)},
- {input: `"0xx"`, wantErr: wrapTypeError(ErrSyntax, uint64T)},
- {input: `"0x1zz01"`, wantErr: wrapTypeError(ErrSyntax, uint64T)},
-
- // valid encoding
- {input: `""`, want: uint64(0)},
- {input: `"0x0"`, want: uint64(0)},
- {input: `"0x2"`, want: uint64(0x2)},
- {input: `"0x2F2"`, want: uint64(0x2f2)},
- {input: `"0X2F2"`, want: uint64(0x2f2)},
- {input: `"0x1122aaff"`, want: uint64(0x1122aaff)},
- {input: `"0xbbb"`, want: uint64(0xbbb)},
- {input: `"0xffffffffffffffff"`, want: uint64(0xffffffffffffffff)},
-}
-
-func TestUnmarshalUint64(t *testing.T) {
- for _, test := range unmarshalUint64Tests {
- var v Uint64
- err := json.Unmarshal([]byte(test.input), &v)
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if uint64(v) != test.want.(uint64) {
- t.Errorf("input %s: value mismatch: got %d, want %d", test.input, v, test.want)
- continue
- }
- }
-}
-
-func BenchmarkUnmarshalUint64(b *testing.B) {
- input := []byte(`"0x123456789abcdf"`)
- for i := 0; i < b.N; i++ {
- var v Uint64
- v.UnmarshalJSON(input)
- }
-}
-
-func TestMarshalUint64(t *testing.T) {
- for _, test := range encodeUint64Tests {
- in := test.input.(uint64)
- out, err := json.Marshal(Uint64(in))
- if err != nil {
- t.Errorf("%d: %v", in, err)
- continue
- }
- if want := `"` + test.want + `"`; string(out) != want {
- t.Errorf("%d: MarshalJSON output mismatch: got %q, want %q", in, out, want)
- continue
- }
- if out := (Uint64)(in).String(); out != test.want {
- t.Errorf("%x: String mismatch: got %q, want %q", in, out, test.want)
- continue
- }
- }
-}
-
-func TestMarshalUint(t *testing.T) {
- for _, test := range encodeUintTests {
- in := test.input.(uint)
- out, err := json.Marshal(Uint(in))
- if err != nil {
- t.Errorf("%d: %v", in, err)
- continue
- }
- if want := `"` + test.want + `"`; string(out) != want {
- t.Errorf("%d: MarshalJSON output mismatch: got %q, want %q", in, out, want)
- continue
- }
- if out := (Uint)(in).String(); out != test.want {
- t.Errorf("%x: String mismatch: got %q, want %q", in, out, test.want)
- continue
- }
- }
-}
-
-var (
- // These are variables (not constants) to avoid constant overflow
- // checks in the compiler on 32bit platforms.
- maxUint33bits = uint64(^uint32(0)) + 1
- maxUint64bits = ^uint64(0)
-)
-
-var unmarshalUintTests = []unmarshalTest{
- // invalid encoding
- {input: "", wantErr: errJSONEOF},
- {input: "null", wantErr: errNonString(uintT)},
- {input: "10", wantErr: errNonString(uintT)},
- {input: `"0"`, wantErr: wrapTypeError(ErrMissingPrefix, uintT)},
- {input: `"0x"`, wantErr: wrapTypeError(ErrEmptyNumber, uintT)},
- {input: `"0x01"`, wantErr: wrapTypeError(ErrLeadingZero, uintT)},
- {input: `"0x100000000"`, want: uint(maxUint33bits), wantErr32bit: wrapTypeError(ErrUintRange, uintT)},
- {input: `"0xfffffffffffffffff"`, wantErr: wrapTypeError(ErrUintRange, uintT)},
- {input: `"0xx"`, wantErr: wrapTypeError(ErrSyntax, uintT)},
- {input: `"0x1zz01"`, wantErr: wrapTypeError(ErrSyntax, uintT)},
-
- // valid encoding
- {input: `""`, want: uint(0)},
- {input: `"0x0"`, want: uint(0)},
- {input: `"0x2"`, want: uint(0x2)},
- {input: `"0x2F2"`, want: uint(0x2f2)},
- {input: `"0X2F2"`, want: uint(0x2f2)},
- {input: `"0x1122aaff"`, want: uint(0x1122aaff)},
- {input: `"0xbbb"`, want: uint(0xbbb)},
- {input: `"0xffffffff"`, want: uint(0xffffffff)},
- {input: `"0xffffffffffffffff"`, want: uint(maxUint64bits), wantErr32bit: wrapTypeError(ErrUintRange, uintT)},
-}
-
-func TestUnmarshalUint(t *testing.T) {
- for _, test := range unmarshalUintTests {
- var v Uint
- err := json.Unmarshal([]byte(test.input), &v)
- if uintBits == 32 && test.wantErr32bit != nil {
- checkError(t, test.input, err, test.wantErr32bit)
- continue
- }
- if !checkError(t, test.input, err, test.wantErr) {
- continue
- }
- if uint(v) != test.want.(uint) {
- t.Errorf("input %s: value mismatch: got %d, want %d", test.input, v, test.want)
- continue
- }
- }
-}
-
-func TestUnmarshalFixedUnprefixedText(t *testing.T) {
- tests := []struct {
- input string
- want []byte
- wantErr error
- }{
- {input: "0x2", wantErr: ErrOddLength},
- {input: "2", wantErr: ErrOddLength},
- {input: "4444", wantErr: errors.New("hex string has length 4, want 8 for x")},
- {input: "4444", wantErr: errors.New("hex string has length 4, want 8 for x")},
- // check that output is not modified for partially correct input
- {input: "444444gg", wantErr: ErrSyntax, want: []byte{0, 0, 0, 0}},
- {input: "0x444444gg", wantErr: ErrSyntax, want: []byte{0, 0, 0, 0}},
- // valid inputs
- {input: "44444444", want: []byte{0x44, 0x44, 0x44, 0x44}},
- {input: "0x44444444", want: []byte{0x44, 0x44, 0x44, 0x44}},
- }
-
- for _, test := range tests {
- out := make([]byte, 4)
- err := UnmarshalFixedUnprefixedText("x", []byte(test.input), out)
- switch {
- case err == nil && test.wantErr != nil:
- t.Errorf("%q: got no error, expected %q", test.input, test.wantErr)
- case err != nil && test.wantErr == nil:
- t.Errorf("%q: unexpected error %q", test.input, err)
- case err != nil && err.Error() != test.wantErr.Error():
- t.Errorf("%q: error mismatch: got %q, want %q", test.input, err, test.wantErr)
- }
- if test.want != nil && !bytes.Equal(out, test.want) {
- t.Errorf("%q: output mismatch: got %x, want %x", test.input, out, test.want)
- }
- }
-}
diff --git a/common/lru/basiclru.go b/common/lru/basiclru.go
deleted file mode 100644
index a429157fe5..0000000000
--- a/common/lru/basiclru.go
+++ /dev/null
@@ -1,223 +0,0 @@
-// Copyright 2022 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 .
-
-// Package lru implements generically-typed LRU caches.
-package lru
-
-// BasicLRU is a simple LRU cache.
-//
-// This type is not safe for concurrent use.
-// The zero value is not valid, instances must be created using NewCache.
-type BasicLRU[K comparable, V any] struct {
- list *list[K]
- items map[K]cacheItem[K, V]
- cap int
-}
-
-type cacheItem[K any, V any] struct {
- elem *listElem[K]
- value V
-}
-
-// NewBasicLRU creates a new LRU cache.
-func NewBasicLRU[K comparable, V any](capacity int) BasicLRU[K, V] {
- if capacity <= 0 {
- capacity = 1
- }
- c := BasicLRU[K, V]{
- items: make(map[K]cacheItem[K, V]),
- list: newList[K](),
- cap: capacity,
- }
- return c
-}
-
-// Add adds a value to the cache. Returns true if an item was evicted to store the new item.
-func (c *BasicLRU[K, V]) Add(key K, value V) (evicted bool) {
- item, ok := c.items[key]
- if ok {
- // Already exists in cache.
- item.value = value
- c.items[key] = item
- c.list.moveToFront(item.elem)
- return false
- }
-
- var elem *listElem[K]
- if c.Len() >= c.cap {
- elem = c.list.removeLast()
- delete(c.items, elem.v)
- evicted = true
- } else {
- elem = new(listElem[K])
- }
-
- // Store the new item.
- // Note that, if another item was evicted, we re-use its list element here.
- elem.v = key
- c.items[key] = cacheItem[K, V]{elem, value}
- c.list.pushElem(elem)
- return evicted
-}
-
-// Contains reports whether the given key exists in the cache.
-func (c *BasicLRU[K, V]) Contains(key K) bool {
- _, ok := c.items[key]
- return ok
-}
-
-// Get retrieves a value from the cache. This marks the key as recently used.
-func (c *BasicLRU[K, V]) Get(key K) (value V, ok bool) {
- item, ok := c.items[key]
- if !ok {
- return value, false
- }
- c.list.moveToFront(item.elem)
- return item.value, true
-}
-
-// GetOldest retrieves the least-recently-used item.
-// Note that this does not update the item's recency.
-func (c *BasicLRU[K, V]) GetOldest() (key K, value V, ok bool) {
- lastElem := c.list.last()
- if lastElem == nil {
- return key, value, false
- }
- key = lastElem.v
- item := c.items[key]
- return key, item.value, true
-}
-
-// Len returns the current number of items in the cache.
-func (c *BasicLRU[K, V]) Len() int {
- return len(c.items)
-}
-
-// Peek retrieves a value from the cache, but does not mark the key as recently used.
-func (c *BasicLRU[K, V]) Peek(key K) (value V, ok bool) {
- item, ok := c.items[key]
- return item.value, ok
-}
-
-// Purge empties the cache.
-func (c *BasicLRU[K, V]) Purge() {
- c.list.init()
- for k := range c.items {
- delete(c.items, k)
- }
-}
-
-// Remove drops an item from the cache. Returns true if the key was present in cache.
-func (c *BasicLRU[K, V]) Remove(key K) bool {
- item, ok := c.items[key]
- if ok {
- delete(c.items, key)
- c.list.remove(item.elem)
- }
- return ok
-}
-
-// RemoveOldest drops the least recently used item.
-func (c *BasicLRU[K, V]) RemoveOldest() (key K, value V, ok bool) {
- lastElem := c.list.last()
- if lastElem == nil {
- return key, value, false
- }
-
- key = lastElem.v
- item := c.items[key]
- delete(c.items, key)
- c.list.remove(lastElem)
- return key, item.value, true
-}
-
-// Keys returns all keys in the cache.
-func (c *BasicLRU[K, V]) Keys() []K {
- keys := make([]K, 0, len(c.items))
- return c.list.appendTo(keys)
-}
-
-// list is a doubly-linked list holding items of type he.
-// The zero value is not valid, use newList to create lists.
-type list[T any] struct {
- root listElem[T]
-}
-
-type listElem[T any] struct {
- next *listElem[T]
- prev *listElem[T]
- v T
-}
-
-func newList[T any]() *list[T] {
- l := new(list[T])
- l.init()
- return l
-}
-
-// init reinitializes the list, making it empty.
-func (l *list[T]) init() {
- l.root.next = &l.root
- l.root.prev = &l.root
-}
-
-// push adds an element to the front of the list.
-func (l *list[T]) pushElem(e *listElem[T]) {
- e.prev = &l.root
- e.next = l.root.next
- l.root.next = e
- e.next.prev = e
-}
-
-// moveToFront makes 'node' the head of the list.
-func (l *list[T]) moveToFront(e *listElem[T]) {
- e.prev.next = e.next
- e.next.prev = e.prev
- l.pushElem(e)
-}
-
-// remove removes an element from the list.
-func (l *list[T]) remove(e *listElem[T]) {
- e.prev.next = e.next
- e.next.prev = e.prev
- e.next, e.prev = nil, nil
-}
-
-// removeLast removes the last element of the list.
-func (l *list[T]) removeLast() *listElem[T] {
- last := l.last()
- if last != nil {
- l.remove(last)
- }
- return last
-}
-
-// last returns the last element of the list, or nil if the list is empty.
-func (l *list[T]) last() *listElem[T] {
- e := l.root.prev
- if e == &l.root {
- return nil
- }
- return e
-}
-
-// appendTo appends all list elements to a slice.
-func (l *list[T]) appendTo(slice []T) []T {
- for e := l.root.prev; e != &l.root; e = e.prev {
- slice = append(slice, e.v)
- }
- return slice
-}
diff --git a/common/lru/basiclru_test.go b/common/lru/basiclru_test.go
deleted file mode 100644
index 29812bda15..0000000000
--- a/common/lru/basiclru_test.go
+++ /dev/null
@@ -1,255 +0,0 @@
-// Copyright 2022 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 .
-
-package lru
-
-import (
- crand "crypto/rand"
- "fmt"
- "io"
- "math/rand"
- "testing"
-)
-
-// Some of these test cases were adapted
-// from https://github.com/hashicorp/golang-lru/blob/master/simplelru/lru_test.go
-
-func TestBasicLRU(t *testing.T) {
- cache := NewBasicLRU[int, int](128)
-
- for i := 0; i < 256; i++ {
- cache.Add(i, i)
- }
- if cache.Len() != 128 {
- t.Fatalf("bad len: %v", cache.Len())
- }
-
- // Check that Keys returns least-recent key first.
- keys := cache.Keys()
- if len(keys) != 128 {
- t.Fatal("wrong Keys() length", len(keys))
- }
- for i, k := range keys {
- v, ok := cache.Peek(k)
- if !ok {
- t.Fatalf("expected key %d be present", i)
- }
- if v != k {
- t.Fatalf("expected %d == %d", k, v)
- }
- if v != i+128 {
- t.Fatalf("wrong value at key %d: %d, want %d", i, v, i+128)
- }
- }
-
- for i := 0; i < 128; i++ {
- _, ok := cache.Get(i)
- if ok {
- t.Fatalf("%d should be evicted", i)
- }
- }
- for i := 128; i < 256; i++ {
- _, ok := cache.Get(i)
- if !ok {
- t.Fatalf("%d should not be evicted", i)
- }
- }
-
- for i := 128; i < 192; i++ {
- ok := cache.Remove(i)
- if !ok {
- t.Fatalf("%d should be in cache", i)
- }
- ok = cache.Remove(i)
- if ok {
- t.Fatalf("%d should not be in cache", i)
- }
- _, ok = cache.Get(i)
- if ok {
- t.Fatalf("%d should be deleted", i)
- }
- }
-
- // Request item 192.
- cache.Get(192)
- // It should be the last item returned by Keys().
- for i, k := range cache.Keys() {
- if (i < 63 && k != i+193) || (i == 63 && k != 192) {
- t.Fatalf("out of order key: %v", k)
- }
- }
-
- cache.Purge()
- if cache.Len() != 0 {
- t.Fatalf("bad len: %v", cache.Len())
- }
- if _, ok := cache.Get(200); ok {
- t.Fatalf("should contain nothing")
- }
-}
-
-func TestBasicLRUAddExistingKey(t *testing.T) {
- cache := NewBasicLRU[int, int](1)
-
- cache.Add(1, 1)
- cache.Add(1, 2)
-
- v, _ := cache.Get(1)
- if v != 2 {
- t.Fatal("wrong value:", v)
- }
-}
-
-// This test checks GetOldest and RemoveOldest.
-func TestBasicLRUGetOldest(t *testing.T) {
- cache := NewBasicLRU[int, int](128)
- for i := 0; i < 256; i++ {
- cache.Add(i, i)
- }
-
- k, _, ok := cache.GetOldest()
- if !ok {
- t.Fatalf("missing")
- }
- if k != 128 {
- t.Fatalf("bad: %v", k)
- }
-
- k, _, ok = cache.RemoveOldest()
- if !ok {
- t.Fatalf("missing")
- }
- if k != 128 {
- t.Fatalf("bad: %v", k)
- }
-
- k, _, ok = cache.RemoveOldest()
- if !ok {
- t.Fatalf("missing oldest item")
- }
- if k != 129 {
- t.Fatalf("wrong oldest item: %v", k)
- }
-}
-
-// Test that Add returns true/false if an eviction occurred
-func TestBasicLRUAddReturnValue(t *testing.T) {
- cache := NewBasicLRU[int, int](1)
- if cache.Add(1, 1) {
- t.Errorf("first add shouldn't have evicted")
- }
- if !cache.Add(2, 2) {
- t.Errorf("second add should have evicted")
- }
-}
-
-// This test verifies that Contains doesn't change item recency.
-func TestBasicLRUContains(t *testing.T) {
- cache := NewBasicLRU[int, int](2)
- cache.Add(1, 1)
- cache.Add(2, 2)
- if !cache.Contains(1) {
- t.Errorf("1 should be in the cache")
- }
- cache.Add(3, 3)
- if cache.Contains(1) {
- t.Errorf("Contains should not have updated recency of 1")
- }
-}
-
-// Test that Peek doesn't update recent-ness
-func TestBasicLRUPeek(t *testing.T) {
- cache := NewBasicLRU[int, int](2)
- cache.Add(1, 1)
- cache.Add(2, 2)
- if v, ok := cache.Peek(1); !ok || v != 1 {
- t.Errorf("1 should be set to 1")
- }
- cache.Add(3, 3)
- if cache.Contains(1) {
- t.Errorf("should not have updated recent-ness of 1")
- }
-}
-
-func BenchmarkLRU(b *testing.B) {
- var (
- capacity = 1000
- indexes = make([]int, capacity*20)
- keys = make([]string, capacity)
- values = make([][]byte, capacity)
- )
- for i := range indexes {
- indexes[i] = rand.Intn(capacity)
- }
- for i := range keys {
- b := make([]byte, 32)
- crand.Read(b)
- keys[i] = string(b)
- crand.Read(b)
- values[i] = b
- }
-
- var sink []byte
-
- b.Run("Add/BasicLRU", func(b *testing.B) {
- cache := NewBasicLRU[int, int](capacity)
- for i := 0; i < b.N; i++ {
- cache.Add(i, i)
- }
- })
- b.Run("Get/BasicLRU", func(b *testing.B) {
- cache := NewBasicLRU[string, []byte](capacity)
- for i := 0; i < capacity; i++ {
- index := indexes[i]
- cache.Add(keys[index], values[index])
- }
-
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- k := keys[indexes[i%len(indexes)]]
- v, ok := cache.Get(k)
- if ok {
- sink = v
- }
- }
- })
-
- // // vs. github.com/hashicorp/golang-lru/simplelru
- // b.Run("Add/simplelru.LRU", func(b *testing.B) {
- // cache, _ := simplelru.NewLRU(capacity, nil)
- // for i := 0; i < b.N; i++ {
- // cache.Add(i, i)
- // }
- // })
- // b.Run("Get/simplelru.LRU", func(b *testing.B) {
- // cache, _ := simplelru.NewLRU(capacity, nil)
- // for i := 0; i < capacity; i++ {
- // index := indexes[i]
- // cache.Add(keys[index], values[index])
- // }
- //
- // b.ResetTimer()
- // for i := 0; i < b.N; i++ {
- // k := keys[indexes[i%len(indexes)]]
- // v, ok := cache.Get(k)
- // if ok {
- // sink = v.([]byte)
- // }
- // }
- // })
-
- fmt.Fprintln(io.Discard, sink)
-}
diff --git a/common/lru/blob_lru.go b/common/lru/blob_lru.go
deleted file mode 100644
index c9b3398503..0000000000
--- a/common/lru/blob_lru.go
+++ /dev/null
@@ -1,84 +0,0 @@
-// Copyright 2022 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 .
-
-package lru
-
-import (
- "math"
- "sync"
-)
-
-// blobType is the type constraint for values stored in SizeConstrainedCache.
-type blobType interface {
- ~[]byte | ~string
-}
-
-// SizeConstrainedCache is a cache where capacity is in bytes (instead of item count). When the cache
-// is at capacity, and a new item is added, older items are evicted until the size
-// constraint is met.
-//
-// OBS: This cache assumes that items are content-addressed: keys are unique per content.
-// In other words: two Add(..) with the same key K, will always have the same value V.
-type SizeConstrainedCache[K comparable, V blobType] struct {
- size uint64
- maxSize uint64
- lru BasicLRU[K, V]
- lock sync.Mutex
-}
-
-// NewSizeConstrainedCache creates a new size-constrained LRU cache.
-func NewSizeConstrainedCache[K comparable, V blobType](maxSize uint64) *SizeConstrainedCache[K, V] {
- return &SizeConstrainedCache[K, V]{
- size: 0,
- maxSize: maxSize,
- lru: NewBasicLRU[K, V](math.MaxInt),
- }
-}
-
-// Add adds a value to the cache. Returns true if an eviction occurred.
-// OBS: This cache assumes that items are content-addressed: keys are unique per content.
-// In other words: two Add(..) with the same key K, will always have the same value V.
-// OBS: The value is _not_ copied on Add, so the caller must not modify it afterwards.
-func (c *SizeConstrainedCache[K, V]) Add(key K, value V) (evicted bool) {
- c.lock.Lock()
- defer c.lock.Unlock()
-
- // Unless it is already present, might need to evict something.
- // OBS: If it is present, we still call Add internally to bump the recentness.
- if !c.lru.Contains(key) {
- targetSize := c.size + uint64(len(value))
- for targetSize > c.maxSize {
- evicted = true
- _, v, ok := c.lru.RemoveOldest()
- if !ok {
- // list is now empty. Break
- break
- }
- targetSize -= uint64(len(v))
- }
- c.size = targetSize
- }
- c.lru.Add(key, value)
- return evicted
-}
-
-// Get looks up a key's value from the cache.
-func (c *SizeConstrainedCache[K, V]) Get(key K) (V, bool) {
- c.lock.Lock()
- defer c.lock.Unlock()
-
- return c.lru.Get(key)
-}
diff --git a/common/lru/blob_lru_test.go b/common/lru/blob_lru_test.go
deleted file mode 100644
index ca1b0ddd74..0000000000
--- a/common/lru/blob_lru_test.go
+++ /dev/null
@@ -1,155 +0,0 @@
-// Copyright 2022 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 .
-
-package lru
-
-import (
- "encoding/binary"
- "fmt"
- "testing"
-)
-
-type testKey [8]byte
-
-func mkKey(i int) (key testKey) {
- binary.LittleEndian.PutUint64(key[:], uint64(i))
- return key
-}
-
-func TestSizeConstrainedCache(t *testing.T) {
- lru := NewSizeConstrainedCache[testKey, []byte](100)
- var want uint64
- // Add 11 items of 10 byte each. First item should be swapped out
- for i := 0; i < 11; i++ {
- k := mkKey(i)
- v := fmt.Sprintf("value-%04d", i)
- lru.Add(k, []byte(v))
- want += uint64(len(v))
- if want > 100 {
- want = 100
- }
- if have := lru.size; have != want {
- t.Fatalf("size wrong, have %d want %d", have, want)
- }
- }
- // Zero:th should be evicted
- {
- k := mkKey(0)
- if _, ok := lru.Get(k); ok {
- t.Fatalf("should be evicted: %v", k)
- }
- }
- // Elems 1-11 should be present
- for i := 1; i < 11; i++ {
- k := mkKey(i)
- want := fmt.Sprintf("value-%04d", i)
- have, ok := lru.Get(k)
- if !ok {
- t.Fatalf("missing key %v", k)
- }
- if string(have) != want {
- t.Fatalf("wrong value, have %v want %v", have, want)
- }
- }
-}
-
-// This test adds inserting an element exceeding the max size.
-func TestSizeConstrainedCacheOverflow(t *testing.T) {
- lru := NewSizeConstrainedCache[testKey, []byte](100)
-
- // Add 10 items of 10 byte each, filling the cache
- for i := 0; i < 10; i++ {
- k := mkKey(i)
- v := fmt.Sprintf("value-%04d", i)
- lru.Add(k, []byte(v))
- }
- // Add one single large elem. We expect it to swap out all entries.
- {
- k := mkKey(1337)
- v := make([]byte, 200)
- lru.Add(k, v)
- }
- // Elems 0-9 should be missing
- for i := 1; i < 10; i++ {
- k := mkKey(i)
- if _, ok := lru.Get(k); ok {
- t.Fatalf("should be evicted: %v", k)
- }
- }
- // The size should be accurate
- if have, want := lru.size, uint64(200); have != want {
- t.Fatalf("size wrong, have %d want %d", have, want)
- }
- // Adding one small item should swap out the large one
- {
- i := 0
- k := mkKey(i)
- v := fmt.Sprintf("value-%04d", i)
- lru.Add(k, []byte(v))
- if have, want := lru.size, uint64(10); have != want {
- t.Fatalf("size wrong, have %d want %d", have, want)
- }
- }
-}
-
-// This checks what happens when inserting the same k/v multiple times.
-func TestSizeConstrainedCacheSameItem(t *testing.T) {
- lru := NewSizeConstrainedCache[testKey, []byte](100)
-
- // Add one 10 byte-item 10 times.
- k := mkKey(0)
- v := fmt.Sprintf("value-%04d", 0)
- for i := 0; i < 10; i++ {
- lru.Add(k, []byte(v))
- }
-
- // The size should be accurate.
- if have, want := lru.size, uint64(10); have != want {
- t.Fatalf("size wrong, have %d want %d", have, want)
- }
-}
-
-// This tests that empty/nil values are handled correctly.
-func TestSizeConstrainedCacheEmpties(t *testing.T) {
- lru := NewSizeConstrainedCache[testKey, []byte](100)
-
- // This test abuses the lru a bit, using different keys for identical value(s).
- for i := 0; i < 10; i++ {
- lru.Add(testKey{byte(i)}, []byte{})
- lru.Add(testKey{byte(255 - i)}, nil)
- }
-
- // The size should not count, only the values count. So this could be a DoS
- // since it basically has no cap, and it is intentionally overloaded with
- // different-keyed 0-length values.
- if have, want := lru.size, uint64(0); have != want {
- t.Fatalf("size wrong, have %d want %d", have, want)
- }
-
- for i := 0; i < 10; i++ {
- if v, ok := lru.Get(testKey{byte(i)}); !ok {
- t.Fatalf("test %d: expected presence", i)
- } else if v == nil {
- t.Fatalf("test %d, v is nil", i)
- }
-
- if v, ok := lru.Get(testKey{byte(255 - i)}); !ok {
- t.Fatalf("test %d: expected presence", i)
- } else if v != nil {
- t.Fatalf("test %d, v is not nil", i)
- }
- }
-}
diff --git a/common/lru/lru.go b/common/lru/lru.go
deleted file mode 100644
index 45965adb0d..0000000000
--- a/common/lru/lru.go
+++ /dev/null
@@ -1,95 +0,0 @@
-// Copyright 2022 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 .
-
-package lru
-
-import "sync"
-
-// Cache is a LRU cache.
-// This type is safe for concurrent use.
-type Cache[K comparable, V any] struct {
- cache BasicLRU[K, V]
- mu sync.Mutex
-}
-
-// NewCache creates an LRU cache.
-func NewCache[K comparable, V any](capacity int) *Cache[K, V] {
- return &Cache[K, V]{cache: NewBasicLRU[K, V](capacity)}
-}
-
-// Add adds a value to the cache. Returns true if an item was evicted to store the new item.
-func (c *Cache[K, V]) Add(key K, value V) (evicted bool) {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- return c.cache.Add(key, value)
-}
-
-// Contains reports whether the given key exists in the cache.
-func (c *Cache[K, V]) Contains(key K) bool {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- return c.cache.Contains(key)
-}
-
-// Get retrieves a value from the cache. This marks the key as recently used.
-func (c *Cache[K, V]) Get(key K) (value V, ok bool) {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- return c.cache.Get(key)
-}
-
-// Len returns the current number of items in the cache.
-func (c *Cache[K, V]) Len() int {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- return c.cache.Len()
-}
-
-// Peek retrieves a value from the cache, but does not mark the key as recently used.
-func (c *Cache[K, V]) Peek(key K) (value V, ok bool) {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- return c.cache.Peek(key)
-}
-
-// Purge empties the cache.
-func (c *Cache[K, V]) Purge() {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- c.cache.Purge()
-}
-
-// Remove drops an item from the cache. Returns true if the key was present in cache.
-func (c *Cache[K, V]) Remove(key K) bool {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- return c.cache.Remove(key)
-}
-
-// Keys returns all keys of items currently in the LRU.
-func (c *Cache[K, V]) Keys() []K {
- c.mu.Lock()
- defer c.mu.Unlock()
-
- return c.cache.Keys()
-}
diff --git a/common/math/big.go b/common/math/big.go
deleted file mode 100644
index 013c0ba4b6..0000000000
--- a/common/math/big.go
+++ /dev/null
@@ -1,270 +0,0 @@
-// Copyright 2017 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 .
-
-// Package math provides integer math utilities.
-package math
-
-import (
- "fmt"
- "math/big"
-)
-
-// Various big integer limit values.
-var (
- tt255 = BigPow(2, 255)
- tt256 = BigPow(2, 256)
- tt256m1 = new(big.Int).Sub(tt256, big.NewInt(1))
- tt63 = BigPow(2, 63)
- MaxBig256 = new(big.Int).Set(tt256m1)
- MaxBig63 = new(big.Int).Sub(tt63, big.NewInt(1))
-)
-
-const (
- // number of bits in a big.Word
- wordBits = 32 << (uint64(^big.Word(0)) >> 63)
- // number of bytes in a big.Word
- wordBytes = wordBits / 8
-)
-
-// HexOrDecimal256 marshals big.Int as hex or decimal.
-type HexOrDecimal256 big.Int
-
-// NewHexOrDecimal256 creates a new HexOrDecimal256
-func NewHexOrDecimal256(x int64) *HexOrDecimal256 {
- b := big.NewInt(x)
- h := HexOrDecimal256(*b)
- return &h
-}
-
-// UnmarshalJSON implements json.Unmarshaler.
-//
-// It is similar to UnmarshalText, but allows parsing real decimals too, not just
-// quoted decimal strings.
-func (i *HexOrDecimal256) UnmarshalJSON(input []byte) error {
- if len(input) > 0 && input[0] == '"' {
- input = input[1 : len(input)-1]
- }
- return i.UnmarshalText(input)
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler.
-func (i *HexOrDecimal256) UnmarshalText(input []byte) error {
- bigint, ok := ParseBig256(string(input))
- if !ok {
- return fmt.Errorf("invalid hex or decimal integer %q", input)
- }
- *i = HexOrDecimal256(*bigint)
- return nil
-}
-
-// MarshalText implements encoding.TextMarshaler.
-func (i *HexOrDecimal256) MarshalText() ([]byte, error) {
- if i == nil {
- return []byte("0x0"), nil
- }
- return []byte(fmt.Sprintf("%#x", (*big.Int)(i))), nil
-}
-
-// Decimal256 unmarshals big.Int as a decimal string. When unmarshalling,
-// it however accepts either "0x"-prefixed (hex encoded) or non-prefixed (decimal)
-type Decimal256 big.Int
-
-// NewDecimal256 creates a new Decimal256
-func NewDecimal256(x int64) *Decimal256 {
- b := big.NewInt(x)
- d := Decimal256(*b)
- return &d
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler.
-func (i *Decimal256) UnmarshalText(input []byte) error {
- bigint, ok := ParseBig256(string(input))
- if !ok {
- return fmt.Errorf("invalid hex or decimal integer %q", input)
- }
- *i = Decimal256(*bigint)
- return nil
-}
-
-// MarshalText implements encoding.TextMarshaler.
-func (i *Decimal256) MarshalText() ([]byte, error) {
- return []byte(i.String()), nil
-}
-
-// String implements Stringer.
-func (i *Decimal256) String() string {
- if i == nil {
- return "0"
- }
- return fmt.Sprintf("%#d", (*big.Int)(i))
-}
-
-// ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax.
-// Leading zeros are accepted. The empty string parses as zero.
-func ParseBig256(s string) (*big.Int, bool) {
- if s == "" {
- return new(big.Int), true
- }
- var bigint *big.Int
- var ok bool
- if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
- bigint, ok = new(big.Int).SetString(s[2:], 16)
- } else {
- bigint, ok = new(big.Int).SetString(s, 10)
- }
- if ok && bigint.BitLen() > 256 {
- bigint, ok = nil, false
- }
- return bigint, ok
-}
-
-// MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.
-func MustParseBig256(s string) *big.Int {
- v, ok := ParseBig256(s)
- if !ok {
- panic("invalid 256 bit integer: " + s)
- }
- return v
-}
-
-// BigPow returns a ** b as a big integer.
-func BigPow(a, b int64) *big.Int {
- r := big.NewInt(a)
- return r.Exp(r, big.NewInt(b), nil)
-}
-
-// BigMax returns the larger of x or y.
-func BigMax(x, y *big.Int) *big.Int {
- if x.Cmp(y) < 0 {
- return y
- }
- return x
-}
-
-// BigMin returns the smaller of x or y.
-func BigMin(x, y *big.Int) *big.Int {
- if x.Cmp(y) > 0 {
- return y
- }
- return x
-}
-
-// FirstBitSet returns the index of the first 1 bit in v, counting from LSB.
-func FirstBitSet(v *big.Int) int {
- for i := 0; i < v.BitLen(); i++ {
- if v.Bit(i) > 0 {
- return i
- }
- }
- return v.BitLen()
-}
-
-// PaddedBigBytes encodes a big integer as a big-endian byte slice. The length
-// of the slice is at least n bytes.
-func PaddedBigBytes(bigint *big.Int, n int) []byte {
- if bigint.BitLen()/8 >= n {
- return bigint.Bytes()
- }
- ret := make([]byte, n)
- ReadBits(bigint, ret)
- return ret
-}
-
-// bigEndianByteAt returns the byte at position n,
-// in Big-Endian encoding
-// So n==0 returns the least significant byte
-func bigEndianByteAt(bigint *big.Int, n int) byte {
- words := bigint.Bits()
- // Check word-bucket the byte will reside in
- i := n / wordBytes
- if i >= len(words) {
- return byte(0)
- }
- word := words[i]
- // Offset of the byte
- shift := 8 * uint(n%wordBytes)
-
- return byte(word >> shift)
-}
-
-// Byte returns the byte at position n,
-// with the supplied padlength in Little-Endian encoding.
-// n==0 returns the MSB
-// Example: bigint '5', padlength 32, n=31 => 5
-func Byte(bigint *big.Int, padlength, n int) byte {
- if n >= padlength {
- return byte(0)
- }
- return bigEndianByteAt(bigint, padlength-1-n)
-}
-
-// ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure
-// that buf has enough space. If buf is too short the result will be incomplete.
-func ReadBits(bigint *big.Int, buf []byte) {
- i := len(buf)
- for _, d := range bigint.Bits() {
- for j := 0; j < wordBytes && i > 0; j++ {
- i--
- buf[i] = byte(d)
- d >>= 8
- }
- }
-}
-
-// U256 encodes as a 256 bit two's complement number. This operation is destructive.
-func U256(x *big.Int) *big.Int {
- return x.And(x, tt256m1)
-}
-
-// U256Bytes converts a big Int into a 256bit EVM number.
-// This operation is destructive.
-func U256Bytes(n *big.Int) []byte {
- return PaddedBigBytes(U256(n), 32)
-}
-
-// S256 interprets x as a two's complement number.
-// x must not exceed 256 bits (the result is undefined if it does) and is not modified.
-//
-// S256(0) = 0
-// S256(1) = 1
-// S256(2**255) = -2**255
-// S256(2**256-1) = -1
-func S256(x *big.Int) *big.Int {
- if x.Cmp(tt255) < 0 {
- return x
- }
- return new(big.Int).Sub(x, tt256)
-}
-
-// Exp implements exponentiation by squaring.
-// Exp returns a newly-allocated big integer and does not change
-// base or exponent. The result is truncated to 256 bits.
-//
-// Courtesy @karalabe and @chfast
-func Exp(base, exponent *big.Int) *big.Int {
- result := big.NewInt(1)
-
- for _, word := range exponent.Bits() {
- for i := 0; i < wordBits; i++ {
- if word&1 == 1 {
- U256(result.Mul(result, base))
- }
- U256(base.Mul(base, base))
- word >>= 1
- }
- }
- return result
-}
diff --git a/common/math/big_test.go b/common/math/big_test.go
deleted file mode 100644
index 803b5e1cc6..0000000000
--- a/common/math/big_test.go
+++ /dev/null
@@ -1,324 +0,0 @@
-// Copyright 2017 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 .
-
-package math
-
-import (
- "bytes"
- "encoding/hex"
- "math/big"
- "testing"
-
- "github.com/ethereum/go-ethereum/common"
-)
-
-func TestHexOrDecimal256(t *testing.T) {
- tests := []struct {
- input string
- num *big.Int
- ok bool
- }{
- {"", big.NewInt(0), true},
- {"0", big.NewInt(0), true},
- {"0x0", big.NewInt(0), true},
- {"12345678", big.NewInt(12345678), true},
- {"0x12345678", big.NewInt(0x12345678), true},
- {"0X12345678", big.NewInt(0x12345678), true},
- // Tests for leading zero behaviour:
- {"0123456789", big.NewInt(123456789), true}, // note: not octal
- {"00", big.NewInt(0), true},
- {"0x00", big.NewInt(0), true},
- {"0x012345678abc", big.NewInt(0x12345678abc), true},
- // Invalid syntax:
- {"abcdef", nil, false},
- {"0xgg", nil, false},
- // Larger than 256 bits:
- {"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false},
- }
- for _, test := range tests {
- var num HexOrDecimal256
- err := num.UnmarshalText([]byte(test.input))
- if (err == nil) != test.ok {
- t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok)
- continue
- }
- if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 {
- t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num)
- }
- }
-}
-
-func TestMustParseBig256(t *testing.T) {
- defer func() {
- if recover() == nil {
- t.Error("MustParseBig should've panicked")
- }
- }()
- MustParseBig256("ggg")
-}
-
-func TestBigMax(t *testing.T) {
- a := big.NewInt(10)
- b := big.NewInt(5)
-
- max1 := BigMax(a, b)
- if max1 != a {
- t.Errorf("Expected %d got %d", a, max1)
- }
-
- max2 := BigMax(b, a)
- if max2 != a {
- t.Errorf("Expected %d got %d", a, max2)
- }
-}
-
-func TestBigMin(t *testing.T) {
- a := big.NewInt(10)
- b := big.NewInt(5)
-
- min1 := BigMin(a, b)
- if min1 != b {
- t.Errorf("Expected %d got %d", b, min1)
- }
-
- min2 := BigMin(b, a)
- if min2 != b {
- t.Errorf("Expected %d got %d", b, min2)
- }
-}
-
-func TestFirstBigSet(t *testing.T) {
- tests := []struct {
- num *big.Int
- ix int
- }{
- {big.NewInt(0), 0},
- {big.NewInt(1), 0},
- {big.NewInt(2), 1},
- {big.NewInt(0x100), 8},
- }
- for _, test := range tests {
- if ix := FirstBitSet(test.num); ix != test.ix {
- t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
- }
- }
-}
-
-func TestPaddedBigBytes(t *testing.T) {
- tests := []struct {
- num *big.Int
- n int
- result []byte
- }{
- {num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
- {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
- {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
- {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
- }
- for _, test := range tests {
- if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
- t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
- }
- }
-}
-
-func BenchmarkPaddedBigBytesLargePadding(b *testing.B) {
- bigint := MustParseBig256("123456789123456789123456789123456789")
- for i := 0; i < b.N; i++ {
- PaddedBigBytes(bigint, 200)
- }
-}
-
-func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) {
- bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
- for i := 0; i < b.N; i++ {
- PaddedBigBytes(bigint, 5)
- }
-}
-
-func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) {
- bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
- for i := 0; i < b.N; i++ {
- PaddedBigBytes(bigint, 32)
- }
-}
-
-func BenchmarkByteAtBrandNew(b *testing.B) {
- bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
- for i := 0; i < b.N; i++ {
- bigEndianByteAt(bigint, 15)
- }
-}
-
-func BenchmarkByteAt(b *testing.B) {
- bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
- for i := 0; i < b.N; i++ {
- bigEndianByteAt(bigint, 15)
- }
-}
-
-func BenchmarkByteAtOld(b *testing.B) {
- bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
- for i := 0; i < b.N; i++ {
- PaddedBigBytes(bigint, 32)
- }
-}
-
-func TestReadBits(t *testing.T) {
- check := func(input string) {
- want, _ := hex.DecodeString(input)
- int, _ := new(big.Int).SetString(input, 16)
- buf := make([]byte, len(want))
- ReadBits(int, buf)
- if !bytes.Equal(buf, want) {
- t.Errorf("have: %x\nwant: %x", buf, want)
- }
- }
- check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
- check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
- check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
-}
-
-func TestU256(t *testing.T) {
- tests := []struct{ x, y *big.Int }{
- {x: big.NewInt(0), y: big.NewInt(0)},
- {x: big.NewInt(1), y: big.NewInt(1)},
- {x: BigPow(2, 255), y: BigPow(2, 255)},
- {x: BigPow(2, 256), y: big.NewInt(0)},
- {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
- // negative values
- {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
- {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
- {x: BigPow(2, -255), y: big.NewInt(1)},
- }
- for _, test := range tests {
- if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
- t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
- }
- }
-}
-
-func TestU256Bytes(t *testing.T) {
- ubytes := make([]byte, 32)
- ubytes[31] = 1
-
- unsigned := U256Bytes(big.NewInt(1))
- if !bytes.Equal(unsigned, ubytes) {
- t.Errorf("expected %x got %x", ubytes, unsigned)
- }
-}
-
-func TestBigEndianByteAt(t *testing.T) {
- tests := []struct {
- x string
- y int
- exp byte
- }{
- {"00", 0, 0x00},
- {"01", 1, 0x00},
- {"00", 1, 0x00},
- {"01", 0, 0x01},
- {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30},
- {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20},
- {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB},
- {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
- {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00},
- }
- for _, test := range tests {
- v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
- actual := bigEndianByteAt(v, test.y)
- if actual != test.exp {
- t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
- }
- }
-}
-func TestLittleEndianByteAt(t *testing.T) {
- tests := []struct {
- x string
- y int
- exp byte
- }{
- {"00", 0, 0x00},
- {"01", 1, 0x00},
- {"00", 1, 0x00},
- {"01", 0, 0x00},
- {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00},
- {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00},
- {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00},
- {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
- {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB},
- {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD},
- {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00},
- {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD},
- {"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30},
- {"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20},
- {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0},
- {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF},
- {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0},
- }
- for _, test := range tests {
- v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
- actual := Byte(v, 32, test.y)
- if actual != test.exp {
- t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
- }
- }
-}
-
-func TestS256(t *testing.T) {
- tests := []struct{ x, y *big.Int }{
- {x: big.NewInt(0), y: big.NewInt(0)},
- {x: big.NewInt(1), y: big.NewInt(1)},
- {x: big.NewInt(2), y: big.NewInt(2)},
- {
- x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
- y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
- },
- {
- x: BigPow(2, 255),
- y: new(big.Int).Neg(BigPow(2, 255)),
- },
- {
- x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
- y: big.NewInt(-1),
- },
- {
- x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
- y: big.NewInt(-2),
- },
- }
- for _, test := range tests {
- if y := S256(test.x); y.Cmp(test.y) != 0 {
- t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
- }
- }
-}
-
-func TestExp(t *testing.T) {
- tests := []struct{ base, exponent, result *big.Int }{
- {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
- {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
- {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
- {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
- {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
- {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
- }
- for _, test := range tests {
- if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
- t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
- }
- }
-}
diff --git a/common/math/integer.go b/common/math/integer.go
deleted file mode 100644
index da01c0a08e..0000000000
--- a/common/math/integer.go
+++ /dev/null
@@ -1,109 +0,0 @@
-// Copyright 2017 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 .
-
-package math
-
-import (
- "fmt"
- "math/bits"
- "strconv"
-)
-
-// Integer limit values.
-const (
- MaxInt8 = 1<<7 - 1
- MinInt8 = -1 << 7
- MaxInt16 = 1<<15 - 1
- MinInt16 = -1 << 15
- MaxInt32 = 1<<31 - 1
- MinInt32 = -1 << 31
- MaxInt64 = 1<<63 - 1
- MinInt64 = -1 << 63
- MaxUint8 = 1<<8 - 1
- MaxUint16 = 1<<16 - 1
- MaxUint32 = 1<<32 - 1
- MaxUint64 = 1<<64 - 1
-)
-
-// HexOrDecimal64 marshals uint64 as hex or decimal.
-type HexOrDecimal64 uint64
-
-// UnmarshalJSON implements json.Unmarshaler.
-//
-// It is similar to UnmarshalText, but allows parsing real decimals too, not just
-// quoted decimal strings.
-func (i *HexOrDecimal64) UnmarshalJSON(input []byte) error {
- if len(input) > 0 && input[0] == '"' {
- input = input[1 : len(input)-1]
- }
- return i.UnmarshalText(input)
-}
-
-// UnmarshalText implements encoding.TextUnmarshaler.
-func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
- int, ok := ParseUint64(string(input))
- if !ok {
- return fmt.Errorf("invalid hex or decimal integer %q", input)
- }
- *i = HexOrDecimal64(int)
- return nil
-}
-
-// MarshalText implements encoding.TextMarshaler.
-func (i HexOrDecimal64) MarshalText() ([]byte, error) {
- return []byte(fmt.Sprintf("%#x", uint64(i))), nil
-}
-
-// ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
-// Leading zeros are accepted. The empty string parses as zero.
-func ParseUint64(s string) (uint64, bool) {
- if s == "" {
- return 0, true
- }
- if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
- v, err := strconv.ParseUint(s[2:], 16, 64)
- return v, err == nil
- }
- v, err := strconv.ParseUint(s, 10, 64)
- return v, err == nil
-}
-
-// MustParseUint64 parses s as an integer and panics if the string is invalid.
-func MustParseUint64(s string) uint64 {
- v, ok := ParseUint64(s)
- if !ok {
- panic("invalid unsigned 64 bit integer: " + s)
- }
- return v
-}
-
-// SafeSub returns x-y and checks for overflow.
-func SafeSub(x, y uint64) (uint64, bool) {
- diff, borrowOut := bits.Sub64(x, y, 0)
- return diff, borrowOut != 0
-}
-
-// SafeAdd returns x+y and checks for overflow.
-func SafeAdd(x, y uint64) (uint64, bool) {
- sum, carryOut := bits.Add64(x, y, 0)
- return sum, carryOut != 0
-}
-
-// SafeMul returns x*y and checks for overflow.
-func SafeMul(x, y uint64) (uint64, bool) {
- hi, lo := bits.Mul64(x, y)
- return lo, hi != 0
-}
diff --git a/common/math/integer_test.go b/common/math/integer_test.go
deleted file mode 100644
index b31c7c26c2..0000000000
--- a/common/math/integer_test.go
+++ /dev/null
@@ -1,116 +0,0 @@
-// Copyright 2017 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 .
-
-package math
-
-import (
- "testing"
-)
-
-type operation byte
-
-const (
- sub operation = iota
- add
- mul
-)
-
-func TestOverflow(t *testing.T) {
- for i, test := range []struct {
- x uint64
- y uint64
- overflow bool
- op operation
- }{
- // add operations
- {MaxUint64, 1, true, add},
- {MaxUint64 - 1, 1, false, add},
-
- // sub operations
- {0, 1, true, sub},
- {0, 0, false, sub},
-
- // mul operations
- {0, 0, false, mul},
- {10, 10, false, mul},
- {MaxUint64, 2, true, mul},
- {MaxUint64, 1, false, mul},
- } {
- var overflows bool
- switch test.op {
- case sub:
- _, overflows = SafeSub(test.x, test.y)
- case add:
- _, overflows = SafeAdd(test.x, test.y)
- case mul:
- _, overflows = SafeMul(test.x, test.y)
- }
-
- if test.overflow != overflows {
- t.Errorf("%d failed. Expected test to be %v, got %v", i, test.overflow, overflows)
- }
- }
-}
-
-func TestHexOrDecimal64(t *testing.T) {
- tests := []struct {
- input string
- num uint64
- ok bool
- }{
- {"", 0, true},
- {"0", 0, true},
- {"0x0", 0, true},
- {"12345678", 12345678, true},
- {"0x12345678", 0x12345678, true},
- {"0X12345678", 0x12345678, true},
- // Tests for leading zero behaviour:
- {"0123456789", 123456789, true}, // note: not octal
- {"0x00", 0, true},
- {"0x012345678abc", 0x12345678abc, true},
- // Invalid syntax:
- {"abcdef", 0, false},
- {"0xgg", 0, false},
- // Doesn't fit into 64 bits:
- {"18446744073709551617", 0, false},
- }
- for _, test := range tests {
- var num HexOrDecimal64
- err := num.UnmarshalText([]byte(test.input))
- if (err == nil) != test.ok {
- t.Errorf("ParseUint64(%q) -> (err == nil) = %t, want %t", test.input, err == nil, test.ok)
- continue
- }
- if err == nil && uint64(num) != test.num {
- t.Errorf("ParseUint64(%q) -> %d, want %d", test.input, num, test.num)
- }
- }
-}
-
-func TestMustParseUint64(t *testing.T) {
- if v := MustParseUint64("12345"); v != 12345 {
- t.Errorf(`MustParseUint64("12345") = %d, want 12345`, v)
- }
-}
-
-func TestMustParseUint64Panic(t *testing.T) {
- defer func() {
- if recover() == nil {
- t.Error("MustParseBig should've panicked")
- }
- }()
- MustParseUint64("ggg")
-}
diff --git a/common/mclock/alarm.go b/common/mclock/alarm.go
deleted file mode 100644
index e83810a6a0..0000000000
--- a/common/mclock/alarm.go
+++ /dev/null
@@ -1,106 +0,0 @@
-// Copyright 2022 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 .
-
-package mclock
-
-import (
- "time"
-)
-
-// Alarm sends timed notifications on a channel. This is very similar to a regular timer,
-// but is easier to use in code that needs to re-schedule the same timer over and over.
-//
-// When scheduling an Alarm, the channel returned by C() will receive a value no later
-// than the scheduled time. An Alarm can be reused after it has fired and can also be
-// canceled by calling Stop.
-type Alarm struct {
- ch chan struct{}
- clock Clock
- timer Timer
- deadline AbsTime
-}
-
-// NewAlarm creates an Alarm.
-func NewAlarm(clock Clock) *Alarm {
- if clock == nil {
- panic("nil clock")
- }
- return &Alarm{
- ch: make(chan struct{}, 1),
- clock: clock,
- }
-}
-
-// C returns the alarm notification channel. This channel remains identical for
-// the entire lifetime of the alarm, and is never closed.
-func (e *Alarm) C() <-chan struct{} {
- return e.ch
-}
-
-// Stop cancels the alarm and drains the channel.
-// This method is not safe for concurrent use.
-func (e *Alarm) Stop() {
- // Clear timer.
- if e.timer != nil {
- e.timer.Stop()
- }
- e.deadline = 0
-
- // Drain the channel.
- select {
- case <-e.ch:
- default:
- }
-}
-
-// Schedule sets the alarm to fire no later than the given time. If the alarm was already
-// scheduled but has not fired yet, it may fire earlier than the newly-scheduled time.
-func (e *Alarm) Schedule(time AbsTime) {
- now := e.clock.Now()
- e.schedule(now, time)
-}
-
-func (e *Alarm) schedule(now, newDeadline AbsTime) {
- if e.timer != nil {
- if e.deadline > now && e.deadline <= newDeadline {
- // Here, the current timer can be reused because it is already scheduled to
- // occur earlier than the new deadline.
- //
- // The e.deadline > now part of the condition is important. If the old
- // deadline lies in the past, we assume the timer has already fired and needs
- // to be rescheduled.
- return
- }
- e.timer.Stop()
- }
-
- // Set the timer.
- d := time.Duration(0)
- if newDeadline < now {
- newDeadline = now
- } else {
- d = newDeadline.Sub(now)
- }
- e.timer = e.clock.AfterFunc(d, e.send)
- e.deadline = newDeadline
-}
-
-func (e *Alarm) send() {
- select {
- case e.ch <- struct{}{}:
- default:
- }
-}
diff --git a/common/mclock/alarm_test.go b/common/mclock/alarm_test.go
deleted file mode 100644
index d2ad9913fd..0000000000
--- a/common/mclock/alarm_test.go
+++ /dev/null
@@ -1,116 +0,0 @@
-// Copyright 2022 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 .
-
-package mclock
-
-import "testing"
-
-// This test checks basic functionality of Alarm.
-func TestAlarm(t *testing.T) {
- clk := new(Simulated)
- clk.Run(20)
- a := NewAlarm(clk)
-
- a.Schedule(clk.Now() + 10)
- if recv(a.C()) {
- t.Fatal("Alarm fired before scheduled deadline")
- }
- if ntimers := clk.ActiveTimers(); ntimers != 1 {
- t.Fatal("clock has", ntimers, "active timers, want", 1)
- }
- clk.Run(5)
- if recv(a.C()) {
- t.Fatal("Alarm fired too early")
- }
-
- clk.Run(5)
- if !recv(a.C()) {
- t.Fatal("Alarm did not fire")
- }
- if recv(a.C()) {
- t.Fatal("Alarm fired twice")
- }
- if ntimers := clk.ActiveTimers(); ntimers != 0 {
- t.Fatal("clock has", ntimers, "active timers, want", 0)
- }
-
- a.Schedule(clk.Now() + 5)
- if recv(a.C()) {
- t.Fatal("Alarm fired before scheduled deadline when scheduling the second event")
- }
-
- clk.Run(5)
- if !recv(a.C()) {
- t.Fatal("Alarm did not fire when scheduling the second event")
- }
- if recv(a.C()) {
- t.Fatal("Alarm fired twice when scheduling the second event")
- }
-}
-
-// This test checks that scheduling an Alarm to an earlier time than the
-// one already scheduled works properly.
-func TestAlarmScheduleEarlier(t *testing.T) {
- clk := new(Simulated)
- clk.Run(20)
- a := NewAlarm(clk)
-
- a.Schedule(clk.Now() + 50)
- clk.Run(5)
- a.Schedule(clk.Now() + 1)
- clk.Run(3)
- if !recv(a.C()) {
- t.Fatal("Alarm did not fire")
- }
-}
-
-// This test checks that scheduling an Alarm to a later time than the
-// one already scheduled works properly.
-func TestAlarmScheduleLater(t *testing.T) {
- clk := new(Simulated)
- clk.Run(20)
- a := NewAlarm(clk)
-
- a.Schedule(clk.Now() + 50)
- clk.Run(5)
- a.Schedule(clk.Now() + 100)
- clk.Run(50)
- if !recv(a.C()) {
- t.Fatal("Alarm did not fire")
- }
-}
-
-// This test checks that scheduling an Alarm in the past makes it fire immediately.
-func TestAlarmNegative(t *testing.T) {
- clk := new(Simulated)
- clk.Run(50)
- a := NewAlarm(clk)
-
- a.Schedule(-1)
- clk.Run(1) // needed to process timers
- if !recv(a.C()) {
- t.Fatal("Alarm did not fire for negative time")
- }
-}
-
-func recv(ch <-chan struct{}) bool {
- select {
- case <-ch:
- return true
- default:
- return false
- }
-}
diff --git a/common/mclock/mclock.go b/common/mclock/mclock.go
deleted file mode 100644
index c05738cf2b..0000000000
--- a/common/mclock/mclock.go
+++ /dev/null
@@ -1,127 +0,0 @@
-// Copyright 2016 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 .
-
-// Package mclock is a wrapper for a monotonic clock source
-package mclock
-
-import (
- "time"
-
- _ "unsafe" // for go:linkname
-)
-
-//go:noescape
-//go:linkname nanotime runtime.nanotime
-func nanotime() int64
-
-// AbsTime represents absolute monotonic time.
-type AbsTime int64
-
-// Now returns the current absolute monotonic time.
-func Now() AbsTime {
- return AbsTime(nanotime())
-}
-
-// Add returns t + d as absolute time.
-func (t AbsTime) Add(d time.Duration) AbsTime {
- return t + AbsTime(d)
-}
-
-// Sub returns t - t2 as a duration.
-func (t AbsTime) Sub(t2 AbsTime) time.Duration {
- return time.Duration(t - t2)
-}
-
-// The Clock interface makes it possible to replace the monotonic system clock with
-// a simulated clock.
-type Clock interface {
- Now() AbsTime
- Sleep(time.Duration)
- NewTimer(time.Duration) ChanTimer
- After(time.Duration) <-chan AbsTime
- AfterFunc(d time.Duration, f func()) Timer
-}
-
-// Timer is a cancellable event created by AfterFunc.
-type Timer interface {
- // Stop cancels the timer. It returns false if the timer has already
- // expired or been stopped.
- Stop() bool
-}
-
-// ChanTimer is a cancellable event created by NewTimer.
-type ChanTimer interface {
- Timer
-
- // The channel returned by C receives a value when the timer expires.
- C() <-chan AbsTime
- // Reset reschedules the timer with a new timeout.
- // It should be invoked only on stopped or expired timers with drained channels.
- Reset(time.Duration)
-}
-
-// System implements Clock using the system clock.
-type System struct{}
-
-// Now returns the current monotonic time.
-func (c System) Now() AbsTime {
- return Now()
-}
-
-// Sleep blocks for the given duration.
-func (c System) Sleep(d time.Duration) {
- time.Sleep(d)
-}
-
-// NewTimer creates a timer which can be rescheduled.
-func (c System) NewTimer(d time.Duration) ChanTimer {
- ch := make(chan AbsTime, 1)
- t := time.AfterFunc(d, func() {
- // This send is non-blocking because that's how time.Timer
- // behaves. It doesn't matter in the happy case, but does
- // when Reset is misused.
- select {
- case ch <- c.Now():
- default:
- }
- })
- return &systemTimer{t, ch}
-}
-
-// After returns a channel which receives the current time after d has elapsed.
-func (c System) After(d time.Duration) <-chan AbsTime {
- ch := make(chan AbsTime, 1)
- time.AfterFunc(d, func() { ch <- c.Now() })
- return ch
-}
-
-// AfterFunc runs f on a new goroutine after the duration has elapsed.
-func (c System) AfterFunc(d time.Duration, f func()) Timer {
- return time.AfterFunc(d, f)
-}
-
-type systemTimer struct {
- *time.Timer
- ch <-chan AbsTime
-}
-
-func (st *systemTimer) Reset(d time.Duration) {
- st.Timer.Reset(d)
-}
-
-func (st *systemTimer) C() <-chan AbsTime {
- return st.ch
-}
diff --git a/common/mclock/mclock.s b/common/mclock/mclock.s
deleted file mode 100644
index 99a7a878f0..0000000000
--- a/common/mclock/mclock.s
+++ /dev/null
@@ -1 +0,0 @@
-// This file exists in order to be able to use go:linkname.
diff --git a/common/mclock/simclock.go b/common/mclock/simclock.go
deleted file mode 100644
index f5ad3f8bc0..0000000000
--- a/common/mclock/simclock.go
+++ /dev/null
@@ -1,209 +0,0 @@
-// Copyright 2018 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 .
-
-package mclock
-
-import (
- "container/heap"
- "sync"
- "time"
-)
-
-// Simulated implements a virtual Clock for reproducible time-sensitive tests. It
-// simulates a scheduler on a virtual timescale where actual processing takes zero time.
-//
-// The virtual clock doesn't advance on its own, call Run to advance it and execute timers.
-// Since there is no way to influence the Go scheduler, testing timeout behaviour involving
-// goroutines needs special care. A good way to test such timeouts is as follows: First
-// perform the action that is supposed to time out. Ensure that the timer you want to test
-// is created. Then run the clock until after the timeout. Finally observe the effect of
-// the timeout using a channel or semaphore.
-type Simulated struct {
- now AbsTime
- scheduled simTimerHeap
- mu sync.RWMutex
- cond *sync.Cond
-}
-
-// simTimer implements ChanTimer on the virtual clock.
-type simTimer struct {
- at AbsTime
- index int // position in s.scheduled
- s *Simulated
- do func()
- ch <-chan AbsTime
-}
-
-func (s *Simulated) init() {
- if s.cond == nil {
- s.cond = sync.NewCond(&s.mu)
- }
-}
-
-// Run moves the clock by the given duration, executing all timers before that duration.
-func (s *Simulated) Run(d time.Duration) {
- s.mu.Lock()
- s.init()
-
- end := s.now.Add(d)
- var do []func()
- for len(s.scheduled) > 0 && s.scheduled[0].at <= end {
- ev := heap.Pop(&s.scheduled).(*simTimer)
- do = append(do, ev.do)
- }
- s.now = end
- s.mu.Unlock()
-
- for _, fn := range do {
- fn()
- }
-}
-
-// ActiveTimers returns the number of timers that haven't fired.
-func (s *Simulated) ActiveTimers() int {
- s.mu.RLock()
- defer s.mu.RUnlock()
-
- return len(s.scheduled)
-}
-
-// WaitForTimers waits until the clock has at least n scheduled timers.
-func (s *Simulated) WaitForTimers(n int) {
- s.mu.Lock()
- defer s.mu.Unlock()
- s.init()
-
- for len(s.scheduled) < n {
- s.cond.Wait()
- }
-}
-
-// Now returns the current virtual time.
-func (s *Simulated) Now() AbsTime {
- s.mu.RLock()
- defer s.mu.RUnlock()
-
- return s.now
-}
-
-// Sleep blocks until the clock has advanced by d.
-func (s *Simulated) Sleep(d time.Duration) {
- <-s.After(d)
-}
-
-// NewTimer creates a timer which fires when the clock has advanced by d.
-func (s *Simulated) NewTimer(d time.Duration) ChanTimer {
- s.mu.Lock()
- defer s.mu.Unlock()
-
- ch := make(chan AbsTime, 1)
- var timer *simTimer
- timer = s.schedule(d, func() { ch <- timer.at })
- timer.ch = ch
- return timer
-}
-
-// After returns a channel which receives the current time after the clock
-// has advanced by d.
-func (s *Simulated) After(d time.Duration) <-chan AbsTime {
- return s.NewTimer(d).C()
-}
-
-// AfterFunc runs fn after the clock has advanced by d. Unlike with the system
-// clock, fn runs on the goroutine that calls Run.
-func (s *Simulated) AfterFunc(d time.Duration, fn func()) Timer {
- s.mu.Lock()
- defer s.mu.Unlock()
-
- return s.schedule(d, fn)
-}
-
-func (s *Simulated) schedule(d time.Duration, fn func()) *simTimer {
- s.init()
-
- at := s.now.Add(d)
- ev := &simTimer{do: fn, at: at, s: s}
- heap.Push(&s.scheduled, ev)
- s.cond.Broadcast()
- return ev
-}
-
-func (ev *simTimer) Stop() bool {
- ev.s.mu.Lock()
- defer ev.s.mu.Unlock()
-
- if ev.index < 0 {
- return false
- }
- heap.Remove(&ev.s.scheduled, ev.index)
- ev.s.cond.Broadcast()
- ev.index = -1
- return true
-}
-
-func (ev *simTimer) Reset(d time.Duration) {
- if ev.ch == nil {
- panic("mclock: Reset() on timer created by AfterFunc")
- }
-
- ev.s.mu.Lock()
- defer ev.s.mu.Unlock()
- ev.at = ev.s.now.Add(d)
- if ev.index < 0 {
- heap.Push(&ev.s.scheduled, ev) // already expired
- } else {
- heap.Fix(&ev.s.scheduled, ev.index) // hasn't fired yet, reschedule
- }
- ev.s.cond.Broadcast()
-}
-
-func (ev *simTimer) C() <-chan AbsTime {
- if ev.ch == nil {
- panic("mclock: C() on timer created by AfterFunc")
- }
- return ev.ch
-}
-
-type simTimerHeap []*simTimer
-
-func (h *simTimerHeap) Len() int {
- return len(*h)
-}
-
-func (h *simTimerHeap) Less(i, j int) bool {
- return (*h)[i].at < (*h)[j].at
-}
-
-func (h *simTimerHeap) Swap(i, j int) {
- (*h)[i], (*h)[j] = (*h)[j], (*h)[i]
- (*h)[i].index = i
- (*h)[j].index = j
-}
-
-func (h *simTimerHeap) Push(x interface{}) {
- t := x.(*simTimer)
- t.index = len(*h)
- *h = append(*h, t)
-}
-
-func (h *simTimerHeap) Pop() interface{} {
- end := len(*h) - 1
- t := (*h)[end]
- t.index = -1
- (*h)[end] = nil
- *h = (*h)[:end]
- return t
-}
diff --git a/common/mclock/simclock_test.go b/common/mclock/simclock_test.go
deleted file mode 100644
index 582bc31dcd..0000000000
--- a/common/mclock/simclock_test.go
+++ /dev/null
@@ -1,162 +0,0 @@
-// Copyright 2019 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 .
-
-package mclock
-
-import (
- "testing"
- "time"
-)
-
-var _ Clock = System{}
-var _ Clock = new(Simulated)
-
-func TestSimulatedAfter(t *testing.T) {
- var (
- timeout = 30 * time.Minute
- offset = 99 * time.Hour
- adv = 11 * time.Minute
- c Simulated
- )
- c.Run(offset)
-
- end := c.Now().Add(timeout)
- ch := c.After(timeout)
- for c.Now() < end.Add(-adv) {
- c.Run(adv)
- select {
- case <-ch:
- t.Fatal("Timer fired early")
- default:
- }
- }
-
- c.Run(adv)
- select {
- case stamp := <-ch:
- want := AbsTime(0).Add(offset).Add(timeout)
- if stamp != want {
- t.Errorf("Wrong time sent on timer channel: got %v, want %v", stamp, want)
- }
- default:
- t.Fatal("Timer didn't fire")
- }
-}
-
-func TestSimulatedAfterFunc(t *testing.T) {
- var c Simulated
-
- called1 := false
- timer1 := c.AfterFunc(100*time.Millisecond, func() { called1 = true })
- if c.ActiveTimers() != 1 {
- t.Fatalf("%d active timers, want one", c.ActiveTimers())
- }
- if fired := timer1.Stop(); !fired {
- t.Fatal("Stop returned false even though timer didn't fire")
- }
- if c.ActiveTimers() != 0 {
- t.Fatalf("%d active timers, want zero", c.ActiveTimers())
- }
- if called1 {
- t.Fatal("timer 1 called")
- }
- if fired := timer1.Stop(); fired {
- t.Fatal("Stop returned true after timer was already stopped")
- }
-
- called2 := false
- timer2 := c.AfterFunc(100*time.Millisecond, func() { called2 = true })
- c.Run(50 * time.Millisecond)
- if called2 {
- t.Fatal("timer 2 called")
- }
- c.Run(51 * time.Millisecond)
- if !called2 {
- t.Fatal("timer 2 not called")
- }
- if fired := timer2.Stop(); fired {
- t.Fatal("Stop returned true after timer has fired")
- }
-}
-
-func TestSimulatedSleep(t *testing.T) {
- var (
- c Simulated
- timeout = 1 * time.Hour
- done = make(chan AbsTime, 1)
- )
- go func() {
- c.Sleep(timeout)
- done <- c.Now()
- }()
-
- c.WaitForTimers(1)
- c.Run(2 * timeout)
- select {
- case stamp := <-done:
- want := AbsTime(2 * timeout)
- if stamp != want {
- t.Errorf("Wrong time after sleep: got %v, want %v", stamp, want)
- }
- case <-time.After(5 * time.Second):
- t.Fatal("Sleep didn't return in time")
- }
-}
-
-func TestSimulatedTimerReset(t *testing.T) {
- var (
- c Simulated
- timeout = 1 * time.Hour
- )
- timer := c.NewTimer(timeout)
- c.Run(2 * timeout)
- select {
- case ftime := <-timer.C():
- if ftime != AbsTime(timeout) {
- t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(timeout))
- }
- default:
- t.Fatal("timer didn't fire")
- }
-
- timer.Reset(timeout)
- c.Run(2 * timeout)
- select {
- case ftime := <-timer.C():
- if ftime != AbsTime(3*timeout) {
- t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(3*timeout))
- }
- default:
- t.Fatal("timer didn't fire again")
- }
-}
-
-func TestSimulatedTimerStop(t *testing.T) {
- var (
- c Simulated
- timeout = 1 * time.Hour
- )
- timer := c.NewTimer(timeout)
- c.Run(2 * timeout)
- if timer.Stop() {
- t.Errorf("Stop returned true for fired timer")
- }
- select {
- case <-timer.C():
- default:
- t.Fatal("timer didn't fire")
- }
-}
diff --git a/common/path.go b/common/path.go
deleted file mode 100644
index c1e382fd29..0000000000
--- a/common/path.go
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright 2014 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 .
-
-package common
-
-import (
- "os"
- "path/filepath"
-)
-
-// FileExist checks if a file exists at filePath.
-func FileExist(filePath string) bool {
- _, err := os.Stat(filePath)
- if err != nil && os.IsNotExist(err) {
- return false
- }
-
- return true
-}
-
-// AbsolutePath returns datadir + filename, or filename if it is absolute.
-func AbsolutePath(datadir string, filename string) string {
- if filepath.IsAbs(filename) {
- return filename
- }
- return filepath.Join(datadir, filename)
-}
diff --git a/common/prque/lazyqueue.go b/common/prque/lazyqueue.go
deleted file mode 100644
index 59bda72fa7..0000000000
--- a/common/prque/lazyqueue.go
+++ /dev/null
@@ -1,195 +0,0 @@
-// Copyright 2019 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 .
-
-package prque
-
-import (
- "container/heap"
- "time"
-
- "github.com/ethereum/go-ethereum/common/mclock"
- "golang.org/x/exp/constraints"
-)
-
-// LazyQueue is a priority queue data structure where priorities can change over
-// time and are only evaluated on demand.
-// Two callbacks are required:
-// - priority evaluates the actual priority of an item
-// - maxPriority gives an upper estimate for the priority in any moment between
-// now and the given absolute time
-//
-// If the upper estimate is exceeded then Update should be called for that item.
-// A global Refresh function should also be called periodically.
-type LazyQueue[P constraints.Ordered, V any] struct {
- clock mclock.Clock
- // Items are stored in one of two internal queues ordered by estimated max
- // priority until the next and the next-after-next refresh. Update and Refresh
- // always places items in queue[1].
- queue [2]*sstack[P, V]
- popQueue *sstack[P, V]
- period time.Duration
- maxUntil mclock.AbsTime
- indexOffset int
- setIndex SetIndexCallback[V]
- priority PriorityCallback[P, V]
- maxPriority MaxPriorityCallback[P, V]
- lastRefresh1, lastRefresh2 mclock.AbsTime
-}
-
-type (
- PriorityCallback[P constraints.Ordered, V any] func(data V) P // actual priority callback
- MaxPriorityCallback[P constraints.Ordered, V any] func(data V, until mclock.AbsTime) P // estimated maximum priority callback
-)
-
-// NewLazyQueue creates a new lazy queue
-func NewLazyQueue[P constraints.Ordered, V any](setIndex SetIndexCallback[V], priority PriorityCallback[P, V], maxPriority MaxPriorityCallback[P, V], clock mclock.Clock, refreshPeriod time.Duration) *LazyQueue[P, V] {
- q := &LazyQueue[P, V]{
- popQueue: newSstack[P, V](nil),
- setIndex: setIndex,
- priority: priority,
- maxPriority: maxPriority,
- clock: clock,
- period: refreshPeriod,
- lastRefresh1: clock.Now(),
- lastRefresh2: clock.Now(),
- }
- q.Reset()
- q.refresh(clock.Now())
- return q
-}
-
-// Reset clears the contents of the queue
-func (q *LazyQueue[P, V]) Reset() {
- q.queue[0] = newSstack[P, V](q.setIndex0)
- q.queue[1] = newSstack[P, V](q.setIndex1)
-}
-
-// Refresh performs queue re-evaluation if necessary
-func (q *LazyQueue[P, V]) Refresh() {
- now := q.clock.Now()
- for time.Duration(now-q.lastRefresh2) >= q.period*2 {
- q.refresh(now)
- q.lastRefresh2 = q.lastRefresh1
- q.lastRefresh1 = now
- }
-}
-
-// refresh re-evaluates items in the older queue and swaps the two queues
-func (q *LazyQueue[P, V]) refresh(now mclock.AbsTime) {
- q.maxUntil = now.Add(q.period)
- for q.queue[0].Len() != 0 {
- q.Push(heap.Pop(q.queue[0]).(*item[P, V]).value)
- }
- q.queue[0], q.queue[1] = q.queue[1], q.queue[0]
- q.indexOffset = 1 - q.indexOffset
- q.maxUntil = q.maxUntil.Add(q.period)
-}
-
-// Push adds an item to the queue
-func (q *LazyQueue[P, V]) Push(data V) {
- heap.Push(q.queue[1], &item[P, V]{data, q.maxPriority(data, q.maxUntil)})
-}
-
-// Update updates the upper priority estimate for the item with the given queue index
-func (q *LazyQueue[P, V]) Update(index int) {
- q.Push(q.Remove(index))
-}
-
-// Pop removes and returns the item with the greatest actual priority
-func (q *LazyQueue[P, V]) Pop() (V, P) {
- var (
- resData V
- resPri P
- )
- q.MultiPop(func(data V, priority P) bool {
- resData = data
- resPri = priority
- return false
- })
- return resData, resPri
-}
-
-// peekIndex returns the index of the internal queue where the item with the
-// highest estimated priority is or -1 if both are empty
-func (q *LazyQueue[P, V]) peekIndex() int {
- if q.queue[0].Len() != 0 {
- if q.queue[1].Len() != 0 && q.queue[1].blocks[0][0].priority > q.queue[0].blocks[0][0].priority {
- return 1
- }
- return 0
- }
- if q.queue[1].Len() != 0 {
- return 1
- }
- return -1
-}
-
-// MultiPop pops multiple items from the queue and is more efficient than calling
-// Pop multiple times. Popped items are passed to the callback. MultiPop returns
-// when the callback returns false or there are no more items to pop.
-func (q *LazyQueue[P, V]) MultiPop(callback func(data V, priority P) bool) {
- nextIndex := q.peekIndex()
- for nextIndex != -1 {
- data := heap.Pop(q.queue[nextIndex]).(*item[P, V]).value
- heap.Push(q.popQueue, &item[P, V]{data, q.priority(data)})
- nextIndex = q.peekIndex()
- for q.popQueue.Len() != 0 && (nextIndex == -1 || q.queue[nextIndex].blocks[0][0].priority < q.popQueue.blocks[0][0].priority) {
- i := heap.Pop(q.popQueue).(*item[P, V])
- if !callback(i.value, i.priority) {
- for q.popQueue.Len() != 0 {
- q.Push(heap.Pop(q.popQueue).(*item[P, V]).value)
- }
- return
- }
- nextIndex = q.peekIndex() // re-check because callback is allowed to push items back
- }
- }
-}
-
-// PopItem pops the item from the queue only, dropping the associated priority value.
-func (q *LazyQueue[P, V]) PopItem() V {
- i, _ := q.Pop()
- return i
-}
-
-// Remove removes the item with the given index.
-func (q *LazyQueue[P, V]) Remove(index int) V {
- return heap.Remove(q.queue[index&1^q.indexOffset], index>>1).(*item[P, V]).value
-}
-
-// Empty checks whether the priority queue is empty.
-func (q *LazyQueue[P, V]) Empty() bool {
- return q.queue[0].Len() == 0 && q.queue[1].Len() == 0
-}
-
-// Size returns the number of items in the priority queue.
-func (q *LazyQueue[P, V]) Size() int {
- return q.queue[0].Len() + q.queue[1].Len()
-}
-
-// setIndex0 translates internal queue item index to the virtual index space of LazyQueue
-func (q *LazyQueue[P, V]) setIndex0(data V, index int) {
- if index == -1 {
- q.setIndex(data, -1)
- } else {
- q.setIndex(data, index+index)
- }
-}
-
-// setIndex1 translates internal queue item index to the virtual index space of LazyQueue
-func (q *LazyQueue[P, V]) setIndex1(data V, index int) {
- q.setIndex(data, index+index+1)
-}
diff --git a/common/prque/lazyqueue_test.go b/common/prque/lazyqueue_test.go
deleted file mode 100644
index ffb7e5e9e3..0000000000
--- a/common/prque/lazyqueue_test.go
+++ /dev/null
@@ -1,123 +0,0 @@
-// Copyright 2019 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 .
-
-package prque
-
-import (
- "math/rand"
- "sync"
- "testing"
- "time"
-
- "github.com/ethereum/go-ethereum/common/mclock"
-)
-
-const (
- testItems = 1000
- testPriorityStep = 100
- testSteps = 1000000
- testStepPeriod = time.Millisecond
- testQueueRefresh = time.Second
- testAvgRate = float64(testPriorityStep) / float64(testItems) / float64(testStepPeriod)
-)
-
-type lazyItem struct {
- p, maxp int64
- last mclock.AbsTime
- index int
-}
-
-func testPriority(a interface{}) int64 {
- return a.(*lazyItem).p
-}
-
-func testMaxPriority(a interface{}, until mclock.AbsTime) int64 {
- i := a.(*lazyItem)
- dt := until - i.last
- i.maxp = i.p + int64(float64(dt)*testAvgRate)
- return i.maxp
-}
-
-func testSetIndex(a interface{}, i int) {
- a.(*lazyItem).index = i
-}
-
-func TestLazyQueue(t *testing.T) {
- clock := &mclock.Simulated{}
- q := NewLazyQueue(testSetIndex, testPriority, testMaxPriority, clock, testQueueRefresh)
-
- var (
- items [testItems]lazyItem
- maxPri int64
- )
-
- for i := range items[:] {
- items[i].p = rand.Int63n(testPriorityStep * 10)
- if items[i].p > maxPri {
- maxPri = items[i].p
- }
- items[i].index = -1
- q.Push(&items[i])
- }
-
- var (
- lock sync.Mutex
- wg sync.WaitGroup
- stopCh = make(chan chan struct{})
- )
- defer wg.Wait()
- wg.Add(1)
- go func() {
- defer wg.Done()
- for {
- select {
- case <-clock.After(testQueueRefresh):
- lock.Lock()
- q.Refresh()
- lock.Unlock()
- case <-stopCh:
- return
- }
- }
- }()
-
- for c := 0; c < testSteps; c++ {
- i := rand.Intn(testItems)
- lock.Lock()
- items[i].p += rand.Int63n(testPriorityStep*2-1) + 1
- if items[i].p > maxPri {
- maxPri = items[i].p
- }
- items[i].last = clock.Now()
- if items[i].p > items[i].maxp {
- q.Update(items[i].index)
- }
- if rand.Intn(100) == 0 {
- p := q.PopItem().(*lazyItem)
- if p.p != maxPri {
- lock.Unlock()
- close(stopCh)
- t.Fatalf("incorrect item (best known priority %d, popped %d)", maxPri, p.p)
- }
- q.Push(p)
- }
- lock.Unlock()
- clock.Run(testStepPeriod)
- clock.WaitForTimers(1)
- }
-
- close(stopCh)
-}
diff --git a/common/prque/prque.go b/common/prque/prque.go
deleted file mode 100755
index 0e8c9f897f..0000000000
--- a/common/prque/prque.go
+++ /dev/null
@@ -1,77 +0,0 @@
-// CookieJar - A contestant's algorithm toolbox
-// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
-//
-// CookieJar is dual licensed: use of this source code is governed by a BSD
-// license that can be found in the LICENSE file. Alternatively, the CookieJar
-// toolbox may be used in accordance with the terms and conditions contained
-// in a signed written agreement between you and the author(s).
-
-// This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
-
-// Package prque implements a priority queue data structure supporting arbitrary
-// value types and int64 priorities.
-//
-// If you would like to use a min-priority queue, simply negate the priorities.
-//
-// Internally the queue is based on the standard heap package working on a
-// sortable version of the block based stack.
-package prque
-
-import (
- "container/heap"
-
- "golang.org/x/exp/constraints"
-)
-
-// Priority queue data structure.
-type Prque[P constraints.Ordered, V any] struct {
- cont *sstack[P, V]
-}
-
-// New creates a new priority queue.
-func New[P constraints.Ordered, V any](setIndex SetIndexCallback[V]) *Prque[P, V] {
- return &Prque[P, V]{newSstack[P, V](setIndex)}
-}
-
-// Pushes a value with a given priority into the queue, expanding if necessary.
-func (p *Prque[P, V]) Push(data V, priority P) {
- heap.Push(p.cont, &item[P, V]{data, priority})
-}
-
-// Peek returns the value with the greatest priority but does not pop it off.
-func (p *Prque[P, V]) Peek() (V, P) {
- item := p.cont.blocks[0][0]
- return item.value, item.priority
-}
-
-// Pops the value with the greatest priority off the stack and returns it.
-// Currently no shrinking is done.
-func (p *Prque[P, V]) Pop() (V, P) {
- item := heap.Pop(p.cont).(*item[P, V])
- return item.value, item.priority
-}
-
-// Pops only the item from the queue, dropping the associated priority value.
-func (p *Prque[P, V]) PopItem() V {
- return heap.Pop(p.cont).(*item[P, V]).value
-}
-
-// Remove removes the element with the given index.
-func (p *Prque[P, V]) Remove(i int) V {
- return heap.Remove(p.cont, i).(*item[P, V]).value
-}
-
-// Checks whether the priority queue is empty.
-func (p *Prque[P, V]) Empty() bool {
- return p.cont.Len() == 0
-}
-
-// Returns the number of element in the priority queue.
-func (p *Prque[P, V]) Size() int {
- return p.cont.Len()
-}
-
-// Clears the contents of the priority queue.
-func (p *Prque[P, V]) Reset() {
- *p = *New[P, V](p.cont.setIndex)
-}
diff --git a/common/prque/prque_test.go b/common/prque/prque_test.go
deleted file mode 100644
index c4910f205a..0000000000
--- a/common/prque/prque_test.go
+++ /dev/null
@@ -1,133 +0,0 @@
-// CookieJar - A contestant's algorithm toolbox
-// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
-//
-// CookieJar is dual licensed: use of this source code is governed by a BSD
-// license that can be found in the LICENSE file. Alternatively, the CookieJar
-// toolbox may be used in accordance with the terms and conditions contained
-// in a signed written agreement between you and the author(s).
-
-package prque
-
-import (
- "math/rand"
- "testing"
-)
-
-func TestPrque(t *testing.T) {
- // Generate a batch of random data and a specific priority order
- size := 16 * blockSize
- prio := rand.Perm(size)
- data := make([]int, size)
- for i := 0; i < size; i++ {
- data[i] = rand.Int()
- }
- queue := New[int, int](nil)
-
- for rep := 0; rep < 2; rep++ {
- // Fill a priority queue with the above data
- for i := 0; i < size; i++ {
- queue.Push(data[i], prio[i])
- if queue.Size() != i+1 {
- t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1)
- }
- }
- // Create a map the values to the priorities for easier verification
- dict := make(map[int]int)
- for i := 0; i < size; i++ {
- dict[prio[i]] = data[i]
- }
-
- // Pop out the elements in priority order and verify them
- prevPrio := size + 1
- for !queue.Empty() {
- val, prio := queue.Pop()
- if prio > prevPrio {
- t.Errorf("invalid priority order: %v after %v.", prio, prevPrio)
- }
- prevPrio = prio
- if val != dict[prio] {
- t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio])
- }
- delete(dict, prio)
- }
- }
-}
-
-func TestReset(t *testing.T) {
- // Generate a batch of random data and a specific priority order
- size := 16 * blockSize
- prio := rand.Perm(size)
- data := make([]int, size)
- for i := 0; i < size; i++ {
- data[i] = rand.Int()
- }
- queue := New[int, int](nil)
-
- for rep := 0; rep < 2; rep++ {
- // Fill a priority queue with the above data
- for i := 0; i < size; i++ {
- queue.Push(data[i], prio[i])
- if queue.Size() != i+1 {
- t.Errorf("queue size mismatch: have %v, want %v.", queue.Size(), i+1)
- }
- }
- // Create a map the values to the priorities for easier verification
- dict := make(map[int]int)
- for i := 0; i < size; i++ {
- dict[prio[i]] = data[i]
- }
- // Pop out half the elements in priority order and verify them
- prevPrio := size + 1
- for i := 0; i < size/2; i++ {
- val, prio := queue.Pop()
- if prio > prevPrio {
- t.Errorf("invalid priority order: %v after %v.", prio, prevPrio)
- }
- prevPrio = prio
- if val != dict[prio] {
- t.Errorf("push/pop mismatch: have %v, want %v.", val, dict[prio])
- }
- delete(dict, prio)
- }
- // Reset and ensure it's empty
- queue.Reset()
- if !queue.Empty() {
- t.Errorf("priority queue not empty after reset: %v", queue)
- }
- }
-}
-
-func BenchmarkPush(b *testing.B) {
- // Create some initial data
- data := make([]int, b.N)
- prio := make([]int64, b.N)
- for i := 0; i < len(data); i++ {
- data[i] = rand.Int()
- prio[i] = rand.Int63()
- }
- // Execute the benchmark
- b.ResetTimer()
- queue := New[int64, int](nil)
- for i := 0; i < len(data); i++ {
- queue.Push(data[i], prio[i])
- }
-}
-
-func BenchmarkPop(b *testing.B) {
- // Create some initial data
- data := make([]int, b.N)
- prio := make([]int64, b.N)
- for i := 0; i < len(data); i++ {
- data[i] = rand.Int()
- prio[i] = rand.Int63()
- }
- queue := New[int64, int](nil)
- for i := 0; i < len(data); i++ {
- queue.Push(data[i], prio[i])
- }
- // Execute the benchmark
- b.ResetTimer()
- for !queue.Empty() {
- queue.Pop()
- }
-}
diff --git a/common/prque/sstack.go b/common/prque/sstack.go
deleted file mode 100755
index 5dcd1d9dd0..0000000000
--- a/common/prque/sstack.go
+++ /dev/null
@@ -1,113 +0,0 @@
-// CookieJar - A contestant's algorithm toolbox
-// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
-//
-// CookieJar is dual licensed: use of this source code is governed by a BSD
-// license that can be found in the LICENSE file. Alternatively, the CookieJar
-// toolbox may be used in accordance with the terms and conditions contained
-// in a signed written agreement between you and the author(s).
-
-// This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
-
-package prque
-
-import "golang.org/x/exp/constraints"
-
-// The size of a block of data
-const blockSize = 4096
-
-// A prioritized item in the sorted stack.
-type item[P constraints.Ordered, V any] struct {
- value V
- priority P
-}
-
-// SetIndexCallback is called when the element is moved to a new index.
-// Providing SetIndexCallback is optional, it is needed only if the application needs
-// to delete elements other than the top one.
-type SetIndexCallback[V any] func(data V, index int)
-
-// Internal sortable stack data structure. Implements the Push and Pop ops for
-// the stack (heap) functionality and the Len, Less and Swap methods for the
-// sortability requirements of the heaps.
-type sstack[P constraints.Ordered, V any] struct {
- setIndex SetIndexCallback[V]
- size int
- capacity int
- offset int
-
- blocks [][]*item[P, V]
- active []*item[P, V]
-}
-
-// Creates a new, empty stack.
-func newSstack[P constraints.Ordered, V any](setIndex SetIndexCallback[V]) *sstack[P, V] {
- result := new(sstack[P, V])
- result.setIndex = setIndex
- result.active = make([]*item[P, V], blockSize)
- result.blocks = [][]*item[P, V]{result.active}
- result.capacity = blockSize
- return result
-}
-
-// Pushes a value onto the stack, expanding it if necessary. Required by
-// heap.Interface.
-func (s *sstack[P, V]) Push(data any) {
- if s.size == s.capacity {
- s.active = make([]*item[P, V], blockSize)
- s.blocks = append(s.blocks, s.active)
- s.capacity += blockSize
- s.offset = 0
- } else if s.offset == blockSize {
- s.active = s.blocks[s.size/blockSize]
- s.offset = 0
- }
- if s.setIndex != nil {
- s.setIndex(data.(*item[P, V]).value, s.size)
- }
- s.active[s.offset] = data.(*item[P, V])
- s.offset++
- s.size++
-}
-
-// Pops a value off the stack and returns it. Currently no shrinking is done.
-// Required by heap.Interface.
-func (s *sstack[P, V]) Pop() (res any) {
- s.size--
- s.offset--
- if s.offset < 0 {
- s.offset = blockSize - 1
- s.active = s.blocks[s.size/blockSize]
- }
- res, s.active[s.offset] = s.active[s.offset], nil
- if s.setIndex != nil {
- s.setIndex(res.(*item[P, V]).value, -1)
- }
- return
-}
-
-// Returns the length of the stack. Required by sort.Interface.
-func (s *sstack[P, V]) Len() int {
- return s.size
-}
-
-// Compares the priority of two elements of the stack (higher is first).
-// Required by sort.Interface.
-func (s *sstack[P, V]) Less(i, j int) bool {
- return s.blocks[i/blockSize][i%blockSize].priority > s.blocks[j/blockSize][j%blockSize].priority
-}
-
-// Swaps two elements in the stack. Required by sort.Interface.
-func (s *sstack[P, V]) Swap(i, j int) {
- ib, io, jb, jo := i/blockSize, i%blockSize, j/blockSize, j%blockSize
- a, b := s.blocks[jb][jo], s.blocks[ib][io]
- if s.setIndex != nil {
- s.setIndex(a.value, i)
- s.setIndex(b.value, j)
- }
- s.blocks[ib][io], s.blocks[jb][jo] = a, b
-}
-
-// Resets the stack, effectively clearing its contents.
-func (s *sstack[P, V]) Reset() {
- *s = *newSstack[P, V](s.setIndex)
-}
diff --git a/common/prque/sstack_test.go b/common/prque/sstack_test.go
deleted file mode 100644
index edc99955e8..0000000000
--- a/common/prque/sstack_test.go
+++ /dev/null
@@ -1,100 +0,0 @@
-// CookieJar - A contestant's algorithm toolbox
-// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
-//
-// CookieJar is dual licensed: use of this source code is governed by a BSD
-// license that can be found in the LICENSE file. Alternatively, the CookieJar
-// toolbox may be used in accordance with the terms and conditions contained
-// in a signed written agreement between you and the author(s).
-
-package prque
-
-import (
- "math/rand"
- "sort"
- "testing"
-)
-
-func TestSstack(t *testing.T) {
- // Create some initial data
- size := 16 * blockSize
- data := make([]*item[int64, int], size)
- for i := 0; i < size; i++ {
- data[i] = &item[int64, int]{rand.Int(), rand.Int63()}
- }
- stack := newSstack[int64, int](nil)
- for rep := 0; rep < 2; rep++ {
- // Push all the data into the stack, pop out every second
- secs := []*item[int64, int]{}
- for i := 0; i < size; i++ {
- stack.Push(data[i])
- if i%2 == 0 {
- secs = append(secs, stack.Pop().(*item[int64, int]))
- }
- }
- rest := []*item[int64, int]{}
- for stack.Len() > 0 {
- rest = append(rest, stack.Pop().(*item[int64, int]))
- }
- // Make sure the contents of the resulting slices are ok
- for i := 0; i < size; i++ {
- if i%2 == 0 && data[i] != secs[i/2] {
- t.Errorf("push/pop mismatch: have %v, want %v.", secs[i/2], data[i])
- }
- if i%2 == 1 && data[i] != rest[len(rest)-i/2-1] {
- t.Errorf("push/pop mismatch: have %v, want %v.", rest[len(rest)-i/2-1], data[i])
- }
- }
- }
-}
-
-func TestSstackSort(t *testing.T) {
- // Create some initial data
- size := 16 * blockSize
- data := make([]*item[int64, int], size)
- for i := 0; i < size; i++ {
- data[i] = &item[int64, int]{rand.Int(), int64(i)}
- }
- // Push all the data into the stack
- stack := newSstack[int64, int](nil)
- for _, val := range data {
- stack.Push(val)
- }
- // Sort and pop the stack contents (should reverse the order)
- sort.Sort(stack)
- for _, val := range data {
- out := stack.Pop()
- if out != val {
- t.Errorf("push/pop mismatch after sort: have %v, want %v.", out, val)
- }
- }
-}
-
-func TestSstackReset(t *testing.T) {
- // Create some initial data
- size := 16 * blockSize
- data := make([]*item[int64, int], size)
- for i := 0; i < size; i++ {
- data[i] = &item[int64, int]{rand.Int(), rand.Int63()}
- }
- stack := newSstack[int64, int](nil)
- for rep := 0; rep < 2; rep++ {
- // Push all the data into the stack, pop out every second
- secs := []*item[int64, int]{}
- for i := 0; i < size; i++ {
- stack.Push(data[i])
- if i%2 == 0 {
- secs = append(secs, stack.Pop().(*item[int64, int]))
- }
- }
- // Reset and verify both pulled and stack contents
- stack.Reset()
- if stack.Len() != 0 {
- t.Errorf("stack not empty after reset: %v", stack)
- }
- for i := 0; i < size; i++ {
- if i%2 == 0 && data[i] != secs[i/2] {
- t.Errorf("push/pop mismatch: have %v, want %v.", secs[i/2], data[i])
- }
- }
- }
-}
diff --git a/common/size.go b/common/size.go
deleted file mode 100644
index 097b6304a8..0000000000
--- a/common/size.go
+++ /dev/null
@@ -1,56 +0,0 @@
-// Copyright 2014 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 .
-
-package common
-
-import (
- "fmt"
-)
-
-// StorageSize is a wrapper around a float value that supports user friendly
-// formatting.
-type StorageSize float64
-
-// String implements the stringer interface.
-func (s StorageSize) String() string {
- if s > 1099511627776 {
- return fmt.Sprintf("%.2f TiB", s/1099511627776)
- } else if s > 1073741824 {
- return fmt.Sprintf("%.2f GiB", s/1073741824)
- } else if s > 1048576 {
- return fmt.Sprintf("%.2f MiB", s/1048576)
- } else if s > 1024 {
- return fmt.Sprintf("%.2f KiB", s/1024)
- } else {
- return fmt.Sprintf("%.2f B", s)
- }
-}
-
-// TerminalString implements log.TerminalStringer, formatting a string for console
-// output during logging.
-func (s StorageSize) TerminalString() string {
- if s > 1099511627776 {
- return fmt.Sprintf("%.2fTiB", s/1099511627776)
- } else if s > 1073741824 {
- return fmt.Sprintf("%.2fGiB", s/1073741824)
- } else if s > 1048576 {
- return fmt.Sprintf("%.2fMiB", s/1048576)
- } else if s > 1024 {
- return fmt.Sprintf("%.2fKiB", s/1024)
- } else {
- return fmt.Sprintf("%.2fB", s)
- }
-}
diff --git a/common/size_test.go b/common/size_test.go
deleted file mode 100644
index 28f053d39f..0000000000
--- a/common/size_test.go
+++ /dev/null
@@ -1,59 +0,0 @@
-// Copyright 2014 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 .
-
-package common
-
-import (
- "testing"
-)
-
-func TestStorageSizeString(t *testing.T) {
- tests := []struct {
- size StorageSize
- str string
- }{
- {2839274474874, "2.58 TiB"},
- {2458492810, "2.29 GiB"},
- {2381273, "2.27 MiB"},
- {2192, "2.14 KiB"},
- {12, "12.00 B"},
- }
-
- for _, test := range tests {
- if test.size.String() != test.str {
- t.Errorf("%f: got %q, want %q", float64(test.size), test.size.String(), test.str)
- }
- }
-}
-
-func TestStorageSizeTerminalString(t *testing.T) {
- tests := []struct {
- size StorageSize
- str string
- }{
- {2839274474874, "2.58TiB"},
- {2458492810, "2.29GiB"},
- {2381273, "2.27MiB"},
- {2192, "2.14KiB"},
- {12, "12.00B"},
- }
-
- for _, test := range tests {
- if test.size.TerminalString() != test.str {
- t.Errorf("%f: got %q, want %q", float64(test.size), test.size.TerminalString(), test.str)
- }
- }
-}
diff --git a/common/test_utils.go b/common/test_utils.go
deleted file mode 100644
index 7a175412f4..0000000000
--- a/common/test_utils.go
+++ /dev/null
@@ -1,53 +0,0 @@
-// 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 .
-
-package common
-
-import (
- "encoding/json"
- "fmt"
- "os"
-)
-
-// LoadJSON reads the given file and unmarshals its content.
-func LoadJSON(file string, val interface{}) error {
- content, err := os.ReadFile(file)
- if err != nil {
- return err
- }
- if err := json.Unmarshal(content, val); err != nil {
- if syntaxerr, ok := err.(*json.SyntaxError); ok {
- line := findLine(content, syntaxerr.Offset)
- return fmt.Errorf("JSON syntax error at %v:%v: %v", file, line, err)
- }
- return fmt.Errorf("JSON unmarshal error in %v: %v", file, err)
- }
- return nil
-}
-
-// findLine returns the line number for the given offset into data.
-func findLine(data []byte, offset int64) (line int) {
- line = 1
- for i, r := range string(data) {
- if int64(i) >= offset {
- return
- }
- if r == '\n' {
- line++
- }
- }
- return
-}
diff --git a/common/types.go b/common/types.go
deleted file mode 100644
index aadca87f82..0000000000
--- a/common/types.go
+++ /dev/null
@@ -1,477 +0,0 @@
-// 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 .
-
-package common
-
-import (
- "bytes"
- "database/sql/driver"
- "encoding/hex"
- "encoding/json"
- "errors"
- "fmt"
- "math/big"
- "math/rand"
- "reflect"
- "strconv"
- "strings"
-
- "github.com/ethereum/go-ethereum/common/hexutil"
- "golang.org/x/crypto/sha3"
-)
-
-// Lengths of hashes and addresses in bytes.
-const (
- // HashLength is the expected length of the hash
- HashLength = 32
- // AddressLength is the expected length of the address
- AddressLength = 20
-)
-
-var (
- hashT = reflect.TypeOf(Hash{})
- addressT = reflect.TypeOf(Address{})
-
- // MaxAddress represents the maximum possible address value.
- MaxAddress = HexToAddress("0xffffffffffffffffffffffffffffffffffffffff")
-
- // MaxHash represents the maximum possible hash value.
- MaxHash = HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
-)
-
-// Hash represents the 32 byte Keccak256 hash of arbitrary data.
-type Hash [HashLength]byte
-
-// BytesToHash sets b to hash.
-// If b is larger than len(h), b will be cropped from the left.
-func BytesToHash(b []byte) Hash {
- var h Hash
- h.SetBytes(b)
- return h
-}
-
-// BigToHash sets byte representation of b to hash.
-// If b is larger than len(h), b will be cropped from the left.
-func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
-
-// HexToHash sets byte representation of s to hash.
-// If b is larger than len(h), b will be cropped from the left.
-func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
-
-// Cmp compares two hashes.
-func (h Hash) Cmp(other Hash) int {
- return bytes.Compare(h[:], other[:])
-}
-
-// Bytes gets the byte representation of the underlying hash.
-func (h Hash) Bytes() []byte { return h[:] }
-
-// Big converts a hash to a big integer.
-func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
-
-// Hex converts a hash to a hex string.
-func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
-
-// TerminalString implements log.TerminalStringer, formatting a string for console
-// output during logging.
-func (h Hash) TerminalString() string {
- return fmt.Sprintf("%x..%x", h[:3], h[29:])
-}
-
-// String implements the stringer interface and is used also by the logger when
-// doing full logging into a file.
-func (h Hash) String() string {
- return h.Hex()
-}
-
-// Format implements fmt.Formatter.
-// Hash supports the %v, %s, %q, %x, %X and %d format verbs.
-func (h Hash) Format(s fmt.State, c rune) {
- hexb := make([]byte, 2+len(h)*2)
- copy(hexb, "0x")
- hex.Encode(hexb[2:], h[:])
-
- switch c {
- case 'x', 'X':
- if !s.Flag('#') {
- hexb = hexb[2:]
- }
- if c == 'X' {
- hexb = bytes.ToUpper(hexb)
- }
- fallthrough
- case 'v', 's':
- s.Write(hexb)
- case 'q':
- q := []byte{'"'}
- s.Write(q)
- s.Write(hexb)
- s.Write(q)
- case 'd':
- fmt.Fprint(s, ([len(h)]byte)(h))
- default:
- fmt.Fprintf(s, "%%!%c(hash=%x)", c, h)
- }
-}
-
-// UnmarshalText parses a hash in hex syntax.
-func (h *Hash) UnmarshalText(input []byte) error {
- return hexutil.UnmarshalFixedText("Hash", input, h[:])
-}
-
-// UnmarshalJSON parses a hash in hex syntax.
-func (h *Hash) UnmarshalJSON(input []byte) error {
- return hexutil.UnmarshalFixedJSON(hashT, input, h[:])
-}
-
-// MarshalText returns the hex representation of h.
-func (h Hash) MarshalText() ([]byte, error) {
- return hexutil.Bytes(h[:]).MarshalText()
-}
-
-// SetBytes sets the hash to the value of b.
-// If b is larger than len(h), b will be cropped from the left.
-func (h *Hash) SetBytes(b []byte) {
- if len(b) > len(h) {
- b = b[len(b)-HashLength:]
- }
-
- copy(h[HashLength-len(b):], b)
-}
-
-// Generate implements testing/quick.Generator.
-func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
- m := rand.Intn(len(h))
- for i := len(h) - 1; i > m; i-- {
- h[i] = byte(rand.Uint32())
- }
- return reflect.ValueOf(h)
-}
-
-// Scan implements Scanner for database/sql.
-func (h *Hash) Scan(src interface{}) error {
- srcB, ok := src.([]byte)
- if !ok {
- return fmt.Errorf("can't scan %T into Hash", src)
- }
- if len(srcB) != HashLength {
- return fmt.Errorf("can't scan []byte of len %d into Hash, want %d", len(srcB), HashLength)
- }
- copy(h[:], srcB)
- return nil
-}
-
-// Value implements valuer for database/sql.
-func (h Hash) Value() (driver.Value, error) {
- return h[:], nil
-}
-
-// ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.
-func (Hash) ImplementsGraphQLType(name string) bool { return name == "Bytes32" }
-
-// UnmarshalGraphQL unmarshals the provided GraphQL query data.
-func (h *Hash) UnmarshalGraphQL(input interface{}) error {
- var err error
- switch input := input.(type) {
- case string:
- err = h.UnmarshalText([]byte(input))
- default:
- err = fmt.Errorf("unexpected type %T for Hash", input)
- }
- return err
-}
-
-// UnprefixedHash allows marshaling a Hash without 0x prefix.
-type UnprefixedHash Hash
-
-// UnmarshalText decodes the hash from hex. The 0x prefix is optional.
-func (h *UnprefixedHash) UnmarshalText(input []byte) error {
- return hexutil.UnmarshalFixedUnprefixedText("UnprefixedHash", input, h[:])
-}
-
-// MarshalText encodes the hash as hex.
-func (h UnprefixedHash) MarshalText() ([]byte, error) {
- return []byte(hex.EncodeToString(h[:])), nil
-}
-
-/////////// Address
-
-// Address represents the 20 byte address of an Ethereum account.
-type Address [AddressLength]byte
-
-// BytesToAddress returns Address with value b.
-// If b is larger than len(h), b will be cropped from the left.
-func BytesToAddress(b []byte) Address {
- var a Address
- a.SetBytes(b)
- return a
-}
-
-// BigToAddress returns Address with byte values of b.
-// If b is larger than len(h), b will be cropped from the left.
-func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
-
-// HexToAddress returns Address with byte values of s.
-// If s is larger than len(h), s will be cropped from the left.
-func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
-
-// IsHexAddress verifies whether a string can represent a valid hex-encoded
-// Ethereum address or not.
-func IsHexAddress(s string) bool {
- if has0xPrefix(s) {
- s = s[2:]
- }
- return len(s) == 2*AddressLength && isHex(s)
-}
-
-// Cmp compares two addresses.
-func (a Address) Cmp(other Address) int {
- return bytes.Compare(a[:], other[:])
-}
-
-// Bytes gets the string representation of the underlying address.
-func (a Address) Bytes() []byte { return a[:] }
-
-// Big converts an address to a big integer.
-func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) }
-
-// Hex returns an EIP55-compliant hex string representation of the address.
-func (a Address) Hex() string {
- return string(a.checksumHex())
-}
-
-// String implements fmt.Stringer.
-func (a Address) String() string {
- return a.Hex()
-}
-
-func (a *Address) checksumHex() []byte {
- buf := a.hex()
-
- // compute checksum
- sha := sha3.NewLegacyKeccak256()
- sha.Write(buf[2:])
- hash := sha.Sum(nil)
- for i := 2; i < len(buf); i++ {
- hashByte := hash[(i-2)/2]
- if i%2 == 0 {
- hashByte = hashByte >> 4
- } else {
- hashByte &= 0xf
- }
- if buf[i] > '9' && hashByte > 7 {
- buf[i] -= 32
- }
- }
- return buf[:]
-}
-
-func (a Address) hex() []byte {
- var buf [len(a)*2 + 2]byte
- copy(buf[:2], "0x")
- hex.Encode(buf[2:], a[:])
- return buf[:]
-}
-
-// Format implements fmt.Formatter.
-// Address supports the %v, %s, %q, %x, %X and %d format verbs.
-func (a Address) Format(s fmt.State, c rune) {
- switch c {
- case 'v', 's':
- s.Write(a.checksumHex())
- case 'q':
- q := []byte{'"'}
- s.Write(q)
- s.Write(a.checksumHex())
- s.Write(q)
- case 'x', 'X':
- // %x disables the checksum.
- hex := a.hex()
- if !s.Flag('#') {
- hex = hex[2:]
- }
- if c == 'X' {
- hex = bytes.ToUpper(hex)
- }
- s.Write(hex)
- case 'd':
- fmt.Fprint(s, ([len(a)]byte)(a))
- default:
- fmt.Fprintf(s, "%%!%c(address=%x)", c, a)
- }
-}
-
-// SetBytes sets the address to the value of b.
-// If b is larger than len(a), b will be cropped from the left.
-func (a *Address) SetBytes(b []byte) {
- if len(b) > len(a) {
- b = b[len(b)-AddressLength:]
- }
- copy(a[AddressLength-len(b):], b)
-}
-
-// MarshalText returns the hex representation of a.
-func (a Address) MarshalText() ([]byte, error) {
- return hexutil.Bytes(a[:]).MarshalText()
-}
-
-// UnmarshalText parses a hash in hex syntax.
-func (a *Address) UnmarshalText(input []byte) error {
- return hexutil.UnmarshalFixedText("Address", input, a[:])
-}
-
-// UnmarshalJSON parses a hash in hex syntax.
-func (a *Address) UnmarshalJSON(input []byte) error {
- return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
-}
-
-// Scan implements Scanner for database/sql.
-func (a *Address) Scan(src interface{}) error {
- srcB, ok := src.([]byte)
- if !ok {
- return fmt.Errorf("can't scan %T into Address", src)
- }
- if len(srcB) != AddressLength {
- return fmt.Errorf("can't scan []byte of len %d into Address, want %d", len(srcB), AddressLength)
- }
- copy(a[:], srcB)
- return nil
-}
-
-// Value implements valuer for database/sql.
-func (a Address) Value() (driver.Value, error) {
- return a[:], nil
-}
-
-// ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.
-func (a Address) ImplementsGraphQLType(name string) bool { return name == "Address" }
-
-// UnmarshalGraphQL unmarshals the provided GraphQL query data.
-func (a *Address) UnmarshalGraphQL(input interface{}) error {
- var err error
- switch input := input.(type) {
- case string:
- err = a.UnmarshalText([]byte(input))
- default:
- err = fmt.Errorf("unexpected type %T for Address", input)
- }
- return err
-}
-
-// UnprefixedAddress allows marshaling an Address without 0x prefix.
-type UnprefixedAddress Address
-
-// UnmarshalText decodes the address from hex. The 0x prefix is optional.
-func (a *UnprefixedAddress) UnmarshalText(input []byte) error {
- return hexutil.UnmarshalFixedUnprefixedText("UnprefixedAddress", input, a[:])
-}
-
-// MarshalText encodes the address as hex.
-func (a UnprefixedAddress) MarshalText() ([]byte, error) {
- return []byte(hex.EncodeToString(a[:])), nil
-}
-
-// MixedcaseAddress retains the original string, which may or may not be
-// correctly checksummed
-type MixedcaseAddress struct {
- addr Address
- original string
-}
-
-// NewMixedcaseAddress constructor (mainly for testing)
-func NewMixedcaseAddress(addr Address) MixedcaseAddress {
- return MixedcaseAddress{addr: addr, original: addr.Hex()}
-}
-
-// NewMixedcaseAddressFromString is mainly meant for unit-testing
-func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error) {
- if !IsHexAddress(hexaddr) {
- return nil, errors.New("invalid address")
- }
- a := FromHex(hexaddr)
- return &MixedcaseAddress{addr: BytesToAddress(a), original: hexaddr}, nil
-}
-
-// UnmarshalJSON parses MixedcaseAddress
-func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error {
- if err := hexutil.UnmarshalFixedJSON(addressT, input, ma.addr[:]); err != nil {
- return err
- }
- return json.Unmarshal(input, &ma.original)
-}
-
-// MarshalJSON marshals the original value
-func (ma MixedcaseAddress) MarshalJSON() ([]byte, error) {
- if strings.HasPrefix(ma.original, "0x") || strings.HasPrefix(ma.original, "0X") {
- return json.Marshal(fmt.Sprintf("0x%s", ma.original[2:]))
- }
- return json.Marshal(fmt.Sprintf("0x%s", ma.original))
-}
-
-// Address returns the address
-func (ma *MixedcaseAddress) Address() Address {
- return ma.addr
-}
-
-// String implements fmt.Stringer
-func (ma *MixedcaseAddress) String() string {
- if ma.ValidChecksum() {
- return fmt.Sprintf("%s [chksum ok]", ma.original)
- }
- return fmt.Sprintf("%s [chksum INVALID]", ma.original)
-}
-
-// ValidChecksum returns true if the address has valid checksum
-func (ma *MixedcaseAddress) ValidChecksum() bool {
- return ma.original == ma.addr.Hex()
-}
-
-// Original returns the mixed-case input string
-func (ma *MixedcaseAddress) Original() string {
- return ma.original
-}
-
-// AddressEIP55 is an alias of Address with a customized json marshaller
-type AddressEIP55 Address
-
-// String returns the hex representation of the address in the manner of EIP55.
-func (addr AddressEIP55) String() string {
- return Address(addr).Hex()
-}
-
-// MarshalJSON marshals the address in the manner of EIP55.
-func (addr AddressEIP55) MarshalJSON() ([]byte, error) {
- return json.Marshal(addr.String())
-}
-
-type Decimal uint64
-
-func isString(input []byte) bool {
- return len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"'
-}
-
-// UnmarshalJSON parses a hash in hex syntax.
-func (d *Decimal) UnmarshalJSON(input []byte) error {
- if !isString(input) {
- return &json.UnmarshalTypeError{Value: "non-string", Type: reflect.TypeOf(uint64(0))}
- }
- if i, err := strconv.ParseInt(string(input[1:len(input)-1]), 10, 64); err == nil {
- *d = Decimal(i)
- return nil
- } else {
- return err
- }
-}
diff --git a/common/types_test.go b/common/types_test.go
deleted file mode 100644
index cec689ea39..0000000000
--- a/common/types_test.go
+++ /dev/null
@@ -1,597 +0,0 @@
-// 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 .
-
-package common
-
-import (
- "bytes"
- "database/sql/driver"
- "encoding/json"
- "fmt"
- "math/big"
- "reflect"
- "strings"
- "testing"
- "time"
-)
-
-func TestBytesConversion(t *testing.T) {
- bytes := []byte{5}
- hash := BytesToHash(bytes)
-
- var exp Hash
- exp[31] = 5
-
- if hash != exp {
- t.Errorf("expected %x got %x", exp, hash)
- }
-}
-
-func TestIsHexAddress(t *testing.T) {
- tests := []struct {
- str string
- exp bool
- }{
- {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
- {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
- {"0X5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", true},
- {"0XAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
- {"0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", true},
- {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed1", false},
- {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beae", false},
- {"5aaeb6053f3e94c9b9a09f33669435e7ef1beaed11", false},
- {"0xxaaeb6053f3e94c9b9a09f33669435e7ef1beaed", false},
- }
-
- for _, test := range tests {
- if result := IsHexAddress(test.str); result != test.exp {
- t.Errorf("IsHexAddress(%s) == %v; expected %v",
- test.str, result, test.exp)
- }
- }
-}
-
-func TestHashJsonValidation(t *testing.T) {
- var tests = []struct {
- Prefix string
- Size int
- Error string
- }{
- {"", 62, "json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash"},
- {"0x", 66, "hex string has length 66, want 64 for common.Hash"},
- {"0x", 63, "json: cannot unmarshal hex string of odd length into Go value of type common.Hash"},
- {"0x", 0, "hex string has length 0, want 64 for common.Hash"},
- {"0x", 64, ""},
- {"0X", 64, ""},
- }
- for _, test := range tests {
- input := `"` + test.Prefix + strings.Repeat("0", test.Size) + `"`
- var v Hash
- err := json.Unmarshal([]byte(input), &v)
- if err == nil {
- if test.Error != "" {
- t.Errorf("%s: error mismatch: have nil, want %q", input, test.Error)
- }
- } else {
- if err.Error() != test.Error {
- t.Errorf("%s: error mismatch: have %q, want %q", input, err, test.Error)
- }
- }
- }
-}
-
-func TestAddressUnmarshalJSON(t *testing.T) {
- var tests = []struct {
- Input string
- ShouldErr bool
- Output *big.Int
- }{
- {"", true, nil},
- {`""`, true, nil},
- {`"0x"`, true, nil},
- {`"0x00"`, true, nil},
- {`"0xG000000000000000000000000000000000000000"`, true, nil},
- {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)},
- {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)},
- }
- for i, test := range tests {
- var v Address
- err := json.Unmarshal([]byte(test.Input), &v)
- if err != nil && !test.ShouldErr {
- t.Errorf("test #%d: unexpected error: %v", i, err)
- }
- if err == nil {
- if test.ShouldErr {
- t.Errorf("test #%d: expected error, got none", i)
- }
- if got := new(big.Int).SetBytes(v.Bytes()); got.Cmp(test.Output) != 0 {
- t.Errorf("test #%d: address mismatch: have %v, want %v", i, got, test.Output)
- }
- }
- }
-}
-
-func TestAddressHexChecksum(t *testing.T) {
- var tests = []struct {
- Input string
- Output string
- }{
- // Test cases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#specification
- {"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"},
- {"0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"},
- {"0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"},
- {"0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"},
- // Ensure that non-standard length input values are handled correctly
- {"0xa", "0x000000000000000000000000000000000000000A"},
- {"0x0a", "0x000000000000000000000000000000000000000A"},
- {"0x00a", "0x000000000000000000000000000000000000000A"},
- {"0x000000000000000000000000000000000000000a", "0x000000000000000000000000000000000000000A"},
- }
- for i, test := range tests {
- output := HexToAddress(test.Input).Hex()
- if output != test.Output {
- t.Errorf("test #%d: failed to match when it should (%s != %s)", i, output, test.Output)
- }
- }
-}
-
-func BenchmarkAddressHex(b *testing.B) {
- testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
- for n := 0; n < b.N; n++ {
- testAddr.Hex()
- }
-}
-
-// Test checks if the customized json marshaller of MixedcaseAddress object
-// is invoked correctly. In golang the struct pointer will inherit the
-// non-pointer receiver methods, the reverse is not true. In the case of
-// MixedcaseAddress, it must define the MarshalJSON method in the object
-// but not the pointer level, so that this customized marshalled can be used
-// for both MixedcaseAddress object and pointer.
-func TestMixedcaseAddressMarshal(t *testing.T) {
- var (
- output string
- input = "0xae967917c465db8578ca9024c205720b1a3651A9"
- )
- addr, err := NewMixedcaseAddressFromString(input)
- if err != nil {
- t.Fatal(err)
- }
- blob, err := json.Marshal(*addr)
- if err != nil {
- t.Fatal(err)
- }
- json.Unmarshal(blob, &output)
- if output != input {
- t.Fatal("Failed to marshal/unmarshal MixedcaseAddress object")
- }
-}
-
-func TestMixedcaseAccount_Address(t *testing.T) {
- // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
- // Note: 0X{checksum_addr} is not valid according to spec above
-
- var res []struct {
- A MixedcaseAddress
- Valid bool
- }
- if err := json.Unmarshal([]byte(`[
- {"A" : "0xae967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
- {"A" : "0xAe967917c465db8578ca9024c205720b1a3651A9", "Valid": true},
- {"A" : "0XAe967917c465db8578ca9024c205720b1a3651A9", "Valid": false},
- {"A" : "0x1111111111111111111112222222222223333323", "Valid": true}
- ]`), &res); err != nil {
- t.Fatal(err)
- }
-
- for _, r := range res {
- if got := r.A.ValidChecksum(); got != r.Valid {
- t.Errorf("Expected checksum %v, got checksum %v, input %v", r.Valid, got, r.A.String())
- }
- }
-
- // These should throw exceptions:
- var r2 []MixedcaseAddress
- for _, r := range []string{
- `["0x11111111111111111111122222222222233333"]`, // Too short
- `["0x111111111111111111111222222222222333332"]`, // Too short
- `["0x11111111111111111111122222222222233333234"]`, // Too long
- `["0x111111111111111111111222222222222333332344"]`, // Too long
- `["1111111111111111111112222222222223333323"]`, // Missing 0x
- `["x1111111111111111111112222222222223333323"]`, // Missing 0
- `["0xG111111111111111111112222222222223333323"]`, //Non-hex
- } {
- if err := json.Unmarshal([]byte(r), &r2); err == nil {
- t.Errorf("Expected failure, input %v", r)
- }
- }
-}
-
-func TestHash_Scan(t *testing.T) {
- type args struct {
- src interface{}
- }
- tests := []struct {
- name string
- args args
- wantErr bool
- }{
- {
- name: "working scan",
- args: args{src: []byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- 0x10, 0x00,
- }},
- wantErr: false,
- },
- {
- name: "non working scan",
- args: args{src: int64(1234567890)},
- wantErr: true,
- },
- {
- name: "invalid length scan",
- args: args{src: []byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- }},
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- h := &Hash{}
- if err := h.Scan(tt.args.src); (err != nil) != tt.wantErr {
- t.Errorf("Hash.Scan() error = %v, wantErr %v", err, tt.wantErr)
- }
-
- if !tt.wantErr {
- for i := range h {
- if h[i] != tt.args.src.([]byte)[i] {
- t.Errorf(
- "Hash.Scan() didn't scan the %d src correctly (have %X, want %X)",
- i, h[i], tt.args.src.([]byte)[i],
- )
- }
- }
- }
- })
- }
-}
-
-func TestHash_Value(t *testing.T) {
- b := []byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- 0x10, 0x00,
- }
- var usedH Hash
- usedH.SetBytes(b)
- tests := []struct {
- name string
- h Hash
- want driver.Value
- wantErr bool
- }{
- {
- name: "Working value",
- h: usedH,
- want: b,
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := tt.h.Value()
- if (err != nil) != tt.wantErr {
- t.Errorf("Hash.Value() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Hash.Value() = %v, want %v", got, tt.want)
- }
- })
- }
-}
-
-func TestAddress_Scan(t *testing.T) {
- type args struct {
- src interface{}
- }
- tests := []struct {
- name string
- args args
- wantErr bool
- }{
- {
- name: "working scan",
- args: args{src: []byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- }},
- wantErr: false,
- },
- {
- name: "non working scan",
- args: args{src: int64(1234567890)},
- wantErr: true,
- },
- {
- name: "invalid length scan",
- args: args{src: []byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a,
- }},
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- a := &Address{}
- if err := a.Scan(tt.args.src); (err != nil) != tt.wantErr {
- t.Errorf("Address.Scan() error = %v, wantErr %v", err, tt.wantErr)
- }
-
- if !tt.wantErr {
- for i := range a {
- if a[i] != tt.args.src.([]byte)[i] {
- t.Errorf(
- "Address.Scan() didn't scan the %d src correctly (have %X, want %X)",
- i, a[i], tt.args.src.([]byte)[i],
- )
- }
- }
- }
- })
- }
-}
-
-func TestAddress_Value(t *testing.T) {
- b := []byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- }
- var usedA Address
- usedA.SetBytes(b)
- tests := []struct {
- name string
- a Address
- want driver.Value
- wantErr bool
- }{
- {
- name: "Working value",
- a: usedA,
- want: b,
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := tt.a.Value()
- if (err != nil) != tt.wantErr {
- t.Errorf("Address.Value() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Address.Value() = %v, want %v", got, tt.want)
- }
- })
- }
-}
-
-func TestAddress_Format(t *testing.T) {
- b := []byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- }
- var addr Address
- addr.SetBytes(b)
-
- tests := []struct {
- name string
- out string
- want string
- }{
- {
- name: "println",
- out: fmt.Sprintln(addr),
- want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15\n",
- },
- {
- name: "print",
- out: fmt.Sprint(addr),
- want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15",
- },
- {
- name: "printf-s",
- out: func() string {
- buf := new(bytes.Buffer)
- fmt.Fprintf(buf, "%s", addr)
- return buf.String()
- }(),
- want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15",
- },
- {
- name: "printf-q",
- out: fmt.Sprintf("%q", addr),
- want: `"0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15"`,
- },
- {
- name: "printf-x",
- out: fmt.Sprintf("%x", addr),
- want: "b26f2b342aab24bcf63ea218c6a9274d30ab9a15",
- },
- {
- name: "printf-X",
- out: fmt.Sprintf("%X", addr),
- want: "B26F2B342AAB24BCF63EA218C6A9274D30AB9A15",
- },
- {
- name: "printf-#x",
- out: fmt.Sprintf("%#x", addr),
- want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15",
- },
- {
- name: "printf-v",
- out: fmt.Sprintf("%v", addr),
- want: "0xB26f2b342AAb24BCF63ea218c6A9274D30Ab9A15",
- },
- // The original default formatter for byte slice
- {
- name: "printf-d",
- out: fmt.Sprintf("%d", addr),
- want: "[178 111 43 52 42 171 36 188 246 62 162 24 198 169 39 77 48 171 154 21]",
- },
- // Invalid format char.
- {
- name: "printf-t",
- out: fmt.Sprintf("%t", addr),
- want: "%!t(address=b26f2b342aab24bcf63ea218c6a9274d30ab9a15)",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if tt.out != tt.want {
- t.Errorf("%s does not render as expected:\n got %s\nwant %s", tt.name, tt.out, tt.want)
- }
- })
- }
-}
-
-func TestHash_Format(t *testing.T) {
- var hash Hash
- hash.SetBytes([]byte{
- 0xb2, 0x6f, 0x2b, 0x34, 0x2a, 0xab, 0x24, 0xbc, 0xf6, 0x3e,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- 0xa2, 0x18, 0xc6, 0xa9, 0x27, 0x4d, 0x30, 0xab, 0x9a, 0x15,
- 0x10, 0x00,
- })
-
- tests := []struct {
- name string
- out string
- want string
- }{
- {
- name: "println",
- out: fmt.Sprintln(hash),
- want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000\n",
- },
- {
- name: "print",
- out: fmt.Sprint(hash),
- want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
- },
- {
- name: "printf-s",
- out: func() string {
- buf := new(bytes.Buffer)
- fmt.Fprintf(buf, "%s", hash)
- return buf.String()
- }(),
- want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
- },
- {
- name: "printf-q",
- out: fmt.Sprintf("%q", hash),
- want: `"0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000"`,
- },
- {
- name: "printf-x",
- out: fmt.Sprintf("%x", hash),
- want: "b26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
- },
- {
- name: "printf-X",
- out: fmt.Sprintf("%X", hash),
- want: "B26F2B342AAB24BCF63EA218C6A9274D30AB9A15A218C6A9274D30AB9A151000",
- },
- {
- name: "printf-#x",
- out: fmt.Sprintf("%#x", hash),
- want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
- },
- {
- name: "printf-#X",
- out: fmt.Sprintf("%#X", hash),
- want: "0XB26F2B342AAB24BCF63EA218C6A9274D30AB9A15A218C6A9274D30AB9A151000",
- },
- {
- name: "printf-v",
- out: fmt.Sprintf("%v", hash),
- want: "0xb26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000",
- },
- // The original default formatter for byte slice
- {
- name: "printf-d",
- out: fmt.Sprintf("%d", hash),
- want: "[178 111 43 52 42 171 36 188 246 62 162 24 198 169 39 77 48 171 154 21 162 24 198 169 39 77 48 171 154 21 16 0]",
- },
- // Invalid format char.
- {
- name: "printf-t",
- out: fmt.Sprintf("%t", hash),
- want: "%!t(hash=b26f2b342aab24bcf63ea218c6a9274d30ab9a15a218c6a9274d30ab9a151000)",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if tt.out != tt.want {
- t.Errorf("%s does not render as expected:\n got %s\nwant %s", tt.name, tt.out, tt.want)
- }
- })
- }
-}
-
-func TestAddressEIP55(t *testing.T) {
- addr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
- addrEIP55 := AddressEIP55(addr)
-
- if addr.Hex() != addrEIP55.String() {
- t.Fatal("AddressEIP55 should match original address hex")
- }
-
- blob, err := addrEIP55.MarshalJSON()
- if err != nil {
- t.Fatal("Failed to marshal AddressEIP55", err)
- }
- if strings.Trim(string(blob), "\"") != addr.Hex() {
- t.Fatal("Address with checksum is expected")
- }
- var dec Address
- if err := json.Unmarshal(blob, &dec); err != nil {
- t.Fatal("Failed to unmarshal AddressEIP55", err)
- }
- if addr != dec {
- t.Fatal("Unexpected address after unmarshal")
- }
-}
-
-func BenchmarkPrettyDuration(b *testing.B) {
- var x = PrettyDuration(time.Duration(int64(1203123912312)))
- b.Logf("Pre %s", time.Duration(x).String())
- var a string
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- a = x.String()
- }
- b.Logf("Post %s", a)
-}