public void ShouldBeVerifiable(int repeats) { for (int i = 0; i < repeats; i++) { PrivateKey key = PrivateKey.CreateKey(); PrivateKey wrongKey = key.CKD(key.Bytes); byte[] rndBytes = RndProvider.GetNonZeroBytes(1024); Payload body = new Payload.Builder() .Value("rndBytes", rndBytes) .Build(); Message message = new Message.Builder() .WithBody(body) .Build(key); // serialize & deserialize Message received = Message.Parse(message.ToBytes()); Assert.True(received.Verify(key.Address)); Assert.False(received.Verify(wrongKey.Address)); // verify failed if body has changed received.Body.Set("rndBytes", RndProvider.GetNonZeroBytes(128)); Assert.False(received.Verify(key.Address)); } }
public void ShouldBeSerializable(int repeats) { for (int i = 0; i < repeats; i++) { PrivateKey key = PrivateKey.CreateKey(); byte[] rndBytes = RndProvider.GetNonZeroBytes(1024); Payload body = new Payload.Builder() .Value("rndBytes", rndBytes) .Build(); Message message = new Message.Builder() .WithBody(body) .Build(key); // serialize & deserialize Message received = Message.Parse(message.ToBytes()); Assert.Equal(message.Hash, received.Hash); Assert.Equal(rndBytes, message.Value <byte[]>("rndBytes")); Assert.Equal(rndBytes, received.Value <byte[]>("rndBytes")); Assert.Equal(message.Value <byte[]>("rndBytes"), received.Value <byte[]>("rndBytes")); Assert.Equal(message.Headers.Hash, received.Headers.Hash); Assert.Equal(message.Body.Hash, received.Body.Hash); } }
public void ShouldBeModifiable(int repeats) { for (int i = 0; i < repeats; i++) { PrivateKey key = PrivateKey.CreateKey(); byte[] rndBytes = RndProvider.GetNonZeroBytes(1024); Payload body = new Payload.Builder() .Value("rndBytes", rndBytes) .Build(); Message message = new Message.Builder() .WithBody(body) .Build(key); // serialize & deserialize Message received = Message.Parse(message.ToBytes()); Assert.True(received.Verify(key.Address)); // verify ok if header changed received.Headers.Set("timestamp", DateTime.Now); Assert.True(received.Verify(key.Address)); // verify failed if body changed received.Body.Set("id", RndProvider.Next(100)); Assert.False(received.Verify(key.Address)); } }
public void ShouldBeAbleToOverlap() { Payload header = new Payload.Builder() .Value("ver", 0x01) .Build(); Payload body = new Payload.Builder() .Value("message", "Hello, Bryllite!") .Value("rndBytes", RndProvider.GetNonZeroBytes(128)) .Build(); Payload message = new Payload.Builder() .Value("header", header) .Value("body", body) .Build(); Payload received = Payload.Parse(message.ToBytes()); Payload receivedHeader = received.Value <Payload>("header"); Payload receivedBody = received.Value <Payload>("body"); Assert.Equal(message.Hash, received.Hash); Assert.Equal(header.Hash, receivedHeader.Hash); Assert.Equal(body.Hash, receivedBody.Hash); Assert.Equal(header.Value <int>("ver"), receivedHeader.Value <int>("ver")); Assert.Equal(body.Value <string>("message"), receivedBody.Value <string>("message")); Assert.Equal(body.Value <byte[]>("rndBytes"), receivedBody.Value <byte[]>("rndBytes")); }
public void ShouldBeSerializable() { string name = RndProvider.GetString(16); int age = RndProvider.Next(1, 100); byte[] rndBytes = RndProvider.GetNonZeroBytes(128); // build Payload payload = new Payload.Builder() .Value("name", name) .Value("age", age) .Value("rndBytes", rndBytes) .Build(); // serialize & deserialize Payload received = Payload.Parse(payload.ToBytes()); // assert Assert.Equal(name, received.Value <string>("name")); Assert.Equal(age, received.Value <int>("age")); Assert.True(rndBytes.SequenceEqual(received.Value <byte[]>("rndBytes"))); Assert.Equal(payload.Hash.ToHex(), received.Hash.ToHex()); Assert.Equal(payload.Hash, received.Hash); }
public void Test2() { PrivateKey key = PrivateKey.CreateKey(); ElasticLayout layout = ElasticLayout.DefineLayout(1024); Elastic3D to = new Elastic3D((byte)(1 + RndProvider.Next(layout.X)), (byte)(1 + RndProvider.Next(layout.Y)), (byte)(1 + RndProvider.Next(layout.Z))); Message message = new Message.Builder() .Body("rndBytes", RndProvider.GetNonZeroBytes(128)) .Build(key); PrivateKey routerKey = PrivateKey.CreateKey(); message.RouteTo(to.TimeToLive(), to, layout, routerKey); Payload routes = message.Routes(); Assert.Equal(to.TimeToLive(), routes.Value <byte>("ttl")); Assert.Equal(to, routes.Value <Elastic3D>("to")); Assert.Equal(layout, routes.Value <ElasticLayout>("layout")); Address router = message.Router(); Assert.Equal(routerKey.Address, router); Assert.True(message.VerifyRouter()); message.TimeToLive(0); Assert.Equal(0, message.TimeToLive()); Assert.Equal(0, new Elastic3D(0, 0, 0).TimeToLive()); Assert.Equal(1, new Elastic3D(16, 0, 0).TimeToLive()); Assert.Equal(2, new Elastic3D(13, 15, 0).TimeToLive()); Assert.Equal(3, new Elastic3D(1, 2, 3).TimeToLive()); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); ILoggable logger = new BLog.Builder() .WithConsole(true) .WithFilter(LogFilter.All) .Build(); using (CancellationTokenSource cts = new CancellationTokenSource()) { Console.CancelKeyPress += (sender, e) => { e.Cancel = true; cts.Cancel(); }; while (!cts.IsCancellationRequested) { // Thread.Sleep(100); Payload payload = new Payload.Builder() .Value("rndBytes", RndProvider.GetNonZeroBytes(MessageLength)) .Build(); TcpClient.SendTo("127.0.0.1", PORT, payload.ToBytes()); } } }
public void ShouldBeParsableElastic3D() { PrivateKey messageKey = PrivateKey.CreateKey(); Elastic3D to = new Elastic3D(1, 2, 3); ElasticLayout layout = new ElasticLayout(16, 16, 16); Message message = new Message.Builder() .Body("rndBytes", RndProvider.GetNonZeroBytes(32)) .Build(messageKey); }
public void DateTimeTest() { PrivateKey key = PrivateKey.CreateKey(); Message ping = new Message.Builder() .Body("rndBytes", RndProvider.GetNonZeroBytes(32)) .Build(key); Message received = Message.Parse(ping.ToBytes()); Assert.Equal(ping.TimeStamp, received.TimeStamp); }
static void Main(string[] args) { ILoggable logger = new BLog.Builder() .WithConsole(true) .WithFilter(LogFilter.All) .Build(); PrivateKey key = PrivateKey.CreateKey(); using (CancellationTokenSource cts = new CancellationTokenSource()) { NodeServer server = new NodeServer(logger); server.Start(PORT, cts); Console.CancelKeyPress += (sender, e) => { e.Cancel = true; cts.Cancel(); }; byte[] rndBytes = RndProvider.GetNonZeroBytes(1024 * 1024); while (!cts.IsCancellationRequested) { Thread.Sleep(10); rndBytes = RndProvider.GetNonZeroBytes(1024 * 1024); Message message = new Message.Builder() .Body("rndBytes", rndBytes) .Build(key); byte[] data = message.ToBytes(); // ElasticNode.SendTo(data, "127.0.0.1", PORT); NodeClient client = new NodeClient(logger); client.Start("127.0.0.1", PORT); Thread.Sleep(1000); // client.Write(data); client.Stop(); } } }
public void Main() { byte n = Env.Value("n", DEFAULT_N); int nTimes = Env.Value("nTimes", DEFAULT_NTIMES); int msgKBytes = Env.Value("msgKBytes", DEFAULT_MSGKBYTES); int nPeers = Peers.Count; int nonce = 0; int waitTime = Math.Min(60000, Math.Max(1000, nPeers * 32)); new BConsole.MessageBuilder() .Append(DarkGreen, $"[{TimeCode}] ") .Append(Blue, ">> TEST STARTED!") .Append(" n=") .Append(DarkGreen, n) .Append(", nPeers=") .Append(DarkGreen, nPeers) .Append(", nTimes=") .Append(DarkGreen, nTimes) .Append(", msgKBytes=") .Append(DarkGreen, msgKBytes) .Append(", waitTime=") .Append(DarkGreen, waitTime) .WriteLine("(ms)"); // 보고서 초기화 Reports.Start(); // 5초에 한번씩 전체 메세지 전송한다. Stopwatch sw = Stopwatch.StartNew(); while (!cts.IsCancellationRequested && nonce++ < nTimes) { byte[] rndBytes = RndProvider.GetNonZeroBytes(msgKBytes * 1024); // message Message message = new Message.Builder() .Action("ping") .Body("nonce", nonce) .Body("rndBytes", rndBytes) .Build(NodeKey); ElasticLayout layout = ElasticLayout.DefineLayout(Peers.Count, n); Elastic3D me = layout.DefineCoordinates(NodeKey.Address); // new report item for this message TestReports.ReportItem report = Reports.NewItem(message, Peers.Count); new BConsole.MessageBuilder() .Append(">> Sending message[nonce=") .Append(Cyan, nonce) .Append("](") .Append(DarkCyan, message.ID.Ellipsis()) .Append("): message.length=") .Append(DarkGreen, message.Length) .Append(", n=") .Append(DarkGreen, n) .Append(", layout=") .Append(DarkGreen, layout) .Append(", host.coordinates=") .Append(DarkGreen, me) .WriteLine(); // send message to all peers SendAll(message, n); // wait for send message complete sw.Restart(); while (!report.ReceivedAll && sw.ElapsedMilliseconds < waitTime * 3 && !cts.IsCancellationRequested) { Thread.Sleep(10); } // message result new BConsole.MessageBuilder() .Append(">> message[nonce=") .Append(Cyan, nonce) .Append("](") .Append(DarkCyan, message.ID.Ellipsis()) .Append(") result=") .WriteLine(report.ToString()); // waiting for next message if (nonce < nTimes) { BConsole.WriteLine(ConsoleColor.White, "Waiting for next message..."); while (waitTime - (int)sw.ElapsedMilliseconds > 0 && !cts.IsCancellationRequested) { Thread.Sleep(10); } } } Report(); }
public static PrivateKey CreateKey() { byte[] bytes = RndProvider.GetNonZeroBytes(KEY_BYTES); return(Secp256k1.SecretKeyVerify(bytes) ? new PrivateKey(bytes) : CreateKey()); }