refactor out binary
This commit is contained in:
parent
15f8441068
commit
1c4f5e2506
|
@ -0,0 +1,9 @@
|
|||
package binary
|
||||
|
||||
import "io"
|
||||
|
||||
type Binary interface {
|
||||
ByteSize() int
|
||||
WriteTo(io.Writer) (int64, error)
|
||||
Equals(Binary) bool
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package merkle
|
||||
package binary
|
||||
|
||||
const (
|
||||
TYPE_NIL = byte(0x00)
|
|
@ -1,4 +1,4 @@
|
|||
package merkle
|
||||
package binary
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
@ -24,7 +24,7 @@ func (self Byte) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self Byte) Less(other Key) bool {
|
||||
func (self Byte) Less(other Binary) bool {
|
||||
if o, ok := other.(Byte); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -52,7 +52,7 @@ func (self Int8) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self Int8) Less(other Key) bool {
|
||||
func (self Int8) Less(other Binary) bool {
|
||||
if o, ok := other.(Int8); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -80,7 +80,7 @@ func (self UInt8) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self UInt8) Less(other Key) bool {
|
||||
func (self UInt8) Less(other Binary) bool {
|
||||
if o, ok := other.(UInt8); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -108,7 +108,7 @@ func (self Int16) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self Int16) Less(other Key) bool {
|
||||
func (self Int16) Less(other Binary) bool {
|
||||
if o, ok := other.(Int16); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -136,7 +136,7 @@ func (self UInt16) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self UInt16) Less(other Key) bool {
|
||||
func (self UInt16) Less(other Binary) bool {
|
||||
if o, ok := other.(UInt16); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -164,7 +164,7 @@ func (self Int32) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self Int32) Less(other Key) bool {
|
||||
func (self Int32) Less(other Binary) bool {
|
||||
if o, ok := other.(Int32); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -192,7 +192,7 @@ func (self UInt32) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self UInt32) Less(other Key) bool {
|
||||
func (self UInt32) Less(other Binary) bool {
|
||||
if o, ok := other.(UInt32); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -220,7 +220,7 @@ func (self Int64) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self Int64) Less(other Key) bool {
|
||||
func (self Int64) Less(other Binary) bool {
|
||||
if o, ok := other.(Int64); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -248,7 +248,7 @@ func (self UInt64) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self UInt64) Less(other Key) bool {
|
||||
func (self UInt64) Less(other Binary) bool {
|
||||
if o, ok := other.(UInt64); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -276,7 +276,7 @@ func (self Int) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self Int) Less(other Key) bool {
|
||||
func (self Int) Less(other Binary) bool {
|
||||
if o, ok := other.(Int); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -303,7 +303,7 @@ func (self UInt) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self UInt) Less(other Key) bool {
|
||||
func (self UInt) Less(other Binary) bool {
|
||||
if o, ok := other.(UInt); ok {
|
||||
return self < o
|
||||
} else {
|
|
@ -1,4 +1,4 @@
|
|||
package merkle
|
||||
package binary
|
||||
|
||||
import "io"
|
||||
import "bytes"
|
||||
|
@ -12,7 +12,7 @@ func (self String) Equals(other Binary) bool {
|
|||
return self == other
|
||||
}
|
||||
|
||||
func (self String) Less(other Key) bool {
|
||||
func (self String) Less(other Binary) bool {
|
||||
if o, ok := other.(String); ok {
|
||||
return self < o
|
||||
} else {
|
||||
|
@ -49,7 +49,7 @@ func (self ByteSlice) Equals(other Binary) bool {
|
|||
}
|
||||
}
|
||||
|
||||
func (self ByteSlice) Less(other Key) bool {
|
||||
func (self ByteSlice) Less(other Binary) bool {
|
||||
if o, ok := other.(ByteSlice); ok {
|
||||
return bytes.Compare(self, o) < 0 // -1 if a < b
|
||||
} else {
|
|
@ -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,6 +1,7 @@
|
|||
package merkle
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
"testing"
|
||||
"fmt"
|
||||
"os"
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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…
Reference in New Issue