public UnaryResult <bool> PutTransaction(byte[] txBytes) { try { Transaction <PolymorphicAction <ActionBase> > tx = Transaction <PolymorphicAction <ActionBase> > .Deserialize(txBytes); try { tx.Validate(); Log.Debug("PutTransaction: (nonce: {nonce}, id: {id})", tx.Nonce, tx.Id); Log.Debug("StagedTransactions: {txIds}", string.Join(", ", _blockChain.GetStagedTransactionIds())); _blockChain.StageTransaction(tx); _swarm.BroadcastTxs(new[] { tx }); return(UnaryResult(true)); } catch (InvalidTxException ite) { Log.Error(ite, $"{nameof(InvalidTxException)} occurred during {nameof(PutTransaction)}(). {{e}}", ite); return(UnaryResult(false)); } } catch (Exception e) { Log.Error(e, $"Unexpected exception occurred during {nameof(PutTransaction)}(). {{e}}", e); throw; } }
public async Task BroadcastTxWhileMining() { Swarm <DumbAction> swarmA = CreateSwarm(); var minerC = new PrivateKey(); Swarm <DumbAction> swarmC = CreateSwarm(minerC); BlockChain <DumbAction> chainA = swarmA.BlockChain; BlockChain <DumbAction> chainC = swarmC.BlockChain; var privateKey = new PrivateKey(); var address = privateKey.ToAddress(); var txCount = 10; var txs = Enumerable.Range(0, txCount).Select(_ => chainA.MakeTransaction( new PrivateKey(), new[] { new DumbAction(address, "foo") })) .ToArray(); try { await StartAsync(swarmA); await StartAsync(swarmC); await swarmC.AddPeersAsync(new[] { swarmA.AsPeer }, null); Assert.Contains(swarmC.AsPeer, swarmA.Peers); Assert.Contains(swarmA.AsPeer, swarmC.Peers); Task miningTask = Task.Run(async() => { for (var i = 0; i < 10; i++) { await chainC.MineBlock(minerC); } }); Task txReceivedTask = swarmC.TxReceived.WaitAsync(); for (var i = 0; i < 100; i++) { swarmA.BroadcastTxs(txs); } await txReceivedTask; await miningTask; for (var i = 0; i < txCount; i++) { Assert.NotNull(chainC.GetTransaction(txs[i].Id)); } } finally { await StopAsync(swarmA); await StopAsync(swarmC); } }
public async Task BroadcastTx() { Swarm <DumbAction> swarmA = _swarms[0]; Swarm <DumbAction> swarmB = _swarms[1]; Swarm <DumbAction> swarmC = _swarms[2]; BlockChain <DumbAction> chainA = _blockchains[0]; BlockChain <DumbAction> chainB = _blockchains[1]; BlockChain <DumbAction> chainC = _blockchains[2]; Transaction <DumbAction> tx = Transaction <DumbAction> .Create( 0, new PrivateKey(), new DumbAction[] { } ); chainA.StageTransactions( new Dictionary <Transaction <DumbAction>, bool> { { tx, true } }); chainA.MineBlock(_fx1.Address1); try { await StartAsync(swarmA); await StartAsync(swarmB); await StartAsync(swarmC); await swarmA.AddPeersAsync(new[] { swarmB.AsPeer }); await swarmA.AddPeersAsync(new[] { swarmC.AsPeer }); await EnsureExchange(swarmA, swarmB); await EnsureExchange(swarmA, swarmC); await EnsureExchange(swarmB, swarmC); swarmA.BroadcastTxs(new[] { tx }); await swarmC.TxReceived.WaitAsync(); await swarmB.TxReceived.WaitAsync(); Assert.Equal(tx, chainB.Transactions[tx.Id]); Assert.Equal(tx, chainC.Transactions[tx.Id]); } finally { await Task.WhenAll( swarmA.StopAsync(), swarmB.StopAsync(), swarmC.StopAsync()); } }
public async Task BroadcastTx() { var minerA = new PrivateKey(); Swarm <DumbAction> swarmA = CreateSwarm(minerA); Swarm <DumbAction> swarmB = CreateSwarm(); Swarm <DumbAction> swarmC = CreateSwarm(); BlockChain <DumbAction> chainA = swarmA.BlockChain; BlockChain <DumbAction> chainB = swarmB.BlockChain; BlockChain <DumbAction> chainC = swarmC.BlockChain; Transaction <DumbAction> tx = Transaction <DumbAction> .Create( 0, new PrivateKey(), chainA.Genesis.Hash, new DumbAction[] { } ); chainA.StageTransaction(tx); await chainA.MineBlock(minerA); try { await StartAsync(swarmA); await StartAsync(swarmB); await StartAsync(swarmC); await swarmA.AddPeersAsync(new[] { swarmB.AsPeer }, null); await swarmB.AddPeersAsync(new[] { swarmC.AsPeer }, null); await swarmC.AddPeersAsync(new[] { swarmA.AsPeer }, null); swarmA.BroadcastTxs(new[] { tx }); await swarmC.TxReceived.WaitAsync(); await swarmB.TxReceived.WaitAsync(); Assert.Equal(tx, chainB.GetTransaction(tx.Id)); Assert.Equal(tx, chainC.GetTransaction(tx.Id)); } finally { await StopAsync(swarmA); await StopAsync(swarmB); await StopAsync(swarmC); } }
public UnaryResult <bool> PutTransaction(byte[] txBytes) { Transaction <PolymorphicAction <ActionBase> > tx = Transaction <PolymorphicAction <ActionBase> > .Deserialize(txBytes); try { tx.Validate(); _blockChain.StageTransaction(tx); _swarm.BroadcastTxs(new[] { tx }); return(UnaryResult(true)); } catch (InvalidTxException) { return(UnaryResult(false)); } }