Browse Source

All numbers are in BigEndian

pull/67/head
Jae Kwon 9 years ago
parent
commit
3c96890d2d
9 changed files with 194 additions and 85 deletions
  1. +2
    -2
      account/signature_test.go
  2. +97
    -67
      binary/int.go
  3. +79
    -0
      binary/int_test.go
  4. +1
    -1
      node/node.go
  5. +6
    -6
      p2p/addrbook.go
  6. +3
    -3
      rpc/test/.tendermint/genesis.json
  7. +4
    -4
      rpc/test/helpers_test.go
  8. +1
    -1
      state/tx_cache.go
  9. +1
    -1
      vm/test/fake_app_state.go

+ 2
- 2
account/signature_test.go View File

@ -48,8 +48,8 @@ func TestBinaryDecode(t *testing.T) {
t.Fatalf("Failed to write Signature: %v", err)
}
if len(buf.Bytes()) != ed25519.SignatureSize+2 {
// 1 byte TypeByte, 1 byte length, 64 bytes signature bytes
if len(buf.Bytes()) != ed25519.SignatureSize+3 {
// 1 byte TypeByte, 2 bytes length, 64 bytes signature bytes
t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
}
if buf.Bytes()[0] != SignatureTypeEd25519 {


+ 97
- 67
binary/int.go View File

@ -42,7 +42,7 @@ func ReadUint8(r io.Reader, n *int64, err *error) uint8 {
func WriteInt16(i int16, w io.Writer, n *int64, err *error) {
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, uint16(i))
binary.BigEndian.PutUint16(buf, uint16(i))
*n += 2
WriteTo(buf, w, n, err)
}
@ -50,14 +50,14 @@ func WriteInt16(i int16, w io.Writer, n *int64, err *error) {
func ReadInt16(r io.Reader, n *int64, err *error) int16 {
buf := make([]byte, 2)
ReadFull(buf, r, n, err)
return int16(binary.LittleEndian.Uint16(buf))
return int16(binary.BigEndian.Uint16(buf))
}
// Uint16
func WriteUint16(i uint16, w io.Writer, n *int64, err *error) {
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, uint16(i))
binary.BigEndian.PutUint16(buf, uint16(i))
*n += 2
WriteTo(buf, w, n, err)
}
@ -65,7 +65,7 @@ func WriteUint16(i uint16, w io.Writer, n *int64, err *error) {
func ReadUint16(r io.Reader, n *int64, err *error) uint16 {
buf := make([]byte, 2)
ReadFull(buf, r, n, err)
return uint16(binary.LittleEndian.Uint16(buf))
return uint16(binary.BigEndian.Uint16(buf))
}
// []Uint16
@ -100,7 +100,7 @@ func ReadUint16s(r io.Reader, n *int64, err *error) []uint16 {
func WriteInt32(i int32, w io.Writer, n *int64, err *error) {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(i))
binary.BigEndian.PutUint32(buf, uint32(i))
*n += 4
WriteTo(buf, w, n, err)
}
@ -108,14 +108,14 @@ func WriteInt32(i int32, w io.Writer, n *int64, err *error) {
func ReadInt32(r io.Reader, n *int64, err *error) int32 {
buf := make([]byte, 4)
ReadFull(buf, r, n, err)
return int32(binary.LittleEndian.Uint32(buf))
return int32(binary.BigEndian.Uint32(buf))
}
// Uint32
func WriteUint32(i uint32, w io.Writer, n *int64, err *error) {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(i))
binary.BigEndian.PutUint32(buf, uint32(i))
*n += 4
WriteTo(buf, w, n, err)
}
@ -123,14 +123,14 @@ func WriteUint32(i uint32, w io.Writer, n *int64, err *error) {
func ReadUint32(r io.Reader, n *int64, err *error) uint32 {
buf := make([]byte, 4)
ReadFull(buf, r, n, err)
return uint32(binary.LittleEndian.Uint32(buf))
return uint32(binary.BigEndian.Uint32(buf))
}
// Int64
func WriteInt64(i int64, w io.Writer, n *int64, err *error) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i))
binary.BigEndian.PutUint64(buf, uint64(i))
*n += 8
WriteTo(buf, w, n, err)
}
@ -138,14 +138,14 @@ func WriteInt64(i int64, w io.Writer, n *int64, err *error) {
func ReadInt64(r io.Reader, n *int64, err *error) int64 {
buf := make([]byte, 8)
ReadFull(buf, r, n, err)
return int64(binary.LittleEndian.Uint64(buf))
return int64(binary.BigEndian.Uint64(buf))
}
// Uint64
func WriteUint64(i uint64, w io.Writer, n *int64, err *error) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i))
binary.BigEndian.PutUint64(buf, uint64(i))
*n += 8
WriteTo(buf, w, n, err)
}
@ -153,80 +153,110 @@ func WriteUint64(i uint64, w io.Writer, n *int64, err *error) {
func ReadUint64(r io.Reader, n *int64, err *error) uint64 {
buf := make([]byte, 8)
ReadFull(buf, r, n, err)
return uint64(binary.LittleEndian.Uint64(buf))
return uint64(binary.BigEndian.Uint64(buf))
}
// Varint
func uvarIntSize(i uint) int {
if i < 1<<8 {
return 1
}
if i < 1<<16 {
return 2
}
if i < 1<<24 {
return 3
}
if i < 1<<32 {
return 4
}
if i < 1<<40 {
return 5
}
if i < 1<<48 {
return 6
}
if i < 1<<56 {
return 7
}
return 8
}
func WriteVarint(i int, w io.Writer, n *int64, err *error) {
buf := make([]byte, binary.MaxVarintLen64)
n_ := int64(binary.PutVarint(buf, int64(i)))
*n += n_
WriteTo(buf[:n_], w, n, err)
var negate = false
if i < 0 {
negate = true
i = -i
}
var size = uvarIntSize(uint(i))
if negate {
// e.g. 0xF1 for a single negative byte
WriteUint8(uint8(size+0xF0), w, n, err)
} else {
WriteUint8(uint8(size), w, n, err)
}
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
WriteTo(buf[(8-size):], w, n, err)
*n += int64(1 + size)
}
func ReadVarint(r io.Reader, n *int64, err *error) int {
res, n_, err_ := readVarint(r)
*n += n_
*err = err_
return int(res)
var size = ReadUint8(r, n, err)
var negate = false
if (size >> 4) == 0xF {
negate = true
size = size & 0x0F
}
if size > 8 {
setFirstErr(err, errors.New("Varint overflow"))
return 0
}
if size == 0 {
setFirstErr(err, errors.New("Varint underflow"))
return 0
}
buf := make([]byte, 8)
ReadFull(buf[(8-size):], r, n, err)
*n += int64(1 + size)
var i = int(binary.BigEndian.Uint64(buf))
if negate {
return -i
} else {
return i
}
}
// Uvarint
func WriteUvarint(i uint, w io.Writer, n *int64, err *error) {
buf := make([]byte, binary.MaxVarintLen64)
n_ := int64(binary.PutUvarint(buf, uint64(i)))
*n += n_
WriteTo(buf[:n_], w, n, err)
var size = uvarIntSize(i)
WriteUint8(uint8(size), w, n, err)
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
WriteTo(buf[(8-size):], w, n, err)
*n += int64(1 + size)
}
func ReadUvarint(r io.Reader, n *int64, err *error) uint {
res, n_, err_ := readUvarint(r)
*n += n_
*err = err_
return uint(res)
}
//-----------------------------------------------------------------------------
var overflow = errors.New("binary: varint overflows a 64-bit integer")
// Modified to return number of bytes read, from
// http://golang.org/src/pkg/encoding/binary/varint.go?s=3652:3699#L116
func readUvarint(r io.Reader) (uint64, int64, error) {
var x uint64
var s uint
var buf = make([]byte, 1)
for i := 0; ; i++ {
for {
n, err := r.Read(buf)
if err != nil {
return x, int64(i), err
}
if n > 0 {
break
}
}
b := buf[0]
if b < 0x80 {
if i > 9 || i == 9 && b > 1 {
return x, int64(i), overflow
}
return x | uint64(b)<<s, int64(i), nil
}
x |= uint64(b&0x7f) << s
s += 7
var size = ReadUint8(r, n, err)
if size > 8 {
setFirstErr(err, errors.New("Uvarint overflow"))
return 0
}
if size == 0 {
setFirstErr(err, errors.New("Uvarint underflow"))
return 0
}
buf := make([]byte, 8)
ReadFull(buf[(8-size):], r, n, err)
*n += int64(1 + size)
return uint(binary.BigEndian.Uint64(buf))
}
// Modified to return number of bytes read, from
// http://golang.org/src/pkg/encoding/binary/varint.go?s=3652:3699#L116
func readVarint(r io.Reader) (int64, int64, error) {
ux, n, err := readUvarint(r) // ok to continue in presence of error
x := int64(ux >> 1)
if ux&1 != 0 {
x = ^x
func setFirstErr(err *error, newErr error) {
if *err == nil && newErr != nil {
*err = newErr
}
return x, n, err
}

+ 79
- 0
binary/int_test.go View File

@ -0,0 +1,79 @@
package binary
import (
"bytes"
"fmt"
"testing"
)
func TestVarint(t *testing.T) {
check := func(i int, s string) {
buf := new(bytes.Buffer)
n, err := new(int64), new(error)
WriteVarint(i, buf, n, err)
bufBytes := buf.Bytes() // Read before consuming below.
i_ := ReadVarint(buf, n, err)
if i != i_ {
fmt.Println(bufBytes)
t.Fatalf("Encoded %v and got %v", i, i_)
}
if s != "" {
if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
t.Fatalf("Encoded %v, expected %v", bufHex, s)
}
}
}
// 123457 is some prime.
for i := -(2 << 33); i < (2 << 33); i += 123457 {
check(i, "")
}
// Near zero
check(-1, "F101")
check(0, "0100")
check(1, "0101")
// Positives
check(1<<32-1, "04FFFFFFFF")
check(1<<32+0, "050100000000")
check(1<<32+1, "050100000001")
check(1<<53-1, "071FFFFFFFFFFFFF")
// Negatives
check(-1<<32+1, "F4FFFFFFFF")
check(-1<<32-0, "F50100000000")
check(-1<<32-1, "F50100000001")
check(-1<<53+1, "F71FFFFFFFFFFFFF")
}
func TestUvarint(t *testing.T) {
check := func(i uint, s string) {
buf := new(bytes.Buffer)
n, err := new(int64), new(error)
WriteUvarint(i, buf, n, err)
bufBytes := buf.Bytes()
i_ := ReadUvarint(buf, n, err)
if i != i_ {
fmt.Println(buf.Bytes())
t.Fatalf("Encoded %v and got %v", i, i_)
}
if s != "" {
if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
t.Fatalf("Encoded %v, expected %v", bufHex, s)
}
}
}
// 123457 is some prime.
for i := 0; i < (2 << 33); i += 123457 {
check(uint(i), "")
}
check(1, "0101")
check(1<<32-1, "04FFFFFFFF")
check(1<<32+0, "050100000000")
check(1<<32+1, "050100000001")
check(1<<53-1, "071FFFFFFFFFFFFF")
}

+ 1
- 1
node/node.go View File

@ -218,7 +218,7 @@ func makeNodeInfo(sw *p2p.Switch) *types.NodeInfo {
nodeInfo := &types.NodeInfo{
Moniker: config.App().GetString("Moniker"),
Network: config.App().GetString("Network"),
Version: "0.1.0", // all JSON fields are under_score
Version: "0.2.0", // Everything is in Big Endian.
}
if !sw.IsListening() {
return nodeInfo


+ 6
- 6
p2p/addrbook.go View File

@ -623,17 +623,17 @@ func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
data1 = append(data1, []byte(groupKey(addr))...)
data1 = append(data1, []byte(groupKey(src))...)
hash1 := doubleSha256(data1)
hash64 := binary.LittleEndian.Uint64(hash1)
hash64 := binary.BigEndian.Uint64(hash1)
hash64 %= newBucketsPerGroup
var hashbuf [8]byte
binary.LittleEndian.PutUint64(hashbuf[:], hash64)
binary.BigEndian.PutUint64(hashbuf[:], hash64)
data2 := []byte{}
data2 = append(data2, []byte(a.key)...)
data2 = append(data2, groupKey(src)...)
data2 = append(data2, hashbuf[:]...)
hash2 := doubleSha256(data2)
return int(binary.LittleEndian.Uint64(hash2) % newBucketCount)
return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
}
// doublesha256(key + group + truncate_to_64bits(doublesha256(key + addr))%buckets_per_group) % num_buckets
@ -642,17 +642,17 @@ func (a *AddrBook) calcOldBucket(addr *NetAddress) int {
data1 = append(data1, []byte(a.key)...)
data1 = append(data1, []byte(addr.String())...)
hash1 := doubleSha256(data1)
hash64 := binary.LittleEndian.Uint64(hash1)
hash64 := binary.BigEndian.Uint64(hash1)
hash64 %= oldBucketsPerGroup
var hashbuf [8]byte
binary.LittleEndian.PutUint64(hashbuf[:], hash64)
binary.BigEndian.PutUint64(hashbuf[:], hash64)
data2 := []byte{}
data2 = append(data2, []byte(a.key)...)
data2 = append(data2, groupKey(addr)...)
data2 = append(data2, hashbuf[:]...)
hash2 := doubleSha256(data2)
return int(binary.LittleEndian.Uint64(hash2) % oldBucketCount)
return int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
}
// Return a string representing the network group of this address.


+ 3
- 3
rpc/test/.tendermint/genesis.json View File

@ -1,7 +1,7 @@
{
"accounts": [
{
"address": "d7dff9806078899c8da3fe3633cc0bf3c6c2b1bb",
"address": "1D7A91CB32F758A02EBB9BE1FB6F8DEE56F90D42",
"amount": 200000000
},
{
@ -11,11 +11,11 @@
],
"validators": [
{
"pub_key": [1, "2239c21c81ea7173a6c489145490c015e05d4b97448933b708a7ec5b7b4921e3"],
"pub_key": [1, "06FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"],
"amount": 1000000,
"unbond_to": [
{
"address": "d7dff9806078899c8da3fe3633cc0bf3c6c2b1bb",
"address": "1D7A91CB32F758A02EBB9BE1FB6F8DEE56F90D42",
"amount": 100000
}
]


+ 4
- 4
rpc/test/helpers_test.go View File

@ -28,9 +28,9 @@ var (
mempoolCount = 0
userAddr = "D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB"
userPriv = "FDE3BD94CB327D19464027BA668194C5EFA46AE83E8419D7542CFF41F00C81972239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"
userPub = "2239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"
userAddr = "1D7A91CB32F758A02EBB9BE1FB6F8DEE56F90D42"
userPriv = "C453604BD6480D5538B4C6FD2E3E314B5BCE518D75ADE4DA3DA85AB8ADFD819606FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"
userPub = "06FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"
userByteAddr, userBytePriv = initUserBytes()
clients = map[string]cclient.Client{
@ -193,7 +193,7 @@ func signTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byt
privAcc := account.GenPrivAccountFromKey(key)
if bytes.Compare(privAcc.PubKey.Address(), fromAddr) != 0 {
t.Fatal("Faield to generate correct priv acc")
t.Fatal("Failed to generate correct priv acc")
}
client := clients[typ]


+ 1
- 1
state/tx_cache.go View File

@ -147,7 +147,7 @@ func (cache *TxCache) AddLog(log *vm.Log) {
func NewContractAddress(caller []byte, nonce uint64) []byte {
temp := make([]byte, 32+8)
copy(temp, caller)
PutUint64LE(temp[32:], nonce)
PutUint64BE(temp[32:], nonce)
return sha3.Sha3(temp)[:20]
}


+ 1
- 1
vm/test/fake_app_state.go View File

@ -89,6 +89,6 @@ func createAddress(creator *Account) Word256 {
creator.Nonce += 1
temp := make([]byte, 32+8)
copy(temp, creator.Address[:])
PutUint64LE(temp[32:], nonce)
PutUint64BE(temp[32:], nonce)
return LeftPadWord256(sha3.Sha3(temp)[:20])
}

Loading…
Cancel
Save