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 Proof API refactor #3453

Merged
merged 24 commits into from
Oct 8, 2021
Merged

Merkle Proof API refactor #3453

merged 24 commits into from
Oct 8, 2021

Conversation

BeniaminDrasovean
Copy link
Contributor

Added a new endpoint that returns the proof for an account, and the proof for a given key from the data trie, all in a single call.

For tessting, check that the following endpoints work on a node:
"/proof/address/:address"
"/proof/root-hash/:roothash/address/:address"
"/proof/root-hash/:roothash/address/:address/key/:key"
"/proof/verify"

# Conflicts:
#	api/groups/proofGroup.go
#	api/groups/proofGroup_test.go
#	facade/nodeFacade.go
}

c.JSON(
http.StatusOK,
shared.GenericAPIResponse{
Data: gin.H{"proof": hexProof},
Data: gin.H{
"mainProof": mainProofHex,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should have been:

{
 "data": {
     "proofs": {  
           "mainProof": ..
            ....
      }
 }
....

you placed the attributes right under the data field which doesn't follow the format of other endpoints

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

// GetProofDataTrie returns the Merkle Proof for the given address, and another Merkle Proof
// for the given key, if it is present in the dataTrie
Copy link
Contributor

Choose a reason for hiding this comment

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

"if it is present in the dataTrie" -> "if it exists in the dataTrie"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

func (nf *nodeFacade) GetProof(rootHash string, address string) ([][]byte, error) {
rootHashBytes, err := hex.DecodeString(rootHash)
func (nf *nodeFacade) GetProof(rootHash string, address string) (*shared.GetProofResponse, error) {
proof, value, err := nf.getProof(rootHash, address)
Copy link
Contributor

Choose a reason for hiding this comment

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

previously, nodeFacade was only a middleware between the API and node.go. you introduced some logic here and also a marshalizer, that already exists in node.go.
Maybe move the logic there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

api/groups/proofGroup.go Outdated Show resolved Hide resolved
facade/nodeFacade.go Outdated Show resolved Hide resolved
addressBytes, err := nf.DecodeAddressPubkey(address)
if err != nil {
return nil, nil, err
}

proof, err := trie.GetProof(addressBytes)
account, err := nf.accountsState.LoadAccount(addressBytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

the effect of "changing the trie data" while this function executes is more obvious in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


// GetProofDataTrie returns the Merkle Proof for the given address, and another Merkle Proof
// for the given key, if it is present in the dataTrie
func (nf *nodeFacade) GetProofDataTrie(rootHash string, address string, key string) (*shared.GetProofResponse, *shared.GetProofResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add an integration test with one node that demonstrates that proof operations work with the verify proof operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@codecov
Copy link

codecov bot commented Sep 22, 2021

Codecov Report

Merging #3453 (3565a4e) into development (4e7cc22) will decrease coverage by 0.01%.
The diff coverage is 72.47%.

❗ Current head 3565a4e differs from pull request most recent head 64d217b. Consider uploading reports for the commit 64d217b to get more accurate results
Impacted file tree graph

@@               Coverage Diff               @@
##           development    #3453      +/-   ##
===============================================
- Coverage        73.89%   73.87%   -0.02%     
===============================================
  Files              581      582       +1     
  Lines            74278    74441     +163     
===============================================
+ Hits             54889    54996     +107     
- Misses           15002    15044      +42     
- Partials          4387     4401      +14     
Impacted Files Coverage Δ
process/txsimulator/wrappedAccountsDB.go 81.81% <0.00%> (-3.90%) ⬇️
state/accountsDB.go 77.33% <55.55%> (-0.65%) ⬇️
api/groups/proofGroup.go 71.76% <64.28%> (-5.04%) ⬇️
trie/patriciaMerkleTrie.go 75.00% <66.66%> (+0.46%) ⬆️
facade/initial/initialNodeFacade.go 94.33% <75.00%> (-1.82%) ⬇️
node/node.go 69.93% <78.08%> (+0.89%) ⬆️
facade/nodeFacade.go 67.84% <100.00%> (-2.28%) ⬇️
trie/branchNode.go 80.33% <100.00%> (+0.07%) ⬆️
trie/extensionNode.go 71.83% <100.00%> (+0.13%) ⬆️
trie/leafNode.go 81.27% <100.00%> (+0.14%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0f4656f...64d217b. Read the comment docs.

api/mock/facade.go Outdated Show resolved Hide resolved
common/dtos.go Outdated Show resolved Hide resolved
integrationTests/state/stateTrie/stateTrie_test.go Outdated Show resolved Hide resolved
node/export_test.go Outdated Show resolved Hide resolved
state/accountsDB.go Outdated Show resolved Hide resolved
state/accountsDB.go Show resolved Hide resolved
node/node.go Outdated
return false, err
}

return trie.VerifyProof(key, proof)
Copy link
Contributor

Choose a reason for hiding this comment

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

It now has become obvious that you actually need all the trie nodes to verify a proof. This should have been a stateless function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. This will be added on erdgo.

return nil, nil, fmt.Errorf("the address does not belong to a user account")
}
dataTrieRootHash := userAccount.GetRootHash()

Copy link
Contributor

Choose a reason for hiding this comment

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

remove empty line here and add it on L1345

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -602,15 +602,11 @@ func (tr *patriciaMerkleTrie) GetProof(key []byte) ([][]byte, []byte, error) {
}

// VerifyProof verifies the given Merkle proof
func (tr *patriciaMerkleTrie) VerifyProof(key []byte, proof [][]byte) (bool, error) {
func (tr *patriciaMerkleTrie) VerifyProof(rootHash []byte, key []byte, proof [][]byte) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to provide a roothash here? I thought this process is stateless and it can work with an empty roothash.

iulianpascalau
iulianpascalau previously approved these changes Oct 6, 2021
Copy link
Contributor

@iulianpascalau iulianpascalau left a comment

Choose a reason for hiding this comment

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

Maybe removing the verify proof endpoint route wasn't such a good idea from integration and testing point of view

bogdan-rosianu
bogdan-rosianu previously approved these changes Oct 6, 2021
iulianpascalau
iulianpascalau previously approved these changes Oct 6, 2021
bogdan-rosianu
bogdan-rosianu previously approved these changes Oct 6, 2021
Copy link
Collaborator

@gabi-vuls gabi-vuls left a comment

Choose a reason for hiding this comment

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

System test passed.
image

@gabi-vuls gabi-vuls merged commit ef91386 into development Oct 8, 2021
@gabi-vuls gabi-vuls deleted the merkle-proof-api-refactor branch October 8, 2021 13:59
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.

None yet

5 participants