diff --git a/embed.go b/embed.go index 2fee303..06f15d6 100644 --- a/embed.go +++ b/embed.go @@ -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") @@ -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") @@ -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)") @@ -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") @@ -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) diff --git a/extend.go b/extend.go index b40558b..15a3c28 100644 --- a/extend.go +++ b/extend.go @@ -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") diff --git a/library.go b/library.go index 100b745..85d0644 100644 --- a/library.go +++ b/library.go @@ -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 @@ -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 @@ -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) } diff --git a/library_test.go b/library_test.go index 21c15dc..a42552c 100644 --- a/library_test.go +++ b/library_test.go @@ -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) - - }) -} diff --git a/serde.go b/serde.go index cbf5843..aad259d 100644 --- a/serde.go +++ b/serde.go @@ -3,7 +3,6 @@ package easyfl import ( "bytes" "encoding/binary" - "fmt" "io" "sort" @@ -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) @@ -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) @@ -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 -}