@@ -13,23 +13,17 @@ import (
13
13
"github.com/tendermint/tendermint/pkg/wrapper"
14
14
)
15
15
16
- // BatchSize defines an amount of IPLD Nodes to be buffered and written at once.
17
- // This configuration is very database backend specific and the current default(128) is not optimized for the version of
18
- // Badger we use. We set it to one to avoid test flakiness, as some tests may read for data that was not written yet.
19
- // TODO(@Wondertan): Find out the perfect value for Badger(e.g. ask PL folks) or migrate to go-car IPLD
20
- // storage(preferred).
21
- const BatchSize = 1
22
-
23
16
// PutData posts erasured block data to IPFS using the provided ipld.NodeAdder.
24
17
func PutData (ctx context.Context , shares [][]byte , adder ipld.NodeAdder ) (* rsmt2d.ExtendedDataSquare , error ) {
25
18
if len (shares ) == 0 {
26
19
return nil , fmt .Errorf ("empty data" ) // empty block is not an empty Data
27
20
}
28
- // create nmt adder wrapping batch adder
29
- batchAdder := NewNmtNodeAdder (ctx , ipld .NewBatch (ctx , adder , ipld .MaxSizeBatchOption (BatchSize )))
21
+ squareSize := int (math .Sqrt (float64 (len (shares ))))
22
+ // create nmt adder wrapping batch adder with calculated size
23
+ bs := batchSize (squareSize * 2 )
24
+ batchAdder := NewNmtNodeAdder (ctx , ipld .NewBatch (ctx , adder , ipld .MaxSizeBatchOption (bs )))
30
25
// create the nmt wrapper to generate row and col commitments
31
- squareSize := uint64 (math .Sqrt (float64 (len (shares ))))
32
- tree := wrapper .NewErasuredNamespacedMerkleTree (squareSize , nmt .NodeVisitor (batchAdder .Visit ))
26
+ tree := wrapper .NewErasuredNamespacedMerkleTree (uint64 (squareSize ), nmt .NodeVisitor (batchAdder .Visit ))
33
27
// recompute the eds
34
28
eds , err := rsmt2d .ComputeExtendedDataSquare (shares , rsmt2d .NewRSGF8Codec (), tree .Constructor )
35
29
if err != nil {
@@ -55,3 +49,20 @@ func ExtractODSShares(eds *rsmt2d.ExtendedDataSquare) [][]byte {
55
49
}
56
50
return origShares
57
51
}
52
+
53
+ // batchSize calculates the amount of nodes that are generated from block of 'squareSizes'
54
+ // to be batched in one write.
55
+ func batchSize (squareSize int ) int {
56
+ // (squareSize*2-1) - amount of nodes in a generated binary tree
57
+ // squareSize*2 - the total number of trees, both for rows and cols
58
+ // (squareSize*squareSize) - all the shares
59
+ //
60
+ // Note that while our IPLD tree looks like this:
61
+ // ---X
62
+ // -X---X
63
+ // X-X-X-X
64
+ // X-X-X-X
65
+ // here we count leaves only once: the CIDs are the same for columns and rows
66
+ // and for the last two layers as well:
67
+ return (squareSize * 2 - 1 )* squareSize * 2 - (squareSize * squareSize )
68
+ }
0 commit comments