[#1] chain: Fix ID type from string to bytes

ID may be non UTF-8 string, so from developers POV
it is just byte slice.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2024-01-18 11:01:39 +03:00
parent 5f13d91c0d
commit dd0f582fc3
8 changed files with 57 additions and 60 deletions

View file

@ -10,7 +10,7 @@ import (
)
// ID is the ID of rule chain.
type ID string
type ID []byte
// MatchType is the match type for chain rules.
type MatchType uint8

View file

@ -37,7 +37,12 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(in *jl
}
switch key {
case "ID":
(out.ID).UnmarshalEasyJSON(in)
if in.IsNull() {
in.Skip()
out.ID = nil
} else {
out.ID = in.Bytes()
}
case "Rules":
if in.IsNull() {
in.Skip()
@ -54,9 +59,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(in *jl
out.Rules = (out.Rules)[:0]
}
for !in.IsDelim(']') {
var v1 Rule
easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(in, &v1)
out.Rules = append(out.Rules, v1)
var v2 Rule
easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(in, &v2)
out.Rules = append(out.Rules, v2)
in.WantComma()
}
in.Delim(']')
@ -80,7 +85,7 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(out *j
{
const prefix string = ",\"ID\":"
out.RawString(prefix[1:])
(in.ID).MarshalEasyJSON(out)
out.Base64Bytes(in.ID)
}
{
const prefix string = ",\"Rules\":"
@ -89,11 +94,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(out *j
out.RawString("null")
} else {
out.RawByte('[')
for v2, v3 := range in.Rules {
if v2 > 0 {
for v5, v6 := range in.Rules {
if v5 > 0 {
out.RawByte(',')
}
easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(out, v3)
easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(out, v6)
}
out.RawByte(']')
}
@ -172,9 +177,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(in *j
out.Condition = (out.Condition)[:0]
}
for !in.IsDelim(']') {
var v4 Condition
easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(in, &v4)
out.Condition = append(out.Condition, v4)
var v7 Condition
easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(in, &v7)
out.Condition = append(out.Condition, v7)
in.WantComma()
}
in.Delim(']')
@ -220,11 +225,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(out *
out.RawString("null")
} else {
out.RawByte('[')
for v5, v6 := range in.Condition {
if v5 > 0 {
for v8, v9 := range in.Condition {
if v8 > 0 {
out.RawByte(',')
}
easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(out, v6)
easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(out, v9)
}
out.RawByte(']')
}
@ -331,9 +336,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain3(in *j
out.Names = (out.Names)[:0]
}
for !in.IsDelim(']') {
var v7 string
v7 = string(in.String())
out.Names = append(out.Names, v7)
var v10 string
v10 = string(in.String())
out.Names = append(out.Names, v10)
in.WantComma()
}
in.Delim(']')
@ -364,11 +369,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain3(out *
out.RawString("null")
} else {
out.RawByte('[')
for v8, v9 := range in.Names {
if v8 > 0 {
for v11, v12 := range in.Names {
if v11 > 0 {
out.RawByte(',')
}
out.String(string(v9))
out.String(string(v12))
}
out.RawByte(']')
}
@ -412,9 +417,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain2(in *j
out.Names = (out.Names)[:0]
}
for !in.IsDelim(']') {
var v10 string
v10 = string(in.String())
out.Names = append(out.Names, v10)
var v13 string
v13 = string(in.String())
out.Names = append(out.Names, v13)
in.WantComma()
}
in.Delim(']')
@ -445,11 +450,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain2(out *
out.RawString("null")
} else {
out.RawByte('[')
for v11, v12 := range in.Names {
if v11 > 0 {
for v14, v15 := range in.Names {
if v14 > 0 {
out.RawByte(',')
}
out.String(string(v12))
out.String(string(v15))
}
out.RawByte(']')
}

View file

@ -19,7 +19,7 @@ var (
func (c *Chain) MarshalBinary() ([]byte, error) {
s := marshal.UInt8Size // Marshaller version
s += marshal.UInt8Size // Chain version
s += marshal.StringSize(string(c.ID))
s += marshal.SliceSize(c.ID, func(byte) int { return marshal.ByteSize })
s += marshal.SliceSize(c.Rules, ruleSize)
s += marshal.UInt8Size // MatchType
@ -34,7 +34,7 @@ func (c *Chain) MarshalBinary() ([]byte, error) {
if err != nil {
return nil, err
}
offset, err = marshal.StringMarshal(buf, offset, string(c.ID))
offset, err = marshal.SliceMarshal(buf, offset, c.ID, marshal.ByteMarshal)
if err != nil {
return nil, err
}
@ -72,11 +72,11 @@ func (c *Chain) UnmarshalBinary(data []byte) error {
return fmt.Errorf("unsupported chain version %d", chainVersion)
}
idStr, offset, err := marshal.StringUnmarshal(data, offset)
idBytes, offset, err := marshal.SliceUnmarshal(data, offset, marshal.ByteUnmarshal)
if err != nil {
return err
}
c.ID = ID(idStr)
c.ID = ID(idBytes)
c.Rules, offset, err = marshal.SliceUnmarshal(data, offset, unmarshalRule)
if err != nil {

View file

@ -143,11 +143,3 @@ func (ct *ConditionType) UnmarshalEasyJSON(l *jlexer.Lexer) {
}
*ct = ConditionType(v)
}
func (id ID) MarshalEasyJSON(w *jwriter.Writer) {
w.Base64Bytes([]byte(id))
}
func (id *ID) UnmarshalEasyJSON(l *jlexer.Lexer) {
*id = ID(l.Bytes())
}

View file

@ -38,11 +38,11 @@ func TestMatchTypeJson(t *testing.T) {
data, err := chain.MarshalJSON()
require.NoError(t, err)
if mt == MatchTypeDenyPriority {
require.Equal(t, []byte("{\"ID\":\"\",\"Rules\":null,\"MatchType\":\"DenyPriority\"}"), data)
require.Equal(t, []byte("{\"ID\":null,\"Rules\":null,\"MatchType\":\"DenyPriority\"}"), data)
} else if mt == MatchTypeFirstMatch {
require.Equal(t, []byte("{\"ID\":\"\",\"Rules\":null,\"MatchType\":\"FirstMatch\"}"), data)
require.Equal(t, []byte("{\"ID\":null,\"Rules\":null,\"MatchType\":\"FirstMatch\"}"), data)
} else {
require.Equal(t, []byte(fmt.Sprintf("{\"ID\":\"\",\"Rules\":null,\"MatchType\":\"%d\"}", mt)), data)
require.Equal(t, []byte(fmt.Sprintf("{\"ID\":null,\"Rules\":null,\"MatchType\":\"%d\"}", mt)), data)
}
var parsed Chain
@ -55,7 +55,7 @@ func TestMatchTypeJson(t *testing.T) {
func TestJsonEnums(t *testing.T) {
chain := Chain{
ID: "2cca5ae7-cee8-428d-b45f-567fb1d03f01", // will be encoded to base64
ID: []byte("2cca5ae7-cee8-428d-b45f-567fb1d03f01"), // will be encoded to base64
MatchType: MatchTypeFirstMatch,
Rules: []Rule{
{

View file

@ -1,6 +1,7 @@
package inmemory
import (
"bytes"
"fmt"
"math/rand"
"strings"
@ -14,14 +15,14 @@ import (
type targetToChain map[engine.Target][]*chain.Chain
type inmemoryLocalStorage struct {
usedChainID map[chain.ID]struct{}
usedChainID map[string]struct{}
nameToResourceChains map[chain.Name]targetToChain
guard *sync.RWMutex
}
func NewInmemoryLocalStorage() engine.LocalOverrideStorage {
return &inmemoryLocalStorage{
usedChainID: map[chain.ID]struct{}{},
usedChainID: map[string]struct{}{},
nameToResourceChains: make(map[chain.Name]targetToChain),
guard: &sync.RWMutex{},
}
@ -35,12 +36,13 @@ func (s *inmemoryLocalStorage) generateChainID(name chain.Name, target engine.Ta
sid = strings.ReplaceAll(sid, "*", "")
sid = strings.ReplaceAll(sid, "/", ":")
sid = strings.ReplaceAll(sid, "::", ":")
id = chain.ID(sid)
_, ok := s.usedChainID[id]
_, ok := s.usedChainID[sid]
if ok {
continue
}
s.usedChainID[id] = struct{}{}
s.usedChainID[sid] = struct{}{}
id = chain.ID(sid)
break
}
return id
@ -51,7 +53,7 @@ func (s *inmemoryLocalStorage) AddOverride(name chain.Name, target engine.Target
defer s.guard.Unlock()
// AddOverride assigns generated chain ID if it has not been assigned.
if c.ID == "" {
if len(c.ID) == 0 {
c.ID = s.generateChainID(name, target)
}
if s.nameToResourceChains[name] == nil {
@ -59,7 +61,7 @@ func (s *inmemoryLocalStorage) AddOverride(name chain.Name, target engine.Target
}
rc := s.nameToResourceChains[name]
for i := range rc[target] {
if rc[target][i].ID == c.ID {
if bytes.Equal(rc[target][i].ID, c.ID) {
rc[target][i] = c
return c.ID, nil
}
@ -80,7 +82,7 @@ func (s *inmemoryLocalStorage) GetOverride(name chain.Name, target engine.Target
return nil, engine.ErrResourceNotFound
}
for _, c := range chains {
if c.ID == chainID {
if bytes.Equal(c.ID, chainID) {
return c, nil
}
}
@ -99,7 +101,7 @@ func (s *inmemoryLocalStorage) RemoveOverride(name chain.Name, target engine.Tar
return engine.ErrResourceNotFound
}
for i, c := range chains {
if c.ID == chainID {
if bytes.Equal(c.ID, chainID) {
s.nameToResourceChains[name][target] = append(chains[:i], chains[i+1:]...)
return nil
}

View file

@ -14,9 +14,7 @@ const (
nonExistChainId = "ingress:LxGyWyL"
)
var (
resrc = engine.ContainerTarget(container)
)
var resrc = engine.ContainerTarget(container)
func testInmemLocalStorage() *inmemoryLocalStorage {
return NewInmemoryLocalStorage().(*inmemoryLocalStorage)
@ -210,12 +208,12 @@ func TestGenerateID(t *testing.T) {
}
func hasDuplicates(ids []chain.ID) bool {
seen := make(map[chain.ID]bool)
seen := make(map[string]bool)
for _, id := range ids {
if seen[id] {
if seen[string(id)] {
return true
}
seen[id] = true
seen[string(id)] = true
}
return false
}

View file

@ -44,7 +44,7 @@ func NewContractStorageWithSimpleActor(rpcActor actor.RPCActor, acc *wallet.Acco
}
func (s *ContractStorage) AddMorphRuleChain(name chain.Name, target engine.Target, c *chain.Chain) (txHash util.Uint256, vub uint32, err error) {
if c.ID == "" {
if len(c.ID) == 0 {
err = ErrEmptyChainID
return
}
@ -61,7 +61,7 @@ func (s *ContractStorage) AddMorphRuleChain(name chain.Name, target engine.Targe
}
func (s *ContractStorage) RemoveMorphRuleChain(name chain.Name, target engine.Target, chainID chain.ID) (txHash util.Uint256, vub uint32, err error) {
if chainID == "" {
if len(chainID) == 0 {
err = ErrEmptyChainID
return
}