From edadb627022b3fd7798322f76fc47d422479495e Mon Sep 17 00:00:00 2001 From: OsauravO Date: Sun, 28 Apr 2024 10:30:30 +0530 Subject: [PATCH] fixing the merkle root --- main.go | 40 +++++++++++++++++++++------------------- output.txt | 6 +++--- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 91516b4..c8de968 100644 --- a/main.go +++ b/main.go @@ -263,25 +263,27 @@ func merkNode(lnode *MerkleNode, rnode *MerkleNode, data []byte) *MerkleNode { } func merkTree(leaves []string) *MerkleNode { - var nodes []*MerkleNode - for _, leaf := range leaves { - data, _ := hex.DecodeString(leaf) - node := merkNode(nil, nil, data) - nodes = append(nodes, node) - } - for len(nodes) > 1 { - var nextLevel []*MerkleNode - for i := 0; i < len(nodes); i += 2 { - if i+1 == len(nodes) { - nextLevel = append(nextLevel, nodes[i]) - break - } - node := merkNode(nodes[i], nodes[i+1], nil) - nextLevel = append(nextLevel, node) - } - nodes = nextLevel - } - return nodes[0] + var nodes []MerkleNode + + for _, leaf := range leaves { + data, _ := hex.DecodeString(leaf) + var node MerkleNode = *merkNode(nil, nil, data) + nodes = append(nodes, node) + } + + for len(nodes) > 1 { + var newLevel []MerkleNode + for i := 0; i < len(nodes); i += 2 { + // handleError case where the total number of nodes is odd. + if len(nodes)%2 != 0 { + nodes = append(nodes, nodes[len(nodes)-1]) + } + node := *merkNode(&nodes[i], &nodes[i+1], nil) + newLevel = append(newLevel, node) + } + nodes = newLevel + } + return &nodes[0] } diff --git a/output.txt b/output.txt index 4b53338..9d0ab7c 100644 --- a/output.txt +++ b/output.txt @@ -1,6 +1,6 @@ -07000000000000000000000000000000000000000000000000000000000000000000000027a51c6f2c505dda24e565b0827cbcbe8f7a024551a8379896bc363ad84e091ec0d62d66ffff001ffc2d0200 -010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1b03951a0604f15ccf5609013803062b9b5a0100072f425443432f20ffffffff027891860100000000160014df4bf9f3621073202be59ae590f55f42879a21a00000000000000000266a24aa21a9ed857be26be2d48ea615af6abc625860f4a86ed2bef314e57681b02d17efd13bc10120000000000000000000000000000000000000000000000000000000000000000000000000 -35638ec7d9e798458871742209c407b7e9111c30955d71bf2f3f7c8fe81dd683 +070000000000000000000000000000000000000000000000000000000000000000000000aeb45f1bbf1b863ffac6db63cb34a24d3e20a04dcd8a07d9ba5d4c301c572fe17cd72d66ffff001f56dc0000 +010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff1b03951a0604f15ccf5609013803062b9b5a0100072f425443432f20ffffffff027891860100000000160014df4bf9f3621073202be59ae590f55f42879a21a00000000000000000266a24aa21a9ed99171a393943106a454cfae7971989270d276d042541b25f2c14ea5487d3b8b80120000000000000000000000000000000000000000000000000000000000000000000000000 +4ccf620acb55f525fec4f6cd7a6c0b5ef2de4e59aa9936e8c1b386aeb4f822f0 82f9f96db7bdbb9e70626747632e373b34eefd50d613dfea7092744169591b6e 7cb2a4f55245bae141a5d6ad51c08d7a9fdf2c2b905e4d97639ed80b82e69800 a9e537569db3c64340ed5abcdd983e9bb1b6ad6f90c93bc80d31c5cc0490bcea