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

Rename event id to event hash #1144

Merged
merged 4 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:eventID"`
krhubert marked this conversation as resolved.
Show resolved Hide resolved
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use this function is way more test ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int is fine :P

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 TestRnadom(t *testing.T) {
krhubert marked this conversation as resolved.
Show resolved Hide resolved
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't know about this quick method 👍

}

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