@@ -18,6 +18,7 @@ package backend
1818
1919import (
2020 "errors"
21+ "fmt"
2122 "sort"
2223 "time"
2324
@@ -61,7 +62,51 @@ func (fs *ChainDB) Metrics() time.Duration {
6162 return fs .treeUpdates
6263}
6364
64- // Make sure the block group is increasing by number
65+ // handleNormalAdd handles the standard adding of a new leaf to the Merkle tree.
66+ func (fs * ChainDB ) handleNormalAdd (leaf * merkletree.BlockContent , number uint64 ) error {
67+ // Add the new node to the tree.
68+ if err := fs .tree .AddNode (leaf ); err != nil {
69+ return fmt .Errorf ("failed to add node to Merkle tree: %w" , err )
70+ }
71+
72+ // Update the checkpoint if the current block number is higher.
73+ if number > fs .checkPoint .Load () {
74+ fs .checkPoint .Store (number )
75+ }
76+
77+ return nil
78+ }
79+
80+ // handleMessing handles the special "messing" logic by rebuilding the tree from a sorted leaf list.
81+ func (fs * ChainDB ) handleMessing (leaf * merkletree.BlockContent , number uint64 , dup bool ) error {
82+ log .Debug ("Messing mode activated, preparing to rebuild tree" , "number" , number )
83+
84+ // Add the leaf if it's not a duplicate. This logic is moved here.
85+ if ! dup {
86+ fs .leaves = append (fs .leaves , leaf )
87+ }
88+
89+ // Sort the leaves by block number.
90+ sort .Slice (fs .leaves , func (i , j int ) bool {
91+ return fs .leaves [i ].(* merkletree.BlockContent ).N () < fs .leaves [j ].(* merkletree.BlockContent ).N ()
92+ })
93+
94+ // Find the insertion point for the current block number.
95+ i := sort .Search (len (fs .leaves ), func (i int ) bool { return fs .leaves [i ].(* merkletree.BlockContent ).N () >= number })
96+ if i >= len (fs .leaves ) {
97+ i = len (fs .leaves )
98+ }
99+
100+ log .Warn ("Messing solved, rebuilding tree" , "number" , number , "leaves_count" , len (fs .leaves ), "rebuild_until_index" , i )
101+
102+ // Rebuild the tree with the sorted and filtered leaves.
103+ if err := fs .tree .RebuildTreeWith (fs .leaves [0 :i ]); err != nil {
104+ return fmt .Errorf ("failed to rebuild Merkle tree: %w" , err )
105+ }
106+
107+ return nil
108+ }
109+
65110func (fs * ChainDB ) addLeaf (block * types.Block , mes bool , dup bool ) error {
66111 if fs .tree == nil {
67112 return errors .New ("mkt is nil" )
@@ -70,51 +115,40 @@ func (fs *ChainDB) addLeaf(block *types.Block, mes bool, dup bool) error {
70115 number := block .Number
71116 leaf := merkletree .NewContent (block .Hash .String (), number )
72117
73- l , e := fs .tree .VerifyContent (leaf )
74- if ! l {
75- if ! dup {
76- fs .leaves = append (fs .leaves , leaf )
77- }
78- } else {
79- log .Debug ("Node is already in the tree" , "num" , number , "len" , len (fs .blocks ), "leaf" , len (fs .leaves ), "ckp" , fs .checkPoint .Load (), "mes" , mes , "dup" , dup , "err" , e )
118+ // Verify if the content already exists in the tree.
119+ inTree , verifyErr := fs .tree .VerifyContent (leaf )
120+ if inTree {
121+ log .Debug ("Node is already in the tree" , "num" , number , "mes" , mes , "dup" , dup , "err" , verifyErr )
80122 if ! mes {
123+ // If not in messing mode, we can simply return if the node is a duplicate.
81124 return nil
82125 }
126+ } else {
127+ // If the node is not in the tree, we need to add it to the leaves list for the `mes` logic.
128+ // The `dup` parameter seems to control this, but the logic is a bit confusing.
129+ // Assuming `!dup` means it's a new leaf that needs to be appended.
130+ if ! dup {
131+ fs .leaves = append (fs .leaves , leaf )
132+ }
83133 }
84134
135+ // Choose the appropriate handler based on the `mes` flag.
136+ var err error
85137 if mes {
86- log .Debug ("Messing" , "num" , number , "len" , len (fs .blocks ), "leaf" , len (fs .leaves ), "ckp" , fs .checkPoint .Load (), "mes" , mes , "dup" , dup )
87- sort .Slice (fs .leaves , func (i , j int ) bool {
88- return fs .leaves [i ].(* merkletree.BlockContent ).N () < fs .leaves [j ].(* merkletree.BlockContent ).N ()
89- })
90-
91- i := sort .Search (len (fs .leaves ), func (i int ) bool { return fs .leaves [i ].(* merkletree.BlockContent ).N () > number })
92-
93- if i > len (fs .leaves ) {
94- i = len (fs .leaves )
95- }
96-
97- log .Warn ("Messing solved" , "num" , number , "len" , len (fs .blocks ), "leaf" , len (fs .leaves ), "ckp" , fs .checkPoint .Load (), "mes" , mes , "dup" , dup , "i" , i )
98-
99- if err := fs .tree .RebuildTreeWith (fs .leaves [0 :i ]); err != nil {
100- return err
101- }
102-
138+ err = fs .handleMessing (leaf , number , dup )
103139 } else {
104- if err := fs .tree .AddNode (leaf ); err != nil {
105- return err
106- }
107-
108- // TODO
140+ err = fs .handleNormalAdd (leaf , number )
141+ }
109142
110- if number > fs .checkPoint .Load () {
111- fs .checkPoint .Store (number )
112- }
143+ if err != nil {
144+ return err
113145 }
114146
147+ // Write the root after all operations.
115148 if err := fs .writeRoot (number , fs .tree .MerkleRoot ()); err != nil {
116- return err
149+ return fmt . Errorf ( "failed to write root: %w" , err )
117150 }
151+
118152 return nil
119153}
120154
0 commit comments