-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefetch content from car file (#27)
* Prefetch content from car file * Improve checks
- Loading branch information
1 parent
b5934a7
commit 4ee5995
Showing
4 changed files
with
228 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
// CalcEpochForSlot returns the epoch for the given slot. | ||
func CalcEpochForSlot(slot uint64) uint64 { | ||
return slot / EpochLen | ||
} | ||
|
||
const EpochLen = 432000 | ||
|
||
// CalcEpochLimits returns the start and stop slots for the given epoch (inclusive). | ||
func CalcEpochLimits(epoch uint64) (uint64, uint64) { | ||
epochStart := epoch * EpochLen | ||
epochStop := epochStart + EpochLen - 1 | ||
return epochStart, epochStop | ||
} | ||
|
||
// Uint64RangesHavePartialOverlapIncludingEdges returns true if the two ranges have any overlap. | ||
func Uint64RangesHavePartialOverlapIncludingEdges(r1 [2]uint64, r2 [2]uint64) bool { | ||
if r1[0] < r2[0] { | ||
return r1[1] >= r2[0] | ||
} else { | ||
return r2[1] >= r1[0] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCalcEpochForSlot(t *testing.T) { | ||
require.Equal(t, uint64(0), CalcEpochForSlot(0)) | ||
require.Equal(t, uint64(0), CalcEpochForSlot(1)) | ||
require.Equal(t, uint64(0), CalcEpochForSlot(431999)) | ||
require.Equal(t, uint64(1), CalcEpochForSlot(432000)) | ||
require.Equal(t, uint64(1), CalcEpochForSlot(863999)) | ||
require.Equal(t, uint64(2), CalcEpochForSlot(864000)) | ||
require.Equal(t, uint64(477), CalcEpochForSlot(206459118)) | ||
} | ||
|
||
func TestCalcEpochLimits(t *testing.T) { | ||
{ | ||
epochStart, epochStop := CalcEpochLimits(0) | ||
require.Equal(t, uint64(0), epochStart) | ||
require.Equal(t, uint64(431_999), epochStop) | ||
} | ||
{ | ||
epochStart, epochStop := CalcEpochLimits(1) | ||
require.Equal(t, uint64(432_000), epochStart) | ||
require.Equal(t, uint64(863_999), epochStop) | ||
} | ||
{ | ||
epochStart, epochStop := CalcEpochLimits(333) | ||
require.Equal(t, uint64(143_856_000), epochStart) | ||
require.Equal(t, uint64(144_287_999), epochStop) | ||
} | ||
{ | ||
epochStart, epochStop := CalcEpochLimits(447) | ||
require.Equal(t, uint64(193_104_000), epochStart) | ||
require.Equal(t, uint64(193_535_999), epochStop) | ||
} | ||
} | ||
|
||
func TestUint64RangesHavePartialOverlapIncludingEdges(t *testing.T) { | ||
{ | ||
r1 := [2]uint64{0, 10} | ||
r2 := [2]uint64{5, 15} | ||
require.True(t, Uint64RangesHavePartialOverlapIncludingEdges(r1, r2)) | ||
} | ||
{ | ||
r1 := [2]uint64{0, 10} | ||
r2 := [2]uint64{10, 15} | ||
require.True(t, Uint64RangesHavePartialOverlapIncludingEdges(r1, r2)) | ||
} | ||
{ | ||
r1 := [2]uint64{0, 10} | ||
r2 := [2]uint64{11, 15} | ||
require.False(t, Uint64RangesHavePartialOverlapIncludingEdges(r1, r2)) | ||
} | ||
{ | ||
r1 := [2]uint64{0, 10} | ||
r2 := [2]uint64{0, 10} | ||
require.True(t, Uint64RangesHavePartialOverlapIncludingEdges(r1, r2)) | ||
} | ||
{ | ||
r1 := [2]uint64{10, 20} | ||
r2 := [2]uint64{0, 10} | ||
require.True(t, Uint64RangesHavePartialOverlapIncludingEdges(r1, r2)) | ||
} | ||
} |