public void TestRpcSpeed() { CoreMethods.SetLogger((level, file, line, message) => { //Console.Error.WriteLine(message); }, 0); var def = new NtRpcDefinition(1, "myfunc1", new[] { new NtRpcParamDef("param1", RpcValue.MakeDouble(0.0)) }, new[] { new NtRpcResultDef("result1", NtType.Double) }); RemoteProcedureCall.CreateRpc("func1", def, Callback1); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 10000; ++i) { uint call1Uid = RemoteProcedureCall.CallRpc("func1", RpcValue.MakeDouble(i)); byte[] call1Result = RemoteProcedureCall.GetRpcResult(true, call1Uid); var res = RemoteProcedureCall.UnpackRpcValues(call1Result, NtType.Double); Assert.AreNotEqual(0, res.Count, "RPC Result empty"); } sw.Stop(); Console.WriteLine(sw.Elapsed); }
private static object FromRpcValue <T>(RpcValue value) { var type = typeof(T); if (type.IsAssignableFrom(typeof(bool))) { return(value.ToBoolean()); } else if (type.IsAssignableFrom(typeof(string))) { return(value.ToString()); } else if (type.IsAssignableFrom(typeof(BigInteger))) { return(value.ToInteger()); } else if (type.IsAssignableFrom(typeof(byte[]))) { return(value.ToByteArray()); } else if (type.IsAssignableFrom(typeof(Bytes))) { return(value.ToBytes()); } else if (type.IsAssignableFrom(typeof(Address))) { return(value.ToAddress()); } else if (type.IsAssignableFrom(typeof(RpcItem))) { return(value); } return(null); }
public TestWireDecoder() { List <string> sa = new List <string>(); for (int i = 0; i < 255; i++) { sa.Add("h"); } v_stringArrayBig = RpcValue.MakeStringArray(sa.ToArray()); StringBuilder builder = new StringBuilder(); for (int i = 0; i < 127; i++) { builder.Append('*'); } builder.Append('x'); s_long = builder.ToString(); builder.Clear(); for (int i = 0; i < 65534; i++) { builder.Append('*'); } builder.Append('x'); builder.Append('x'); builder.Append('x'); s_big = builder.ToString(); }
public void TestRawValue() { byte[] rawData = new byte[] { 0x05, (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', 0x03, (byte)'b', (byte)'y', (byte)'e', 0x55, }; WireDecoder d = new WireDecoder(rawData); var val = d.ReadValue(NtType.Raw); Assert.That(val.Type, Is.EqualTo(NtType.Raw)); Assert.That(val.Value, Is.EqualTo(v_raw.Value)); var vFalse = RpcValue.MakeRaw((byte)'b', (byte)'y', (byte)'e'); val = d.ReadValue(NtType.Raw); Assert.That(val.Type, Is.EqualTo(NtType.Raw)); Assert.That(val.Value, Is.EqualTo(vFalse.Value)); byte b = 0; Assert.That(d.Read8(ref b)); Assert.That(b, Is.EqualTo(0x55)); Assert.That(d.ReadValue(NtType.Raw), Is.Null); Assert.That(d.Error, Is.Null); }
public RpcValueTest() { _plainStringValue = new RpcValue("string value"); _bytesValue = new RpcValue(new byte[] { 1, 2, 3, 4, 5 }); _oddIntegerValue = new RpcValue(BigInteger.Parse("1234")); _evenIntegerValue = new RpcValue(BigInteger.Parse("61731")); _booleanValue = new RpcValue(true); }
public int GetValueSize(RpcValue value) { if (value == null) { return(0); } int size; switch (value.Type) { case NtType.Boolean: return(1); case NtType.Double: return(8); case NtType.Raw: return(GetRawSize((byte[])value.Value)); case NtType.Rpc: case NtType.String: return(GetStringSize((string)value.Value)); case NtType.BooleanArray: size = ((bool[])value.Value).Length; if (size > 0xff) { size = 0xff; } return(1 + size); case NtType.DoubleArray: size = ((double[])value.Value).Length; if (size > 0xff) { size = 0xff; } return(1 + size * 8); case NtType.StringArray: string[] v = (string[])value.Value; size = v.Length; if (size > 0xff) { size = 0xff; } int len = 1; for (int i = 0; i < size; i++) { len += GetStringSize(v[i]); } return(len); default: return(0); } }
public void TestWriteBooleanValue() { WireEncoder e = new WireEncoder(); e.WriteValue(v_boolean); var v_false = RpcValue.MakeBoolean(false); e.WriteValue(v_false); Assert.That(e.Error, Is.Null); Assert.That(e.Buffer.Length, Is.EqualTo(2)); Assert.That(e.Buffer, Is.EquivalentTo(new byte[] { 0x01, 0x00 })); }
private byte[] Callback2(string names, byte[] paramsStr) { var param = RemoteProcedureCall.UnpackRpcValues(paramsStr, NtType.Boolean, NtType.BooleanArray, NtType.Double, NtType.DoubleArray, NtType.Raw, NtType.String, NtType.StringArray); if (param.Count == 0) { Console.Error.WriteLine("Empty Params?"); return(new byte[] { 0 }); } return(RemoteProcedureCall.PackRpcValues(RpcValue.MakeBoolean(true), RpcValue.MakeBooleanArray(new[] { true, false }), RpcValue.MakeDouble(2.2), RpcValue.MakeDoubleArray(new[] { 2.8, 6.876 }), RpcValue.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 }), RpcValue.MakeString("NewString"), RpcValue.MakeStringArray(new[] { "String1", "String2" }))); }
private byte[] Callback1(string names, byte[] paramsStr) { var param = RemoteProcedureCall.UnpackRpcValues(paramsStr, NtType.Double); if (param.Count == 0) { Console.Error.WriteLine("Empty Params?"); return(new byte[] { 0 }); } double val = (double)param[0].Value; //Console.WriteLine($"Called with {val}"); return(RemoteProcedureCall.PackRpcValues(RpcValue.MakeDouble(val + 1.2))); }
public void TestBooleanValue() { byte[] rawData = new byte[] { 0x01, 0x00 }; WireDecoder d = new WireDecoder(rawData); var val = d.ReadValue(NtType.Boolean); Assert.That(val.Type, Is.EqualTo(NtType.Boolean)); Assert.That(val.Value, Is.EqualTo(v_boolean.Value)); var vFalse = RpcValue.MakeBoolean(false); val = d.ReadValue(NtType.Boolean); Assert.That(val.Type, Is.EqualTo(NtType.Boolean)); Assert.That(val.Value, Is.EqualTo(vFalse.Value)); Assert.That(d.ReadValue(NtType.Boolean), Is.Null); Assert.That(d.Error, Is.Null); }
public void TestAsInteger() { Assert.Throws <RpcValueException>(() => _plainStringValue.ToInteger()); Assert.Equal(BigInteger.Parse("0102030405", NumberStyles.HexNumber), _bytesValue.ToInteger()); Assert.Equal(BigInteger.Parse("1234"), _oddIntegerValue.ToInteger()); var intValue = _evenIntegerValue.ToInteger(); Assert.Equal(BigInteger.Parse("61731"), intValue); Assert.Equal(BigInteger.Parse("1"), _booleanValue.ToInteger()); var minusHex = new RpcValue("-0x4d2"); Assert.Equal(BigInteger.Parse("-1234"), minusHex.ToInteger()); var plusHex = new RpcValue("+0x4d2"); Assert.Throws <RpcValueException>(() => plusHex.ToInteger()); }
private BigInteger ConvertHex(RpcValue value) { // The value of 'value' and nonce in v2 specs is a decimal string. // But there are hex strings without 0x in v2 blocks. // // This method converts the value as hex no matter it has 0x prefix or not. var stringValue = value.ToString(); var sign = ""; if (stringValue[0] == '-') { sign = stringValue.Substring(0, 1); stringValue = stringValue.Substring(1); } return(BigInteger.Parse(sign + Bytes.CleanHexPrefix(stringValue), NumberStyles.HexNumber)); }
public void TestReadBooleanArray() { byte[] b = { 0x03, 0x00, 0x01, 0x00, 0x02, 0x01, 0x00, 0xff }; WireDecoder d = new WireDecoder(b); var val = d.ReadValue(NtType.BooleanArray); Assert.That(val.Type, Is.EqualTo(NtType.BooleanArray)); Assert.That(val.Value, Is.EqualTo(v_boolArray.Value)); var boolArray2 = RpcValue.MakeBooleanArray(true, false); val = d.ReadValue(NtType.BooleanArray); Assert.That(val.Type, Is.EqualTo(NtType.BooleanArray)); Assert.That(val.Value, Is.EqualTo(boolArray2.Value)); Assert.That(d.ReadValue(NtType.BooleanArray), Is.Null); Assert.That(d.Error, Is.Null); }
public void TestRpcAllTypes() { CoreMethods.SetLogger((level, file, line, message) => { //Console.Error.WriteLine(message); }, 0); var def = new NtRpcDefinition(1, "myfunc1", new[] { new NtRpcParamDef("param1", RpcValue.MakeBoolean(true)), new NtRpcParamDef("param2", RpcValue.MakeBooleanArray(new[] { true, false })), new NtRpcParamDef("param3", RpcValue.MakeDouble(0.0)), new NtRpcParamDef("param4", RpcValue.MakeDoubleArray(new[] { 2.8, 6.87 })), new NtRpcParamDef("param5", RpcValue.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 })), new NtRpcParamDef("param6", RpcValue.MakeString("NewString")), new NtRpcParamDef("param7", RpcValue.MakeStringArray(new[] { "String1", "String2" })), }, new[] { new NtRpcResultDef("result1", NtType.Boolean), new NtRpcResultDef("result2", NtType.BooleanArray), new NtRpcResultDef("result3", NtType.Double), new NtRpcResultDef("result4", NtType.DoubleArray), new NtRpcResultDef("result5", NtType.Raw), new NtRpcResultDef("result6", NtType.String), new NtRpcResultDef("result7", NtType.StringArray), }); RemoteProcedureCall.CreateRpc("func1", def, Callback2); Console.WriteLine("Calling RPC"); uint call1Uid = RemoteProcedureCall.CallRpc("func1", RpcValue.MakeBoolean(true), RpcValue.MakeBooleanArray(new[] { true, false }), RpcValue.MakeDouble(2.2), RpcValue.MakeDoubleArray(new[] { 2.8, 6.87 }), RpcValue.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 }), RpcValue.MakeString("NewString"), RpcValue.MakeStringArray(new[] { "String1", "String2" })); Console.WriteLine("Waiting for RPC Result"); byte[] result = RemoteProcedureCall.GetRpcResult(true, call1Uid); var call1Result = RemoteProcedureCall.UnpackRpcValues(result, NtType.Boolean, NtType.BooleanArray, NtType.Double, NtType.DoubleArray, NtType.Raw, NtType.String, NtType.StringArray); Assert.AreNotEqual(0, call1Result.Count, "RPC Result empty"); Console.WriteLine(call1Result[0].ToString()); }
private BigInteger ConvertDecimal(RpcValue value) { // The value of timestamp and nonce in v2 specs is a decimal string. // But there are decimal strings, numbers and 0x included hex strings in v2 blocks. // e.g.) "12345", 12345, "0x12345" // // RpcValue class converts numbers and 0x included hex strings to 0x included hex string // and holds it // // So, stringValue is a decimal string or a 0x included hex string.("12345", "0x12345") // if it has 0x, the method converts it as hex otherwise decimal var stringValue = value.ToString(); if (stringValue.StartsWith(Bytes.HexPrefix) || stringValue.StartsWith("-" + Bytes.HexPrefix)) { return(ConvertHex(value)); } else { return(BigInteger.Parse(stringValue)); } }
public void TestRpcLocal() { CoreMethods.SetLogger((level, file, line, message) => { //Console.Error.WriteLine(message); }, 0); var def = new NtRpcDefinition(1, "myfunc1", new[] { new NtRpcParamDef("param1", RpcValue.MakeDouble(0.0)) }, new[] { new NtRpcResultDef("result1", NtType.Double) }); RemoteProcedureCall.CreateRpc("func1", def, Callback1); Console.WriteLine("Calling RPC"); uint call1Uid = RemoteProcedureCall.CallRpc("func1", RpcValue.MakeDouble(2.0)); Console.WriteLine("Waiting for RPC Result"); byte[] result = RemoteProcedureCall.GetRpcResult(true, call1Uid); var call1Result = RemoteProcedureCall.UnpackRpcValues(result, NtType.Double); Assert.AreNotEqual(0, call1Result.Count, "RPC Result empty"); Console.WriteLine(call1Result[0].ToString()); }
public int GetValueSize(RpcValue value) { if (value == null) return -1; int size; switch (value.Type) { case NtType.Boolean: return 1; case NtType.Double: return 8; case NtType.Raw: return GetRawSize((byte[])value.Value); case NtType.Rpc: case NtType.String: return GetStringSize((string)value.Value); case NtType.BooleanArray: size = ((bool[])value.Value).Length; if (size > 0xff) size = 0xff; return 1 + size; case NtType.DoubleArray: size = ((double[])value.Value).Length; if (size > 0xff) size = 0xff; return 1 + size * 8; case NtType.StringArray: string[] v = (string[])value.Value; size = v.Length; if (size > 0xff) size = 0xff; int len = 1; for (int i = 0; i < size; i++) { len += GetStringSize(v[i]); } return len; default: throw new ArgumentOutOfRangeException(); } }
public void TestRpcSerializer() { RpcItem intValue = new RpcValue(BigInteger.Parse("1234")); RpcItem booleanValue = new RpcValue(false); RpcItem stringValue = new RpcValue("string"); RpcItem bytesValue = new RpcValue(new byte[] { 0x1, 0x2, 0x3 }); RpcItem escapeValue = new RpcValue("\\.{}[]"); RpcItem @object = new RpcObject.Builder() .Put("intValue", intValue) .Put("booleanValue", booleanValue) .Put("stringValue", stringValue) .Put("bytesValue", bytesValue) .Put("escapeValue", escapeValue) .Build(); RpcItem array = new RpcArray.Builder() .Add(@object) .Add(intValue) .Add(booleanValue) .Add(stringValue) .Add(bytesValue) .Build(); RpcItem root = new RpcObject.Builder() .Put("object", @object) .Put("array", array) .Put("intValue", intValue) .Put("booleanValue", booleanValue) .Put("stringValue", stringValue) .Put("bytesValue", bytesValue) .Build(); var json = JsonConvert.SerializeObject(root, Formatting.Indented, new RpcItemSerializer()); Assert.True(json.Length > 2); }
public void WriteValue(RpcValue value) { if (value == null) return; switch (value.Type) { case NtType.Boolean: Write8((bool)value.Value ? (byte)1 : (byte)0); break; case NtType.Double: WriteDouble((double)value.Value); break; case NtType.Raw: WriteRaw((byte[])value.Value); break; case NtType.String: case NtType.Rpc: WriteString((string)value.Value); break; case NtType.BooleanArray: var vB = (bool[])value.Value; int sizeB = vB.Length; if (sizeB > 0xff) sizeB = 0xff; Write8((byte)sizeB); for (int i = 0; i < sizeB; i++) { Write8(vB[i] ? (byte)1 : (byte)0); } break; case NtType.DoubleArray: var vD = (double[])value.Value; int sizeD = vD.Length; if (sizeD > 0xff) sizeD = 0xff; for (int i = 0; i < sizeD; i++) { WriteDouble(vD[i]); } break; case NtType.StringArray: var vS = (string[])value.Value; int sizeS = vS.Length; if (sizeS > 0xff) sizeS = 0xff; for (int i = 0; i < sizeS; i++) { WriteString(vS[i]); } break; default: Console.WriteLine("unrecognized type when writing value"); return; } }
public void WriteValue(RpcValue value) { if (value == null) { Error = "Value cannot be null"; return; } switch (value.Type) { case NtType.Boolean: Write8((bool)value.Value ? (byte)1 : (byte)0); break; case NtType.Double: WriteDouble((double)value.Value); break; case NtType.Raw: WriteRaw((byte[])value.Value); break; case NtType.String: case NtType.Rpc: WriteString((string)value.Value); break; case NtType.BooleanArray: var vB = (bool[])value.Value; int sizeB = vB.Length; if (sizeB > 0xff) { sizeB = 0xff; } Write8((byte)sizeB); for (int i = 0; i < sizeB; i++) { Write8(vB[i] ? (byte)1 : (byte)0); } break; case NtType.DoubleArray: var vD = (double[])value.Value; int sizeD = vD.Length; if (sizeD > 0xff) { sizeD = 0xff; } Write8((byte)sizeD); for (int i = 0; i < sizeD; i++) { WriteDouble(vD[i]); } break; case NtType.StringArray: var vS = (string[])value.Value; int sizeS = vS.Length; if (sizeS > 0xff) { sizeS = 0xff; } Write8((byte)sizeS); for (int i = 0; i < sizeS; i++) { WriteString(vS[i]); } break; default: Error = "unrecognized type when writing value"; Console.WriteLine("unrecognized type when writing value"); return; } Error = null; }
public RpcValue ReadValue(NtType type) { byte size = 0; byte[] buf; Error = null; switch (type) { case NtType.Boolean: byte vB = 0; return(!Read8(ref vB) ? null : RpcValue.MakeBoolean(vB != 0)); case NtType.Double: double vD = 0; return(!ReadDouble(ref vD) ? null : RpcValue.MakeDouble(vD)); case NtType.Raw: byte[] vRa = null; return(!ReadRaw(ref vRa) ? null : RpcValue.MakeRaw(vRa)); case NtType.Rpc: case NtType.String: string vS = ""; return(!ReadString(ref vS) ? null : RpcValue.MakeString(vS)); case NtType.BooleanArray: if (!Read8(ref size)) { return(null); } buf = ReadArray(size); if (buf == null) { return(null); } bool[] bBuf = new bool[buf.Length]; for (int i = 0; i < buf.Length; i++) { bBuf[i] = buf[i] != 0; } return(RpcValue.MakeBooleanArray(bBuf)); case NtType.DoubleArray: if (!Read8(ref size)) { return(null); } buf = ReadArray(size * 8); if (buf == null) { return(null); } double[] dBuf = new double[size]; for (int i = 0; i < size; i++) { dBuf[i] = ReadDouble(buf, i * 8); } return(RpcValue.MakeDoubleArray(dBuf)); case NtType.StringArray: if (!Read8(ref size)) { return(null); } string[] sBuf = new string[size]; for (int i = 0; i < size; i++) { if (!ReadString(ref sBuf[i])) { return(null); } } return(RpcValue.MakeStringArray(sBuf)); default: Error = "invalid type when trying to read value"; Console.WriteLine("invalid type when trying to read value"); return(null); } }