WIP: morph: Fix container resource name transformer #31

Closed
aarifullin wants to merge 1 commit from aarifullin/policy-engine:fix/transform_container_target into master
2 changed files with 96 additions and 1 deletions
Showing only changes of commit a7bb0e6b8b - Show all commits

View file

@ -1,6 +1,7 @@
package policy
import (
"crypto/sha256"
"errors"
"fmt"
"math/big"
@ -49,7 +50,8 @@ func transformNameIfContainer(target engine.Target) (name string) {
if target.Type == engine.Container {
// Container name can be too long and, thus, cannot be
// used as a key name for policy-contract storage.
name = base58.FastBase58Encoding([]byte(target.Name))
encoded := sha256.Sum224([]byte(target.Name))
Review

What about raw []byte slice? it's 32 bytes. Well, it is hash, so taking a prefix is almost equivalent to this Sum224.

What about raw `[]byte` slice? it's 32 bytes. Well, it _is_ hash, so taking a prefix is almost equivalent to this `Sum224`.
Review

And why 224?

And why `224`?
name = base58.FastBase58Encoding(encoded[:])
}
return
}

View file

@ -0,0 +1,93 @@
package policy
import (
"fmt"
"testing"
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
"git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
"github.com/stretchr/testify/require"
)
func TestTransformNameIfContainer(t *testing.T) {
for _, test := range []struct {
Name string
Target engine.Target
}{
{
Name: "check namespace",
Target: engine.NamespaceTarget("namespace1"),
},
{
Name: "check ResourceFormatNamespaceObjects",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceObjects,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatNamespaceContainerObjects",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainerObjects,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatNamespaceContainerObject",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainerObject,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "LxGy5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootObjects",
Target: engine.ContainerTarget(native.ResourceFormatRootObjects),
},
{
Name: "check ResourceFormatRootContainerObjects",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatRootContainerObjects,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootContainerObject",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatRootContainerObject,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatAllObjects",
Target: engine.ContainerTarget(native.ResourceFormatAllObjects),
},
{
Name: "check ResourceFormatNamespaceContainer",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainer,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatNamespaceContainers",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainers,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootContainer",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatRootContainer,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootContainers",
Target: engine.ContainerTarget(native.ResourceFormatRootContainers),
},
{
Name: "check ResourceFormatAllContainers",
Target: engine.ContainerTarget(native.ResourceFormatAllContainers),
},
} {
t.Run(test.Target.Name, func(t *testing.T) {
res := transformNameIfContainer(test.Target)
const contractStorageKeyLength = 64
const entityTypeLength = 1 // 'n', 'c' or 'i'
require.Less(t, len(res), contractStorageKeyLength-entityTypeLength)
})
}
}