Do not use global wire.Marshal*

This commit is contained in:
Jae Kwon 2018-01-21 19:21:17 -08:00
parent 0d22a9106c
commit 68e04e629a
5 changed files with 26 additions and 18 deletions

12
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: a48b72c21e7e4eaf17cf54ae0ab8104c15a9b230adc3f33318fe5c068dd94996
updated: 2018-01-16T11:19:24.354963539-08:00
hash: e8435271aa9fdfc49efff9016427eca1fd6b43093a17187619360edda9f265e6
updated: 2018-01-21T19:17:17.451995428-08:00
imports:
- name: github.com/btcsuite/btcd
version: 2e60448ffcc6bf78332d1fe590260095f554dd78
@ -87,15 +87,15 @@ imports:
- edwards25519
- extra25519
- name: github.com/tendermint/go-crypto
version: 32741be2126500d600cede1e2016bbbe2754cb46
version: 12142af1cb4e3479ea4ac98a3171debff87519c6
subpackages:
- keys
- name: github.com/tendermint/go-wire
version: b93ebdd4f306833936c243561ec30af3455dc764
version: 0cce10e82786f2d501827fbe158747dbc4ceeb43
subpackages:
- data
- name: github.com/tendermint/iavl
version: 1dfe265ab4b491418e88e1da6577a8ad594fc989
version: 02885cf5a7461430e9d9e84b81fbda1e26645ab5
- name: github.com/tendermint/light-client
version: 76313d625e662ed7b284d066d68ff71edd7a9fac
subpackages:
@ -119,7 +119,7 @@ imports:
- state
- types
- name: github.com/tendermint/tmlibs
version: 7a52d47a1676a9fe61d07fde0a48a733cce564c6
version: 80029abc6e20f85079cd751e659a05508773288c
subpackages:
- cli
- cli/flags

View File

@ -40,7 +40,7 @@ import:
- rpc/lib/types
- types
- package: github.com/tendermint/tmlibs
version: sdk2
version: sdk2-hashers-and-simple-map
subpackages:
- cli
- cli/flags

View File

@ -3,7 +3,6 @@ package store
import (
"fmt"
"github.com/tendermint/go-wire"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/merkle"
"golang.org/x/crypto/ripemd160"
@ -198,7 +197,7 @@ type commitState struct {
// Hash returns the simple merkle root hash of the substores sorted by name.
func (cs commitState) Hash() []byte {
// TODO cache to cs.hash []byte
m := make(map[string]interface{}, len(cs.Substores))
m := make(map[string]merkle.Hasher, len(cs.Substores))
for _, substore := range cs.Substores {
m[substore.Name] = substore
}
@ -227,11 +226,13 @@ type substoreCore struct {
// ... maybe add more state
}
// Hash returns the RIPEMD160 of the wire-encoded substore.
func (sc substoreCore) Hash() []byte {
scBytes, _ := wire.MarshalBinary(sc) // Does not error
// Implements merkle.Hasher.
func (sw substore) Hash() []byte {
// Doesn't write Name, since merkle.SimpleHashFromMap() will
// include them via the keys.
bz, _ := cdc.MarshalBinary(sw.substoreCore) // Does not error
hasher := ripemd160.New()
hasher.Write(scBytes)
hasher.Write(bz)
return hasher.Sum(nil)
}
@ -244,7 +245,7 @@ func getLatestVersion(db dbm.DB) int64 {
if latestBytes == nil {
return 0
}
err := wire.UnmarshalBinary(latestBytes, &latest)
err := cdc.UnmarshalBinary(latestBytes, &latest)
if err != nil {
panic(err)
}
@ -253,7 +254,7 @@ func getLatestVersion(db dbm.DB) int64 {
// Set the latest version.
func setLatestVersion(batch dbm.Batch, version int64) {
latestBytes, _ := wire.MarshalBinary(version) // Does not error
latestBytes, _ := cdc.MarshalBinary(version) // Does not error
batch.Set([]byte(latestVersionKey), latestBytes)
}
@ -290,7 +291,7 @@ func getCommitState(db dbm.DB, ver int64) (commitState, error) {
// Parse bytes.
var state commitState
err := wire.UnmarshalBinary(stateBytes, &state)
err := cdc.UnmarshalBinary(stateBytes, &state)
if err != nil {
return commitState{}, fmt.Errorf("Failed to get rootMultiStore: %v", err)
}
@ -299,7 +300,7 @@ func getCommitState(db dbm.DB, ver int64) (commitState, error) {
// Set a commit state for given version.
func setCommitState(batch dbm.Batch, version int64, state commitState) {
stateBytes, err := wire.MarshalBinary(state)
stateBytes, err := cdc.MarshalBinary(state)
if err != nil {
panic(err)
}

View File

@ -94,7 +94,7 @@ func getExpectedCommitID(store *rootMultiStore, ver int64) CommitID {
}
func hashStores(stores map[SubstoreKey]CommitStore) []byte {
m := make(map[string]interface{}, len(stores))
m := make(map[string]merkle.Hasher, len(stores))
for key, store := range stores {
name := key.Name()
m[name] = substore{

7
store/wire.go Normal file
View File

@ -0,0 +1,7 @@
package store
import (
"github.com/tendermint/go-wire"
)
var cdc = wire.NewCodec()