Add Inner Ring code

This commit is contained in:
Stanislav Bogatyrev 2020-07-24 16:54:03 +03:00
parent dadfd90dcd
commit b7b5079934
400 changed files with 11420 additions and 8690 deletions

View file

@ -0,0 +1,37 @@
package epoch
import (
"encoding/binary"
"io"
)
// Size is a size of Epoch
// in a binary form.
const Size = 8
// Marshal encodes Epoch into a
// binary form and returns the result.
//
// Result slice has Size length.
func Marshal(e Epoch) []byte {
d := make([]byte, Size)
binary.BigEndian.PutUint64(d, ToUint64(e))
return d
}
// UnmarshalBinary unmarshals Epoch from
// a binary representation.
//
// If buffer size is insufficient,
// io.ErrUnexpectedEOF is returned.
func (e *Epoch) UnmarshalBinary(data []byte) error {
if len(data) < Size {
return io.ErrUnexpectedEOF
}
*e = FromUint64(binary.BigEndian.Uint64(data))
return nil
}

View file

@ -0,0 +1,20 @@
package epoch
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEpochMarshal(t *testing.T) {
e := FromUint64(1)
e2 := new(Epoch)
require.NoError(t,
e2.UnmarshalBinary(
Marshal(e),
),
)
require.True(t, EQ(e, *e2))
}

View file

@ -0,0 +1,12 @@
package epoch
// Sum returns the result of
// summing up two Epoch.
//
// Function defines a binary
// operation of summing two Epoch.
// Try to avoid using operator
// "+" for better portability.
func Sum(a, b Epoch) Epoch {
return FromUint64(ToUint64(a) + ToUint64(b))
}

View file

@ -0,0 +1,28 @@
package epoch
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEpochMath(t *testing.T) {
items := []struct {
mathFn func(Epoch, Epoch) Epoch
a, b, c uint64
}{
{
mathFn: Sum, a: 1, b: 2, c: 3},
}
for _, item := range items {
require.Equal(t,
item.mathFn(
FromUint64(item.a),
FromUint64(item.b),
),
FromUint64(item.c),
)
}
}

View file

@ -0,0 +1,55 @@
package epoch
// EQ reports whether e and e2 are the same Epoch.
//
// Function defines the relation of equality
// between two Epoch. Try to avoid comparison through
// "==" operator for better portability.
func EQ(e1, e2 Epoch) bool {
return ToUint64(e1) == ToUint64(e2)
}
// NE reports whether e1 and e2 are the different Epoch.
//
// Method defines the relation of inequality
// between two Epoch. Try to avoid comparison through
// "!=" operator for better portability.
func NE(e1, e2 Epoch) bool {
return ToUint64(e1) != ToUint64(e2)
}
// LT reports whether e1 is less Epoch than e2.
//
// Method defines the "less than" relation
// between two Epoch. Try to avoid comparison through
// "<" operator for better portability.
func LT(e1, e2 Epoch) bool {
return ToUint64(e1) < ToUint64(e2)
}
// GT reports whether e1 is greater Epoch than e2.
//
// Method defines the "greater than" relation
// between two Epoch. Try to avoid comparison through
// ">" operator for better portability.
func GT(e1, e2 Epoch) bool {
return ToUint64(e1) > ToUint64(e2)
}
// LE reports whether e1 is less or equal Epoch than e2.
//
// Method defines the "less or equal" relation
// between two Epoch. Try to avoid comparison through
// "<=" operator for better portability.
func LE(e1, e2 Epoch) bool {
return ToUint64(e1) <= ToUint64(e2)
}
// GE reports whether e1 is greater or equal Epoch than e2.
//
// Method defines the "greater or equal" relation
// between two Epoch. Try to avoid comparison through
// ">=" operator for better portability.
func GE(e1, e2 Epoch) bool {
return ToUint64(e1) >= ToUint64(e2)
}

View file

@ -0,0 +1,40 @@
package epoch
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEpochRelations(t *testing.T) {
items := []struct {
relFn func(Epoch, Epoch) bool
base, ok, fail uint64
}{
{relFn: EQ, base: 1, ok: 1, fail: 2},
{relFn: NE, base: 1, ok: 2, fail: 1},
{relFn: LT, base: 1, ok: 2, fail: 0},
{relFn: GT, base: 1, ok: 0, fail: 2},
{relFn: LE, base: 1, ok: 1, fail: 0},
{relFn: LE, base: 1, ok: 2, fail: 0},
{relFn: GE, base: 1, ok: 0, fail: 2},
{relFn: GE, base: 1, ok: 1, fail: 2},
}
for _, item := range items {
require.True(t,
item.relFn(
FromUint64(item.base),
FromUint64(item.ok),
),
)
require.False(t,
item.relFn(
FromUint64(item.base),
FromUint64(item.fail),
),
)
}
}

View file

@ -0,0 +1,23 @@
package epoch
// Epoch represents the
// number of NeoFS epoch.
type Epoch uint64
// FromUint64 converts builtin
// uint64 value to Epoch.
//
// Try to avoid direct cast for
// better portability.
func FromUint64(e uint64) Epoch {
return Epoch(e)
}
// ToUint64 converts Epoch value
// to builtin uint64.
//
// Try to avoid direct cast for
// better portability.
func ToUint64(e Epoch) uint64 {
return uint64(e)
}