Skip to content

Commit

Permalink
Change proto hash type from string to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Aug 29, 2019
1 parent bf61887 commit 3bccb62
Show file tree
Hide file tree
Showing 35 changed files with 455 additions and 627 deletions.
6 changes: 1 addition & 5 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ func deployCoreServices(cfg *config.Config, sdk *enginesdk.SDK) error {
}
}
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))
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
54 changes: 11 additions & 43 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 @@ -76,25 +75,12 @@ func Random() (Hash, error) {
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) {
hash, err := base58.Decode(h)
if err != nil {
return nil, fmt.Errorf("hash: %s", err)
}
if len(hash) != size {
return nil, fmt.Errorf("hash: invalid length string")
}
return Hash(hash), nil
}

// IsZero reports whethere h represents empty hash.
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 +90,14 @@ 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.
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.
// 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,29 +106,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))
return json.Marshal([]byte(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 {
return err
}

if str == "" {
return nil
}

h1, err := Decode(str)
if err != nil {
return err
}
*h = h1
return nil
return json.Unmarshal(data, (*[]byte)(h))
}
49 changes: 4 additions & 45 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,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 +19,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 @@ -55,56 +50,20 @@ func TestRandom(t *testing.T) {
require.NoError(t, quick.Check(f, nil))
}

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

_, err = Decode("0")
assert.Equal(t, "hash: invalid base58 digit ('0')", err.Error())

_, err = Decode("1")
assert.Equal(t, "hash: invalid length string", err.Error())
}

func TestIsZero(t *testing.T) {
assert.True(t, Hash{}.IsZero())
}

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())
}

func TestMarshalJSON(t *testing.T) {
b, err := one.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, oneJSONStr, string(b))
}

func TestUnmarshalJSON(t *testing.T) {
var h Hash
assert.NoError(t, h.UnmarshalJSON([]byte(oneJSONStr)))
assert.Equal(t, one, h)
assert.Equal(t, size, zero.Size())
}
Loading

0 comments on commit 3bccb62

Please sign in to comment.