Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
common: improve documentation comments (#16701)
Browse files Browse the repository at this point in the history
This commit adds many comments and removes unused code.
It also removes the EmptyHash function, which had some uses
but was silly.
  • Loading branch information
kielbarry authored and acud committed Jun 4, 2018
1 parent 95d3fcf commit dc8b45c
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 142 deletions.
26 changes: 16 additions & 10 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ package common

import (
"encoding/hex"
"fmt"
)

// 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)
// Prefer output of "0x0" instead of "0x"
if len(hex) == 0 {
hex = "0"
}
return "0x" + hex
}

// 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" {
Expand All @@ -43,9 +47,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
Expand All @@ -56,14 +58,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
Expand All @@ -76,19 +81,18 @@ 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, err := hex.DecodeString(str)
if err != nil {
panic(fmt.Sprintf("cannot hex decode the string %s, got error %v", str, err))
}

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 {
Expand All @@ -102,6 +106,7 @@ func Hex2BytesFixed(str string, flen int) []byte {
return hh
}

// RightPadBytes zero-pads slice to the right up to length l.
func RightPadBytes(slice []byte, l int) []byte {
if l <= len(slice) {
return slice
Expand All @@ -113,6 +118,7 @@ func RightPadBytes(slice []byte, l int) []byte {
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
Expand Down
5 changes: 2 additions & 3 deletions common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
43 changes: 21 additions & 22 deletions common/number/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,79 +50,79 @@ 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)
}

// 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)
Expand All @@ -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()
}
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions common/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
func FileExist(filePath string) bool {
_, err := os.Stat(filePath)
if err != nil && os.IsNotExist(err) {
Expand All @@ -39,9 +40,10 @@ func FileExist(filePath string) bool {
return true
}

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)
}
Loading

0 comments on commit dc8b45c

Please sign in to comment.