From a570060a1b98c3a652aecd8c72cca496a5fa9e30 Mon Sep 17 00:00:00 2001 From: Kiel barry Date: Mon, 7 May 2018 16:08:50 -0700 Subject: [PATCH 1/7] common: fixes with gofmt for travisCI build --- common/bytes.go | 14 ++++++++++--- common/path.go | 2 ++ common/types.go | 54 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index e2adc80059fb..2892cc035aa4 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -19,6 +19,7 @@ package common import "encoding/hex" +// ToHex returns string representation of b and either prepends '0x' or initializes value of '0'. func ToHex(b []byte) string { hex := Bytes2Hex(b) // Prefer output of "0x0" instead of "0x" @@ -28,6 +29,7 @@ func ToHex(b []byte) string { return "0x" + hex } +//FromHex returns bytes of s after removing prefix, or prepends 0 if s has odd length. func FromHex(s string) []byte { if len(s) > 1 { if s[0:2] == "0x" || s[0:2] == "0X" { @@ -40,9 +42,7 @@ func FromHex(s string) []byte { return Hex2Bytes(s) } -// Copy bytes -// -// Returns an exact copy of the provided bytes +// CopyBytes returns an exact copy of the provided bytes func CopyBytes(b []byte) (copiedBytes []byte) { if b == nil { return nil @@ -53,14 +53,17 @@ func CopyBytes(b []byte) (copiedBytes []byte) { return } +// hasHexPrefix validates str begins with '0x' or '0X'. func hasHexPrefix(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 @@ -73,16 +76,19 @@ func isHex(str string) bool { 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 { @@ -96,6 +102,7 @@ func Hex2BytesFixed(str string, flen int) []byte { return hh } +// RightPadBytes copies slice into a byte slice of length l. func RightPadBytes(slice []byte, l int) []byte { if l <= len(slice) { return slice @@ -107,6 +114,7 @@ func RightPadBytes(slice []byte, l int) []byte { return padded } +// LeftPadBytes copies via inverted index slice into a byte slice of length l. func LeftPadBytes(slice []byte, l int) []byte { if l <= len(slice) { return slice diff --git a/common/path.go b/common/path.go index bd8da86e749e..f848f47f2d4e 100644 --- a/common/path.go +++ b/common/path.go @@ -30,6 +30,7 @@ func MakeName(name, version string) string { return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version()) } +// FileExist checks if a file exists at filePath and returns a bool. func FileExist(filePath string) bool { _, err := os.Stat(filePath) if err != nil && os.IsNotExist(err) { @@ -39,6 +40,7 @@ func FileExist(filePath string) bool { 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 diff --git a/common/types.go b/common/types.go index 0b94fb2c250b..1cb0ce170ce9 100644 --- a/common/types.go +++ b/common/types.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/crypto/sha3" ) +// Sets global constants. const ( HashLength = 32 AddressLength = 20 @@ -42,19 +43,30 @@ var ( // 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()) } -func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) } -// Get the string representation of the underlying hash -func (h Hash) Str() string { return string(h[:]) } +// 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)) } + +// Str gets the string representation of the underlying hash. +func (h Hash) Str() string { return string(h[:]) } + +// Bytes gets the byte representation of the underlying hash. func (h Hash) Bytes() []byte { return h[:] } + +// Big gets the big integer representation of the underlying hash. func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) } -func (h Hash) Hex() string { return hexutil.Encode(h[:]) } + +// Hex gets the hex representation of the underlying hash. +func (h Hash) Hex() string { return hexutil.Encode(h[:]) } // TerminalString implements log.TerminalStringer, formatting a string for console // output during logging. @@ -89,7 +101,7 @@ func (h Hash) MarshalText() ([]byte, error) { return hexutil.Bytes(h[:]).MarshalText() } -// Sets the hash to the value of b. If b is larger than len(h), 'b' will be cropped (from the left). +// 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:] @@ -98,10 +110,10 @@ func (h *Hash) SetBytes(b []byte) { copy(h[HashLength-len(b):], b) } -// Set string `s` to h. If s is larger than len(h) s will be cropped (from left) to fit. +// SetString sets `s` to h. If s is larger than len(h) s will be cropped (from left) to fit. func (h *Hash) SetString(s string) { h.SetBytes([]byte(s)) } -// Sets h to other +// Set sets h to other func (h *Hash) Set(other Hash) { for i, v := range other { h[i] = v @@ -117,6 +129,7 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { return reflect.ValueOf(h) } +// EmptyHash clears h of values. func EmptyHash(h Hash) bool { return h == Hash{} } @@ -139,13 +152,18 @@ func (h UnprefixedHash) MarshalText() ([]byte, error) { // 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()) } -func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } + +//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. @@ -156,11 +174,17 @@ func IsHexAddress(s string) bool { return len(s) == 2*AddressLength && isHex(s) } -// Get the string representation of the underlying address -func (a Address) Str() string { return string(a[:]) } +// Str gets the string representation of the underlying address. +func (a Address) Str() string { return string(a[:]) } + +// Bytes gets the string representation of the underlying address. func (a Address) Bytes() []byte { return a[:] } + +// Big gets the string representation of the underlying address. func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) } -func (a Address) Hash() Hash { return BytesToHash(a[:]) } + +// Hash gets the string representation of the underlying address. +func (a Address) Hash() Hash { return BytesToHash(a[:]) } // Hex returns an EIP55-compliant hex string representation of the address. func (a Address) Hex() string { @@ -195,7 +219,7 @@ func (a Address) Format(s fmt.State, c rune) { fmt.Fprintf(s, "%"+string(c), a[:]) } -// Sets the address to the value of b. If b is larger than len(a) it will panic +// SetBytes sets the address to the value of b. If b is larger than len(a) it will panic func (a *Address) SetBytes(b []byte) { if len(b) > len(a) { b = b[len(b)-AddressLength:] @@ -203,10 +227,10 @@ func (a *Address) SetBytes(b []byte) { copy(a[AddressLength-len(b):], b) } -// Set string `s` to a. If s is larger than len(a) it will panic +// SetString sets `s` to a. If s is larger than len(a) it will panic func (a *Address) SetString(s string) { a.SetBytes([]byte(s)) } -// Sets a to other +// Set sets a to other func (a *Address) Set(other Address) { for i, v := range other { a[i] = v @@ -228,7 +252,7 @@ func (a *Address) UnmarshalJSON(input []byte) error { return hexutil.UnmarshalFixedJSON(addressT, input, a[:]) } -// UnprefixedHash allows marshaling an Address without 0x prefix. +// UnprefixedAddress allows marshaling an Address without 0x prefix. type UnprefixedAddress Address // UnmarshalText decodes the address from hex. The 0x prefix is optional. From d9c8cbe51f8e1bc559ac3882b51e0211e7205791 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 8 May 2018 15:17:03 +0200 Subject: [PATCH 2/7] common: improve comments --- common/bytes.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index 2892cc035aa4..bb605d182f9a 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -19,17 +19,18 @@ package common import "encoding/hex" -// ToHex returns string representation of b and either prepends '0x' or initializes value of '0'. +// ToHex returns the hex representation of b, prefixed with '0x'. +// For empty slices, the return value is "0x0". func ToHex(b []byte) string { hex := Bytes2Hex(b) - // Prefer output of "0x0" instead of "0x" if len(hex) == 0 { hex = "0" } return "0x" + hex } -//FromHex returns bytes of s after removing prefix, or prepends 0 if s has odd length. +// FromHex returns the bytes represented by the hexadecimal string s. +// s may be prefixed with "0x". func FromHex(s string) []byte { if len(s) > 1 { if s[0:2] == "0x" || s[0:2] == "0X" { @@ -42,7 +43,7 @@ func FromHex(s string) []byte { return Hex2Bytes(s) } -// CopyBytes returns an exact copy of the provided bytes +// CopyBytes returns an exact copy of the provided bytes. func CopyBytes(b []byte) (copiedBytes []byte) { if b == nil { return nil @@ -76,7 +77,7 @@ func isHex(str string) bool { return true } -//Bytes2Hex returns the hexadecimal encoding of d. +// Bytes2Hex returns the hexadecimal encoding of d. func Bytes2Hex(d []byte) string { return hex.EncodeToString(d) } @@ -84,7 +85,6 @@ func Bytes2Hex(d []byte) string { // Hex2Bytes returns the bytes represented by the hexadecimal string str. func Hex2Bytes(str string) []byte { h, _ := hex.DecodeString(str) - return h } @@ -102,7 +102,7 @@ func Hex2BytesFixed(str string, flen int) []byte { return hh } -// RightPadBytes copies slice into a byte slice of length l. +// RightPadBytes zero-pads slice to the right up to length l. func RightPadBytes(slice []byte, l int) []byte { if l <= len(slice) { return slice @@ -114,7 +114,7 @@ func RightPadBytes(slice []byte, l int) []byte { return padded } -// LeftPadBytes copies via inverted index slice into a byte slice of length l. +// LeftPadBytes zero-pads slice to the left up to length l. func LeftPadBytes(slice []byte, l int) []byte { if l <= len(slice) { return slice From 1e61e5dcd348b70737543b08a072e06d5df13ed6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 8 May 2018 16:34:03 +0200 Subject: [PATCH 3/7] common: improve more comments Also remove unused methods on Hash and Address. --- common/bytes.go | 2 ++ common/path.go | 2 +- common/types.go | 63 ++++++++++++++------------------------- common/types_template.go | 64 ---------------------------------------- 4 files changed, 25 insertions(+), 106 deletions(-) delete mode 100644 common/types_template.go diff --git a/common/bytes.go b/common/bytes.go index bb605d182f9a..cbab2c3fa9f0 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -21,6 +21,8 @@ import "encoding/hex" // ToHex returns the hex representation of b, prefixed with '0x'. // For empty slices, the return value is "0x0". +// +// Deprecated: use hexutil.Encode instead. func ToHex(b []byte) string { hex := Bytes2Hex(b) if len(hex) == 0 { diff --git a/common/path.go b/common/path.go index f848f47f2d4e..6d0c92caabac 100644 --- a/common/path.go +++ b/common/path.go @@ -30,7 +30,7 @@ func MakeName(name, version string) string { return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version()) } -// FileExist checks if a file exists at filePath and returns a bool. +// FileExist checks if a file exists at filePath. func FileExist(filePath string) bool { _, err := os.Stat(filePath) if err != nil && os.IsNotExist(err) { diff --git a/common/types.go b/common/types.go index 1cb0ce170ce9..2d6a5ce48ef8 100644 --- a/common/types.go +++ b/common/types.go @@ -29,7 +29,6 @@ import ( "github.com/ethereum/go-ethereum/crypto/sha3" ) -// Sets global constants. const ( HashLength = 32 AddressLength = 20 @@ -43,29 +42,29 @@ var ( // 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). +// 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). +// 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). +// 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)) } -// Str gets the string representation of the underlying hash. -func (h Hash) Str() string { return string(h[:]) } - // Bytes gets the byte representation of the underlying hash. func (h Hash) Bytes() []byte { return h[:] } -// Big gets the big integer representation of the underlying hash. +// Big converts a hash to a big integer. func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) } -// Hex gets the hex representation of the underlying hash. +// 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 @@ -101,7 +100,8 @@ 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). +// 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:] @@ -110,16 +110,6 @@ func (h *Hash) SetBytes(b []byte) { copy(h[HashLength-len(b):], b) } -// SetString sets `s` to h. If s is larger than len(h) s will be cropped (from left) to fit. -func (h *Hash) SetString(s string) { h.SetBytes([]byte(s)) } - -// Set sets h to other -func (h *Hash) Set(other Hash) { - for i, v := range other { - h[i] = v - } -} - // Generate implements testing/quick.Generator. func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { m := rand.Intn(len(h)) @@ -129,7 +119,7 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { return reflect.ValueOf(h) } -// EmptyHash clears h of values. +// EmptyHash checks whether h is the zero hash. func EmptyHash(h Hash) bool { return h == Hash{} } @@ -152,17 +142,20 @@ func (h UnprefixedHash) MarshalText() ([]byte, error) { // 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). +// 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). +// 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). +// 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 @@ -174,16 +167,13 @@ func IsHexAddress(s string) bool { return len(s) == 2*AddressLength && isHex(s) } -// Str gets the string representation of the underlying address. -func (a Address) Str() string { return string(a[:]) } - // Bytes gets the string representation of the underlying address. func (a Address) Bytes() []byte { return a[:] } -// Big gets the string representation of the underlying address. +// Big converts an address to a big integer. func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) } -// Hash gets the string representation of the underlying address. +// Hash converts an address to a hash by left-padding it with zeros. func (a Address) Hash() Hash { return BytesToHash(a[:]) } // Hex returns an EIP55-compliant hex string representation of the address. @@ -208,7 +198,7 @@ func (a Address) Hex() string { return "0x" + string(result) } -// String implements the stringer interface and is used also by the logger. +// String implements fmt.Stringer. func (a Address) String() string { return a.Hex() } @@ -219,7 +209,8 @@ func (a Address) Format(s fmt.State, c rune) { fmt.Fprintf(s, "%"+string(c), a[:]) } -// SetBytes sets the address to the value of b. If b is larger than len(a) it will panic +// SetBytes sets the address to the value of b. +// If b is larger than len(a) it will panic. func (a *Address) SetBytes(b []byte) { if len(b) > len(a) { b = b[len(b)-AddressLength:] @@ -227,16 +218,6 @@ func (a *Address) SetBytes(b []byte) { copy(a[AddressLength-len(b):], b) } -// SetString sets `s` to a. If s is larger than len(a) it will panic -func (a *Address) SetString(s string) { a.SetBytes([]byte(s)) } - -// Set sets a to other -func (a *Address) Set(other Address) { - for i, v := range other { - a[i] = v - } -} - // MarshalText returns the hex representation of a. func (a Address) MarshalText() ([]byte, error) { return hexutil.Bytes(a[:]).MarshalText() diff --git a/common/types_template.go b/common/types_template.go deleted file mode 100644 index 9a8f29977b6a..000000000000 --- a/common/types_template.go +++ /dev/null @@ -1,64 +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 . - -// +build none -//sed -e 's/_N_/Hash/g' -e 's/_S_/32/g' -e '1d' types_template.go | gofmt -w hash.go - -package common - -import "math/big" - -type _N_ [_S_]byte - -func BytesTo_N_(b []byte) _N_ { - var h _N_ - h.SetBytes(b) - return h -} -func StringTo_N_(s string) _N_ { return BytesTo_N_([]byte(s)) } -func BigTo_N_(b *big.Int) _N_ { return BytesTo_N_(b.Bytes()) } -func HexTo_N_(s string) _N_ { return BytesTo_N_(FromHex(s)) } - -// Don't use the default 'String' method in case we want to overwrite - -// Get the string representation of the underlying hash -func (h _N_) Str() string { return string(h[:]) } -func (h _N_) Bytes() []byte { return h[:] } -func (h _N_) Big() *big.Int { return new(big.Int).SetBytes(h[:]) } -func (h _N_) Hex() string { return "0x" + Bytes2Hex(h[:]) } - -// Sets the hash to the value of b. If b is larger than len(h) it will panic -func (h *_N_) SetBytes(b []byte) { - // Use the right most bytes - if len(b) > len(h) { - b = b[len(b)-_S_:] - } - - // Reverse the loop - for i := len(b) - 1; i >= 0; i-- { - h[_S_-len(b)+i] = b[i] - } -} - -// Set string `s` to h. If s is larger than len(h) it will panic -func (h *_N_) SetString(s string) { h.SetBytes([]byte(s)) } - -// Sets h to other -func (h *_N_) Set(other _N_) { - for i, v := range other { - h[i] = v - } -} From 9e384f56588ab35858fc9d598a9c37f2540b6991 Mon Sep 17 00:00:00 2001 From: kiel barry Date: Mon, 14 May 2018 10:18:55 -0700 Subject: [PATCH 4/7] common: pr updates --- common/types.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/common/types.go b/common/types.go index 2d6a5ce48ef8..12c26d94bcfc 100644 --- a/common/types.go +++ b/common/types.go @@ -119,11 +119,6 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { return reflect.ValueOf(h) } -// EmptyHash checks whether h is the zero hash. -func EmptyHash(h Hash) bool { - return h == Hash{} -} - // UnprefixedHash allows marshaling a Hash without 0x prefix. type UnprefixedHash Hash From e344ba8653f9cd6d038d755639ba532731f6e734 Mon Sep 17 00:00:00 2001 From: kiel barry Date: Mon, 14 May 2018 10:19:58 -0700 Subject: [PATCH 5/7] common: pr update for datadir --- common/path.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/path.go b/common/path.go index 6d0c92caabac..69820cfe5dec 100644 --- a/common/path.go +++ b/common/path.go @@ -40,10 +40,10 @@ func FileExist(filePath string) bool { return true } -// AbsolutePath returns Datadir + filename, or filename if it is absolute. -func AbsolutePath(Datadir string, filename string) string { +// 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) + return filepath.Join(datadir, filename) } From ed2fe4da18947b0e415acc6163a8ca74a15643b7 Mon Sep 17 00:00:00 2001 From: Kiel barry Date: Tue, 15 May 2018 17:06:19 -0700 Subject: [PATCH 6/7] common: various golint fixes --- common/math/big.go | 5 ++--- common/number/int.go | 43 +++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/common/math/big.go b/common/math/big.go index 78727865032a..dbf2770a94f5 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -78,7 +78,7 @@ func ParseBig256(s string) (*big.Int, bool) { return bigint, ok } -// MustParseBig parses s as a 256 bit big integer and panics if the string is invalid. +// 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 { @@ -186,9 +186,8 @@ func U256(x *big.Int) *big.Int { func S256(x *big.Int) *big.Int { if x.Cmp(tt255) < 0 { return x - } else { - return new(big.Int).Sub(x, tt256) } + return new(big.Int).Sub(x, tt256) } // Exp implements exponentiation by squaring. diff --git a/common/number/int.go b/common/number/int.go index 6dab2436de04..5b50669703e2 100644 --- a/common/number/int.go +++ b/common/number/int.go @@ -34,13 +34,12 @@ func limitUnsigned256(x *Number) *Number { func limitSigned256(x *Number) *Number { if x.num.Cmp(tt255) < 0 { return x - } else { - x.num.Sub(x.num, tt256) - return x } + x.num.Sub(x.num, tt256) + return x } -// Number function +// Initialiser is a Number function type Initialiser func(n int64) *Number // A Number represents a generic integer with a bounding function limiter. Limit is called after each operations @@ -51,65 +50,65 @@ type Number struct { limit func(n *Number) *Number } -// Returns a new initialiser for a new *Number without having to expose certain fields +// NewInitialiser returns a new initialiser for a new *Number without having to expose certain fields func NewInitialiser(limiter func(*Number) *Number) Initialiser { return func(n int64) *Number { return &Number{big.NewInt(n), limiter} } } -// Return a Number with a UNSIGNED limiter up to 256 bits +// Uint256 returns a Number with a UNSIGNED limiter up to 256 bits func Uint256(n int64) *Number { return &Number{big.NewInt(n), limitUnsigned256} } -// Return a Number with a SIGNED limiter up to 256 bits +// Int256 returns Number with a SIGNED limiter up to 256 bits func Int256(n int64) *Number { return &Number{big.NewInt(n), limitSigned256} } -// Returns a Number with a SIGNED unlimited size +// Big returns a Number with a SIGNED unlimited size func Big(n int64) *Number { return &Number{big.NewInt(n), func(x *Number) *Number { return x }} } -// Sets i to sum of x+y +// Add sets i to sum of x+y func (i *Number) Add(x, y *Number) *Number { i.num.Add(x.num, y.num) return i.limit(i) } -// Sets i to difference of x-y +// Sub sets i to difference of x-y func (i *Number) Sub(x, y *Number) *Number { i.num.Sub(x.num, y.num) return i.limit(i) } -// Sets i to product of x*y +// Mul sets i to product of x*y func (i *Number) Mul(x, y *Number) *Number { i.num.Mul(x.num, y.num) return i.limit(i) } -// Sets i to the quotient prodject of x/y +// Div sets i to the quotient prodject of x/y func (i *Number) Div(x, y *Number) *Number { i.num.Div(x.num, y.num) return i.limit(i) } -// Sets i to x % y +// Mod sets i to x % y func (i *Number) Mod(x, y *Number) *Number { i.num.Mod(x.num, y.num) return i.limit(i) } -// Sets i to x << s +// Lsh sets i to x << s func (i *Number) Lsh(x *Number, s uint) *Number { i.num.Lsh(x.num, s) return i.limit(i) } -// Sets i to x^y +// Pow sets i to x^y func (i *Number) Pow(x, y *Number) *Number { i.num.Exp(x.num, y.num, big.NewInt(0)) return i.limit(i) @@ -117,13 +116,13 @@ func (i *Number) Pow(x, y *Number) *Number { // Setters -// Set x to i +// Set sets x to i func (i *Number) Set(x *Number) *Number { i.num.Set(x.num) return i.limit(i) } -// Set x bytes to i +// SetBytes sets x bytes to i func (i *Number) SetBytes(x []byte) *Number { i.num.SetBytes(x) return i.limit(i) @@ -140,12 +139,12 @@ func (i *Number) Cmp(x *Number) int { // Getters -// Returns the string representation of i +// String returns the string representation of i func (i *Number) String() string { return i.num.String() } -// Returns the byte representation of i +// Bytes returns the byte representation of i func (i *Number) Bytes() []byte { return i.num.Bytes() } @@ -160,17 +159,17 @@ func (i *Number) Int64() int64 { return i.num.Int64() } -// Returns the signed version of i +// Int256 returns the signed version of i func (i *Number) Int256() *Number { return Int(0).Set(i) } -// Returns the unsigned version of i +// Uint256 returns the unsigned version of i func (i *Number) Uint256() *Number { return Uint(0).Set(i) } -// Returns the index of the first bit that's set to 1 +// FirstBitSet returns the index of the first bit that's set to 1 func (i *Number) FirstBitSet() int { for j := 0; j < i.num.BitLen(); j++ { if i.num.Bit(j) > 0 { From 75fa19456962a8db1a9cb0564a2c9be96e47a428 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 23 May 2018 14:11:40 +0200 Subject: [PATCH 7/7] remove uses of EmptyHash --- core/state/state_test.go | 2 +- core/vm/gas_table.go | 6 +++--- eth/downloader/downloader.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/state/state_test.go b/core/state/state_test.go index 12778f6f129d..123559ea9bef 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -99,7 +99,7 @@ func (s *StateSuite) TestNull(c *checker.C) { s.state.SetState(address, common.Hash{}, value) s.state.Commit(false) value = s.state.GetState(address, common.Hash{}) - if !common.EmptyHash(value) { + if value != (common.Hash{}) { c.Errorf("expected empty hash. got %x", value) } } diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 83adba428e15..0764c67a4d15 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -124,12 +124,12 @@ func gasSStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m // 1. From a zero-value address to a non-zero value (NEW VALUE) // 2. From a non-zero value address to a zero-value address (DELETE) // 3. From a non-zero to a non-zero (CHANGE) - if common.EmptyHash(val) && !common.EmptyHash(common.BigToHash(y)) { + if val == (common.Hash{}) && y.Sign() != 0 { // 0 => non 0 return params.SstoreSetGas, nil - } else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) { + } else if val != (common.Hash{}) && y.Sign() == 0 { + // non 0 => 0 evm.StateDB.AddRefund(params.SstoreRefundGas) - return params.SstoreClearGas, nil } else { // non 0 => non 0 (or 0 => 0) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index dc23354929bc..51c5936015aa 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -680,7 +680,7 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err } } // If the head fetch already found an ancestor, return - if !common.EmptyHash(hash) { + if hash != (common.Hash{}) { if int64(number) <= floor { p.log.Warn("Ancestor below allowance", "number", number, "hash", hash, "allowance", floor) return 0, errInvalidAncestor