Skip to content

Commit

Permalink
Merge branch 'dev' into fix/param-in-create-api
Browse files Browse the repository at this point in the history
  • Loading branch information
antho1404 authored Jul 1, 2019
2 parents 967265f + 66021ed commit 6943485
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 45 deletions.
6 changes: 3 additions & 3 deletions execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s Status) String() (r string) {
type Execution struct {
Hash hash.Hash `hash:"-"`
ParentHash hash.Hash `hash:"name:parentHash"`
EventID string `hash:"name:eventID"`
EventHash hash.Hash `hash:"name:eventHash"`
Status Status `hash:"-"`
InstanceHash hash.Hash `hash:"name:instanceHash"`
TaskKey string `hash:"name:taskKey"`
Expand All @@ -47,9 +47,9 @@ type Execution struct {
}

// New returns a new execution. It returns an error if inputs are invalid.
func New(instanceHash, parentHash hash.Hash, eventID, taskKey string, inputs map[string]interface{}, tags []string) *Execution {
func New(instanceHash, parentHash, eventHash hash.Hash, taskKey string, inputs map[string]interface{}, tags []string) *Execution {
exec := &Execution{
EventID: eventID,
EventHash: eventHash,
InstanceHash: instanceHash,
ParentHash: parentHash,
Inputs: inputs,
Expand Down
14 changes: 7 additions & 7 deletions execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ import (
func TestNewFromService(t *testing.T) {
var (
parentHash = hash.Int(2)
eventHash = hash.Int(3)
hash = hash.Int(1)
eventID = "1"
taskKey = "key"
tags = []string{"tag"}
)

execution := New(hash, parentHash, eventID, taskKey, nil, tags)
execution := New(hash, parentHash, eventHash, taskKey, nil, tags)
require.NotNil(t, execution)
require.Equal(t, hash, execution.InstanceHash)
require.Equal(t, parentHash, execution.ParentHash)
require.Equal(t, eventID, execution.EventID)
require.Equal(t, eventHash, execution.EventHash)
require.Equal(t, taskKey, execution.TaskKey)
require.Equal(t, map[string]interface{}(nil), execution.Inputs)
require.Equal(t, tags, execution.Tags)
require.Equal(t, Created, execution.Status)
}

func TestExecute(t *testing.T) {
e := New(nil, nil, "", "", nil, nil)
e := New(nil, nil, nil, "", nil, nil)
require.NoError(t, e.Execute())
require.Equal(t, InProgress, e.Status)
require.Error(t, e.Execute())
}

func TestComplete(t *testing.T) {
output := map[string]interface{}{"foo": "bar"}
e := New(nil, nil, "", "", nil, nil)
e := New(nil, nil, nil, "", nil, nil)

e.Execute()
require.NoError(t, e.Complete(output))
Expand All @@ -49,7 +49,7 @@ func TestComplete(t *testing.T) {

func TestFailed(t *testing.T) {
err := errors.New("test")
e := New(nil, nil, "", "", nil, nil)
e := New(nil, nil, nil, "", nil, nil)
e.Execute()
require.NoError(t, e.Failed(err))
require.Equal(t, Failed, e.Status)
Expand All @@ -67,7 +67,7 @@ func TestStatus(t *testing.T) {
func TestExecutionHash(t *testing.T) {
ids := make(map[string]bool)

f := func(instanceHash, parentHash []byte, eventID, taskKey, input string, tags []string) bool {
f := func(instanceHash, parentHash, eventID []byte, taskKey, input string, tags []string) bool {
e := New(instanceHash, parentHash, eventID, taskKey, map[string]interface{}{"input": input}, tags)
if ids[string(e.Hash)] {
return false
Expand Down
16 changes: 16 additions & 0 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hash

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -58,6 +59,21 @@ func Int(h int) Hash {
return hash
}

// Random returns a new random hash.
func Random() (Hash, error) {
hash := make(Hash, size)
n, err := rand.Reader.Read(hash)
if err != nil {
return nil, fmt.Errorf("hash generate random error: %s", err)
}

if n != size {
return nil, fmt.Errorf("hash generate random error: invalid hash length")
}

return hash, nil
}

// Decode decodes the base58 encoded hash. It returns error
// when a hash isn't base58,encoded or hash length is invalid.
func Decode(h string) (Hash, error) {
Expand Down
23 changes: 23 additions & 0 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package hash

import (
"testing"
"testing/quick"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDigest(t *testing.T) {
Expand All @@ -25,6 +27,27 @@ func TestInt(t *testing.T) {
assert.Len(t, Int(1), size)
}

func TestRandom(t *testing.T) {
hash, _ := Random()
assert.Len(t, hash, size)

seen := make(map[string]bool)

f := func() bool {
hash, err := Random()
if err != nil {
return false
}

if seen[hash.String()] {
return false
}
seen[hash.String()] = true
return true
}
require.NoError(t, quick.Check(f, nil))
}

func TestDecode(t *testing.T) {
hash, err := Decode("4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM")
assert.NoError(t, err)
Expand Down
48 changes: 24 additions & 24 deletions protobuf/types/execution.pb.go

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

4 changes: 2 additions & 2 deletions protobuf/types/execution.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ message Execution {
// parentHash is the unique hash of parent execution. if execution is triggered by another one, dependency execution considered as the parent.
string parentHash = 2;

// eventID is unique event id.
string eventID = 3;
// eventHash is unique event hash.
string eventHash = 3;

// Status is the current status of execution.
Status status = 4;
Expand Down
9 changes: 6 additions & 3 deletions sdk/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/mesg-foundation/core/hash"
instancesdk "github.com/mesg-foundation/core/sdk/instance"
servicesdk "github.com/mesg-foundation/core/sdk/service"
uuid "github.com/satori/go.uuid"
)

const (
Expand Down Expand Up @@ -145,8 +144,12 @@ func (e *Execution) Execute(instanceHash hash.Hash, taskKey string, inputData ma
}

// execute the task.
eventID := uuid.NewV4().String()
exec := execution.New(instance.Hash, nil, eventID, taskKey, inputData, tags)
eventHash, err := hash.Random()
if err != nil {
return nil, err
}

exec := execution.New(instance.Hash, nil, eventHash, taskKey, inputData, tags)
if err := exec.Execute(); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var testInstance = &instance.Instance{
func TestGet(t *testing.T) {
sdk, at := newTesting(t)
defer at.close()
exec := execution.New(nil, nil, "", "", nil, nil)
exec := execution.New(nil, nil, nil, "", nil, nil)
require.NoError(t, sdk.execDB.Save(exec))
got, err := sdk.Get(exec.Hash)
require.NoError(t, err)
Expand All @@ -92,7 +92,7 @@ func TestGetStream(t *testing.T) {
sdk, at := newTesting(t)
defer at.close()

exec := execution.New(nil, nil, "", "", nil, nil)
exec := execution.New(nil, nil, nil, "", nil, nil)
exec.Status = execution.InProgress

require.NoError(t, sdk.execDB.Save(exec))
Expand Down
2 changes: 1 addition & 1 deletion server/grpc/api/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func toProtoExecution(exec *execution.Execution) (*types.Execution, error) {
return &types.Execution{
Hash: exec.Hash.String(),
ParentHash: exec.ParentHash.String(),
EventID: exec.EventID,
EventHash: exec.EventHash.String(),
Status: types.Status(exec.Status),
InstanceHash: exec.InstanceHash.String(),
TaskKey: exec.TaskKey,
Expand Down
5 changes: 2 additions & 3 deletions server/grpc/api/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/mesg-foundation/core/execution"
"github.com/mesg-foundation/core/protobuf/api"
"github.com/mesg-foundation/core/sdk"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,7 +20,7 @@ func TestGet(t *testing.T) {
defer db.Close()
defer os.RemoveAll(execdbname)

exec := execution.New(nil, nil, uuid.NewV4().String(), "", nil, nil)
exec := execution.New(nil, nil, nil, "", nil, nil)
require.NoError(t, db.Save(exec))

want, err := toProtoExecution(exec)
Expand All @@ -41,7 +40,7 @@ func TestUpdate(t *testing.T) {
defer db.Close()
defer os.RemoveAll(execdbname)

exec := execution.New(nil, nil, uuid.NewV4().String(), "", nil, nil)
exec := execution.New(nil, nil, nil, "", nil, nil)
require.NoError(t, db.Save(exec))

sdk := sdk.New(nil, nil, nil, db)
Expand Down

0 comments on commit 6943485

Please sign in to comment.