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

core/state, light, les: make signature of ContractCode hash-independent #27209

Merged
merged 7 commits into from
Jun 28, 2023

Conversation

gballet
Copy link
Member

@gballet gballet commented May 2, 2023

Like the previous changes to the state.Trie interface, in which functions were modified to use the un-hashed account address instead of its hashed version, the same thing needs to be applied to ContractCode since it takes the hash of the account's address as a parameter.

The only major problem is with LES: I have changed the request object to contain the un-hashed address instead of its hash.

Alternatively, it seems to me that this parameter is only used in LES. If we remove LES, then we can remove this parameter entirely.

light/trie.go Outdated
@@ -80,14 +80,14 @@ func (db *odrDatabase) ContractCode(addrHash, codeHash common.Hash) ([]byte, err
return code, nil
}
id := *db.id
id.AccKey = addrHash[:]
id.AccKey = addr[:]
Copy link
Contributor

Choose a reason for hiding this comment

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

Feels a bit strange to just change a byteslice representing a hash into representing an address. Just changing it doesn't mean it will work, does it?

Copy link
Member Author

Choose a reason for hiding this comment

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

it won't work if the 'server' doesn't implement this change, but if both client and server do - and if I understand correctly - it will simply replace the hash of the data with the plain address in the request.

I checked that all the locations were updated accordingly, but given the errors in CI, it looks like I missed a point somewhere. I'll wait for Zsolt's input before I fix it, the best approach is still the entire removal of LES if it can be done.

core/state/iterator.go Outdated Show resolved Hide resolved
Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

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

LGTM. LES is going through changes anyway, and it's not working as it is, so that shouldn't be a blocker (?)

@@ -204,7 +204,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
codeResults = make([]trie.CodeSyncResult, len(codeElements))
)
for i, element := range codeElements {
data, err := srcDb.ContractCode(common.Hash{}, element.code)
data, err := srcDb.ContractCode(common.Address{}, element.code)
Copy link
Member

Choose a reason for hiding this comment

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

We must roll out a new snap sync protocol for verkle, later.

In original scheme, contract code is only identified by code hash. While in verkle, contract code is identified by both address and some other information. Therefore, snap protocol has to be changed.

We can just skip it since verkle doesn't support snap sync anyway.

@rjl493456442 rjl493456442 force-pushed the address-in-contractcode branch 2 times, most recently from e837e1d to 59bada1 Compare June 13, 2023 04:09
@@ -628,13 +622,6 @@ func TestIncompleteStateSync(t *testing.T) {
}
batch.Write()

for _, root := range nodehashes {
Copy link
Member

Choose a reason for hiding this comment

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

It's a very hacky check so that I remove it.

What it does is: iterate the newly added nodes, treat them as the root node of the "sub-trie" and traverse the "sub-trie" respectively. This notion is not compatible in path-based also.

Copy link
Member

@rjl493456442 rjl493456442 left a comment

Choose a reason for hiding this comment

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

Lgtm. @holiman Please take another look.

Just one thing to keep in mind: we need to change snap sync protocol for verkle later.

@@ -208,7 +209,7 @@ func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error
// ContractCodeWithPrefix retrieves a particular contract's code. If the
// code can't be found in the cache, then check the existence with **new**
// db scheme.
func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]byte, error) {
func (db *cachingDB) ContractCodeWithPrefix(_, codeHash common.Hash) ([]byte, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

We should make this first parameter a common.Address to be consistent

Copy link
Member

Choose a reason for hiding this comment

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

Good catch!

core/state/iterator.go Outdated Show resolved Hide resolved
@rjl493456442
Copy link
Member

@gballet Please take another look. I have fixed all the comments.

Copy link

@Leena8686 Leena8686 left a comment

Choose a reason for hiding this comment

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

Making of signature of ContractCode hash-independent. Thanks for all you'll hard work.

Copy link
Member Author

@gballet gballet left a comment

Choose a reason for hiding this comment

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

@rjl493456442 thanks for your changes, they LGTM. I have rebased on top of master in order to fix the merge error.

Copy link

@Leena8686 Leena8686 left a comment

Choose a reason for hiding this comment

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

Make signature of ContractCode hash-independent.

Copy link
Member

@rjl493456442 rjl493456442 left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -27,7 +28,8 @@ import (
)

// nodeIterator is an iterator to traverse the entire state trie post-order,
// including all of the contract code and contract state tries.
// including all of the contract code and contract state tries. Preimage is
// required in order to resolve the contract address.
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure this is safe? Preimages are not enabled by default AFAIK in Geth, so does this mean node iteration all of a sudden breaks with this PR?

Copy link
Member

Choose a reason for hiding this comment

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

State iterator is useful to traverse the whole state, but it's not UX friendly, because the state keys are all hash instead of raw keys. In order to provide some meaningful information, preimages are still required.

However, we already have the state dumper, IMO we should merge these two together and get rid of one of them.

Copy link
Member

Choose a reason for hiding this comment

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

In case of this PR, state iterator is only used in test, to verify the state completeness, which I think it's a nice tool so I kept it.

@karalabe karalabe added this to the 1.12.1 milestone Jun 28, 2023
@karalabe karalabe merged commit 8bbb16b into ethereum:master Jun 28, 2023
devopsbo3 pushed a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
…nt (ethereum#27209)

* core/state, light, les: make signature of ContractCode hash-independent

* push current state for feedback

* les: fix unit test

* core, les, light: fix les unittests

* core/state, trie, les, light: fix state iterator

* core, les: address comments

* les: fix lint

---------

Co-authored-by: Gary Rong <garyrong0905@gmail.com>
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
devopsbo3 added a commit to HorizenOfficial/go-ethereum that referenced this pull request Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants