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

Return new node on modification #3193

Merged
merged 12 commits into from
Sep 21, 2022
Merged

Return new node on modification #3193

merged 12 commits into from
Sep 21, 2022

Conversation

CoderZhi
Copy link
Collaborator

@CoderZhi CoderZhi commented Mar 15, 2022

Return a new node on any modification. With this change, it is possible to reuse the nodes and minimize the spending in constructing a trie.

#2847

@CoderZhi CoderZhi requested a review from a team as a code owner March 15, 2022 00:15
@CoderZhi CoderZhi force-pushed the new_node_on_modification branch 3 times, most recently from 0c93b9e to 260b0d9 Compare March 16, 2022 04:35
@codecov
Copy link

codecov bot commented Mar 16, 2022

Codecov Report

Merging #3193 (8fd2de8) into master (e9732a1) will decrease coverage by 0.17%.
The diff coverage is 78.21%.

@@            Coverage Diff             @@
##           master    #3193      +/-   ##
==========================================
- Coverage   74.95%   74.77%   -0.18%     
==========================================
  Files         269      269              
  Lines       23819    23880      +61     
==========================================
+ Hits        17854    17857       +3     
- Misses       5039     5095      +56     
- Partials      926      928       +2     
Impacted Files Coverage Δ
db/trie/mptrie/node.go 100.00% <ø> (ø)
action/protocol/execution/evm/evm.go 44.41% <15.38%> (-1.02%) ⬇️
db/trie/mptrie/hashnode.go 36.36% <41.17%> (-54.55%) ⬇️
db/trie/mptrie/merklepatriciatrie.go 78.73% <46.80%> (-12.70%) ⬇️
db/trie/mptrie/leafiterator.go 62.06% <50.00%> (ø)
action/protocol/execution/evm/evmstatedbadapter.go 65.87% <64.28%> (-0.04%) ⬇️
db/trie/mptrie/extensionnode.go 91.91% <94.28%> (-1.07%) ⬇️
db/trie/mptrie/branchnode.go 96.40% <97.33%> (-2.21%) ⬇️
action/protocol/context.go 68.00% <100.00%> (+0.25%) ⬆️
api/grpcserver.go 83.33% <100.00%> (ø)
... and 6 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Liuhaai
Copy link
Member

Liuhaai commented Mar 17, 2022

Could an new pr be created to introduce client interface?

@CoderZhi
Copy link
Collaborator Author

Could an new pr be created to introduce client interface?

is it hard to understand client interface?

hashVal []byte
}

func newHashNode(mpt *merklePatriciaTrie, ha []byte) *hashNode {
return &hashNode{mpt: mpt, hashVal: ha}
func newHashNode(_ client, ha []byte) *hashNode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why leave client here if it's not used?

li []uint8
isSorted bool // lazy-initilization
li []uint8
sorted bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not hurt to leave the comment

@@ -17,20 +17,28 @@ var ErrNoData = errors.New("no data in hash node")
type (
keyType []byte

client interface {
Copy link
Member

@dustinxie dustinxie Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to me merklePatriciaTrie implements this interface, is there more purpose/use of this interface?

if that's the case, name it like trieOps? the name client is not clear in this context

Delete(client, keyType, uint8) (node, error)
Upsert(client, keyType, uint8, []byte) (node, error)
Hash(client) ([]byte, error)
Flush(client) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible not add client to the func's input, if the node struct (leaf, extension, branch) contains the client interface?
feel it's a bit heavy/clumsy with this extra input

hash(client, bool) ([]byte, error)
proto(client, bool) (proto.Message, error)
delete(client) error
store(client) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
}
li := make([]uint8, 0, len(children))
for k := range children {
li = append(li, k)
}
return &SortedList{
li: li,
isSorted: false,
li: li,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add sorted: false

db/trie/mptrie/extensionnode.go Show resolved Hide resolved
hashVal []byte
ser []byte
}

func (cn *cacheNode) Hash() ([]byte, error) {
return cn.hash(false)
func (cn *cacheNode) Hash(c client) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cli

ser: ser,
},
children: children,
indices: b.indices,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is b.indices.Clone() needed?

return nil, err
}
var indices *SortedList
var children map[byte]node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the creation of children could be moved before if condition

children := make(map[byte]node, len(b.children))
for k, v := range b.children {
   children[k] = v
}
if child == nil {
   delete(b.children, key)
   .......
} else {
   children[key] = child
}

}
}

func (sl *SortedList) sort() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep private func below public func

@Liuhaai
Copy link
Member

Liuhaai commented Mar 21, 2022

Other things seems good for me apart from two things: 1. how is the performance of new func (b *branchNode) updateChild() compared with the old one in production; 2. I am not very confident about the change in sync mode. If a full sync could be done to verify the change, that'd be better.

Comment on lines 246 to 247
mpt.resetRoot(emptyRoot, mpt.emptyRootHash)
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return mpt.resetRoot because error unhandled

}

func (b *branchNode) updateChild(key byte, child node, hashnode bool) (node, error) {
if err := b.delete(); err != nil {
func (b *branchNode) updateChild(cli client, key byte, child node, hashnode bool) (node, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashnode is not used

}

func (e *extensionNode) updateChild(newChild node, hashnode bool) (node, error) {
err := e.delete()
func (e *extensionNode) updateChild(cli client, newChild node, hashnode bool) (node, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashnode is not used

}

func (e *extensionNode) updatePath(path []byte, hashnode bool) (node, error) {
if err := e.delete(); err != nil {
func (e *extensionNode) updatePath(cli client, path []byte, hashnode bool) (node, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashnode is not used

@raullenchai
Copy link
Member

PTAL @dustinxie @Liuhaai @huof6829

@Liuhaai
Copy link
Member

Liuhaai commented Sep 9, 2022

Except from hashnode bool mentioned by @huof6829, other things look good for me.

@sonarcloud
Copy link

sonarcloud bot commented Sep 21, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
6.5% 6.5% Duplication

@huof6829 huof6829 merged commit c53ad41 into master Sep 21, 2022
@dustinxie dustinxie deleted the new_node_on_modification branch December 17, 2022 04:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants