Skip to content

Commit

Permalink
Add method for submission to calculate root hash (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
boqiu authored Feb 27, 2024
1 parent d05e03e commit 58c1781
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 58c1781

Please sign in to comment.