mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
common: fixes with gofmt for travisCI build
This commit is contained in:
parent
6cf0ab38bd
commit
a570060a1b
3 changed files with 53 additions and 19 deletions
|
|
@ -19,6 +19,7 @@ package common
|
||||||
|
|
||||||
import "encoding/hex"
|
import "encoding/hex"
|
||||||
|
|
||||||
|
// ToHex returns string representation of b and either prepends '0x' or initializes value of '0'.
|
||||||
func ToHex(b []byte) string {
|
func ToHex(b []byte) string {
|
||||||
hex := Bytes2Hex(b)
|
hex := Bytes2Hex(b)
|
||||||
// Prefer output of "0x0" instead of "0x"
|
// Prefer output of "0x0" instead of "0x"
|
||||||
|
|
@ -28,6 +29,7 @@ func ToHex(b []byte) string {
|
||||||
return "0x" + hex
|
return "0x" + hex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//FromHex returns bytes of s after removing prefix, or prepends 0 if s has odd length.
|
||||||
func FromHex(s string) []byte {
|
func FromHex(s string) []byte {
|
||||||
if len(s) > 1 {
|
if len(s) > 1 {
|
||||||
if s[0:2] == "0x" || s[0:2] == "0X" {
|
if s[0:2] == "0x" || s[0:2] == "0X" {
|
||||||
|
|
@ -40,9 +42,7 @@ func FromHex(s string) []byte {
|
||||||
return Hex2Bytes(s)
|
return Hex2Bytes(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy bytes
|
// CopyBytes returns an exact copy of the provided bytes
|
||||||
//
|
|
||||||
// Returns an exact copy of the provided bytes
|
|
||||||
func CopyBytes(b []byte) (copiedBytes []byte) {
|
func CopyBytes(b []byte) (copiedBytes []byte) {
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -53,14 +53,17 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hasHexPrefix validates str begins with '0x' or '0X'.
|
||||||
func hasHexPrefix(str string) bool {
|
func hasHexPrefix(str string) bool {
|
||||||
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
|
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 {
|
func isHexCharacter(c byte) bool {
|
||||||
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
|
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 {
|
func isHex(str string) bool {
|
||||||
if len(str)%2 != 0 {
|
if len(str)%2 != 0 {
|
||||||
return false
|
return false
|
||||||
|
|
@ -73,16 +76,19 @@ func isHex(str string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Bytes2Hex returns the hexadecimal encoding of d.
|
||||||
func Bytes2Hex(d []byte) string {
|
func Bytes2Hex(d []byte) string {
|
||||||
return hex.EncodeToString(d)
|
return hex.EncodeToString(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
|
||||||
func Hex2Bytes(str string) []byte {
|
func Hex2Bytes(str string) []byte {
|
||||||
h, _ := hex.DecodeString(str)
|
h, _ := hex.DecodeString(str)
|
||||||
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hex2BytesFixed returns bytes of a specified fixed length flen.
|
||||||
func Hex2BytesFixed(str string, flen int) []byte {
|
func Hex2BytesFixed(str string, flen int) []byte {
|
||||||
h, _ := hex.DecodeString(str)
|
h, _ := hex.DecodeString(str)
|
||||||
if len(h) == flen {
|
if len(h) == flen {
|
||||||
|
|
@ -96,6 +102,7 @@ func Hex2BytesFixed(str string, flen int) []byte {
|
||||||
return hh
|
return hh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RightPadBytes copies slice into a byte slice of length l.
|
||||||
func RightPadBytes(slice []byte, l int) []byte {
|
func RightPadBytes(slice []byte, l int) []byte {
|
||||||
if l <= len(slice) {
|
if l <= len(slice) {
|
||||||
return slice
|
return slice
|
||||||
|
|
@ -107,6 +114,7 @@ func RightPadBytes(slice []byte, l int) []byte {
|
||||||
return padded
|
return padded
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LeftPadBytes copies via inverted index slice into a byte slice of length l.
|
||||||
func LeftPadBytes(slice []byte, l int) []byte {
|
func LeftPadBytes(slice []byte, l int) []byte {
|
||||||
if l <= len(slice) {
|
if l <= len(slice) {
|
||||||
return slice
|
return slice
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ func MakeName(name, version string) string {
|
||||||
return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
|
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 {
|
func FileExist(filePath string) bool {
|
||||||
_, err := os.Stat(filePath)
|
_, err := os.Stat(filePath)
|
||||||
if err != nil && os.IsNotExist(err) {
|
if err != nil && os.IsNotExist(err) {
|
||||||
|
|
@ -39,6 +40,7 @@ func FileExist(filePath string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AbsolutePath returns Datadir + filename, or filename if it is absolute.
|
||||||
func AbsolutePath(Datadir string, filename string) string {
|
func AbsolutePath(Datadir string, filename string) string {
|
||||||
if filepath.IsAbs(filename) {
|
if filepath.IsAbs(filename) {
|
||||||
return filename
|
return filename
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto/sha3"
|
"github.com/ethereum/go-ethereum/crypto/sha3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Sets global constants.
|
||||||
const (
|
const (
|
||||||
HashLength = 32
|
HashLength = 32
|
||||||
AddressLength = 20
|
AddressLength = 20
|
||||||
|
|
@ -42,18 +43,29 @@ var (
|
||||||
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
|
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
|
||||||
type Hash [HashLength]byte
|
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 {
|
func BytesToHash(b []byte) Hash {
|
||||||
var h Hash
|
var h Hash
|
||||||
h.SetBytes(b)
|
h.SetBytes(b)
|
||||||
return h
|
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 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)) }
|
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
|
||||||
|
|
||||||
// Get the string representation of the underlying hash
|
// Str gets the string representation of the underlying hash.
|
||||||
func (h Hash) Str() string { return string(h[:]) }
|
func (h Hash) Str() string { return string(h[:]) }
|
||||||
|
|
||||||
|
// Bytes gets the byte representation of the underlying hash.
|
||||||
func (h Hash) Bytes() []byte { return h[:] }
|
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) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
|
||||||
|
|
||||||
|
// Hex gets the hex representation of the underlying hash.
|
||||||
func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
|
func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
|
||||||
|
|
||||||
// TerminalString implements log.TerminalStringer, formatting a string for console
|
// TerminalString implements log.TerminalStringer, formatting a string for console
|
||||||
|
|
@ -89,7 +101,7 @@ func (h Hash) MarshalText() ([]byte, error) {
|
||||||
return hexutil.Bytes(h[:]).MarshalText()
|
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) {
|
func (h *Hash) SetBytes(b []byte) {
|
||||||
if len(b) > len(h) {
|
if len(b) > len(h) {
|
||||||
b = b[len(b)-HashLength:]
|
b = b[len(b)-HashLength:]
|
||||||
|
|
@ -98,10 +110,10 @@ func (h *Hash) SetBytes(b []byte) {
|
||||||
copy(h[HashLength-len(b):], b)
|
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)) }
|
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) {
|
func (h *Hash) Set(other Hash) {
|
||||||
for i, v := range other {
|
for i, v := range other {
|
||||||
h[i] = v
|
h[i] = v
|
||||||
|
|
@ -117,6 +129,7 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
|
||||||
return reflect.ValueOf(h)
|
return reflect.ValueOf(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EmptyHash clears h of values.
|
||||||
func EmptyHash(h Hash) bool {
|
func EmptyHash(h Hash) bool {
|
||||||
return h == Hash{}
|
return h == Hash{}
|
||||||
}
|
}
|
||||||
|
|
@ -139,12 +152,17 @@ func (h UnprefixedHash) MarshalText() ([]byte, error) {
|
||||||
// Address represents the 20 byte address of an Ethereum account.
|
// Address represents the 20 byte address of an Ethereum account.
|
||||||
type Address [AddressLength]byte
|
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 {
|
func BytesToAddress(b []byte) Address {
|
||||||
var a Address
|
var a Address
|
||||||
a.SetBytes(b)
|
a.SetBytes(b)
|
||||||
return a
|
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 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)) }
|
func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
|
||||||
|
|
||||||
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
||||||
|
|
@ -156,10 +174,16 @@ func IsHexAddress(s string) bool {
|
||||||
return len(s) == 2*AddressLength && isHex(s)
|
return len(s) == 2*AddressLength && isHex(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the string representation of the underlying address
|
// Str gets the string representation of the underlying address.
|
||||||
func (a Address) Str() string { return string(a[:]) }
|
func (a Address) Str() string { return string(a[:]) }
|
||||||
|
|
||||||
|
// Bytes gets the string representation of the underlying address.
|
||||||
func (a Address) Bytes() []byte { return a[:] }
|
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) Big() *big.Int { return new(big.Int).SetBytes(a[:]) }
|
||||||
|
|
||||||
|
// Hash gets the string representation of the underlying address.
|
||||||
func (a Address) Hash() Hash { return BytesToHash(a[:]) }
|
func (a Address) Hash() Hash { return BytesToHash(a[:]) }
|
||||||
|
|
||||||
// Hex returns an EIP55-compliant hex string representation of the address.
|
// Hex returns an EIP55-compliant hex string representation of the address.
|
||||||
|
|
@ -195,7 +219,7 @@ func (a Address) Format(s fmt.State, c rune) {
|
||||||
fmt.Fprintf(s, "%"+string(c), a[:])
|
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) {
|
func (a *Address) SetBytes(b []byte) {
|
||||||
if len(b) > len(a) {
|
if len(b) > len(a) {
|
||||||
b = b[len(b)-AddressLength:]
|
b = b[len(b)-AddressLength:]
|
||||||
|
|
@ -203,10 +227,10 @@ func (a *Address) SetBytes(b []byte) {
|
||||||
copy(a[AddressLength-len(b):], b)
|
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)) }
|
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) {
|
func (a *Address) Set(other Address) {
|
||||||
for i, v := range other {
|
for i, v := range other {
|
||||||
a[i] = v
|
a[i] = v
|
||||||
|
|
@ -228,7 +252,7 @@ func (a *Address) UnmarshalJSON(input []byte) error {
|
||||||
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
|
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
|
type UnprefixedAddress Address
|
||||||
|
|
||||||
// UnmarshalText decodes the address from hex. The 0x prefix is optional.
|
// UnmarshalText decodes the address from hex. The 0x prefix is optional.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue