Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstreams Aptos Labs patches #34

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions serde-generate/runtime/csharp/Serde/BinaryDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public string deserialize_str()
return utf8.GetString(content);
}

public ValueArray<ValueArray<byte>> deserialize_vec_bytes() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be called by a modified version of the code-generator? (that we don't have here)
Why do we need this in the first place?

long len = deserialize_len();
ValueArray<byte>[] content = new ValueArray<byte>[len];

if (len < 0 || len > int.MaxValue)
{
throw new DeserializationException("Incorrect length value for C# array");
}

for (long i = 0; i < len; i++) {
content[i] = deserialize_bytes();
}

return new ValueArray<ValueArray<byte>>(content);
}

public ValueArray<byte> deserialize_bytes()
{
long len = deserialize_len();
Expand Down
7 changes: 7 additions & 0 deletions serde-generate/runtime/csharp/Serde/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public void decrease_container_depth()

public void serialize_str(string value) => serialize_bytes(new ValueArray<byte>(utf8.GetBytes(value)));

public void serialize_vec_bytes(ValueArray<ValueArray<byte>> value) {
serialize_len(value.Count);
foreach (ValueArray<byte> v in value) {
serialize_bytes(v);
}
}

public void serialize_bytes(ValueArray<byte> value)
{
serialize_len(value.Count);
Expand Down
2 changes: 2 additions & 0 deletions serde-generate/runtime/csharp/Serde/IDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface IDeserializer
{
string deserialize_str();

ValueArray<ValueArray<byte>> deserialize_vec_bytes();

ValueArray<byte> deserialize_bytes();

bool deserialize_bool();
Expand Down
2 changes: 2 additions & 0 deletions serde-generate/runtime/csharp/Serde/ISerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface ISerializer
{
void serialize_str(string value);

void serialize_vec_bytes(ValueArray<ValueArray<byte>> value);

void serialize_bytes(ValueArray<byte> value);

void serialize_bool(bool value);
Expand Down
45 changes: 43 additions & 2 deletions serde-generate/runtime/golang/bcs/bcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"fmt"
"testing"

"github.com/novifinancial/serde-reflection/serde-generate/runtime/golang/bcs"
"github.com/novifinancial/serde-reflection/serde-generate/runtime/golang/serde"
"github.com/zefchain/serde-reflection/serde-generate/runtime/golang/bcs"
"github.com/zefchain/serde-reflection/serde-generate/runtime/golang/serde"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -50,6 +50,47 @@ func TestSerializeDeserializeBytes(t *testing.T) {
})
}

func TestSerializeDeserializeVecBytes(t *testing.T) {
cases := []struct {
target [][]byte
expected []byte
}{
{
target: [][]byte{{1, 2, 38}, {0, 1}, {0}},
expected: []byte{3, 3, 1, 2, 38, 2, 0, 1, 1, 0},
},
{
target: [][]byte{{1, 2, 38}, {0, 1}, {}},
expected: []byte{3, 3, 1, 2, 38, 2, 0, 1, 0},
},
{
target: [][]byte{},
expected: []byte{0},
},
}

for _, tc := range cases {
t.Run(fmt.Sprintf("%#v", tc.target), func(t *testing.T) {
s := bcs.NewSerializer()
d := bcs.NewDeserializer(tc.expected)

err := s.SerializeVecBytes(tc.target)
require.NoError(t, err)

deserialized, err := d.DeserializeVecBytes()
require.NoError(t, err)

assert.Equal(t, tc.expected, s.GetBytes())
assert.Equal(t, tc.target, deserialized)
})
}
t.Run("deserialize error: EOF", func(t *testing.T) {
d := bcs.NewDeserializer([]byte{})
_, err := d.DeserializeVecBytes()
require.EqualError(t, err, "EOF")
})
}

func TestSerializeDeserializeStr(t *testing.T) {
cases := []struct {
target string
Expand Down
6 changes: 5 additions & 1 deletion serde-generate/runtime/golang/bcs/deserializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"errors"

"github.com/novifinancial/serde-reflection/serde-generate/runtime/golang/serde"
"github.com/zefchain/serde-reflection/serde-generate/runtime/golang/serde"
)

// Maximum length allowed for sequences (vectors, bytes, strings) and maps.
Expand Down Expand Up @@ -37,6 +37,10 @@ func (d *deserializer) DeserializeF64() (float64, error) {
return 0, errors.New("unimplemented")
}

func (d *deserializer) DeserializeVecBytes() ([][]byte, error) {
return d.BinaryDeserializer.DeserializeVecBytes(d.DeserializeLen)
}

func (d *deserializer) DeserializeBytes() ([]byte, error) {
return d.BinaryDeserializer.DeserializeBytes(d.DeserializeLen)
}
Expand Down
6 changes: 5 additions & 1 deletion serde-generate/runtime/golang/bcs/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"sort"

"github.com/novifinancial/serde-reflection/serde-generate/runtime/golang/serde"
"github.com/zefchain/serde-reflection/serde-generate/runtime/golang/serde"
)

// `serializer` extends `serde.BinarySerializer` to implement `serde.Serializer`.
Expand All @@ -34,6 +34,10 @@ func (s *serializer) SerializeStr(value string) error {
return s.BinarySerializer.SerializeStr(value, s.SerializeLen)
}

func (s *serializer) SerializeVecBytes(value [][]byte) error {
return s.BinarySerializer.SerializeVecBytes(value, s.SerializeLen)
}

func (s *serializer) SerializeBytes(value []byte) error {
return s.BinarySerializer.SerializeBytes(value, s.SerializeLen)
}
Expand Down
6 changes: 5 additions & 1 deletion serde-generate/runtime/golang/bincode/deserializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"math"

"github.com/novifinancial/serde-reflection/serde-generate/runtime/golang/serde"
"github.com/zefchain/serde-reflection/serde-generate/runtime/golang/serde"
)

// MaxSequenceLength is max length supported in practice (e.g. in Java).
Expand All @@ -32,6 +32,10 @@ func (d *deserializer) DeserializeF64() (float64, error) {
return math.Float64frombits(ret), err
}

func (d *deserializer) DeserializeVecBytes() ([][]byte, error) {
return d.BinaryDeserializer.DeserializeVecBytes(d.DeserializeLen)
}

func (d *deserializer) DeserializeBytes() ([]byte, error) {
return d.BinaryDeserializer.DeserializeBytes(d.DeserializeLen)
}
Expand Down
6 changes: 5 additions & 1 deletion serde-generate/runtime/golang/bincode/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package bincode
import (
"math"

"github.com/novifinancial/serde-reflection/serde-generate/runtime/golang/serde"
"github.com/zefchain/serde-reflection/serde-generate/runtime/golang/serde"
)

// `serializer` extends `serde.BinarySerializer` to implement `serde.Serializer`.
Expand All @@ -30,6 +30,10 @@ func (s *serializer) SerializeStr(value string) error {
return s.BinarySerializer.SerializeStr(value, s.SerializeLen)
}

func (s *serializer) SerializeVecBytes(value [][]byte) error {
return s.BinarySerializer.SerializeVecBytes(value, s.SerializeLen)
}

func (s *serializer) SerializeBytes(value []byte) error {
return s.BinarySerializer.SerializeBytes(value, s.SerializeLen)
}
Expand Down
2 changes: 1 addition & 1 deletion serde-generate/runtime/golang/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/novifinancial/serde-reflection/serde-generate/runtime/golang
module github.com/zefchain/serde-reflection/serde-generate/runtime/golang

go 1.14

Expand Down
1 change: 1 addition & 0 deletions serde-generate/runtime/golang/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 17 additions & 0 deletions serde-generate/runtime/golang/serde/binary_deserializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ func (d *BinaryDeserializer) DecreaseContainerDepth() {
d.containerDepthBudget += 1
}

func (d *BinaryDeserializer) DeserializeVecBytes(deserializeLen func() (uint64, error)) ([][]byte, error) {
len, err := deserializeLen()
if err != nil {
return nil, err
}
ret := make([][]byte, len)
for i := 0; uint64(i) < len; i++ {
bytes, err := d.DeserializeBytes(deserializeLen)
if err != nil {
return nil, err
}
ret[i] = bytes
}

return ret, err
}

// `deserializeLen` to be provided by the extending struct.
func (d *BinaryDeserializer) DeserializeBytes(deserializeLen func() (uint64, error)) ([]byte, error) {
len, err := deserializeLen()
Expand Down
8 changes: 8 additions & 0 deletions serde-generate/runtime/golang/serde/binary_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (d *BinarySerializer) DecreaseContainerDepth() {
d.containerDepthBudget += 1
}

func (s *BinarySerializer) SerializeVecBytes(value [][]byte, serializeLen func(uint64) error) error {
serializeLen(uint64(len(value)))
for _, bytes := range value {
s.SerializeBytes(bytes, serializeLen)
}
return nil
}

// `serializeLen` to be provided by the extending struct.
func (s *BinarySerializer) SerializeBytes(value []byte, serializeLen func(uint64) error) error {
serializeLen(uint64(len(value)))
Expand Down
4 changes: 4 additions & 0 deletions serde-generate/runtime/golang/serde/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package serde
type Serializer interface {
SerializeStr(value string) error

SerializeVecBytes(value [][]byte) error

SerializeBytes(value []byte) error

SerializeBool(value bool) error
Expand Down Expand Up @@ -58,6 +60,8 @@ type Serializer interface {
type Deserializer interface {
DeserializeStr() (string, error)

DeserializeVecBytes() ([][]byte, error)

DeserializeBytes() ([]byte, error)

DeserializeBool() (bool, error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.nio.charset.StandardCharsets;
import java.nio.charset.CharacterCodingException;
import java.math.BigInteger;
import java.util.List;
import java.util.ArrayList;

public abstract class BinaryDeserializer implements Deserializer {
protected ByteBuffer input;
Expand Down Expand Up @@ -47,6 +49,19 @@ public String deserialize_str() throws DeserializationError {
return new String(content);
}

public List<Bytes> deserialize_vec_bytes() throws DeserializationError {
long len = deserialize_len();
if (len < 0 || len > Integer.MAX_VALUE) {
throw new DeserializationError("Incorrect length value for Java array");
}
List<Bytes> content = new ArrayList<Bytes>();
for (int i = 0; i < len; i++) {
content.add(deserialize_bytes());
}

return content;
}

public Bytes deserialize_bytes() throws DeserializationError {
long len = deserialize_len();
if (len < 0 || len > Integer.MAX_VALUE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.novi.serde;

import java.math.BigInteger;
import java.util.List;

public abstract class BinarySerializer implements Serializer {
protected MyByteArrayOutputStream output;
Expand All @@ -29,6 +30,13 @@ public void serialize_str(String value) throws SerializationError {
serialize_bytes(new Bytes(value.getBytes()));
}

public void serialize_vec_bytes(List<Bytes> value) throws SerializationError {
serialize_len(value.size());
for (Bytes bytes : value) {
serialize_bytes(bytes);
}
}

public void serialize_bytes(Bytes value) throws SerializationError {
byte[] content = value.content();
serialize_len(content.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
package com.novi.serde;

import java.math.BigInteger;
import java.util.List;

public interface Deserializer {
String deserialize_str() throws DeserializationError;

List<Bytes> deserialize_vec_bytes() throws DeserializationError;

Bytes deserialize_bytes() throws DeserializationError;

Boolean deserialize_bool() throws DeserializationError;
Expand Down
3 changes: 3 additions & 0 deletions serde-generate/runtime/java/com/novi/serde/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
package com.novi.serde;

import java.math.BigInteger;
import java.util.List;

public interface Serializer {
void serialize_str(String value) throws SerializationError;

void serialize_vec_bytes(List<Bytes> value) throws SerializationError;

void serialize_bytes(Bytes value) throws SerializationError;

void serialize_bool(Boolean value) throws SerializationError;
Expand Down
22 changes: 22 additions & 0 deletions serde-generate/runtime/python/bcs/test_bcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ def test_serialize_bytes(self):

self.assertEqual(bcs.deserialize(b"\x00", bytes), (b"", b""))

def test_serialize_vec_bytes(self):
a = []
b = [b"\x00\x00", b"\x01\x01"]
c = [b"\x00" * 128, b"\x01" * 127, b"\x02" * 3, b""]

aser = bcs.serialize(a, typing.Sequence[bytes])
bser = bcs.serialize(b, typing.Sequence[bytes])
cser = bcs.serialize(c, typing.Sequence[bytes])

self.assertEqual(aser, b"\x00")
self.assertEqual(bser, b"\x02\x02\x00\x00\x02\x01\x01")
self.assertEqual(cser,
b"\x04\x80\x01" + b"\x00" * 128 +
b"\x7F" + b"\x01" * 127 +
b"\x03" + b"\x02" * 3 +
b"\x00"
)

self.assertEqual(bcs.deserialize(aser, typing.Sequence[bytes]), (a, b""))
self.assertEqual(bcs.deserialize(bser, typing.Sequence[bytes]), (b, b""))
self.assertEqual(bcs.deserialize(cser, typing.Sequence[bytes]), (c, b""))

def test_serialize_tuple(self):
T = typing.Tuple[st.uint8, st.uint16]
self.assertEqual(bcs.serialize((0, 1), T), b"\x00\x01\x00")
Expand Down
Loading