service: refactor MetaHeader interface

This commit is contained in:
Leonard Lyubich 2020-05-04 14:37:14 +03:00
parent c38a8eddc8
commit eb94cf7549
11 changed files with 131 additions and 62 deletions

View file

@ -31,7 +31,7 @@ type (
// All object operations must have TTL, Epoch, Type, Container ID and // All object operations must have TTL, Epoch, Type, Container ID and
// permission of usage previous network map. // permission of usage previous network map.
Request interface { Request interface {
service.MetaHeader service.SeizedRequestMetaContainer
CID() CID CID() CID
Type() RequestType Type() RequestType

11
service/epoch.go Normal file
View file

@ -0,0 +1,11 @@
package service
// SetEpoch is an Epoch field setter.
func (m *ResponseMetaHeader) SetEpoch(v uint64) {
m.Epoch = v
}
// SetEpoch is an Epoch field setter.
func (m *RequestMetaHeader) SetEpoch(v uint64) {
m.Epoch = v
}

21
service/epoch_test.go Normal file
View file

@ -0,0 +1,21 @@
package service
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGetSetEpoch(t *testing.T) {
v := uint64(5)
items := []EpochContainer{
new(ResponseMetaHeader),
new(RequestMetaHeader),
}
for _, item := range items {
item.SetEpoch(v)
require.Equal(t, v, item.GetEpoch())
}
}

View file

@ -1,44 +1,13 @@
package service package service
type (
// MetaHeader contains meta information of request.
// It provides methods to get or set meta information meta header.
// Also contains methods to reset and restore meta header.
// Also contains methods to get or set request protocol version
MetaHeader interface {
ResetMeta() RequestMetaHeader
RestoreMeta(RequestMetaHeader)
TTLContainer
EpochContainer
VersionContainer
RawContainer
}
)
// SetVersion sets protocol version to ResponseMetaHeader.
func (m *ResponseMetaHeader) SetVersion(v uint32) { m.Version = v }
// SetEpoch sets Epoch to ResponseMetaHeader.
func (m *ResponseMetaHeader) SetEpoch(v uint64) { m.Epoch = v }
// SetVersion sets protocol version to RequestMetaHeader.
func (m *RequestMetaHeader) SetVersion(v uint32) { m.Version = v }
// SetEpoch sets Epoch to RequestMetaHeader.
func (m *RequestMetaHeader) SetEpoch(v uint64) { m.Epoch = v }
// SetRaw is a Raw field setter.
func (m *RequestMetaHeader) SetRaw(raw bool) {
m.Raw = raw
}
// ResetMeta returns current value and sets RequestMetaHeader to empty value. // ResetMeta returns current value and sets RequestMetaHeader to empty value.
func (m *RequestMetaHeader) ResetMeta() RequestMetaHeader { func (m *RequestMetaHeader) CutMeta() RequestMetaHeader {
cp := *m cp := *m
m.Reset() m.Reset()
return cp return cp
} }
// RestoreMeta sets current RequestMetaHeader to passed value. // RestoreMeta sets current RequestMetaHeader to passed value.
func (m *RequestMetaHeader) RestoreMeta(v RequestMetaHeader) { *m = v } func (m *RequestMetaHeader) RestoreMeta(v RequestMetaHeader) {
*m = v
}

View file

@ -6,26 +6,20 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestRequestMetaHeader_SetEpoch(t *testing.T) { func TestCutRestoreMeta(t *testing.T) {
m := new(ResponseMetaHeader) items := []func() SeizedMetaHeaderContainer{
epoch := uint64(3) func() SeizedMetaHeaderContainer {
m.SetEpoch(epoch) m := new(RequestMetaHeader)
require.Equal(t, epoch, m.GetEpoch()) m.SetEpoch(1)
} return m
},
func TestRequestMetaHeader_SetVersion(t *testing.T) { }
m := new(ResponseMetaHeader)
version := uint32(3) for _, item := range items {
m.SetVersion(version) v1 := item()
require.Equal(t, version, m.GetVersion()) m1 := v1.CutMeta()
} v1.RestoreMeta(m1)
func TestRequestMetaHeader_SetRaw(t *testing.T) { require.Equal(t, item(), v1)
m := new(RequestMetaHeader) }
m.SetRaw(true)
require.True(t, m.GetRaw())
m.SetRaw(false)
require.False(t, m.GetRaw())
} }

6
service/raw.go Normal file
View file

@ -0,0 +1,6 @@
package service
// SetRaw is a Raw field setter.
func (m *RequestMetaHeader) SetRaw(raw bool) {
m.Raw = raw
}

24
service/raw_test.go Normal file
View file

@ -0,0 +1,24 @@
package service
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGetSetRaw(t *testing.T) {
items := []RawContainer{
new(RequestMetaHeader),
}
for _, item := range items {
// init with false
item.SetRaw(false)
item.SetRaw(true)
require.True(t, item.GetRaw())
item.SetRaw(false)
require.False(t, item.GetRaw())
}
}

View file

@ -51,6 +51,12 @@ type TTLContainer interface {
SetTTL(uint32) SetTTL(uint32)
} }
// SeizedMetaHeaderContainer is an interface of container of RequestMetaHeader that can be cut and restored.
type SeizedMetaHeaderContainer interface {
CutMeta() RequestMetaHeader
RestoreMeta(RequestMetaHeader)
}
// RequestMetaContainer is an interface of a fixed set of request meta value containers. // RequestMetaContainer is an interface of a fixed set of request meta value containers.
// Contains: // Contains:
// - TTL value; // - TTL value;
@ -64,6 +70,12 @@ type RequestMetaContainer interface {
RawContainer RawContainer
} }
// SeizedRequestMetaContainer is a RequestMetaContainer with seized meta.
type SeizedRequestMetaContainer interface {
RequestMetaContainer
SeizedMetaHeaderContainer
}
// VerbSource is an interface of the container of a token verb value with read access. // VerbSource is an interface of the container of a token verb value with read access.
type VerbSource interface { type VerbSource interface {
GetVerb() Token_Info_Verb GetVerb() Token_Info_Verb

View file

@ -118,8 +118,8 @@ var bytesPool = sync.Pool{New: func() interface{} {
// new signature to headers. If something went wrong, returns error. // new signature to headers. If something went wrong, returns error.
func SignRequestHeader(key *ecdsa.PrivateKey, msg VerifiableRequest) error { func SignRequestHeader(key *ecdsa.PrivateKey, msg VerifiableRequest) error {
// ignore meta header // ignore meta header
if meta, ok := msg.(MetaHeader); ok { if meta, ok := msg.(SeizedRequestMetaContainer); ok {
h := meta.ResetMeta() h := meta.CutMeta()
defer func() { defer func() {
meta.RestoreMeta(h) meta.RestoreMeta(h)
@ -157,8 +157,8 @@ func SignRequestHeader(key *ecdsa.PrivateKey, msg VerifiableRequest) error {
// If something went wrong, returns error. // If something went wrong, returns error.
func VerifyRequestHeader(msg VerifiableRequest) error { func VerifyRequestHeader(msg VerifiableRequest) error {
// ignore meta header // ignore meta header
if meta, ok := msg.(MetaHeader); ok { if meta, ok := msg.(SeizedRequestMetaContainer); ok {
h := meta.ResetMeta() h := meta.CutMeta()
defer func() { defer func() {
meta.RestoreMeta(h) meta.RestoreMeta(h)

11
service/version.go Normal file
View file

@ -0,0 +1,11 @@
package service
// SetVersion is a Version field setter.
func (m *ResponseMetaHeader) SetVersion(v uint32) {
m.Version = v
}
// SetVersion is a Version field setter.
func (m *RequestMetaHeader) SetVersion(v uint32) {
m.Version = v
}

21
service/version_test.go Normal file
View file

@ -0,0 +1,21 @@
package service
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGetSetVersion(t *testing.T) {
v := uint32(7)
items := []VersionContainer{
new(ResponseMetaHeader),
new(RequestMetaHeader),
}
for _, item := range items {
item.SetVersion(v)
require.Equal(t, v, item.GetVersion())
}
}