Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit 4ed0da6

Browse files
committed
use github.com/the729/lcs package to serialize and deserialize structs.
1 parent c0bf81e commit 4ed0da6

18 files changed

+36
-316
lines changed

common/canonical_serialization/deserializer.go

-34
This file was deleted.

common/canonical_serialization/err.go

-8
This file was deleted.

common/canonical_serialization/serializer.go

-41
This file was deleted.

crypto/types.go

-24
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ func (k *PublicKey) UnmarshalText(txt []byte) error {
2121
return nil
2222
}
2323

24-
// func (k *PublicKey) UnmarshalBinary(data []byte) error {
25-
// r, err := serialization.SimpleDeserializer.ByteSlice(data)
26-
// if err != nil {
27-
// return err
28-
// }
29-
// if len(r) != ed25519.PublicKeySize {
30-
// return ErrWrongSize
31-
// }
32-
// *k = r
33-
// return nil
34-
// }
35-
3624
func (k PublicKey) MarshalText() (text []byte, err error) {
3725
return []byte(hex.EncodeToString(k)), nil
3826
}
@@ -49,18 +37,6 @@ func (k *PrivateKey) UnmarshalText(txt []byte) error {
4937
return nil
5038
}
5139

52-
// func (k *PrivateKey) UnmarshalBinary(data []byte) error {
53-
// r, err := serialization.SimpleDeserializer.ByteSlice(data)
54-
// if err != nil {
55-
// return err
56-
// }
57-
// if len(r) != ed25519.PrivateKeySize {
58-
// return ErrWrongSize
59-
// }
60-
// *k = r
61-
// return nil
62-
// }
63-
6440
func (k PrivateKey) MarshalText() (text []byte, err error) {
6541
return []byte(hex.EncodeToString(k)), nil
6642
}

language/stdscript/event_types.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
11
package stdscript
22

33
import (
4-
serialization "github.com/the729/go-libra/common/canonical_serialization"
54
"github.com/the729/go-libra/types"
5+
"github.com/the729/lcs"
66
)
77

88
// SentPaymentEvent is a p2p sent payment event
99
type SentPaymentEvent struct {
10-
Payee types.AccountAddress
1110
Amount uint64
11+
Payee types.AccountAddress
1212
}
1313

1414
// ReceivedPaymentEvent is a p2p received payment event
1515
type ReceivedPaymentEvent struct {
16-
Payer types.AccountAddress
1716
Amount uint64
17+
Payer types.AccountAddress
1818
}
1919

2020
// UnmarshalBinary unmarshals raw bytes into this struct.
2121
func (ev *SentPaymentEvent) UnmarshalBinary(data []byte) error {
22-
ev.Amount = serialization.SimpleDeserializer.Uint64(data)
23-
data = data[8:]
24-
addr, err := serialization.SimpleDeserializer.ByteSlice(data)
25-
if err != nil {
26-
return err
27-
}
28-
ev.Payee = addr
29-
return nil
22+
return lcs.Unmarshal(data, ev)
3023
}
3124

3225
// UnmarshalBinary unmarshals raw bytes into this struct.
3326
func (ev *ReceivedPaymentEvent) UnmarshalBinary(data []byte) error {
34-
ev.Amount = serialization.SimpleDeserializer.Uint64(data)
35-
data = data[8:]
36-
addr, err := serialization.SimpleDeserializer.ByteSlice(data)
37-
if err != nil {
38-
return err
39-
}
40-
ev.Payer = addr
41-
return nil
27+
return lcs.Unmarshal(data, ev)
4228
}

types/access_path.go

-13
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"strings"
99

1010
"github.com/the729/go-libra/crypto/sha3libra"
11-
12-
serialization "github.com/the729/go-libra/common/canonical_serialization"
1311
"github.com/the729/go-libra/generated/pbtypes"
1412
)
1513

@@ -29,17 +27,6 @@ func (ap *AccessPath) FromProto(pb *pbtypes.AccessPath) error {
2927
return nil
3028
}
3129

32-
// SerializeTo serializes this struct into a io.Writer.
33-
func (ap *AccessPath) SerializeTo(w io.Writer) error {
34-
if err := ap.Address.SerializeTo(w); err != nil {
35-
return err
36-
}
37-
if err := serialization.SimpleSerializer.Write(w, ap.Path); err != nil {
38-
return err
39-
}
40-
return nil
41-
}
42-
4330
// Clone deep clones this struct.
4431
func (ap *AccessPath) Clone() *AccessPath {
4532
out := &AccessPath{}

types/account_address.go

-10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package types
22

33
import (
44
"encoding/hex"
5-
"io"
65

7-
serialization "github.com/the729/go-libra/common/canonical_serialization"
86
"github.com/the729/go-libra/crypto/sha3libra"
97
)
108

@@ -16,14 +14,6 @@ const (
1614
// AccountAddress is an account address.
1715
type AccountAddress []byte
1816

19-
// SerializeTo serializes this struct into a io.Writer.
20-
func (a AccountAddress) SerializeTo(w io.Writer) error {
21-
if err := serialization.SimpleSerializer.Write(w, []byte(a)); err != nil {
22-
return err
23-
}
24-
return nil
25-
}
26-
2717
// Hash ouptuts the hash of this struct, using the appropriate hash function.
2818
func (a AccountAddress) Hash() sha3libra.HashValue {
2919
hasher := sha3libra.NewAccountAddress()

types/account_blob.go

+10-36
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ import (
44
"errors"
55
"fmt"
66

7-
serialization "github.com/the729/go-libra/common/canonical_serialization"
87
"github.com/the729/go-libra/crypto/sha3libra"
8+
"github.com/the729/lcs"
99
)
1010

1111
// RawAccountBlob is the raw blob of an account.
1212
type RawAccountBlob []byte
1313

14-
// AccountBlob is the blob of an account.
15-
//
16-
// It consists of the raw blob, and the decoded map of resources.
14+
// AccountBlob is the blob of an account. It is a map of resources.
1715
type AccountBlob struct {
18-
Raw []byte
1916
Map map[string][]byte
2017
}
2118

@@ -41,30 +38,16 @@ func (b *AccountBlob) Hash() sha3libra.HashValue {
4138
if b == nil {
4239
return nil
4340
}
44-
return RawAccountBlob(b.Raw).Hash()
41+
raw, err := lcs.Marshal(b)
42+
if err != nil {
43+
panic(err)
44+
}
45+
return RawAccountBlob(raw).Hash()
4546
}
4647

4748
// ParseToMap parses the raw blob into a map of resources.
48-
func (b *AccountBlob) ParseToMap() error {
49-
data := b.Raw
50-
l := int(serialization.SimpleDeserializer.Uint32(data))
51-
data = data[4:]
52-
m := make(map[string][]byte)
53-
for i := 0; i < l; i++ {
54-
key, err := serialization.SimpleDeserializer.ByteSlice(data)
55-
if err != nil {
56-
return errors.New("error deserizaing key")
57-
}
58-
data = data[len(key)+4:]
59-
val, err := serialization.SimpleDeserializer.ByteSlice(data)
60-
if err != nil {
61-
return errors.New("error deserizaing val")
62-
}
63-
data = data[len(val)+4:]
64-
m[string(key)] = val
65-
}
66-
b.Map = m
67-
return nil
49+
func (b *AccountBlob) ParseToMap(raw RawAccountBlob) error {
50+
return lcs.Unmarshal(raw, b)
6851
}
6952

7053
// GetResource gets a resource from the account blob by its path.
@@ -78,21 +61,12 @@ func (b *AccountBlob) GetResource(path []byte) (*AccountResource, error) {
7861
return nil, errors.New("resource not found")
7962
}
8063
r := &AccountResource{}
81-
err := r.UnmarshalBinary(val)
82-
if err != nil {
64+
if err := lcs.Unmarshal(val, r); err != nil {
8365
return nil, fmt.Errorf("unmarshal resource error: %v", err)
8466
}
8567
return r, nil
8668
}
8769

88-
// GetRawBlob returns a copy of raw blob
89-
func (pb *ProvenAccountBlob) GetRawBlob() []byte {
90-
if !pb.proven {
91-
panic("not valid proven account blob")
92-
}
93-
return cloneBytes(pb.accountBlob.Raw)
94-
}
95-
9670
// GetResource gets a resource from a proven account blob by its path.
9771
//
9872
// To get Libra coin account resource, use AccountResourcePath() to generate the path.

types/account_resource.go

-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package types
22

3-
import (
4-
"github.com/the729/lcs"
5-
)
6-
73
// AccountResource is the Libra coin resource of an account.
84
type AccountResource struct {
95
AuthenticationKey []byte
@@ -22,11 +18,6 @@ type ProvenAccountResource struct {
2218
addr AccountAddress
2319
}
2420

25-
// UnmarshalBinary unmarshals raw bytes into this account resource struct.
26-
func (r *AccountResource) UnmarshalBinary(data []byte) error {
27-
return lcs.Unmarshal(data, r)
28-
}
29-
3021
// Clone deep clones this struct.
3122
func (r *AccountResource) Clone() *AccountResource {
3223
out := &AccountResource{}

types/account_state.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ func (pas *ProvenAccountState) GetAccountBlob() *ProvenAccountBlob {
175175
proven: true,
176176
addr: cloneBytes(pas.addr),
177177
}
178-
pab.accountBlob.Raw = cloneBytes(pas.accountState.RawBlob)
179-
pab.accountBlob.ParseToMap()
178+
if err := pab.accountBlob.ParseToMap(cloneBytes(pas.accountState.RawBlob)); err != nil {
179+
panic(err)
180+
}
180181
return pab
181182
}
182183

types/contract_event.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package types
22

33
import (
4-
"io"
5-
64
"github.com/the729/go-libra/crypto/sha3libra"
7-
8-
serialization "github.com/the729/go-libra/common/canonical_serialization"
95
"github.com/the729/go-libra/generated/pbtypes"
6+
"github.com/the729/lcs"
107
)
118

129
type EventKey []byte
@@ -46,24 +43,10 @@ func (e *ContractEvent) FromProto(pb *pbtypes.Event) error {
4643
return nil
4744
}
4845

49-
// SerializeTo serializes this struct into a io.Writer.
50-
func (e *ContractEvent) SerializeTo(w io.Writer) error {
51-
if err := serialization.SimpleSerializer.Write(w, []byte(e.Key)); err != nil {
52-
return err
53-
}
54-
if err := serialization.SimpleSerializer.Write(w, e.SequenceNumber); err != nil {
55-
return err
56-
}
57-
if err := serialization.SimpleSerializer.Write(w, e.Data); err != nil {
58-
return err
59-
}
60-
return nil
61-
}
62-
6346
// Hash ouptuts the hash of this struct, using the appropriate hash function.
6447
func (e *ContractEvent) Hash() sha3libra.HashValue {
6548
hasher := sha3libra.NewContractEvent()
66-
if err := e.SerializeTo(hasher); err != nil {
49+
if err := lcs.NewEncoder(hasher).Encode(e); err != nil {
6750
panic(err)
6851
}
6952
return hasher.Sum([]byte{})

0 commit comments

Comments
 (0)