Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EnvHash to instance and calculate the hash based on it #1452

Merged
merged 9 commits into from
Oct 31, 2019
2 changes: 1 addition & 1 deletion cosmos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Client) BuildAndBroadcastMsg(msg sdktypes.Msg, accName, accPassword str
return nil, errors.New("result data is not the right type")
}
if data.TxResult.Result.IsErr() {
return nil, errors.New("an error occurred in transaction")
return nil, fmt.Errorf("an error occurred in transaction: %s", data.TxResult.Result.Log)
}
return &data.TxResult.Result, nil
case <-ctx.Done():
Expand Down
6 changes: 5 additions & 1 deletion e2e/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func testInstance(t *testing.T) {
acknowledgement.WaitForStreamToBeReady(stream)

ctx := metadata.NewOutgoingContext(context.Background(), passmd)
resp, err := client.InstanceClient.Create(ctx, &pb.CreateInstanceRequest{ServiceHash: testServiceHash})
resp, err := client.InstanceClient.Create(ctx, &pb.CreateInstanceRequest{
ServiceHash: testServiceHash,
Env: []string{"BAR=3", "REQUIRED=4"},
})
require.NoError(t, err)
testInstanceHash = resp.Hash

Expand All @@ -38,6 +41,7 @@ func testInstance(t *testing.T) {
require.NoError(t, err)
require.Equal(t, testInstanceHash, resp.Hash)
require.Equal(t, testServiceHash, resp.ServiceHash)
require.Equal(t, hash.Dump([]string{"BAR=2", "FOO=1", "REQUIRED", "BAR=3", "REQUIRED=4"}), resp.EnvHash)
krhubert marked this conversation as resolved.
Show resolved Hide resolved
krhubert marked this conversation as resolved.
Show resolved Hide resolved
})

t.Run("list", func(t *testing.T) {
Expand Down
10 changes: 8 additions & 2 deletions e2e/testdata/test-service.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"sid": "test-service",
"name": "test-service",
"configuration": {},
"configuration": {
"env": [
"FOO=1",
"BAR=2",
"REQUIRED"
]
},
"dependencies": [],
"tasks": [
{
Expand Down Expand Up @@ -85,5 +91,5 @@
]
}
],
"source": "QmNhfbBnYYTYJVFeBBPMgBtCMGKe2xXjiSdAFgDQQ3JaMz"
"source": "QmPG1Ze96pH1EgVMWsGKM33jXoG63rigMncSEqZXP7oncq"
}
7 changes: 7 additions & 0 deletions e2e/testdata/test-service/mesg.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
name: test-service
sid: test-service

configuration:
env:
- FOO=1
- BAR=2
- REQUIRED

tasks:
ping:
inputs:
Expand Down
17 changes: 10 additions & 7 deletions instance/instance.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions protobuf/types/instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ message Instance {
];

bytes serviceHash = 2 [
(gogoproto.moretags) = 'hash:"name:2"',
(gogoproto.customtype) = "github.com/mesg-foundation/engine/hash.Hash",
(gogoproto.nullable) = false
];

bytes envHash = 3 [
(gogoproto.moretags) = 'hash:"name:3"',
(gogoproto.customtype) = "github.com/mesg-foundation/engine/hash.Hash",
(gogoproto.nullable) = false
];
Expand Down
22 changes: 10 additions & 12 deletions sdk/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,30 @@ func (i *Instance) Create(serviceHash hash.Hash, env []string) (*instance.Instan
if err != nil {
return nil, err
}

// calculate the final env vars by overwriting user defined one's with defaults.
instanceEnv := xos.EnvMergeMaps(xos.EnvSliceToMap(srv.Configuration.Env), xos.EnvSliceToMap(env))
instanceEnv := xos.EnvMergeSlices(srv.Configuration.Env, env)
krhubert marked this conversation as resolved.
Show resolved Hide resolved
krhubert marked this conversation as resolved.
Show resolved Hide resolved

// calculate instance's hash.
h := hash.New()
h.Write(srv.Hash)
h.Write([]byte(xos.EnvMapToString(instanceEnv)))
instanceHash := h.Sum(nil)
inst := &instance.Instance{
ServiceHash: srv.Hash,
EnvHash: hash.Dump(instanceEnv),
krhubert marked this conversation as resolved.
Show resolved Hide resolved
}
inst.Hash = hash.Dump(inst)

// check if instance already exists
if exist, err := i.instanceDB.Exist(instanceHash); err != nil {
if exist, err := i.instanceDB.Exist(inst.Hash); err != nil {
return nil, err
} else if exist {
return nil, &AlreadyExistsError{Hash: instanceHash}
return nil, &AlreadyExistsError{Hash: inst.Hash}
}

// save & start instance.
inst := &instance.Instance{
Hash: instanceHash,
ServiceHash: srv.Hash,
}
if err := i.instanceDB.Save(inst); err != nil {
return nil, err
}

_, err = i.start(inst, imageHash, xos.EnvMapToSlice(instanceEnv))
_, err = i.start(inst, imageHash, instanceEnv)
return inst, err
}

Expand Down
3 changes: 3 additions & 0 deletions x/xos/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func EnvMergeMaps(values ...map[string]string) map[string]string {
func EnvMergeSlices(values ...[]string) []string {
env := make([]string, 0)
for _, v := range values {
// Make sure envs are sorted to give repeatable output
// It is important for hash instance calculation
sort.Stable(sort.StringSlice(v))
env = append(env, v...)
}
return env
Expand Down
2 changes: 1 addition & 1 deletion x/xos/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestEnvMergeMaps(t *testing.T) {
func TestEnvMergeSlices(t *testing.T) {
values := [][]string{
{"a=1", "b=2"},
{"a=2", "c=3"},
{"c=3", "a=2"},
}
env := EnvMergeSlices(values...)
for i, v := range []string{"a=1", "b=2", "a=2", "c=3"} {
Expand Down
6 changes: 5 additions & 1 deletion x/xvalidator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func IsPortMapping(fl validator.FieldLevel) bool {
}

// IsEnv validates if given field is valid env variable declaration.
// The valid formats are:
// - ENV
// - ENV=
// - ENV=value
func IsEnv(fl validator.FieldLevel) bool {
e := strings.Split(fl.Field().String(), envSeparator)
return len(e) == 2 && envNameRegexp.MatchString(e[0])
return (len(e) == 1 || len(e) == 2) && envNameRegexp.MatchString(e[0])
}