Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merkle root check #1114

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions core/store/ledgerstore/ledger_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,7 @@ func (this *LedgerStoreImp) executeBlock(block *types.Block) (result store.Execu

result.Hash = overlay.ChangeHash()
result.WriteSet = overlay.GetWriteSet()
if block.Header.Height < this.stateHashCheckHeight {
result.MerkleRoot = common.UINT256_EMPTY
} else if block.Header.Height == this.stateHashCheckHeight {
if block.Header.Height == this.stateHashCheckHeight {
res, e := calculateTotalStateHash(overlay)
if e != nil {
err = e
Expand Down
26 changes: 12 additions & 14 deletions core/store/ledgerstore/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,18 @@ func (self *StateStore) init(currBlockHeight uint32) error {
}
self.merkleTree = merkle.NewTree(treeSize, hashes, self.merkleHashStore)

treeSize, hashes, err = self.GetStateMerkleTree()
if err != nil && err != scom.ErrNotFound {
return err
}
expectedTreeSize := currBlockHeight + 1
if currBlockHeight >= self.stateHashCheckHeight {
treeSize, hashes, err := self.GetStateMerkleTree()
if err != nil && err != scom.ErrNotFound {
return err
}
if treeSize > 0 && treeSize != currBlockHeight-self.stateHashCheckHeight+1 {
return fmt.Errorf("merkle tree size is inconsistent with blockheight: %d", currBlockHeight+1)
}
self.deltaMerkleTree = merkle.NewTree(treeSize, hashes, nil)
expectedTreeSize = currBlockHeight - self.stateHashCheckHeight + 1
}
if treeSize > 0 && treeSize != expectedTreeSize {
return fmt.Errorf("merkle tree size is inconsistent with blockheight: %d", currBlockHeight+1)
}
self.deltaMerkleTree = merkle.NewTree(treeSize, hashes, nil)
return nil
}

Expand Down Expand Up @@ -165,9 +167,7 @@ func (self *StateStore) getMerkleTree(key []byte) (uint32, []common.Uint256, err
}

func (self *StateStore) GetStateMerkleRoot(height uint32) (result common.Uint256, err error) {
if height < self.stateHashCheckHeight {
return
}

key := self.genStateMerkleRootKey(height)
var value []byte
value, err = self.store.Get(key)
Expand All @@ -184,9 +184,7 @@ func (self *StateStore) GetStateMerkleRoot(height uint32) (result common.Uint256
}

func (self *StateStore) AddStateMerkleTreeRoot(blockHeight uint32, writeSetHash common.Uint256) error {
if blockHeight < self.stateHashCheckHeight {
return nil
} else if blockHeight == self.stateHashCheckHeight {
if blockHeight == self.stateHashCheckHeight {
self.deltaMerkleTree = merkle.NewTree(0, nil, nil)
}
key := self.genStateMerkleTreeKey()
Expand Down
16 changes: 5 additions & 11 deletions core/store/ledgerstore/state_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,13 @@ func TestStateMerkleRoot(t *testing.T) {
diffHashes = append(diffHashes, hash)
}
db := NewMemStateStore(effectiveStateHashHeight)
for h, hash := range diffHashes[:effectiveStateHashHeight] {
height := uint32(h)
db.NewBatch()
err := db.AddStateMerkleTreeRoot(height, hash)
assert.Nil(t, err)
db.CommitTo()
root, _ := db.GetStateMerkleRoot(height)
assert.Equal(t, root, common.UINT256_EMPTY)
}

merkleTree := merkle.NewTree(0, nil, nil)
for h, hash := range diffHashes[effectiveStateHashHeight:] {
height := uint32(h) + effectiveStateHashHeight
for h, hash := range diffHashes {
height := uint32(h)
if uint32(h) >= effectiveStateHashHeight {
height = uint32(h) + effectiveStateHashHeight
}
merkleTree.AppendHash(hash)
root1 := db.GetStateMerkleRootWithNewHash(hash)
db.NewBatch()
Expand Down
9 changes: 6 additions & 3 deletions wasmtest/wasm-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ func execTxCheckRes(tx *types.Transaction, testCase common3.TestCase, database *
checkExecResult(testCase, res, execEnv)

block, _ := makeBlock(acct, []*types.Transaction{tx})
err = database.AddBlock(block, common.UINT256_EMPTY)
result, err := database.ExecuteBlock(block)
checkErr(err)
err = database.SubmitBlock(block, result)
checkErr(err)
}

Expand Down Expand Up @@ -267,9 +269,10 @@ func main() {
}

block, _ := makeBlock(acct, txes)
err = database.AddBlock(block, common.UINT256_EMPTY)
result, err := database.ExecuteBlock(block)
checkErr(err)
err = database.SubmitBlock(block, result)
checkErr(err)

addrMap := make(map[string]common.Address)
for file, code := range contract {
addrMap[path.Base(file)] = common.AddressFromVmCode(code)
Expand Down