mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-11 04:11:21 +00:00
refactor out binary
This commit is contained in:
9
binary/binary.go
Normal file
9
binary/binary.go
Normal file
@ -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 (
|
const (
|
||||||
TYPE_NIL = byte(0x00)
|
TYPE_NIL = byte(0x00)
|
@ -1,4 +1,4 @@
|
|||||||
package merkle
|
package binary
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
@ -24,7 +24,7 @@ func (self Byte) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Byte) Less(other Key) bool {
|
func (self Byte) Less(other Binary) bool {
|
||||||
if o, ok := other.(Byte); ok {
|
if o, ok := other.(Byte); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -52,7 +52,7 @@ func (self Int8) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Int8) Less(other Key) bool {
|
func (self Int8) Less(other Binary) bool {
|
||||||
if o, ok := other.(Int8); ok {
|
if o, ok := other.(Int8); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -80,7 +80,7 @@ func (self UInt8) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self UInt8) Less(other Key) bool {
|
func (self UInt8) Less(other Binary) bool {
|
||||||
if o, ok := other.(UInt8); ok {
|
if o, ok := other.(UInt8); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -108,7 +108,7 @@ func (self Int16) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Int16) Less(other Key) bool {
|
func (self Int16) Less(other Binary) bool {
|
||||||
if o, ok := other.(Int16); ok {
|
if o, ok := other.(Int16); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -136,7 +136,7 @@ func (self UInt16) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self UInt16) Less(other Key) bool {
|
func (self UInt16) Less(other Binary) bool {
|
||||||
if o, ok := other.(UInt16); ok {
|
if o, ok := other.(UInt16); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -164,7 +164,7 @@ func (self Int32) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Int32) Less(other Key) bool {
|
func (self Int32) Less(other Binary) bool {
|
||||||
if o, ok := other.(Int32); ok {
|
if o, ok := other.(Int32); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -192,7 +192,7 @@ func (self UInt32) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self UInt32) Less(other Key) bool {
|
func (self UInt32) Less(other Binary) bool {
|
||||||
if o, ok := other.(UInt32); ok {
|
if o, ok := other.(UInt32); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -220,7 +220,7 @@ func (self Int64) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Int64) Less(other Key) bool {
|
func (self Int64) Less(other Binary) bool {
|
||||||
if o, ok := other.(Int64); ok {
|
if o, ok := other.(Int64); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -248,7 +248,7 @@ func (self UInt64) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self UInt64) Less(other Key) bool {
|
func (self UInt64) Less(other Binary) bool {
|
||||||
if o, ok := other.(UInt64); ok {
|
if o, ok := other.(UInt64); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -276,7 +276,7 @@ func (self Int) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Int) Less(other Key) bool {
|
func (self Int) Less(other Binary) bool {
|
||||||
if o, ok := other.(Int); ok {
|
if o, ok := other.(Int); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
||||||
@ -303,7 +303,7 @@ func (self UInt) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self UInt) Less(other Key) bool {
|
func (self UInt) Less(other Binary) bool {
|
||||||
if o, ok := other.(UInt); ok {
|
if o, ok := other.(UInt); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} else {
|
@ -1,4 +1,4 @@
|
|||||||
package merkle
|
package binary
|
||||||
|
|
||||||
import "io"
|
import "io"
|
||||||
import "bytes"
|
import "bytes"
|
||||||
@ -12,7 +12,7 @@ func (self String) Equals(other Binary) bool {
|
|||||||
return self == other
|
return self == other
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self String) Less(other Key) bool {
|
func (self String) Less(other Binary) bool {
|
||||||
if o, ok := other.(String); ok {
|
if o, ok := other.(String); ok {
|
||||||
return self < o
|
return self < o
|
||||||
} else {
|
} 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 {
|
if o, ok := other.(ByteSlice); ok {
|
||||||
return bytes.Compare(self, o) < 0 // -1 if a < b
|
return bytes.Compare(self, o) < 0 // -1 if a < b
|
||||||
} else {
|
} else {
|
@ -1,7 +1,7 @@
|
|||||||
package merkle
|
package merkle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
//"fmt"
|
. "github.com/tendermint/tendermint/binary"
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
@ -195,7 +195,7 @@ func (self *IAVLNode) Hash() (ByteSlice, uint64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hasher := sha256.New()
|
hasher := sha256.New()
|
||||||
_, hashCount, err := self.saveToCountHashes(hasher)
|
_, hashCount, err := self.saveToCountHashes(hasher, false)
|
||||||
if err != nil { panic(err) }
|
if err != nil { panic(err) }
|
||||||
self.hash = hasher.Sum(nil)
|
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) {
|
func (self *IAVLNode) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
n, _, err = self.saveToCountHashes(w)
|
n, _, err = self.saveToCountHashes(w, true)
|
||||||
return
|
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
|
var _n int64
|
||||||
|
|
||||||
// height & size
|
if meta {
|
||||||
_n, err = UInt8(self.height).WriteTo(w)
|
// height & size
|
||||||
if err != nil { return } else { n += _n }
|
_n, err = UInt8(self.height).WriteTo(w)
|
||||||
_n, err = UInt64(self.size).WriteTo(w)
|
if err != nil { return } else { n += _n }
|
||||||
if err != nil { return } else { n += _n }
|
_n, err = UInt64(self.size).WriteTo(w)
|
||||||
|
if err != nil { return } else { n += _n }
|
||||||
|
|
||||||
// key
|
// key
|
||||||
_n, err = Byte(GetBinaryType(self.key)).WriteTo(w)
|
_n, err = Byte(GetBinaryType(self.key)).WriteTo(w)
|
||||||
if err != nil { return } else { n += _n }
|
if err != nil { return } else { n += _n }
|
||||||
_n, err = self.key.WriteTo(w)
|
_n, err = self.key.WriteTo(w)
|
||||||
if err != nil { return } else { n += _n }
|
if err != nil { return } else { n += _n }
|
||||||
|
}
|
||||||
|
|
||||||
if self.height == 0 {
|
if self.height == 0 {
|
||||||
// value
|
// value
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package merkle
|
package merkle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
. "github.com/tendermint/tendermint/binary"
|
||||||
"testing"
|
"testing"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
@ -1,23 +1,17 @@
|
|||||||
package merkle
|
package merkle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
. "github.com/tendermint/tendermint/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Binary interface {
|
|
||||||
ByteSize() int
|
|
||||||
WriteTo(io.Writer) (int64, error)
|
|
||||||
Equals(Binary) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type Value interface {
|
type Value interface {
|
||||||
Binary
|
Binary
|
||||||
}
|
}
|
||||||
|
|
||||||
type Key interface {
|
type Key interface {
|
||||||
Binary
|
Binary
|
||||||
Less(b Key) bool
|
Less(b Binary) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Db interface {
|
type Db interface {
|
||||||
|
@ -1,10 +1,34 @@
|
|||||||
package merkle
|
package merkle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
. "github.com/tendermint/tendermint/binary"
|
||||||
"os"
|
"os"
|
||||||
"fmt"
|
"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) {
|
func PrintIAVLNode(node *IAVLNode) {
|
||||||
fmt.Println("==== NODE")
|
fmt.Println("==== NODE")
|
||||||
if node != nil {
|
if node != nil {
|
||||||
|
Reference in New Issue
Block a user