Skip to content

Commit

Permalink
change default hash function to 160b
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi committed Feb 13, 2020
1 parent 411cde2 commit 238725e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion action/protocol/execution/evm/contract.go
Expand Up @@ -171,7 +171,8 @@ func newContract(addr hash.Hash160, account *state.Account, dao db.KVStore) (Con
trie.KVStoreOption(dbForTrie),
trie.KeyLengthOption(len(hash.Hash256{})),
trie.HashFuncOption(func(data []byte) []byte {
return trie.DefaultHashFunc(append(addr[:], data...))
h := hash.Hash256b(append(addr[:], data...))
return h[:]
}),
}
if account.Root != hash.ZeroHash256 {
Expand Down
2 changes: 1 addition & 1 deletion db/trie/trie.go
Expand Up @@ -38,7 +38,7 @@ var (

// DefaultHashFunc implements a default hash function
func DefaultHashFunc(data []byte) []byte {
h := hash.Hash256b(data)
h := hash.Hash160b(data)
return h[:]
}

Expand Down
4 changes: 2 additions & 2 deletions state/factory/factory.go
Expand Up @@ -396,8 +396,8 @@ func (sf *factory) DeleteWorkingSet(blk *block.Block) error {
// private trie constructor functions
//======================================

func (sf *factory) rootHash() hash.Hash256 {
return hash.BytesToHash256(sf.accountTrie.RootHash())
func (sf *factory) rootHash() []byte {
return sf.accountTrie.RootHash()
}

func (sf *factory) state(addr []byte, s interface{}) error {
Expand Down
6 changes: 3 additions & 3 deletions state/factory/statetx.go
Expand Up @@ -47,11 +47,11 @@ func newStateTX(
}

// RootHash returns the hash of the root node of the accountTrie
func (stx *stateTX) RootHash() (hash.Hash256, error) {
func (stx *stateTX) RootHash() ([]byte, error) {
if !stx.finalized {
return hash.ZeroHash256, errors.New("workingset has not been finalized yet")
return nil, errors.New("workingset has not been finalized yet")
}
return hash.ZeroHash256, nil
return nil, nil
}

// Digest returns the delta state digest
Expand Down
20 changes: 10 additions & 10 deletions state/factory/workingset.go
Expand Up @@ -55,7 +55,7 @@ type (
RunActions(context.Context, []action.SealedEnvelope) ([]*action.Receipt, error)
Finalize() error
Commit() error
RootHash() (hash.Hash256, error)
RootHash() ([]byte, error)
Digest() (hash.Hash256, error)
Version() uint64
}
Expand All @@ -64,8 +64,8 @@ type (
workingSet struct {
finalized bool
blockHeight uint64
accountTrie trie.Trie // global account state trie
trieRoots map[int]hash.Hash256 // root of trie at time of snapshot
accountTrie trie.Trie // global account state trie
trieRoots map[int][]byte // root of trie at time of snapshot
flusher db.KVStoreFlusher
}
)
Expand All @@ -74,7 +74,7 @@ type (
func newWorkingSet(
height uint64,
kv db.KVStore,
root hash.Hash256,
root []byte,
opts ...db.KVStoreFlusherOption,
) (WorkingSet, error) {
flusher, err := db.NewKVStoreFlusher(kv, batch.NewCachedBatch(), opts...)
Expand All @@ -95,17 +95,17 @@ func newWorkingSet(
accountTrie: tr,
finalized: false,
blockHeight: height,
trieRoots: make(map[int]hash.Hash256),
trieRoots: make(map[int][]byte),
flusher: flusher,
}, tr.Start(context.Background())
}

// RootHash returns the hash of the root node of the accountTrie
func (ws *workingSet) RootHash() (hash.Hash256, error) {
func (ws *workingSet) RootHash() ([]byte, error) {
if !ws.finalized {
return hash.ZeroHash256, errors.Errorf("working set has not been finalized")
return nil, errors.Errorf("working set has not been finalized")
}
return hash.BytesToHash256(ws.accountTrie.RootHash()), nil
return ws.accountTrie.RootHash(), nil
}

// Digest returns the delta state digest
Expand Down Expand Up @@ -231,7 +231,7 @@ func (ws *workingSet) Finalize() error {

func (ws *workingSet) Snapshot() int {
s := ws.flusher.KVStoreWithBuffer().Snapshot()
ws.trieRoots[s] = hash.BytesToHash256(ws.accountTrie.RootHash())
ws.trieRoots[s] = ws.accountTrie.RootHash()
return s
}

Expand Down Expand Up @@ -314,5 +314,5 @@ func (ws *workingSet) DelState(opts ...protocol.StateOption) (uint64, error) {
// clearCache removes all local changes after committing to trie
func (ws *workingSet) clear() {
ws.trieRoots = nil
ws.trieRoots = make(map[int]hash.Hash256)
ws.trieRoots = make(map[int][]byte)
}

0 comments on commit 238725e

Please sign in to comment.