2019-11-18 16:22:08 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2020-03-31 07:05:26 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/internal"
|
2019-11-26 11:08:52 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-18 16:22:08 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// MetaHeader contains meta information of request.
|
2019-11-19 16:03:42 +00:00
|
|
|
// It provides methods to get or set meta information meta header.
|
|
|
|
// Also contains methods to reset and restore meta header.
|
2019-11-21 12:13:07 +00:00
|
|
|
// Also contains methods to get or set request protocol version
|
2019-11-18 16:22:08 +00:00
|
|
|
MetaHeader interface {
|
2019-11-19 16:03:42 +00:00
|
|
|
ResetMeta() RequestMetaHeader
|
|
|
|
RestoreMeta(RequestMetaHeader)
|
2019-11-18 16:22:08 +00:00
|
|
|
|
|
|
|
// TTLRequest to verify and update ttl requests.
|
|
|
|
GetTTL() uint32
|
|
|
|
SetTTL(uint32)
|
|
|
|
|
2019-11-26 10:57:29 +00:00
|
|
|
// EpochHeader gives possibility to get or set epoch in RPC Requests.
|
|
|
|
EpochHeader
|
2019-11-21 12:13:07 +00:00
|
|
|
|
|
|
|
// VersionHeader allows get or set version of protocol request
|
|
|
|
VersionHeader
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:57:29 +00:00
|
|
|
// EpochHeader interface gives possibility to get or set epoch in RPC Requests.
|
|
|
|
EpochHeader interface {
|
|
|
|
GetEpoch() uint64
|
|
|
|
SetEpoch(v uint64)
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:13:07 +00:00
|
|
|
// VersionHeader allows get or set version of protocol request
|
|
|
|
VersionHeader interface {
|
|
|
|
GetVersion() uint32
|
|
|
|
SetVersion(uint32)
|
2019-11-18 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 16:03:42 +00:00
|
|
|
// TTLCondition is closure, that allows to validate request with ttl.
|
2019-11-18 16:22:08 +00:00
|
|
|
TTLCondition func(ttl uint32) error
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ZeroTTL is empty ttl, should produce ErrZeroTTL.
|
|
|
|
ZeroTTL = iota
|
|
|
|
|
|
|
|
// NonForwardingTTL is a ttl that allows direct connections only.
|
|
|
|
NonForwardingTTL
|
|
|
|
|
|
|
|
// SingleForwardingTTL is a ttl that allows connections through another node.
|
|
|
|
SingleForwardingTTL
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ErrZeroTTL is raised when zero ttl is passed.
|
|
|
|
ErrZeroTTL = internal.Error("zero ttl")
|
|
|
|
|
|
|
|
// ErrIncorrectTTL is raised when NonForwardingTTL is passed and NodeRole != InnerRingNode.
|
|
|
|
ErrIncorrectTTL = internal.Error("incorrect ttl")
|
|
|
|
)
|
|
|
|
|
2020-02-14 09:14:19 +00:00
|
|
|
// 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 }
|
|
|
|
|
2019-11-21 12:20:49 +00:00
|
|
|
// SetVersion sets protocol version to RequestMetaHeader.
|
|
|
|
func (m *RequestMetaHeader) SetVersion(v uint32) { m.Version = v }
|
|
|
|
|
2019-11-19 16:03:42 +00:00
|
|
|
// SetTTL sets TTL to RequestMetaHeader.
|
2019-11-18 16:22:08 +00:00
|
|
|
func (m *RequestMetaHeader) SetTTL(v uint32) { m.TTL = v }
|
|
|
|
|
2019-11-19 16:03:42 +00:00
|
|
|
// SetEpoch sets Epoch to RequestMetaHeader.
|
2019-11-18 16:22:08 +00:00
|
|
|
func (m *RequestMetaHeader) SetEpoch(v uint64) { m.Epoch = v }
|
|
|
|
|
2019-11-19 16:03:42 +00:00
|
|
|
// ResetMeta returns current value and sets RequestMetaHeader to empty value.
|
|
|
|
func (m *RequestMetaHeader) ResetMeta() RequestMetaHeader {
|
|
|
|
cp := *m
|
|
|
|
m.Reset()
|
|
|
|
return cp
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreMeta sets current RequestMetaHeader to passed value.
|
|
|
|
func (m *RequestMetaHeader) RestoreMeta(v RequestMetaHeader) { *m = v }
|
2019-11-18 16:22:08 +00:00
|
|
|
|
|
|
|
// IRNonForwarding condition that allows NonForwardingTTL only for IR
|
|
|
|
func IRNonForwarding(role NodeRole) TTLCondition {
|
|
|
|
return func(ttl uint32) error {
|
|
|
|
if ttl == NonForwardingTTL && role != InnerRingNode {
|
|
|
|
return ErrIncorrectTTL
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessRequestTTL validates and update ttl requests.
|
|
|
|
func ProcessRequestTTL(req MetaHeader, cond ...TTLCondition) error {
|
2020-01-30 13:32:50 +00:00
|
|
|
ttl := req.GetTTL()
|
2019-11-18 16:22:08 +00:00
|
|
|
|
|
|
|
if ttl == ZeroTTL {
|
|
|
|
return status.New(codes.InvalidArgument, ErrZeroTTL.Error()).Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range cond {
|
|
|
|
if cond[i] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check specific condition:
|
|
|
|
if err := cond[i](ttl); err != nil {
|
2019-11-26 11:08:52 +00:00
|
|
|
if st, ok := status.FromError(errors.Cause(err)); ok {
|
2019-11-25 12:47:57 +00:00
|
|
|
return st.Err()
|
|
|
|
}
|
|
|
|
|
2019-11-18 16:22:08 +00:00
|
|
|
return status.New(codes.InvalidArgument, err.Error()).Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetTTL(ttl - 1)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|