-
Notifications
You must be signed in to change notification settings - Fork 337
/
span.go
45 lines (37 loc) · 1.09 KB
/
span.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package file
import (
"math"
"github.com/ethersphere/bee/pkg/swarm"
)
var Spans []int64
// nolint:gochecknoinits
func init() {
Spans = GenerateSpanSizes(9, swarm.Branches)
}
// GenerateSpanSizes generates a dictionary of maximum span lengths per level represented by one SectionSize() of data
func GenerateSpanSizes(levels, branches int) []int64 {
spans := make([]int64, levels)
branchesSixtyfour := int64(branches)
var span int64 = 1
for i := 0; i < 9; i++ {
spans[i] = span
span *= branchesSixtyfour
}
return spans
}
// Levels calculates the last level index which a particular data section count will result in.
// The returned level will be the level of the root hash.
func Levels(length int64, sectionSize, branches int) int {
s := int64(sectionSize)
b := int64(branches)
if length == 0 {
return 0
} else if length <= s*b {
return 1
}
c := (length - 1) / s
return int(math.Log(float64(c))/math.Log(float64(b)) + 1)
}