Skip to content

Commit

Permalink
fix(abi): fix Tuple not encoding dynamic types correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonboukheir committed Jul 14, 2022
1 parent 660dec1 commit be125d3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
20 changes: 13 additions & 7 deletions Runtime/CareBoo.AlgoSdk/Experimental/Abi/Values/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ public EncodedAbiArg Encode(IAbiType type, AbiReferences references, Allocator a
if (type.IsFixedArray)
return tupleEncoding;

var result = new EncodedAbiArg(allocator);
using var k = new UInt16((ushort)value.Length)
.Encode(AbiType.UIntN(16), references, Allocator.Persistent);
result.Bytes.AddRange(k.Bytes);
result.Bytes.AddRange(tupleEncoding.Bytes);
tupleEncoding.Dispose();
return result;
try
{
var result = new EncodedAbiArg(allocator);
using var k = new UInt16((ushort)value.Length)
.Encode(AbiType.UIntN(16), references, Allocator.Persistent);
result.Bytes.AddRange(k.Bytes);
result.Bytes.AddRange(tupleEncoding.Bytes);
return result;
}
finally
{
tupleEncoding.Dispose();
}
}

/// <inheritdoc />
Expand Down
47 changes: 36 additions & 11 deletions Runtime/CareBoo.AlgoSdk/Experimental/Abi/Values/Tuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ struct Encoder<U>

byte boolShift;

Optional<int> headBytesTotalLength;

public Encoder(T args, U types, AbiReferences references, Allocator allocator)
{
this.args = args;
Expand All @@ -115,6 +117,7 @@ public Encoder(T args, U types, AbiReferences references, Allocator allocator)
this.tailBytes = new NativeList<byte>(allocator);
this.result = new EncodedAbiArg(allocator);
this.boolShift = 0;
this.headBytesTotalLength = default;
}

public EncodedAbiArg Encode(Allocator allocator)
Expand Down Expand Up @@ -183,18 +186,14 @@ void EncodeStaticHead(int t)

void EncodeDynamicHead(int t)
{
boolShift = 0;
var offset = headBytes.Length + tailBytes.Length;
var args = this.args;
do
if (!headBytesTotalLength.HasValue)
{
var type = types[t];
offset += type.IsStatic
? args.LengthOfCurrent(types[t])
: 2
;
t++;
} while (args.TryNext(out args));
headBytesTotalLength = CountHeadBytesLength(t);
}
var ptr = headBytesTotalLength.Value + tailBytes.Length;
var offset = headBytes.Length;
headBytes.Length += 2;
Endianness.CopyToNativeBytesBigEndian((ushort)ptr, ref headBytes, offset);
}

void EncodeBoolHead(int t)
Expand Down Expand Up @@ -224,6 +223,32 @@ void EncodeDynamicTail(int t)
using var bytes = args.EncodeCurrent(types[t], references, Allocator.Persistent);
tailBytes.AddRange(bytes.Bytes);
}

int CountHeadBytesLength(int t)
{
var totalLength = headBytes.Length;
var args = this.args;
do
{
var type = types[t];
if (type.Name == "bool")
{
boolShift %= 8;
if (boolShift == 0)
totalLength++;
boolShift++;
}
else
{
boolShift = 0;
totalLength += type.IsStatic
? args.LengthOfCurrent(type)
: 2;
}
t++;
} while (args.TryNext(out args));
return totalLength;
}
}
}

Expand Down

0 comments on commit be125d3

Please sign in to comment.