Browse Source

fixed-length arrays cleanup

pull/115/head
Jae Kwon 9 years ago
parent
commit
8d1e176180
6 changed files with 39 additions and 52 deletions
  1. +2
    -0
      Makefile
  2. +0
    -12
      account/pub_key.go
  3. +0
    -2
      account/signature.go
  4. +0
    -1
      binary/reflect.go
  5. +37
    -34
      binary/reflect_test.go
  6. +0
    -3
      state/execution.go

+ 2
- 0
Makefile View File

@ -27,6 +27,8 @@ build_race:
go build -race -o build/logjack github.com/tendermint/tendermint/cmd/logjack
test: build
-rm -rf ~/.tendermint_test_bak
mv ~/.tendermint_test ~/.tendermint_test_bak
go test github.com/tendermint/tendermint/...
draw_deps:


+ 0
- 12
account/pub_key.go View File

@ -2,7 +2,6 @@ package account
import (
"bytes"
"errors"
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519/extra25519"
@ -12,7 +11,6 @@ import (
// PubKey is part of Account and Validator.
type PubKey interface {
IsNil() bool
Address() []byte
VerifyBytes(msg []byte, sig Signature) bool
}
@ -33,8 +31,6 @@ var _ = binary.RegisterInterface(
// Implements PubKey
type PubKeyEd25519 [32]byte
func (pubKey PubKeyEd25519) IsNil() bool { return false }
// TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
func (pubKey PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(pubKey[:]) }
@ -60,14 +56,6 @@ func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
return keyCurve25519
}
// redundant: compiler does it for us
func (pubKey PubKeyEd25519) ValidateBasic() error {
if len(pubKey) != ed25519.PublicKeySize {
return errors.New("Invalid PubKeyEd25519 key size")
}
return nil
}
func (pubKey PubKeyEd25519) String() string {
return Fmt("PubKeyEd25519{%X}", pubKey[:])
}


+ 0
- 2
account/signature.go View File

@ -27,8 +27,6 @@ var _ = binary.RegisterInterface(
// Implements Signature
type SignatureEd25519 [64]byte
func (sig SignatureEd25519) IsNil() bool { return false }
func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }

+ 0
- 1
binary/reflect.go View File

@ -670,7 +670,6 @@ func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *erro
return
}
log.Debug("Read bytearray", "bytes", buf)
reflect.Copy(rv, reflect.ValueOf(buf))
} else {
oSlice, ok := o.([]interface{})


+ 37
- 34
binary/reflect_test.go View File

@ -16,40 +16,6 @@ type SimpleStruct struct {
Time time.Time
}
type SimpleArray [5]byte
func TestSimpleArray(t *testing.T) {
var foo SimpleArray
// Type of pointer to array
rt := reflect.TypeOf(&foo)
fmt.Printf("rt: %v\n", rt)
// Type of array itself.
// NOTE: normally this is acquired through other means
// like introspecting on method signatures, or struct fields.
rte := rt.Elem()
fmt.Printf("rte: %v\n", rte)
// Get a new pointer to the array
// NOTE: calling .Interface() is to get the actual value,
// instead of reflection values.
ptr := reflect.New(rte).Interface()
fmt.Printf("ptr: %v", ptr)
// Make a simple int aray
fooArray := SimpleArray([5]byte{1, 10, 50, 100, 200})
fooBytes := BinaryBytes(fooArray)
fooReader := bytes.NewReader(fooBytes)
// Now you can read it.
n, err := new(int64), new(error)
it := ReadBinary(foo, fooReader, n, err)
fmt.Println(it, reflect.TypeOf(it))
}
//-------------------------------------
type Animal interface{}
const (
@ -502,3 +468,40 @@ func TestBadAlloc(t *testing.T) {
res := ReadBinary(instance, b, n, err)
fmt.Println(res, *err)
}
//------------------------------------------------------------------------------
type SimpleArray [5]byte
func TestSimpleArray(t *testing.T) {
var foo SimpleArray
// Type of pointer to array
rt := reflect.TypeOf(&foo)
fmt.Printf("rt: %v\n", rt) // *binary.SimpleArray
// Type of array itself.
// NOTE: normally this is acquired through other means
// like introspecting on method signatures, or struct fields.
rte := rt.Elem()
fmt.Printf("rte: %v\n", rte) // binary.SimpleArray
// Get a new pointer to the array
// NOTE: calling .Interface() is to get the actual value,
// instead of reflection values.
ptr := reflect.New(rte).Interface()
fmt.Printf("ptr: %v\n", ptr) // &[0 0 0 0 0]
// Make a simple int aray
fooArray := SimpleArray([5]byte{1, 10, 50, 100, 200})
fooBytes := BinaryBytes(fooArray)
fooReader := bytes.NewReader(fooBytes)
// Now you can read it.
n, err := new(int64), new(error)
it := ReadBinary(foo, fooReader, n, err).(SimpleArray)
if !bytes.Equal(it[:], fooArray[:]) {
t.Errorf("Expected %v but got %v", fooArray, it)
}
}

+ 0
- 3
state/execution.go View File

@ -666,9 +666,6 @@ func ExecTx(blockCache *BlockCache, tx types.Tx, runCall bool, evc events.Fireab
if err != nil {
return err
}
if err := tx.PubKey.ValidateBasic(); err != nil {
return err
}
if !tx.PubKey.VerifyBytes(signBytes, tx.Signature) {
return types.ErrTxInvalidSignature
}


Loading…
Cancel
Save