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

Change proto hash type from string to bytes #1270

Merged
merged 7 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 2 additions & 6 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ func deployCoreServices(cfg *config.Config, sdk *enginesdk.SDK) error {
return err
}
}
logrus.WithField("module", "main").Infof("Service %q deployed with hash %q", srv.Sid, srv.Hash)
hsh, err := hash.Decode(srv.Hash)
if err != nil {
return err
}
instance, err := sdk.Instance.Create(hsh, xos.EnvMapToSlice(serviceConfig.Env))
logrus.WithField("module", "main").Infof("Service %q deployed with hash %q", srv.Sid, srv.Hash.String())
instance, err := sdk.Instance.Create(srv.Hash, xos.EnvMapToSlice(serviceConfig.Env))
if err != nil {
existsError, ok := err.(*instancesdk.AlreadyExistsError)
if ok {
Expand Down
11 changes: 2 additions & 9 deletions database/service_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,14 @@ func (d *ServiceDB) Get(hash hash.Hash) (*service.Service, error) {
// Save stores service in database.
// If there is an another service that uses the same sid, it'll be deleted.
func (d *ServiceDB) Save(s *service.Service) error {
if s.Hash == "" {
return errCannotSaveWithoutHash
}
h, err := hash.Decode(s.Hash)
if err != nil {
return err
}
if h.IsZero() {
if len(s.Hash) == 0 {
return errCannotSaveWithoutHash
}
b, err := d.marshal(s)
if err != nil {
return err
}
return d.s.Put(h, b)
return d.s.Put(s.Hash, b)
}

// Close closes database.
Expand Down
14 changes: 7 additions & 7 deletions database/service_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestServiceDBSave(t *testing.T) {
db, closer := openServiceDB(t)
defer closer()

s1 := &service.Service{Hash: hash.Int(1).String()}
s1 := &service.Service{Hash: hash.Int(1)}
require.NoError(t, db.Save(s1))

// save same service. should replace
Expand All @@ -41,7 +41,7 @@ func TestServiceDBSave(t *testing.T) {
require.Len(t, ss, 1)

// different hash, different sid. should not replace anything.
s3 := &service.Service{Hash: hash.Int(2).String()}
s3 := &service.Service{Hash: hash.Int(2)}
require.NoError(t, db.Save(s3))
ss, _ = db.All()
require.Len(t, ss, 2)
Expand All @@ -56,7 +56,7 @@ func TestServiceDBGet(t *testing.T) {

hs1 := hash.Int(1)

want := &service.Service{Hash: hs1.String()}
want := &service.Service{Hash: hs1}
require.NoError(t, db.Save(want))
defer db.Delete(hs1)

Expand All @@ -77,7 +77,7 @@ func TestServiceDBDelete(t *testing.T) {
hs1 := hash.Int(1)

// hash.
s := &service.Service{Hash: hs1.String()}
s := &service.Service{Hash: hs1}
require.NoError(t, db.Save(s))
require.NoError(t, db.Delete(hs1))
_, err := db.Get(hs1)
Expand All @@ -92,7 +92,7 @@ func TestServiceDBDeleteConcurrency(t *testing.T) {
defer closer()

hs1 := hash.Int(1)
s := &service.Service{Hash: hs1.String()}
s := &service.Service{Hash: hs1}
db.Save(s)

var wg sync.WaitGroup
Expand Down Expand Up @@ -126,8 +126,8 @@ func TestServiceDBAll(t *testing.T) {
hs1 := hash.Int(1)
hs2 := hash.Int(2)

s1 := &service.Service{Hash: hs1.String()}
s2 := &service.Service{Hash: hs2.String()}
s1 := &service.Service{Hash: hs1}
s2 := &service.Service{Hash: hs2}

require.NoError(t, db.Save(s1))
require.NoError(t, db.Save(s2))
Expand Down
33 changes: 17 additions & 16 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var DefaultHash = sha256.New

// size is a default size for hashing algorithm.
var size = DefaultHash().Size()
var base58size = len(Dump(0).String())

// Digest represents the partial evaluation of a checksum.
type Digest struct {
Expand All @@ -36,7 +35,7 @@ func (d *Digest) WriteObject(v interface{}) (int, error) {
return d.Write(structhash.Dump(v, 0))
}

// A Hash is a type for representing hash with base58 encode and decode functions.
// A Hash is a type for representing common hash.
type Hash []byte

// New returns new hash from a given integer.
Expand Down Expand Up @@ -94,7 +93,7 @@ func (h Hash) IsZero() bool {
return len(h) == 0
}

// String returns the base58 hash representation.
// String returns the hash hex representation.
func (h Hash) String() string {
return base58.Encode(h)
}
Expand All @@ -104,18 +103,19 @@ func (h Hash) Equal(h1 Hash) bool {
return bytes.Equal(h, h1)
}

// Marshal marshals hash into slice of base58 bytes. It's used by protobuf.
// Marshal marshals hash into slice of bytes. It's used by protobuf.
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
func (h Hash) Marshal() ([]byte, error) {
return []byte(base58.Encode(h)), nil
return h, nil
}

// Unmarshal unmarshals slice of base58 bytes into hash. It's used by protobuf.
// MarshalTo marshals hash into slice of bytes. It's used by protobuf.
func (h Hash) MarshalTo(data []byte) (int, error) {
return copy(data, h), nil
}

// Unmarshal unmarshals slice of bytes into hash. It's used by protobuf.
func (h *Hash) Unmarshal(data []byte) error {
h1, err := Decode(string(data))
if err != nil {
return err
}
*h = h1
*h = data
return nil
}

Expand All @@ -124,15 +124,15 @@ func (h Hash) Size() int {
if len(h) == 0 {
return 0
}
return base58size
return size
}

// MarshalJSON mashals hash into base58 encoded json string.
// MarshalJSON mashals hash into encoded json string.
func (h Hash) MarshalJSON() ([]byte, error) {
return json.Marshal(base58.Encode(h))
}

// UnmarshalJSON unmashals base58 encoded json string into hash.
// UnmarshalJSON unmashals hex encoded json string into hash.
func (h *Hash) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
Expand All @@ -143,10 +143,11 @@ func (h *Hash) UnmarshalJSON(data []byte) error {
return nil
}

h1, err := Decode(str)
h1, err := base58.Decode(str)
if err != nil {
return err
}
*h = h1

*h = Hash(h1)
return nil
}
36 changes: 10 additions & 26 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hash

import (
"encoding/json"
"testing"
"testing/quick"

Expand All @@ -9,10 +10,8 @@ import (
)

var (
zero = Int(0)
one = Int(1)
oneStr = "4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM"
oneJSONStr = "\"" + oneStr + "\""
zero = Int(0)
one = Int(1)
)

func TestDigest(t *testing.T) {
Expand All @@ -21,9 +20,6 @@ func TestDigest(t *testing.T) {

hash := d.Sum(nil)
assert.Equal(t, hash.String(), "8RBsoeyoRwajj86MZfZE6gMDJQVYGYcdSfx1zxqxNHbr")

_, err := Decode(hash.String())
assert.NoError(t, err)
}

func TestDump(t *testing.T) {
Expand Down Expand Up @@ -56,7 +52,7 @@ func TestRandom(t *testing.T) {
}

func TestDecode(t *testing.T) {
hash, err := Decode(oneStr)
hash, err := Decode("4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM")
assert.NoError(t, err)
assert.Equal(t, hash, one)

Expand All @@ -72,39 +68,27 @@ func TestIsZero(t *testing.T) {
}

func TestString(t *testing.T) {
assert.Equal(t, one.String(), oneStr)
assert.Equal(t, one.String(), "4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM")
}

func TestEqual(t *testing.T) {
assert.True(t, zero.Equal(zero))
assert.False(t, zero.Equal(one))
}

func TestMarshal(t *testing.T) {
b, err := one.Marshal()
assert.NoError(t, err)
assert.Equal(t, oneStr, string(b))
}

func TestUnmarshal(t *testing.T) {
var h Hash
assert.NoError(t, h.Unmarshal([]byte(oneStr)))
assert.Equal(t, one, h)
}

func TestSize(t *testing.T) {
assert.Equal(t, 0, Hash(nil).Size())
assert.Equal(t, base58size, zero.Size())
assert.Equal(t, size, zero.Size())
}

func TestMarshalJSON(t *testing.T) {
b, err := one.MarshalJSON()
b, err := json.Marshal(Int(1))
assert.NoError(t, err)
assert.Equal(t, oneJSONStr, string(b))
assert.Equal(t, "\"4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM\"", string(b))
}

func TestUnmarshalJSON(t *testing.T) {
var h Hash
assert.NoError(t, h.UnmarshalJSON([]byte(oneJSONStr)))
assert.Equal(t, one, h)
assert.NoError(t, json.Unmarshal([]byte("\"4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM\""), &h))
assert.Equal(t, Int(1), h)
}
63 changes: 32 additions & 31 deletions protobuf/api/event.pb.go

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

Loading