Skip to content

Rename Address to ConnectionId #2220

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

Merged
merged 22 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions crates/bindings-csharp/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"dotnet-csharpier"
],
"rollForward": false
},
"verify.tool": {
"version": "0.6.0",
"commands": [
"dotnet-verify"
],
"rollForward": false
}
}
}
40 changes: 20 additions & 20 deletions crates/bindings-csharp/BSATN.Runtime.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ namespace SpacetimeDB;
public static class BSATNRuntimeTests
{
[Fact]
public static void AddressRoundtrips()
public static void ConnectionIdRoundtrips()
{
var str = "00112233445566778899AABBCCDDEEFF";
var addr = Address.FromHexString(str);
var connId = ConnectionId.FromHexString(str);

Assert.NotNull(addr);
Assert.Equal(addr.ToString(), str);
Assert.NotNull(connId);
Assert.Equal(connId.ToString(), str);

var bytes = Convert.FromHexString(str);

var addr2 = Address.FromBigEndian(bytes);
Assert.Equal(addr2, addr);
var connId2 = ConnectionId.FromBigEndian(bytes);
Assert.Equal(connId2, connId);

Array.Reverse(bytes);
var addr3 = Address.From(bytes);
Assert.Equal(addr3, addr);
var connId3 = ConnectionId.From(bytes);
Assert.Equal(connId3, connId);

var memoryStream = new MemoryStream();
var bsatn = new Address.BSATN();
var bsatn = new ConnectionId.BSATN();
using (var writer = new BinaryWriter(memoryStream))
{
if (addr is { } addrNotNull)
if (connId is { } connIdNotNull)
{
bsatn.Write(writer, addrNotNull);
bsatn.Write(writer, connIdNotNull);
}
else
{
Expand All @@ -39,26 +39,26 @@ public static void AddressRoundtrips()

var littleEndianBytes = memoryStream.ToArray();
var reader = new BinaryReader(new MemoryStream(littleEndianBytes));
var addr4 = bsatn.Read(reader);
Assert.Equal(addr4, addr);
var connId4 = bsatn.Read(reader);
Assert.Equal(connId4, connId);

// Note: From = FromLittleEndian
var addr5 = Address.From(littleEndianBytes);
Assert.Equal(addr5, addr);
var connId5 = ConnectionId.From(littleEndianBytes);
Assert.Equal(connId5, connId);
}

static readonly Gen<string> genHex = Gen.String[Gen.Char["0123456789abcdef"], 0, 128];

[Fact]
public static void AddressLengthCheck()
public static void ConnectionIdLengthCheck()
{
genHex.Sample(s =>
{
if (s.Length == 32)
{
return;
}
Assert.ThrowsAny<Exception>(() => Address.FromHexString(s));
Assert.ThrowsAny<Exception>(() => ConnectionId.FromHexString(s));
});
Gen.Byte.Array[0, 64]
.Sample(arr =>
Expand All @@ -67,8 +67,8 @@ public static void AddressLengthCheck()
{
return;
}
Assert.ThrowsAny<Exception>(() => Address.FromBigEndian(arr));
Assert.ThrowsAny<Exception>(() => Address.From(arr));
Assert.ThrowsAny<Exception>(() => ConnectionId.FromBigEndian(arr));
Assert.ThrowsAny<Exception>(() => ConnectionId.From(arr));
});
}

Expand Down Expand Up @@ -136,7 +136,7 @@ public static void NonHexStrings()
{
// n.b. 32 chars long
Assert.ThrowsAny<Exception>(
() => Address.FromHexString("these are not hex characters....")
() => ConnectionId.FromHexString("these are not hex characters....")
);
}

Expand Down
49 changes: 25 additions & 24 deletions crates/bindings-csharp/BSATN.Runtime/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,68 +118,69 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
}

[StructLayout(LayoutKind.Sequential)]
public readonly record struct Address
public readonly record struct ConnectionId
{
private readonly U128 value;

internal Address(U128 v) => value = v;
internal ConnectionId(U128 v) => value = v;

/// <summary>
/// Create an Address from a LITTLE-ENDIAN byte array.
/// Create a ConnectionId from a LITTLE-ENDIAN byte array.
///
/// If you are parsing an Address from a string, you probably want FromHexString instead,
/// If you are parsing a ConnectionId from a string, you probably want FromHexString instead,
/// or, failing that, FromBigEndian.
///
/// Returns null if the resulting address is the default.
/// Returns null if the resulting ConnectionId is the default.
/// </summary>
/// <param name="bytes"></param>
public static Address? From(ReadOnlySpan<byte> bytes)
public static ConnectionId? From(ReadOnlySpan<byte> bytes)
{
var addr = Util.Read<Address>(bytes, littleEndian: true);
return addr == default ? null : addr;
var id = Util.Read<ConnectionId>(bytes, littleEndian: true);
return id == default ? null : id;
}

/// <summary>
/// Create an Address from a BIG-ENDIAN byte array.
/// Create a ConnectionId from a BIG-ENDIAN byte array.
///
/// This method is the correct choice if you have converted the bytes of a hexadecimal-formatted Address
/// This method is the correct choice if you have converted the bytes of a hexadecimal-formatted ConnectionId
/// to a byte array in the following way:
///
/// "0xb0b1b2..."
/// ->
/// [0xb0, 0xb1, 0xb2, ...]
///
/// Returns null if the resulting address is the default.
/// Returns null if the resulting ConnectionId is the default.
/// </summary>
/// <param name="bytes"></param>
public static Address? FromBigEndian(ReadOnlySpan<byte> bytes)
public static ConnectionId? FromBigEndian(ReadOnlySpan<byte> bytes)
{
var addr = Util.Read<Address>(bytes, littleEndian: false);
return addr == default ? null : addr;
var id = Util.Read<ConnectionId>(bytes, littleEndian: false);
return id == default ? null : id;
}

/// <summary>
/// Create an Address from a hex string.
/// Create a ConnectionId from a hex string.
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public static Address? FromHexString(string hex) => FromBigEndian(Util.StringToByteArray(hex));
public static ConnectionId? FromHexString(string hex) =>
FromBigEndian(Util.StringToByteArray(hex));

public static Address Random()
public static ConnectionId Random()
{
var random = new Random();
var addr = new Address();
random.NextBytes(Util.AsBytes(ref addr));
return addr;
var id = new ConnectionId();
random.NextBytes(Util.AsBytes(ref id));
return id;
}

// --- auto-generated ---
public readonly struct BSATN : IReadWrite<Address>
public readonly struct BSATN : IReadWrite<ConnectionId>
{
public Address Read(BinaryReader reader) =>
public ConnectionId Read(BinaryReader reader) =>
new(new SpacetimeDB.BSATN.U128Stdb().Read(reader));

public void Write(BinaryWriter writer, Address value) =>
public void Write(BinaryWriter writer, ConnectionId value) =>
new SpacetimeDB.BSATN.U128Stdb().Write(writer, value.value);

// --- / auto-generated ---
Expand All @@ -190,7 +191,7 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
new AlgebraicType.Product(
[
// Using this specific name here is important.
new("__address__", new AlgebraicType.U128(default)),
new("__connection_id__", new AlgebraicType.U128(default)),
]
);
// --- / customized ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public partial struct PublicTable
public double DoubleField;
public string StringField;
public Identity IdentityField;
public Address AddressField;
public ConnectionId ConnectionIdField;
public CustomStruct CustomStructField;
public CustomClass CustomClassField;
public CustomEnum CustomEnumField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void ReadFields(System.IO.BinaryReader reader)
DoubleField = BSATN.DoubleField.Read(reader);
StringField = BSATN.StringField.Read(reader);
IdentityField = BSATN.IdentityField.Read(reader);
AddressField = BSATN.AddressField.Read(reader);
ConnectionIdField = BSATN.ConnectionIdField.Read(reader);
CustomStructField = BSATN.CustomStructField.Read(reader);
CustomClassField = BSATN.CustomClassField.Read(reader);
CustomEnumField = BSATN.CustomEnumField.Read(reader);
Expand Down Expand Up @@ -52,7 +52,7 @@ public void WriteFields(System.IO.BinaryWriter writer)
BSATN.DoubleField.Write(writer, DoubleField);
BSATN.StringField.Write(writer, StringField);
BSATN.IdentityField.Write(writer, IdentityField);
BSATN.AddressField.Write(writer, AddressField);
BSATN.ConnectionIdField.Write(writer, ConnectionIdField);
BSATN.CustomStructField.Write(writer, CustomStructField);
BSATN.CustomClassField.Write(writer, CustomClassField);
BSATN.CustomEnumField.Write(writer, CustomEnumField);
Expand Down Expand Up @@ -81,7 +81,7 @@ public void WriteFields(System.IO.BinaryWriter writer)
internal static readonly SpacetimeDB.BSATN.F64 DoubleField = new();
internal static readonly SpacetimeDB.BSATN.String StringField = new();
internal static readonly SpacetimeDB.Identity.BSATN IdentityField = new();
internal static readonly SpacetimeDB.Address.BSATN AddressField = new();
internal static readonly SpacetimeDB.ConnectionId.BSATN ConnectionIdField = new();
internal static readonly CustomStruct.BSATN CustomStructField = new();
internal static readonly CustomClass.BSATN CustomClassField = new();
internal static readonly SpacetimeDB.BSATN.Enum<CustomEnum> CustomEnumField = new();
Expand Down Expand Up @@ -128,7 +128,7 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar
new(nameof(DoubleField), DoubleField.GetAlgebraicType(registrar)),
new(nameof(StringField), StringField.GetAlgebraicType(registrar)),
new(nameof(IdentityField), IdentityField.GetAlgebraicType(registrar)),
new(nameof(AddressField), AddressField.GetAlgebraicType(registrar)),
new(nameof(ConnectionIdField), ConnectionIdField.GetAlgebraicType(registrar)),
new(nameof(CustomStructField), CustomStructField.GetAlgebraicType(registrar)),
new(nameof(CustomClassField), CustomClassField.GetAlgebraicType(registrar)),
new(nameof(CustomEnumField), CustomEnumField.GetAlgebraicType(registrar)),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public partial struct PublicTable
public double DoubleField;
public string StringField;
public Identity IdentityField;
public Address AddressField;
public ConnectionId ConnectionIdField;
public CustomStruct CustomStructField;
public CustomClass CustomClassField;
public CustomEnum CustomEnumField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ namespace SpacetimeDB
{
public sealed record ReducerContext : DbContext<Local>, Internal.IReducerContext
{
public readonly Identity CallerIdentity;
public readonly Address? CallerAddress;
public readonly Identity Sender;
public readonly ConnectionId? ConnectionId;
public readonly Random Rng;
public readonly Timestamp Timestamp;

// We need this property to be non-static for parity with client SDK.
public Identity Identity => Internal.IReducerContext.GetIdentity();

internal ReducerContext(Identity identity, Address? address, Random random, Timestamp time)
internal ReducerContext(
Identity identity,
ConnectionId? connectionId,
Random random,
Timestamp time
)
{
CallerIdentity = identity;
CallerAddress = address;
Sender = identity;
ConnectionId = connectionId;
Rng = random;
Timestamp = time;
}
Expand Down Expand Up @@ -1092,8 +1097,8 @@ public void Invoke(BinaryReader reader, SpacetimeDB.Internal.IReducerContext ctx
public static void Main()
{
SpacetimeDB.Internal.Module.SetReducerContextConstructor(
(identity, address, random, time) =>
new SpacetimeDB.ReducerContext(identity, address, random, time)
(identity, connectionId, random, time) =>
new SpacetimeDB.ReducerContext(identity, connectionId, random, time)
);

SpacetimeDB.Internal.Module.RegisterReducer<Init>();
Expand Down Expand Up @@ -1149,8 +1154,8 @@ public static SpacetimeDB.Internal.Errno __call_reducer__(
ulong sender_1,
ulong sender_2,
ulong sender_3,
ulong address_0,
ulong address_1,
ulong conn_id_0,
ulong conn_id_1,
SpacetimeDB.Timestamp timestamp,
SpacetimeDB.Internal.BytesSource args,
SpacetimeDB.Internal.BytesSink error
Expand All @@ -1161,8 +1166,8 @@ SpacetimeDB.Internal.BytesSink error
sender_1,
sender_2,
sender_3,
address_0,
address_1,
conn_id_0,
conn_id_1,
timestamp,
args,
error
Expand Down
Loading