Code refactoring (#143)

- simplify code
- prealoc slices
- check errors in tests
- regexp Compile replaced with MustCompile
- uint* cannot be negative
This commit is contained in:
Evgeniy Kulikov 2019-02-19 16:22:33 +03:00 committed by fabwa
parent 9c24bf9139
commit cdba88b9f2
12 changed files with 35 additions and 26 deletions

View file

@ -86,7 +86,7 @@ func TestGetHeader(t *testing.T) {
block = newBlock(2) block = newBlock(2)
hash = block.Hash() hash = block.Hash()
_, err = bc.getHeader(block.Hash()) _, err = bc.getHeader(hash)
assert.NotNil(t, err) assert.NotNil(t, err)
} }

View file

@ -63,7 +63,7 @@ func HeaderHashes(s Store) ([]util.Uint256, error) {
}) })
var ( var (
hashes []util.Uint256 hashes = make([]util.Uint256, 0, len(hashMap))
sortedKeys = make([]uint32, 0, len(hashMap)) sortedKeys = make([]uint32, 0, len(hashMap))
) )

View file

@ -15,6 +15,8 @@ type TCPTransport struct {
bindAddr string bindAddr string
} }
var reClosedNetwork = regexp.MustCompile(".* use of closed network connection")
// NewTCPTransport return a new TCPTransport that will listen for // NewTCPTransport return a new TCPTransport that will listen for
// new incoming peer connections. // new incoming peer connections.
func NewTCPTransport(s *Server, bindAddr string) *TCPTransport { func NewTCPTransport(s *Server, bindAddr string) *TCPTransport {
@ -58,13 +60,8 @@ func (t *TCPTransport) Accept() {
} }
func (t *TCPTransport) isCloseError(err error) bool { 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 opErr, ok := err.(*net.OpError); ok {
if regex.Match([]byte(opErr.Error())) { if reClosedNetwork.Match([]byte(opErr.Error())) {
return true return true
} }
} }

View file

@ -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) { func (s NeoScanServer) CalculateInputs(address string, assetIdUint util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
var ( var (
err error err error
num, i = uint16(0), uint16(0) num, i uint16
required = cost required = cost
selected = util.Fixed8(0) selected = util.Fixed8(0)
us []*Unspent us []*Unspent

View file

@ -114,6 +114,10 @@ const (
func TestStackParam_TryParse(t *testing.T) { func TestStackParam_TryParse(t *testing.T) {
// ByteArray to util.Uint160 conversion // ByteArray to util.Uint160 conversion
data, err := hex.DecodeString(hash160) data, err := hex.DecodeString(hash160)
if err != nil {
t.Fatal(err)
}
var ( var (
outputUint160, expectedUint160 util.Uint160 outputUint160, expectedUint160 util.Uint160
input = StackParam{ input = StackParam{
@ -122,6 +126,9 @@ func TestStackParam_TryParse(t *testing.T) {
} }
) )
expectedUint160, err = util.Uint160DecodeString(hash160) expectedUint160, err = util.Uint160DecodeString(hash160)
if err != nil {
t.Fatal(err)
}
if err = input.TryParse(&outputUint160); err != nil { if err = input.TryParse(&outputUint160); err != nil {
t.Errorf("failed to parse stackparam to Uint160: %v", err) 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 // ByteArray to util.Uint256 conversion
data, err = hex.DecodeString(hash256) data, err = hex.DecodeString(hash256)
if err != nil {
t.Fatal(err)
}
var ( var (
outputUint256, expectedUint256 util.Uint256 outputUint256, expectedUint256 util.Uint256
uint256input = StackParam{ uint256input = StackParam{
@ -139,6 +150,9 @@ func TestStackParam_TryParse(t *testing.T) {
} }
) )
expectedUint256, err = util.Uint256DecodeString(hash256) expectedUint256, err = util.Uint256DecodeString(hash256)
if err != nil {
t.Fatal(err)
}
if err = uint256input.TryParse(&outputUint256); err != nil { if err = uint256input.TryParse(&outputUint256); err != nil {
t.Errorf("failed to parse stackparam to []byte: %v", err) t.Errorf("failed to parse stackparam to []byte: %v", err)
} }
@ -160,6 +174,9 @@ func TestStackParam_TryParse(t *testing.T) {
// ByteArray to int64 conversion // ByteArray to int64 conversion
data, err = hex.DecodeString("637829cd0b") data, err = hex.DecodeString("637829cd0b")
if err != nil {
t.Fatal(err)
}
var ( var (
outputInt, expectedInt int64 outputInt, expectedInt int64
intinput = StackParam{ intinput = StackParam{
@ -194,7 +211,13 @@ func TestStackParam_TryParse(t *testing.T) {
// StackParams to []util.Uint160 // StackParams to []util.Uint160
data, err = hex.DecodeString(hash160) data, err = hex.DecodeString(hash160)
if err != nil {
t.Fatal(err)
}
expUint160, err := util.Uint160DecodeString(hash160) expUint160, err := util.Uint160DecodeString(hash160)
if err != nil {
t.Fatal(err)
}
var ( var (
params = StackParams{ params = StackParams{
StackParam{ StackParam{

View file

@ -9,7 +9,7 @@ func TestArrayReverse(t *testing.T) {
arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05} arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
have := ArrayReverse(arr) have := ArrayReverse(arr)
want := []byte{0x05, 0x04, 0x03, 0x02, 0x01} 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) t.Fatalf("expected %v got %v", want, have)
} }
} }
@ -19,7 +19,7 @@ func TestArrayReverseLen2(t *testing.T) {
arr := []byte{0x01} arr := []byte{0x01}
have := ArrayReverse(arr) have := ArrayReverse(arr)
want := []byte{0x01} want := []byte{0x01}
if bytes.Compare(have, want) != 0 { if !bytes.Equal(have, want) {
t.Fatalf("expected %v got %v", want, have) t.Fatalf("expected %v got %v", want, have)
} }
} }

View file

@ -3,7 +3,6 @@ package util
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors"
"io" "io"
) )
@ -39,9 +38,6 @@ func ReadVarUint(r io.Reader) uint64 {
// WriteVarUint writes a variable unsigned integer. // WriteVarUint writes a variable unsigned integer.
func WriteVarUint(w io.Writer, val uint64) error { func WriteVarUint(w io.Writer, val uint64) error {
if val < 0 {
return errors.New("value out of range")
}
if val < 0xfd { if val < 0xfd {
return binary.Write(w, binary.LittleEndian, uint8(val)) return binary.Write(w, binary.LittleEndian, uint8(val))
} }

View file

@ -74,9 +74,7 @@ func (u *Uint160) UnmarshalJSON(data []byte) (err error) {
if err = json.Unmarshal(data, &js); err != nil { if err = json.Unmarshal(data, &js); err != nil {
return err return err
} }
if strings.HasPrefix(js, "0x") { js = strings.TrimPrefix(js, "0x")
js = js[2:]
}
*u, err = Uint160DecodeString(js) *u, err = Uint160DecodeString(js)
return err return err
} }

View file

@ -60,9 +60,7 @@ func (u *Uint256) UnmarshalJSON(data []byte) (err error) {
if err = json.Unmarshal(data, &js); err != nil { if err = json.Unmarshal(data, &js); err != nil {
return err return err
} }
if strings.HasPrefix(js, "0x") { js = strings.TrimPrefix(js, "0x")
js = js[2:]
}
*u, err = Uint256DecodeString(js) *u, err = Uint256DecodeString(js)
return err return err
} }

View file

@ -757,9 +757,6 @@ func (c *codegen) writeJumps() {
continue continue
} }
offset := uint16(c.l[index] - i) 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) binary.LittleEndian.PutUint16(b[j:j+2], offset)
} }
} }

View file

@ -37,7 +37,7 @@ func TestStateFromString(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, faultState|breakState, s) assert.Equal(t, faultState|breakState, s)
s, err = StateFromString("HALT, KEK") _, err = StateFromString("HALT, KEK")
assert.Error(t, err) assert.Error(t, err)
} }

View file

@ -125,7 +125,7 @@ func compareAddressHash(priv *PrivateKey, hash []byte) bool {
return false return false
} }
addrHash := hashAddress(address)[0:4] addrHash := hashAddress(address)[0:4]
return bytes.Compare(addrHash, hash) == 0 return bytes.Equal(addrHash, hash)
} }
func validateNEP2Format(b []byte) error { func validateNEP2Format(b []byte) error {