Skip to content

Commit

Permalink
stop
Browse files Browse the repository at this point in the history
  • Loading branch information
lunfardo314 committed May 14, 2024
1 parent e9bb9f5 commit 399900b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 129 deletions.
14 changes: 7 additions & 7 deletions embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ var (
// embedding functions with inline tests

func (lib *Library) embedMain() {
lib.EmbedShort(embedShortBase...)
lib.EmbedLong(embedLongBase...)
lib.UpgradeWithEmbeddedShort(embedShortBase...)
lib.UpgradeWthEmbeddedLong(embedLongBase...)

// inline tests
lib.MustEqual("concat", "0x")
Expand Down Expand Up @@ -110,7 +110,7 @@ func (lib *Library) embedMain() {
}

func (lib *Library) embedArithmetics() {
lib.EmbedShort(embedArithmeticsShort...)
lib.UpgradeWithEmbeddedShort(embedArithmeticsShort...)

lib.MustEqual("add(5,6)", "add(10,1)")
lib.MustEqual("add(5,6)", "u64/11")
Expand Down Expand Up @@ -153,8 +153,8 @@ func (lib *Library) embedArithmetics() {
}

func (lib *Library) embedBitwiseAndCmp() {
lib.EmbedShort(embedBitwiseAndCmpShort...)
lib.EmbedLong(embedBitwiseAndCmpLong...)
lib.UpgradeWithEmbeddedShort(embedBitwiseAndCmpShort...)
lib.UpgradeWthEmbeddedLong(embedBitwiseAndCmpLong...)

// comparison lexicographical (equivalent to bigendian for binary integers)
lib.MustTrue("lessThan(1,2)")
Expand Down Expand Up @@ -187,7 +187,7 @@ func (lib *Library) embedBitwiseAndCmp() {
}

func (lib *Library) embedBaseCrypto() {
lib.EmbedLong(embedBaseCrypto...)
lib.UpgradeWthEmbeddedLong(embedBaseCrypto...)

h := blake2b.Sum256([]byte{1})
lib.MustEqual("len(blake2b(1))", "u64/32")
Expand All @@ -204,7 +204,7 @@ func (lib *Library) embedBytecodeManipulation() {
//lib.embedLong("unwrapBytecodeArg", 3, lib.evalUnwrapBytecodeArg)
//lib.embedLong("parseBytecodePrefix", 1, lib.evalParseBytecodePrefix)
//lib.embedLong("evalBytecodeArg", 3, lib.evalEvalBytecodeArg)
lib.EmbedLong(embedBytecodeManipulation(lib)...)
lib.UpgradeWthEmbeddedLong(embedBytecodeManipulation(lib)...)

_, _, binCode, err := lib.CompileExpression("slice(0x01020304,1,2)")
AssertNoError(err)
Expand Down
2 changes: 1 addition & 1 deletion extend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var extendWithUtilityFunctions = []*ExtendFunction{
}

func (lib *Library) extendBase() {
lib.Extend(extendWithUtilityFunctions...)
lib.UpgradeWithExtensions(extendWithUtilityFunctions...)

lib.MustError("require(nil, !!!requirement_failed)", "requirement failed")
lib.MustEqual("require(true, !!!something_wrong)", "true")
Expand Down
14 changes: 7 additions & 7 deletions library.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ func (lib *Library) embedLongErr(sym string, requiredNumPar int, evalFun EvalFun
return dscr.funCode, nil
}

func (lib *Library) EmbedShort(funList ...*EmbedFunction) {
err := lib.EmbedShortErr(funList...)
func (lib *Library) UpgradeWithEmbeddedShort(funList ...*EmbedFunction) {
err := lib.UpgradeWithEmbeddedShortErr(funList...)
AssertNoError(err)
}

func (lib *Library) EmbedShortErr(funList ...*EmbedFunction) (err error) {
func (lib *Library) UpgradeWithEmbeddedShortErr(funList ...*EmbedFunction) (err error) {
for _, fun := range funList {
if _, err = lib.embedShortErr(fun.Sym, fun.RequiredNumPar, fun.EvalFun); err != nil {
return
Expand All @@ -251,12 +251,12 @@ func (lib *Library) EmbedShortErr(funList ...*EmbedFunction) (err error) {
return
}

func (lib *Library) EmbedLong(funList ...*EmbedFunction) {
err := lib.EmbedLongErr(funList...)
func (lib *Library) UpgradeWthEmbeddedLong(funList ...*EmbedFunction) {
err := lib.UpgradeWithEmbedLongErr(funList...)
AssertNoError(err)
}

func (lib *Library) EmbedLongErr(funList ...*EmbedFunction) (err error) {
func (lib *Library) UpgradeWithEmbedLongErr(funList ...*EmbedFunction) (err error) {
for _, fun := range funList {
if _, err = lib.embedLongErr(fun.Sym, fun.RequiredNumPar, fun.EvalFun); err != nil {
return
Expand All @@ -265,7 +265,7 @@ func (lib *Library) EmbedLongErr(funList ...*EmbedFunction) (err error) {
return
}

func (lib *Library) Extend(funList ...*ExtendFunction) {
func (lib *Library) UpgradeWithExtensions(funList ...*ExtendFunction) {
for _, fun := range funList {
lib.extend(fun.Sym, fun.Source)
}
Expand Down
40 changes: 0 additions & 40 deletions library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,43 +942,3 @@ func TestLocalLibrary(t *testing.T) {
})

}

func TestSerde(t *testing.T) {
t.Run("descriptor", func(t *testing.T) {
fd := funDescriptor{
sym: "dummy",
funCode: 5,
bytecode: nil,
requiredNumParams: 0,
evalFun: nil,
}
var buf bytes.Buffer
fd.write(&buf)
bin := buf.Bytes()

lib := New()
fd1 := funDescriptor{}
err := fd1.read(bytes.NewReader(bin), lib)
require.NoError(t, err)
})
t.Run("library", func(t *testing.T) {
lib := NewBase()
bin := lib.libraryBytes()
t.Logf("serialized base library: %d bytes", len(bin))
t.Logf("------ library original")
lib.PrintLibraryStats()

lib1 := New()
lib1.embedBase()
rdr := bytes.NewReader(bin)
err := lib1.read(rdr)
require.NoError(t, err)
t.Logf("------ library back")
lib1.PrintLibraryStats()

bin1 := lib1.libraryBytes()
t.Logf("serialized base library 1: %d bytes", len(bin1))
require.EqualValues(t, bin, bin1)

})
}
77 changes: 3 additions & 74 deletions serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package easyfl
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"sort"

Expand All @@ -22,6 +21,9 @@ func (lib *Library) libraryBytes() []byte {
return buf.Bytes()
}

// currently only serialization is implemented. Deserialization is not.
// Serialization is only used for calculating library hash, to support library upgrades

func (lib *Library) write(w io.Writer) {
_ = binary.Write(w, binary.BigEndian, lib.numEmbeddedShort)
_ = binary.Write(w, binary.BigEndian, lib.numEmbeddedLong)
Expand All @@ -39,29 +41,6 @@ func (lib *Library) write(w io.Writer) {
}
}

func (lib *Library) read(r io.Reader) (err error) {
var numEmbeddedShort, numEmbeddedLong, numExtended uint16
if err = binary.Read(r, binary.BigEndian, &numEmbeddedShort); err != nil {
return err
}
if err = binary.Read(r, binary.BigEndian, &numEmbeddedLong); err != nil {
return err
}
if err = binary.Read(r, binary.BigEndian, &numExtended); err != nil {
return err
}
expected := numEmbeddedShort + numEmbeddedLong + numExtended

for lib.NumFunctions() < expected {
fd := funDescriptor{}
if err = fd.read(r, lib); err != nil {
return
}
lib.addDescriptor(&fd)
}
return nil
}

func (fd *funDescriptor) write(w io.Writer) {
// fun code
_ = binary.Write(w, binary.BigEndian, fd.funCode)
Expand All @@ -82,53 +61,3 @@ func (fd *funDescriptor) write(w io.Writer) {
_ = binary.Write(w, binary.BigEndian, uint16(len(fd.bytecode)))
_, _ = w.Write(fd.bytecode)
}

func (fd *funDescriptor) read(r io.Reader, lib *Library) (err error) {
// fun code
if err = binary.Read(r, binary.BigEndian, &fd.funCode); err != nil {
return err
}
// required number of parameters
var np byte
if err = binary.Read(r, binary.BigEndian, &np); err != nil {
return err
}
fd.requiredNumParams = int(np)
if np == 0xff {
fd.requiredNumParams = -1
}
// function name
var size byte
if err = binary.Read(r, binary.BigEndian, &size); err != nil {
return err
}
buf := make([]byte, size)
if _, err = r.Read(buf); err != nil {
return err
}
fd.sym = string(buf)
// bytecode
if err = binary.Read(r, binary.BigEndian, &size); err != nil {
return err
}
if size > 0 {
// extension
fd.bytecode = make([]byte, size)
if _, err = r.Read(fd.bytecode); err != nil {
return err
}
if fd.evalFun, err = lib.evalFunctionForBytecode(fd.sym, fd.bytecode); err != nil {
return err
}
} else {
// embedded
var sym string
if fd.evalFun, fd.requiredNumParams, sym, err = lib.functionByCode(fd.funCode); err != nil {
return fmt.Errorf("can't find embedded function '%s' with code %d: %w", fd.sym, fd.funCode, err)
}
if sym != fd.sym {
return fmt.Errorf("embedded function with code %d: expected name '%s', got '%s'", fd.funCode, fd.sym, sym)
}
}
return nil
}

0 comments on commit 399900b

Please sign in to comment.