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:
parent
9c24bf9139
commit
cdba88b9f2
12 changed files with 35 additions and 26 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue