Skip to content

Commit

Permalink
Refactor Packer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
learnforpractice committed Nov 26, 2022
1 parent 6172c6d commit 3c45611
Show file tree
Hide file tree
Showing 21 changed files with 201 additions and 192 deletions.
42 changes: 7 additions & 35 deletions action.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package chain

import (
"unsafe"

"github.com/uuosio/chain/eosio"
)

Expand Down Expand Up @@ -105,46 +103,20 @@ func NewAction(perm *PermissionLevel, account Name, name Name, args ...interface
return a
}

func PackUint64(n uint64) []byte {
p := [8]byte{}
pp := (*[8]byte)(unsafe.Pointer(&n))
copy(p[:], pp[:])
return p[:]
}

func PackArray(a []Serializer) []byte {
buf := []byte{byte(len(a))}
for _, v := range a {
buf = append(buf, v.Pack()...)
}
return buf
}

func (a *Action) Size() int {
return 8 + 8 + 5 + len(a.Authorization)*8 + 5 + len(a.Data)
}

func (a *Action) Pack() []byte {
enc := NewEncoder(a.Size())
func (a *Action) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.PackName(a.Account)
enc.PackName(a.Name)
enc.PackLength(len(a.Authorization))
for _, v := range a.Authorization {
enc.Pack(v)
v.Pack(enc)
}
enc.Pack(a.Data)
return enc.GetBytes()
// buf := []byte{}
// buf = append(buf, PackUint64(a.Account)...)
// buf = append(buf, PackUint64(a.Name)...)

// buf = append(buf, PackUint32(uint32(len(a.Authorization)))...)
// for _, v := range a.Authorization {
// buf = append(buf, v.Pack()...)
// }

// buf = append(buf, a.Data.Pack()...)
// return buf
enc.PackBytes(a.Data)
return enc.GetSize() - oldSize
}

func (a *Action) Unpack(b []byte) int {
Expand Down Expand Up @@ -178,12 +150,12 @@ func (a *Action) AddPermission(actor Name, permission Name) {
}

func (a *Action) Send() {
data := a.Pack()
data := EncoderPack(a)
SendInline(data)
}

//send action directly, no cache
func (a *Action) SendEx() {
data := a.Pack()
data := EncoderPack(a)
SendInline(data)
}
41 changes: 21 additions & 20 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func NewPermissionLevel(actor Name, permission Name) *PermissionLevel {
return &PermissionLevel{actor, permission}
}

func (t *PermissionLevel) Pack() []byte {
enc := NewEncoder(t.Size())
func (t *PermissionLevel) Pack(enc *Encoder) int {
size := enc.GetSize()
enc.PackUint64(t.Actor.N)
enc.PackUint64(t.Permission.N)
return enc.GetBytes()
return enc.GetSize() - size
}

func (t *PermissionLevel) Unpack(data []byte) int {
Expand All @@ -52,11 +52,11 @@ func (t *PermissionLevel) Size() int {
return size
}

func (t *PermissionLevelWeight) Pack() []byte {
enc := NewEncoder(t.Size())
enc.Pack(&t.Permission)
func (t *PermissionLevelWeight) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
t.Permission.Pack(enc)
enc.PackUint16(t.Weight)
return enc.GetBytes()
return enc.GetSize() - oldSize
}

func (t *PermissionLevelWeight) Unpack(data []byte) int {
Expand All @@ -73,11 +73,11 @@ func (t *PermissionLevelWeight) Size() int {
return size
}

func (t *WaitWeight) Pack() []byte {
enc := NewEncoder(t.Size())
func (t *WaitWeight) Pack(enc *Encoder) int {
size := enc.GetSize()
enc.PackUint32(t.WaitSec)
enc.PackUint16(t.Weight)
return enc.GetBytes()
return enc.GetSize() - size
}

func (t *WaitWeight) Unpack(data []byte) int {
Expand All @@ -94,31 +94,32 @@ func (t *WaitWeight) Size() int {
return size
}

func (t *Authority) Pack() []byte {
enc := NewEncoder(t.Size())
func (t *Authority) Pack(enc *Encoder) int {
oldSize := enc.GetSize()

enc.PackUint32(t.Threshold)
{
enc.PackLength(len(t.Keys))
for i := range t.Keys {
enc.Pack(&t.Keys[i])
t.Keys[i].Pack(enc)
}
}

{
enc.PackLength(len(t.Accounts))
for i := range t.Accounts {
enc.Pack(&t.Accounts[i])
t.Accounts[i].Pack(enc)
}
}

{
enc.PackLength(len(t.Waits))
for i := range t.Waits {
enc.Pack(&t.Waits[i])
t.Waits[i].Pack(enc)
}
}

return enc.GetBytes()
return enc.GetSize() - oldSize
}

func (t *Authority) Unpack(data []byte) int {
Expand Down Expand Up @@ -172,11 +173,11 @@ func (t *Authority) Size() int {
return size
}

func (t *KeyWeight) Pack() []byte {
enc := NewEncoder(t.Size())
enc.Pack(&t.Key)
func (t *KeyWeight) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
t.Key.Pack(enc)
enc.PackUint16(t.Weight)
return enc.GetBytes()
return enc.GetSize() - oldSize
}

func (t *KeyWeight) Unpack(data []byte) int {
Expand Down
36 changes: 21 additions & 15 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

type Checksum160 [20]byte

func (t *Checksum160) Pack() []byte {
return t[:]
func (t *Checksum160) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteBytes(t[:])
return enc.GetSize() - oldSize
}

func (t *Checksum160) Unpack(data []byte) int {
Expand All @@ -22,8 +24,10 @@ func (t *Checksum160) Size() int {

type Checksum256 [32]byte

func (t *Checksum256) Pack() []byte {
return t[:]
func (t *Checksum256) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteBytes(t[:])
return enc.GetSize() - oldSize
}

func (t *Checksum256) Unpack(data []byte) int {
Expand All @@ -38,8 +42,10 @@ func (t *Checksum256) Size() int {

type Checksum512 [64]byte

func (t *Checksum512) Pack() []byte {
return t[:]
func (t *Checksum512) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteBytes(t[:])
return enc.GetSize() - oldSize
}

func (t *Checksum512) Unpack(data []byte) int {
Expand Down Expand Up @@ -94,7 +100,7 @@ func Ripemd160(data []byte) Checksum160 {

//Recover the public key from digest and signature
func RecoverKey(digest Checksum256, sig *Signature) *PublicKey {
_sig := sig.Pack()
_sig := EncoderPack(sig)
pub := eosio.RecoverKey(digest, _sig)
_pub := &PublicKey{}
_pub.Unpack(pub[:])
Expand All @@ -103,8 +109,8 @@ func RecoverKey(digest Checksum256, sig *Signature) *PublicKey {

//Tests a given public key with the generated key from digest and the signature
func AssertRecoverKey(digest Checksum256, sig Signature, pub PublicKey) {
_sig := sig.Pack()
_pub := pub.Pack()
_sig := EncoderPack(&sig)
_pub := EncoderPack(&pub)
eosio.AssertRecoverKey(digest, _sig, _pub)
}

Expand All @@ -113,11 +119,11 @@ type Signature struct {
Data [65]byte
}

func (t *Signature) Pack() []byte {
enc := NewEncoder(1 + len(t.Data))
func (t *Signature) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteUint8(t.Type)
enc.WriteBytes(t.Data[:])
return enc.GetBytes()
return enc.GetSize() - oldSize
}

func (t *Signature) Unpack(data []byte) int {
Expand All @@ -137,11 +143,11 @@ type PublicKey struct {
Data [33]byte
}

func (t *PublicKey) Pack() []byte {
enc := NewEncoder(34)
func (t *PublicKey) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteUint8(t.Type)
enc.WriteBytes(t.Data[:])
return enc.GetBytes()
return enc.GetSize() - oldSize
}

func (t *PublicKey) Unpack(data []byte) int {
Expand Down
3 changes: 1 addition & 2 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ type SecondaryTable interface {
}

type TableValue interface {
chain.Serializer
GetPrimary() uint64
Pack() []byte
Unpack(data []byte) int
}
9 changes: 5 additions & 4 deletions database/mi.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (mi *MultiIndex) SetTable(code chain.Name, scope chain.Name, table chain.Na
}

func (mi *MultiIndex) Store(v MultiIndexValue, payer chain.Name) *Iterator {
it := mi.Table.Store(v.GetPrimary(), v.Pack(), payer)
it := mi.Table.Store(v.GetPrimary(), chain.EncoderPack(v), payer)
primary := v.GetPrimary()
for i, db := range mi.IDXTables {
db.StoreEx(primary, v.GetSecondaryValue(i), payer.N)
Expand All @@ -123,7 +123,7 @@ func (mi *MultiIndex) Set(primary uint64, v MultiIndexValue, payer chain.Name) {
chain.Check(primary == v.GetPrimary(), "mi.Store: Invalid primary key")
it := mi.Find(primary)
if !it.IsOk() {
mi.Table.Store(primary, v.Pack(), payer)
mi.Table.Store(primary, chain.EncoderPack(v), payer)
for i, db := range mi.IDXTables {
db.StoreEx(primary, v.GetSecondaryValue(i), payer.N)
}
Expand Down Expand Up @@ -215,7 +215,8 @@ func (mi *MultiIndex) Update(it *Iterator, v MultiIndexValue, payer chain.Name)

chain.Check(mi.code == chain.CurrentReceiver(), "mi.Update: Can not update other contract")

mi.Table.Update(it, v.Pack(), payer)
mi.Table.Update(it, chain.EncoderPack(v), payer)

for i, db := range mi.IDXTables {
it, oldSecondary := db.FindByPrimary(primary)
// logger.Println(primary, i, oldSecondary, ":")
Expand Down Expand Up @@ -312,7 +313,7 @@ func (mi *MultiIndex) IdxUpdate(it *SecondaryIterator, secondary interface{}, pa
return
}
_v.SetSecondaryValue(idxTable.GetIndex(), secondary)
mi.Table.Update(itPrimary, _v.Pack(), payer)
mi.Table.Update(itPrimary, chain.EncoderPack(_v), payer)
idxTable.UpdateEx(it, secondary, payer.N)
}

Expand Down
2 changes: 1 addition & 1 deletion database/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewSingletonTable(code, scope, table chain.Name, unpacker ...Unpacker) *Sin
}

func (t *SingletonTable) Set(data TableValue, payer chain.Name) {
t.TableI64.Set(t.TableI64.GetTableName(), data.Pack(), payer)
t.TableI64.Set(t.TableI64.GetTableName(), chain.EncoderPack(data), payer)
}

func (t *SingletonTable) Get() interface{} {
Expand Down
6 changes: 4 additions & 2 deletions float128.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ func NewFloat128(v float64) Float128 {
return t
}

func (n *Float128) Pack() []byte {
return n[:]
func (n *Float128) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteBytes(n[:])
return enc.GetSize() - oldSize
}

func (n *Float128) Unpack(data []byte) int {
Expand Down
6 changes: 4 additions & 2 deletions int128.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func NewInt128FromBytes(bs []byte) Int128 {
return a
}

func (n *Int128) Pack() []byte {
return n[:]
func (n *Int128) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteBytes(n[:])
return enc.GetSize() - oldSize
}

func (n *Int128) Unpack(data []byte) int {
Expand Down
6 changes: 3 additions & 3 deletions name.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func N(s string) Name {
return Name{N: S2N(s)}
}

func (a *Name) Pack() []byte {
enc := NewEncoder(8)
func (a *Name) Pack(enc *Encoder) int {
oldSize := enc.GetSize()
enc.WriteUint64(a.N)
return enc.GetBytes()
return enc.GetSize() - oldSize
}

func (a *Name) Unpack(data []byte) int {
Expand Down
Loading

0 comments on commit 3c45611

Please sign in to comment.