diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 033769ef5..810c77622 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -86,7 +86,7 @@ func TestGetHeader(t *testing.T) { block = newBlock(2) hash = block.Hash() - _, err = bc.getHeader(block.Hash()) + _, err = bc.getHeader(hash) assert.NotNil(t, err) } diff --git a/pkg/core/storage/helpers.go b/pkg/core/storage/helpers.go index 6ba5b97a1..88dde7a5a 100644 --- a/pkg/core/storage/helpers.go +++ b/pkg/core/storage/helpers.go @@ -63,7 +63,7 @@ func HeaderHashes(s Store) ([]util.Uint256, error) { }) var ( - hashes []util.Uint256 + hashes = make([]util.Uint256, 0, len(hashMap)) sortedKeys = make([]uint32, 0, len(hashMap)) ) diff --git a/pkg/network/tcp_transport.go b/pkg/network/tcp_transport.go index a9c1b4cd3..82ea5f351 100644 --- a/pkg/network/tcp_transport.go +++ b/pkg/network/tcp_transport.go @@ -15,6 +15,8 @@ type TCPTransport struct { bindAddr string } +var reClosedNetwork = regexp.MustCompile(".* use of closed network connection") + // NewTCPTransport return a new TCPTransport that will listen for // new incoming peer connections. func NewTCPTransport(s *Server, bindAddr string) *TCPTransport { @@ -58,13 +60,8 @@ func (t *TCPTransport) Accept() { } func (t *TCPTransport) isCloseError(err error) bool { - regex, err := regexp.Compile(".* use of closed network connection") - if err != nil { - return false - } - if opErr, ok := err.(*net.OpError); ok { - if regex.Match([]byte(opErr.Error())) { + if reClosedNetwork.Match([]byte(opErr.Error())) { return true } } diff --git a/pkg/rpc/neoScanBalanceGetter.go b/pkg/rpc/neoScanBalanceGetter.go index dcc01bf84..6bd243042 100644 --- a/pkg/rpc/neoScanBalanceGetter.go +++ b/pkg/rpc/neoScanBalanceGetter.go @@ -52,7 +52,7 @@ func filterSpecificAsset(asset string, balance []*Unspent, assetBalance *Unspent func (s NeoScanServer) CalculateInputs(address string, assetIdUint util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) { var ( err error - num, i = uint16(0), uint16(0) + num, i uint16 required = cost selected = util.Fixed8(0) us []*Unspent diff --git a/pkg/rpc/stack_param_test.go b/pkg/rpc/stack_param_test.go index 0dbe2997d..e1fd8a60d 100644 --- a/pkg/rpc/stack_param_test.go +++ b/pkg/rpc/stack_param_test.go @@ -114,6 +114,10 @@ const ( func TestStackParam_TryParse(t *testing.T) { // ByteArray to util.Uint160 conversion data, err := hex.DecodeString(hash160) + if err != nil { + t.Fatal(err) + } + var ( outputUint160, expectedUint160 util.Uint160 input = StackParam{ @@ -122,6 +126,9 @@ func TestStackParam_TryParse(t *testing.T) { } ) expectedUint160, err = util.Uint160DecodeString(hash160) + if err != nil { + t.Fatal(err) + } if err = input.TryParse(&outputUint160); err != nil { t.Errorf("failed to parse stackparam to Uint160: %v", err) } @@ -131,6 +138,10 @@ func TestStackParam_TryParse(t *testing.T) { // ByteArray to util.Uint256 conversion data, err = hex.DecodeString(hash256) + if err != nil { + t.Fatal(err) + } + var ( outputUint256, expectedUint256 util.Uint256 uint256input = StackParam{ @@ -139,6 +150,9 @@ func TestStackParam_TryParse(t *testing.T) { } ) expectedUint256, err = util.Uint256DecodeString(hash256) + if err != nil { + t.Fatal(err) + } if err = uint256input.TryParse(&outputUint256); err != nil { t.Errorf("failed to parse stackparam to []byte: %v", err) } @@ -160,6 +174,9 @@ func TestStackParam_TryParse(t *testing.T) { // ByteArray to int64 conversion data, err = hex.DecodeString("637829cd0b") + if err != nil { + t.Fatal(err) + } var ( outputInt, expectedInt int64 intinput = StackParam{ @@ -194,7 +211,13 @@ func TestStackParam_TryParse(t *testing.T) { // StackParams to []util.Uint160 data, err = hex.DecodeString(hash160) + if err != nil { + t.Fatal(err) + } expUint160, err := util.Uint160DecodeString(hash160) + if err != nil { + t.Fatal(err) + } var ( params = StackParams{ StackParam{ diff --git a/pkg/util/array_test.go b/pkg/util/array_test.go index dae4fc65a..f8d78c979 100644 --- a/pkg/util/array_test.go +++ b/pkg/util/array_test.go @@ -9,7 +9,7 @@ func TestArrayReverse(t *testing.T) { arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05} have := ArrayReverse(arr) want := []byte{0x05, 0x04, 0x03, 0x02, 0x01} - if bytes.Compare(have, want) != 0 { + if !bytes.Equal(have, want) { t.Fatalf("expected %v got %v", want, have) } } @@ -19,7 +19,7 @@ func TestArrayReverseLen2(t *testing.T) { arr := []byte{0x01} have := ArrayReverse(arr) want := []byte{0x01} - if bytes.Compare(have, want) != 0 { + if !bytes.Equal(have, want) { t.Fatalf("expected %v got %v", want, have) } } diff --git a/pkg/util/io.go b/pkg/util/io.go index 21beccb97..fcc737a73 100644 --- a/pkg/util/io.go +++ b/pkg/util/io.go @@ -3,7 +3,6 @@ package util import ( "bytes" "encoding/binary" - "errors" "io" ) @@ -39,9 +38,6 @@ func ReadVarUint(r io.Reader) uint64 { // WriteVarUint writes a variable unsigned integer. func WriteVarUint(w io.Writer, val uint64) error { - if val < 0 { - return errors.New("value out of range") - } if val < 0xfd { return binary.Write(w, binary.LittleEndian, uint8(val)) } diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 9e7973265..84251841f 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -74,9 +74,7 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) { if err = json.Unmarshal(data, &js); err != nil { return err } - if strings.HasPrefix(js, "0x") { - js = js[2:] - } + js = strings.TrimPrefix(js, "0x") *u, err = Uint160DecodeString(js) return err } diff --git a/pkg/util/uint256.go b/pkg/util/uint256.go index 69e0c1b6b..4a699c890 100644 --- a/pkg/util/uint256.go +++ b/pkg/util/uint256.go @@ -60,9 +60,7 @@ func (u *Uint256) UnmarshalJSON(data []byte) (err error) { if err = json.Unmarshal(data, &js); err != nil { return err } - if strings.HasPrefix(js, "0x") { - js = js[2:] - } + js = strings.TrimPrefix(js, "0x") *u, err = Uint256DecodeString(js) return err } diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index 8f482e399..df77461e1 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -757,9 +757,6 @@ func (c *codegen) writeJumps() { continue } offset := uint16(c.l[index] - i) - if offset < 0 { - log.Fatalf("new offset is negative, table list %v", c.l) - } binary.LittleEndian.PutUint16(b[j:j+2], offset) } } diff --git a/pkg/vm/state_test.go b/pkg/vm/state_test.go index f1d2154f1..0b4dc64f7 100644 --- a/pkg/vm/state_test.go +++ b/pkg/vm/state_test.go @@ -37,7 +37,7 @@ func TestStateFromString(t *testing.T) { assert.NoError(t, err) assert.Equal(t, faultState|breakState, s) - s, err = StateFromString("HALT, KEK") + _, err = StateFromString("HALT, KEK") assert.Error(t, err) } diff --git a/pkg/wallet/nep2.go b/pkg/wallet/nep2.go index b9409fc03..651567a9c 100644 --- a/pkg/wallet/nep2.go +++ b/pkg/wallet/nep2.go @@ -125,7 +125,7 @@ func compareAddressHash(priv *PrivateKey, hash []byte) bool { return false } addrHash := hashAddress(address)[0:4] - return bytes.Compare(addrHash, hash) == 0 + return bytes.Equal(addrHash, hash) } func validateNEP2Format(b []byte) error {