Browse Source

refactor out binary

pull/9/head
Jae Kwon 11 years ago
parent
commit
1c4f5e2506
8 changed files with 68 additions and 38 deletions
  1. +9
    -0
      binary/binary.go
  2. +1
    -1
      binary/codec.go
  3. +12
    -12
      binary/int.go
  4. +3
    -3
      binary/string.go
  5. +16
    -14
      merkle/iavl.go
  6. +1
    -0
      merkle/iavl_test.go
  7. +2
    -8
      merkle/types.go
  8. +24
    -0
      merkle/util.go

+ 9
- 0
binary/binary.go View File

@ -0,0 +1,9 @@
package binary
import "io"
type Binary interface {
ByteSize() int
WriteTo(io.Writer) (int64, error)
Equals(Binary) bool
}

merkle/binary.go → binary/codec.go View File


merkle/int.go → binary/int.go View File


merkle/string.go → binary/string.go View File


+ 16
- 14
merkle/iavl.go View File

@ -1,7 +1,7 @@
package merkle
import (
//"fmt"
. "github.com/tendermint/tendermint/binary"
"bytes"
"io"
"crypto/sha256"
@ -195,7 +195,7 @@ func (self *IAVLNode) Hash() (ByteSlice, uint64) {
}
hasher := sha256.New()
_, hashCount, err := self.saveToCountHashes(hasher)
_, hashCount, err := self.saveToCountHashes(hasher, false)
if err != nil { panic(err) }
self.hash = hasher.Sum(nil)
@ -325,24 +325,26 @@ func (self *IAVLNode) ByteSize() int {
}
func (self *IAVLNode) WriteTo(w io.Writer) (n int64, err error) {
n, _, err = self.saveToCountHashes(w)
n, _, err = self.saveToCountHashes(w, true)
return
}
func (self *IAVLNode) saveToCountHashes(w io.Writer) (n int64, hashCount uint64, err error) {
func (self *IAVLNode) saveToCountHashes(w io.Writer, meta bool) (n int64, hashCount uint64, err error) {
var _n int64
// height & size
_n, err = UInt8(self.height).WriteTo(w)
if err != nil { return } else { n += _n }
_n, err = UInt64(self.size).WriteTo(w)
if err != nil { return } else { n += _n }
if meta {
// height & size
_n, err = UInt8(self.height).WriteTo(w)
if err != nil { return } else { n += _n }
_n, err = UInt64(self.size).WriteTo(w)
if err != nil { return } else { n += _n }
// key
_n, err = Byte(GetBinaryType(self.key)).WriteTo(w)
if err != nil { return } else { n += _n }
_n, err = self.key.WriteTo(w)
if err != nil { return } else { n += _n }
// key
_n, err = Byte(GetBinaryType(self.key)).WriteTo(w)
if err != nil { return } else { n += _n }
_n, err = self.key.WriteTo(w)
if err != nil { return } else { n += _n }
}
if self.height == 0 {
// value


+ 1
- 0
merkle/iavl_test.go View File

@ -1,6 +1,7 @@
package merkle
import (
. "github.com/tendermint/tendermint/binary"
"testing"
"fmt"
"os"


+ 2
- 8
merkle/types.go View File

@ -1,23 +1,17 @@
package merkle
import (
"io"
. "github.com/tendermint/tendermint/binary"
"fmt"
)
type Binary interface {
ByteSize() int
WriteTo(io.Writer) (int64, error)
Equals(Binary) bool
}
type Value interface {
Binary
}
type Key interface {
Binary
Less(b Key) bool
Less(b Binary) bool
}
type Db interface {


+ 24
- 0
merkle/util.go View File

@ -1,10 +1,34 @@
package merkle
import (
. "github.com/tendermint/tendermint/binary"
"os"
"fmt"
"crypto/sha256"
)
/*
Compute a deterministic merkle hash from a list of byteslices.
*/
func HashFromBinarySlice(items []Binary) ByteSlice {
switch len(items) {
case 0:
panic("Cannot compute hash of empty slice")
case 1:
hasher := sha256.New()
_, err := items[0].WriteTo(hasher)
if err != nil { panic(err) }
return ByteSlice(hasher.Sum(nil))
default:
hasher := sha256.New()
_, err := HashFromBinarySlice(items[0:len(items)/2]).WriteTo(hasher)
if err != nil { panic(err) }
_, err = HashFromBinarySlice(items[len(items)/2:]).WriteTo(hasher)
if err != nil { panic(err) }
return ByteSlice(hasher.Sum(nil))
}
}
func PrintIAVLNode(node *IAVLNode) {
fmt.Println("==== NODE")
if node != nil {


Loading…
Cancel
Save