-
Notifications
You must be signed in to change notification settings - Fork 38
/
flow.go
129 lines (107 loc) · 2.82 KB
/
flow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package core
import (
"context"
"math"
"math/big"
"github.com/0glabs/0g-storage-client/common"
"github.com/0glabs/0g-storage-client/common/parallel"
"github.com/0glabs/0g-storage-client/contract"
"github.com/0glabs/0g-storage-client/core/merkle"
"github.com/sirupsen/logrus"
)
type Flow struct {
data IterableData
tags []byte
logger *logrus.Logger
}
func NewFlow(data IterableData, tags []byte, opts ...common.LogOption) *Flow {
return &Flow{data: data, tags: tags, logger: common.NewLogger(opts...)}
}
func (flow *Flow) CreateSubmission() (*contract.Submission, error) {
// TODO(kevin): limit file size, e.g., 2^31
submission := contract.Submission{
Length: big.NewInt(flow.data.Size()),
Tags: flow.tags,
}
var offset int64
for _, chunks := range flow.splitNodes() {
node, err := flow.createNode(offset, chunks)
if err != nil {
return nil, err
}
submission.Nodes = append(submission.Nodes, *node)
offset += chunks * DefaultChunkSize
}
return &submission, nil
}
func NextPow2(input uint64) uint64 {
x := input
x -= 1
x |= x >> 32
x |= x >> 16
x |= x >> 8
x |= x >> 4
x |= x >> 2
x |= x >> 1
x += 1
return x
}
func ComputePaddedSize(chunks uint64) (uint64, uint64) {
chunksNextPow2 := NextPow2(chunks)
if chunksNextPow2 == chunks {
return chunksNextPow2, chunksNextPow2
}
var minChunk uint64
if chunksNextPow2 >= 16 {
minChunk = chunksNextPow2 / 16
} else {
minChunk = 1
}
paddedChunks := ((chunks-1)/minChunk + 1) * minChunk
return paddedChunks, chunksNextPow2
}
// e.g. 64, 32, 1 in chunks
func (flow *Flow) splitNodes() []int64 {
var nodes []int64
chunks := flow.data.NumChunks()
paddedChunks, chunksNextPow2 := ComputePaddedSize(chunks)
nextChunkSize := chunksNextPow2
for paddedChunks > 0 {
if paddedChunks >= nextChunkSize {
paddedChunks -= nextChunkSize
nodes = append(nodes, int64(nextChunkSize))
}
nextChunkSize /= 2
}
flow.logger.WithFields(logrus.Fields{
"chunks": chunks,
"nodeSize": nodes,
}).Debug("SplitNodes")
return nodes
}
func (flow *Flow) createNode(offset, chunks int64) (*contract.SubmissionNode, error) {
batch := chunks
if chunks > DefaultSegmentMaxChunks {
batch = DefaultSegmentMaxChunks
}
return flow.createSegmentNode(offset, DefaultChunkSize*batch, DefaultChunkSize*chunks)
}
func (flow *Flow) createSegmentNode(offset, batch, size int64) (*contract.SubmissionNode, error) {
var builder merkle.TreeBuilder
initializer := &TreeBuilderInitializer{
data: flow.data,
offset: offset,
batch: batch,
builder: &builder,
}
err := parallel.Serial(context.Background(), initializer, int((size-1)/batch+1))
if err != nil {
return nil, err
}
numChunks := size / DefaultChunkSize
height := int64(math.Log2(float64(numChunks)))
return &contract.SubmissionNode{
Root: builder.Build().Root(),
Height: big.NewInt(height),
}, nil
}