Skip to content

Commit 84f8c0c

Browse files
kielbarryfjl
authored andcommitted
common: improve documentation comments (#16701)
This commit adds many comments and removes unused code. It also removes the EmptyHash function, which had some uses but was silly.
1 parent 998f656 commit 84f8c0c

File tree

9 files changed

+83
-137
lines changed

9 files changed

+83
-137
lines changed

common/bytes.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ package common
1919

2020
import "encoding/hex"
2121

22+
// ToHex returns the hex representation of b, prefixed with '0x'.
23+
// For empty slices, the return value is "0x0".
24+
//
25+
// Deprecated: use hexutil.Encode instead.
2226
func ToHex(b []byte) string {
2327
hex := Bytes2Hex(b)
24-
// Prefer output of "0x0" instead of "0x"
2528
if len(hex) == 0 {
2629
hex = "0"
2730
}
2831
return "0x" + hex
2932
}
3033

34+
// FromHex returns the bytes represented by the hexadecimal string s.
35+
// s may be prefixed with "0x".
3136
func FromHex(s string) []byte {
3237
if len(s) > 1 {
3338
if s[0:2] == "0x" || s[0:2] == "0X" {
@@ -40,9 +45,7 @@ func FromHex(s string) []byte {
4045
return Hex2Bytes(s)
4146
}
4247

43-
// Copy bytes
44-
//
45-
// Returns an exact copy of the provided bytes
48+
// CopyBytes returns an exact copy of the provided bytes.
4649
func CopyBytes(b []byte) (copiedBytes []byte) {
4750
if b == nil {
4851
return nil
@@ -53,14 +56,17 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
5356
return
5457
}
5558

59+
// hasHexPrefix validates str begins with '0x' or '0X'.
5660
func hasHexPrefix(str string) bool {
5761
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
5862
}
5963

64+
// isHexCharacter returns bool of c being a valid hexadecimal.
6065
func isHexCharacter(c byte) bool {
6166
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
6267
}
6368

69+
// isHex validates whether each byte is valid hexadecimal string.
6470
func isHex(str string) bool {
6571
if len(str)%2 != 0 {
6672
return false
@@ -73,16 +79,18 @@ func isHex(str string) bool {
7379
return true
7480
}
7581

82+
// Bytes2Hex returns the hexadecimal encoding of d.
7683
func Bytes2Hex(d []byte) string {
7784
return hex.EncodeToString(d)
7885
}
7986

87+
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
8088
func Hex2Bytes(str string) []byte {
8189
h, _ := hex.DecodeString(str)
82-
8390
return h
8491
}
8592

93+
// Hex2BytesFixed returns bytes of a specified fixed length flen.
8694
func Hex2BytesFixed(str string, flen int) []byte {
8795
h, _ := hex.DecodeString(str)
8896
if len(h) == flen {
@@ -96,6 +104,7 @@ func Hex2BytesFixed(str string, flen int) []byte {
96104
return hh
97105
}
98106

107+
// RightPadBytes zero-pads slice to the right up to length l.
99108
func RightPadBytes(slice []byte, l int) []byte {
100109
if l <= len(slice) {
101110
return slice
@@ -107,6 +116,7 @@ func RightPadBytes(slice []byte, l int) []byte {
107116
return padded
108117
}
109118

119+
// LeftPadBytes zero-pads slice to the left up to length l.
110120
func LeftPadBytes(slice []byte, l int) []byte {
111121
if l <= len(slice) {
112122
return slice

common/math/big.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ParseBig256(s string) (*big.Int, bool) {
7878
return bigint, ok
7979
}
8080

81-
// MustParseBig parses s as a 256 bit big integer and panics if the string is invalid.
81+
// MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.
8282
func MustParseBig256(s string) *big.Int {
8383
v, ok := ParseBig256(s)
8484
if !ok {
@@ -186,9 +186,8 @@ func U256(x *big.Int) *big.Int {
186186
func S256(x *big.Int) *big.Int {
187187
if x.Cmp(tt255) < 0 {
188188
return x
189-
} else {
190-
return new(big.Int).Sub(x, tt256)
191189
}
190+
return new(big.Int).Sub(x, tt256)
192191
}
193192

194193
// Exp implements exponentiation by squaring.

common/number/int.go

+21-22
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ func limitUnsigned256(x *Number) *Number {
3434
func limitSigned256(x *Number) *Number {
3535
if x.num.Cmp(tt255) < 0 {
3636
return x
37-
} else {
38-
x.num.Sub(x.num, tt256)
39-
return x
4037
}
38+
x.num.Sub(x.num, tt256)
39+
return x
4140
}
4241

43-
// Number function
42+
// Initialiser is a Number function
4443
type Initialiser func(n int64) *Number
4544

4645
// A Number represents a generic integer with a bounding function limiter. Limit is called after each operations
@@ -51,79 +50,79 @@ type Number struct {
5150
limit func(n *Number) *Number
5251
}
5352

54-
// Returns a new initialiser for a new *Number without having to expose certain fields
53+
// NewInitialiser returns a new initialiser for a new *Number without having to expose certain fields
5554
func NewInitialiser(limiter func(*Number) *Number) Initialiser {
5655
return func(n int64) *Number {
5756
return &Number{big.NewInt(n), limiter}
5857
}
5958
}
6059

61-
// Return a Number with a UNSIGNED limiter up to 256 bits
60+
// Uint256 returns a Number with a UNSIGNED limiter up to 256 bits
6261
func Uint256(n int64) *Number {
6362
return &Number{big.NewInt(n), limitUnsigned256}
6463
}
6564

66-
// Return a Number with a SIGNED limiter up to 256 bits
65+
// Int256 returns Number with a SIGNED limiter up to 256 bits
6766
func Int256(n int64) *Number {
6867
return &Number{big.NewInt(n), limitSigned256}
6968
}
7069

71-
// Returns a Number with a SIGNED unlimited size
70+
// Big returns a Number with a SIGNED unlimited size
7271
func Big(n int64) *Number {
7372
return &Number{big.NewInt(n), func(x *Number) *Number { return x }}
7473
}
7574

76-
// Sets i to sum of x+y
75+
// Add sets i to sum of x+y
7776
func (i *Number) Add(x, y *Number) *Number {
7877
i.num.Add(x.num, y.num)
7978
return i.limit(i)
8079
}
8180

82-
// Sets i to difference of x-y
81+
// Sub sets i to difference of x-y
8382
func (i *Number) Sub(x, y *Number) *Number {
8483
i.num.Sub(x.num, y.num)
8584
return i.limit(i)
8685
}
8786

88-
// Sets i to product of x*y
87+
// Mul sets i to product of x*y
8988
func (i *Number) Mul(x, y *Number) *Number {
9089
i.num.Mul(x.num, y.num)
9190
return i.limit(i)
9291
}
9392

94-
// Sets i to the quotient prodject of x/y
93+
// Div sets i to the quotient prodject of x/y
9594
func (i *Number) Div(x, y *Number) *Number {
9695
i.num.Div(x.num, y.num)
9796
return i.limit(i)
9897
}
9998

100-
// Sets i to x % y
99+
// Mod sets i to x % y
101100
func (i *Number) Mod(x, y *Number) *Number {
102101
i.num.Mod(x.num, y.num)
103102
return i.limit(i)
104103
}
105104

106-
// Sets i to x << s
105+
// Lsh sets i to x << s
107106
func (i *Number) Lsh(x *Number, s uint) *Number {
108107
i.num.Lsh(x.num, s)
109108
return i.limit(i)
110109
}
111110

112-
// Sets i to x^y
111+
// Pow sets i to x^y
113112
func (i *Number) Pow(x, y *Number) *Number {
114113
i.num.Exp(x.num, y.num, big.NewInt(0))
115114
return i.limit(i)
116115
}
117116

118117
// Setters
119118

120-
// Set x to i
119+
// Set sets x to i
121120
func (i *Number) Set(x *Number) *Number {
122121
i.num.Set(x.num)
123122
return i.limit(i)
124123
}
125124

126-
// Set x bytes to i
125+
// SetBytes sets x bytes to i
127126
func (i *Number) SetBytes(x []byte) *Number {
128127
i.num.SetBytes(x)
129128
return i.limit(i)
@@ -140,12 +139,12 @@ func (i *Number) Cmp(x *Number) int {
140139

141140
// Getters
142141

143-
// Returns the string representation of i
142+
// String returns the string representation of i
144143
func (i *Number) String() string {
145144
return i.num.String()
146145
}
147146

148-
// Returns the byte representation of i
147+
// Bytes returns the byte representation of i
149148
func (i *Number) Bytes() []byte {
150149
return i.num.Bytes()
151150
}
@@ -160,17 +159,17 @@ func (i *Number) Int64() int64 {
160159
return i.num.Int64()
161160
}
162161

163-
// Returns the signed version of i
162+
// Int256 returns the signed version of i
164163
func (i *Number) Int256() *Number {
165164
return Int(0).Set(i)
166165
}
167166

168-
// Returns the unsigned version of i
167+
// Uint256 returns the unsigned version of i
169168
func (i *Number) Uint256() *Number {
170169
return Uint(0).Set(i)
171170
}
172171

173-
// Returns the index of the first bit that's set to 1
172+
// FirstBitSet returns the index of the first bit that's set to 1
174173
func (i *Number) FirstBitSet() int {
175174
for j := 0; j < i.num.BitLen(); j++ {
176175
if i.num.Bit(j) > 0 {

common/path.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func MakeName(name, version string) string {
3030
return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
3131
}
3232

33+
// FileExist checks if a file exists at filePath.
3334
func FileExist(filePath string) bool {
3435
_, err := os.Stat(filePath)
3536
if err != nil && os.IsNotExist(err) {
@@ -39,9 +40,10 @@ func FileExist(filePath string) bool {
3940
return true
4041
}
4142

42-
func AbsolutePath(Datadir string, filename string) string {
43+
// AbsolutePath returns datadir + filename, or filename if it is absolute.
44+
func AbsolutePath(datadir string, filename string) string {
4345
if filepath.IsAbs(filename) {
4446
return filename
4547
}
46-
return filepath.Join(Datadir, filename)
48+
return filepath.Join(datadir, filename)
4749
}

0 commit comments

Comments
 (0)