From 58c1781e7f05bb68c87749d0a8949f8b67882477 Mon Sep 17 00:00:00 2001 From: Bo QIU <35757521+boqiu@users.noreply.github.com> Date: Tue, 27 Feb 2024 11:41:21 +0800 Subject: [PATCH] Add method for submission to calculate root hash (#2) --- contract/contract.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/contract/contract.go b/contract/contract.go index bf1c372..d610b72 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/openweb3/web3go" "github.com/zero-gravity-labs/zerog-storage-client/common/blockchain" ) @@ -37,3 +38,21 @@ func (submission Submission) String() string { return fmt.Sprintf("{ Size: %v, Heights: %v }", submission.Length, heights) } + +func (submission Submission) Root() common.Hash { + numNodes := len(submission.Nodes) + + // should be never occur + if numNodes == 0 { + return common.Hash{} + } + + // calculate root in reverse order + root := submission.Nodes[numNodes-1].Root + for i := 1; i < numNodes; i++ { + left := submission.Nodes[numNodes-1-i] + root = crypto.Keccak256Hash(left.Root[:], root[:]) + } + + return root +}