示例#1
0
        public void Can_handle_empty()
        {
            TransactionsMessageSerializer serializer = new TransactionsMessageSerializer();
            TransactionsMessage           message    = new TransactionsMessage(new Transaction[] { });

            SerializerTester.TestZero(serializer, message);
        }
示例#2
0
        public void Roundtrip()
        {
            TransactionsMessageSerializer serializer = new TransactionsMessageSerializer();
            Transaction transaction = new Transaction();

            transaction.Data      = new byte[] { 1, 2, 3 };
            transaction.GasLimit  = 10;
            transaction.GasPrice  = 100;
            transaction.Init      = new byte[] { 4, 5, 6 };
            transaction.Nonce     = 1000;
            transaction.Signature = new Signature(1, 2, 27);
            transaction.To        = Address.Zero;
            transaction.Value     = 10000;
            transaction.Hash      = Transaction.CalculateHash(transaction);

            TransactionsMessage message = new TransactionsMessage(transaction, transaction);

            byte[] bytes = serializer.Serialize(message);
            TransactionsMessage deserialized = serializer.Deserialize(bytes);

            Assert.AreEqual(message.Transactions.Length, deserialized.Transactions.Length, "length");
            // TODO: Chain IDs need to be handled properly
//            Assert.AreEqual(message.Transactions[0].ChainId, deserialized.Transactions[0].ChainId, $"{nameof(Transaction.ChainId)}");
            Assert.AreEqual(message.Transactions[0].Data, deserialized.Transactions[0].Data, $"{nameof(Transaction.Data)}");
            Assert.AreEqual(message.Transactions[0].GasLimit, deserialized.Transactions[0].GasLimit, $"{nameof(Transaction.GasLimit)}");
            Assert.AreEqual(message.Transactions[0].GasPrice, deserialized.Transactions[0].GasPrice, $"{nameof(Transaction.GasPrice)}");
            Assert.AreEqual(message.Transactions[0].Hash, deserialized.Transactions[0].Hash, $"{nameof(Transaction.Hash)}");
            // TODO: cannot test Init and Data at once with one transaction only
//            Assert.AreEqual(message.Transactions[0].Init, deserialized.Transactions[0].Init, $"{nameof(Transaction.Init)}");
            Assert.AreEqual(message.Transactions[0].Nonce, deserialized.Transactions[0].Nonce, $"{nameof(Transaction.Nonce)}");
            Assert.AreEqual(message.Transactions[0].Signature, deserialized.Transactions[0].Signature, $"{nameof(Transaction.Signature)}");
            Assert.AreEqual(message.Transactions[0].To, deserialized.Transactions[0].To, $"{nameof(Transaction.To)}");
            Assert.AreEqual(message.Transactions[0].Value, deserialized.Transactions[0].Value, $"{nameof(Transaction.Value)}");
        }
示例#3
0
        public void Can_handle_transactions()
        {
            TransactionsMessage msg = new TransactionsMessage(new List <Transaction>(Build.A.Transaction.SignedAndResolved().TestObjectNTimes(3)));

            HandleIncomingStatusMessage();
            HandleZeroMessage(msg, Eth62MessageCode.Transactions);
        }
        public void To_string_empty()
        {
            TransactionsMessage message  = new TransactionsMessage(new Transaction[] { });
            TransactionsMessage message2 = new TransactionsMessage(null);

            _ = message.ToString();
            _ = message2.ToString();
        }
        public void Can_handle_empty()
        {
            TransactionsMessageSerializer serializer = new TransactionsMessageSerializer();
            TransactionsMessage           message    = new TransactionsMessage();

            byte[] bytes = serializer.Serialize(message);
            TransactionsMessage deserialized = serializer.Deserialize(bytes);

            Assert.AreEqual(message.Transactions.Length, deserialized.Transactions.Length);
        }
示例#6
0
        public virtual void SendNewTransaction(Transaction transaction, bool isPriority)
        {
            if (transaction.Hash == null)
            {
                throw new InvalidOperationException("Trying to send a transaction with null hash");
            }

            TransactionsMessage msg = new TransactionsMessage(new[] { transaction });

            Send(msg);
        }
 private void Check(PeerConnection peer, TransactionsMessage message)
 {
     foreach (Transaction tx in message.Transactions.Transactions_)
     {
         Item item = new Item(new TransactionMessage(tx).MessageId, InventoryType.Trx);
         if (!peer.InventoryRequest.ContainsKey(item))
         {
             throw new P2pException(P2pException.ErrorType.BAD_MESSAGE,
                                    "tx: " + message.MessageId + " without request.");
         }
         peer.InventoryRequest.TryRemove(item, out _);
     }
 }
示例#8
0
        public void SetUp()
        {
            Console.WriteLine("AAA");
            Session session = new Session(8545, Substitute.For <IChannel>(), Substitute.For <IDisconnectsAnalyzer>(), LimboLogs.Instance);

            session.RemoteNodeId = TestItem.PublicKeyA;
            session.RemoteHost   = "127.0.0.1";
            session.RemotePort   = 30303;
            _ser = new MessageSerializationService();
            _ser.Register(new TransactionsMessageSerializer());
            _ser.Register(new StatusMessageSerializer());
            NodeStatsManager stats = new NodeStatsManager(TimerFactory.Default, LimboLogs.Instance);
            var ecdsa         = new EthereumEcdsa(ChainId.Mainnet, LimboLogs.Instance);
            var tree          = Build.A.BlockTree().TestObject;
            var stateProvider = new StateProvider(new TrieStore(new MemDb(), LimboLogs.Instance), new MemDb(), LimboLogs.Instance);
            var specProvider  = MainnetSpecProvider.Instance;

            TxPool.TxPool txPool = new TxPool.TxPool(
                ecdsa,
                new ChainHeadInfoProvider(new FixedBlockChainHeadSpecProvider(MainnetSpecProvider.Instance), tree, stateProvider),
                new TxPoolConfig(),
                new TxValidator(ChainId.Mainnet),
                LimboLogs.Instance,
                new TransactionComparerProvider(specProvider, tree).GetDefaultComparer());
            ISyncServer syncSrv = Substitute.For <ISyncServer>();
            BlockHeader head    = Build.A.BlockHeader.WithNumber(1).TestObject;

            syncSrv.Head.Returns(head);
            _handler = new Eth62ProtocolHandler(session, _ser, stats, syncSrv, txPool, ShouldGossip.Instance, LimboLogs.Instance);
            _handler.DisableTxFiltering();

            StatusMessage statusMessage = new StatusMessage();

            statusMessage.ProtocolVersion = 63;
            statusMessage.BestHash        = Keccak.Compute("1");
            statusMessage.GenesisHash     = Keccak.Compute("0");
            statusMessage.TotalDifficulty = 131200;
            statusMessage.ChainId         = 1;
            IByteBuffer bufStatus = _ser.ZeroSerialize(statusMessage);

            _zeroPacket            = new ZeroPacket(bufStatus);
            _zeroPacket.PacketType = bufStatus.ReadByte();

            _handler.HandleMessage(_zeroPacket);

            Transaction tx = Build.A.Transaction.SignedAndResolved(ecdsa, TestItem.PrivateKeyA).TestObject;

            _txMsg = new TransactionsMessage(new[] { tx });
        }
        public void Roundtrip_call()
        {
            TransactionsMessageSerializer serializer = new TransactionsMessageSerializer();
            Transaction transaction = new Transaction();

            transaction.Data          = new byte[] { 1, 2, 3 };
            transaction.GasLimit      = 10;
            transaction.GasPrice      = 100;
            transaction.Nonce         = 1000;
            transaction.Signature     = new Signature(1, 2, 27);
            transaction.To            = TestItem.AddressA;
            transaction.Value         = 10000;
            transaction.Hash          = transaction.CalculateHash();
            transaction.SenderAddress = null;

            TransactionsMessage message = new TransactionsMessage(new[] { transaction, transaction });

            SerializerTester.TestZero(serializer, message, "f84ae48203e8640a94b7705ae4c6f81b66cdb323c65f4e8133690fc099822710830102031b0102e48203e8640a94b7705ae4c6f81b66cdb323c65f4e8133690fc099822710830102031b0102");
        }
        public void Roundtrip_init()
        {
            TransactionsMessageSerializer serializer = new TransactionsMessageSerializer();
            Transaction transaction = new Transaction();

            transaction.GasLimit      = 10;
            transaction.GasPrice      = 100;
            transaction.Data          = new byte[] { 4, 5, 6 };
            transaction.Nonce         = 1000;
            transaction.Signature     = new Signature(1, 2, 27);
            transaction.To            = null;
            transaction.Value         = 10000;
            transaction.Hash          = transaction.CalculateHash();
            transaction.SenderAddress = null;

            TransactionsMessage message = new TransactionsMessage(new[] { transaction, transaction });

            SerializerTester.TestZero(serializer, message, "e2d08203e8640a80822710830405061b0102d08203e8640a80822710830405061b0102");
        }
        public void ProcessMessage(PeerConnection peer, MineralMessage message)
        {
            TransactionsMessage tx_message = (TransactionsMessage)message;

            Check(peer, tx_message);

            foreach (Transaction tx in tx_message.Transactions.Transactions_)
            {
                ContractType type = tx.RawData.Contract[0].Type;

                if (type == ContractType.TriggerSmartContract || type == ContractType.CreateSmartContract)
                {
                    this.contract_queue.Enqueue(new TxEvent(peer, new TransactionMessage(tx)));
                }
                else
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(HandleTransaction), new object[] { peer, new TransactionMessage(tx) });
                }
            }
        }
示例#12
0
        public void Roundtrip()
        {
            TransactionsMessageSerializer serializer = new TransactionsMessageSerializer();
            Transaction transaction = new Transaction();

            transaction.Data      = new byte[] { 1, 2, 3 };
            transaction.GasLimit  = 10;
            transaction.GasPrice  = 100;
            transaction.Init      = new byte[] { 4, 5, 6 };
            transaction.Nonce     = 1000;
            transaction.Signature = new Signature(1, 2, 27);
            transaction.To        = Address.Zero;
            transaction.Value     = 10000;
            transaction.Hash      = Transaction.CalculateHash(transaction);

            TransactionsMessage message = new TransactionsMessage(transaction, transaction);

            byte[] bytes         = serializer.Serialize(message);
            byte[] expectedBytes = Bytes.FromHexString("f84ae48203e8640a940000000000000000000000000000000000000000822710830102031b0102e48203e8640a940000000000000000000000000000000000000000822710830102031b0102");

            Assert.True(Bytes.AreEqual(bytes, expectedBytes), "bytes");

            TransactionsMessage deserialized = serializer.Deserialize(bytes);

            Assert.AreEqual(message.Transactions.Length, deserialized.Transactions.Length, "length");
            // TODO: Chain IDs need to be handled properly
//            Assert.AreEqual(message.Transactions[0].ChainId, deserialized.Transactions[0].ChainId, $"{nameof(Transaction.ChainId)}");
            Assert.AreEqual(message.Transactions[0].Data, deserialized.Transactions[0].Data, $"{nameof(Transaction.Data)}");
            Assert.AreEqual(message.Transactions[0].GasLimit, deserialized.Transactions[0].GasLimit, $"{nameof(Transaction.GasLimit)}");
            Assert.AreEqual(message.Transactions[0].GasPrice, deserialized.Transactions[0].GasPrice, $"{nameof(Transaction.GasPrice)}");
            Assert.AreEqual(message.Transactions[0].Hash, deserialized.Transactions[0].Hash, $"{nameof(Transaction.Hash)}");
            // TODO: cannot test Init and Data at once with one transaction only
//            Assert.AreEqual(message.Transactions[0].Init, deserialized.Transactions[0].Init, $"{nameof(Transaction.Init)}");
            Assert.AreEqual(message.Transactions[0].Nonce, deserialized.Transactions[0].Nonce, $"{nameof(Transaction.Nonce)}");
            Assert.AreEqual(message.Transactions[0].Signature, deserialized.Transactions[0].Signature, $"{nameof(Transaction.Signature)}");
            Assert.AreEqual(message.Transactions[0].To, deserialized.Transactions[0].To, $"{nameof(Transaction.To)}");
            Assert.AreEqual(message.Transactions[0].Value, deserialized.Transactions[0].Value, $"{nameof(Transaction.Value)}");

            SerializerTester.Test(serializer, message);
            SerializerTester.TestZero(serializer, message);
        }
示例#13
0
        private void AddTcpMessage(Message message, bool flag)
        {
            if (flag)
            {
                MineralInMessage.Add();
            }
            else
            {
                MineralOutMessage.Add();
            }

            switch (message.Type)
            {
            case MessageTypes.MsgType.P2P_HELLO:
                if (flag)
                {
                    P2pInHello.Add();
                }
                else
                {
                    P2pOutHello.Add();
                }
                break;

            case MessageTypes.MsgType.P2P_PING:
                if (flag)
                {
                    P2pInPing.Add();
                }
                else
                {
                    P2pOutPing.Add();
                }
                break;

            case MessageTypes.MsgType.P2P_PONG:
                if (flag)
                {
                    P2pInPong.Add();
                }
                else
                {
                    P2pOutPong.Add();
                }
                break;

            case MessageTypes.MsgType.P2P_DISCONNECT:
                if (flag)
                {
                    P2pInDisconnect.Add();
                }
                else
                {
                    P2pOutDisconnect.Add();
                }
                break;

            case MessageTypes.MsgType.SYNC_BLOCK_CHAIN:
                if (flag)
                {
                    MineralInSyncBlockChain.Add();
                }
                else
                {
                    MineralOutSyncBlockChain.Add();
                }
                break;

            case MessageTypes.MsgType.BLOCK_CHAIN_INVENTORY:
                if (flag)
                {
                    MineralInBlockChainInventory.Add();
                }
                else
                {
                    MineralOutBlockChainInventory.Add();
                }
                break;

            case MessageTypes.MsgType.INVENTORY:
                InventoryMessage inventory_message = (InventoryMessage)message;
                int inventory_count = inventory_message.Inventory.Ids.Count;
                if (flag)
                {
                    if (inventory_message.InventoryMessageType == MessageTypes.MsgType.TX)
                    {
                        MineralInTrxInventory.Add();
                        MineralInTrxInventoryElement.Add(inventory_count);
                    }
                    else
                    {
                        MineralInBlockInventory.Add();
                        MineralInBlockInventoryElement.Add(inventory_count);
                    }
                }
                else
                {
                    if (inventory_message.InventoryMessageType == MessageTypes.MsgType.TX)
                    {
                        MineralOutTrxInventory.Add();
                        MineralOutTrxInventoryElement.Add(inventory_count);
                    }
                    else
                    {
                        MineralOutBlockInventory.Add();
                        MineralOutBlockInventoryElement.Add(inventory_count);
                    }
                }
                break;

            case MessageTypes.MsgType.FETCH_INV_DATA:
                FetchInventoryDataMessage fetch_inventory_message = (FetchInventoryDataMessage)message;
                int fetch_count = fetch_inventory_message.Inventory.Ids.Count;
                if (flag)
                {
                    if (fetch_inventory_message.InventoryMessageType == MessageTypes.MsgType.TX)
                    {
                        MineralInTrxFetchInvData.Add();
                        MineralInTrxFetchInvDataElement.Add(fetch_count);
                    }
                    else
                    {
                        MineralInBlockFetchInvData.Add();
                        MineralInBlockFetchInvDataElement.Add(fetch_count);
                    }
                }
                else
                {
                    if (fetch_inventory_message.InventoryMessageType == MessageTypes.MsgType.TX)
                    {
                        MineralOutTrxFetchInvData.Add();
                        MineralOutTrxFetchInvDataElement.Add(fetch_count);
                    }
                    else
                    {
                        MineralOutBlockFetchInvData.Add();
                        MineralOutBlockFetchInvDataElement.Add(fetch_count);
                    }
                }
                break;

            case MessageTypes.MsgType.TXS:
                TransactionsMessage transactionsMessage = (TransactionsMessage)message;
                if (flag)
                {
                    MineralInTrxs.Add();
                    MineralInTrx.Add(transactionsMessage.Transactions.Transactions_.Count);
                }
                else
                {
                    MineralOutTrxs.Add();
                    MineralOutTrx.Add(transactionsMessage.Transactions.Transactions_.Count);
                }
                break;

            case MessageTypes.MsgType.TX:
                if (flag)
                {
                    MineralInMessage.Add();
                }
                else
                {
                    MineralOutMessage.Add();
                }
                break;

            case MessageTypes.MsgType.BLOCK:
                if (flag)
                {
                    MineralInBlock.Add();
                }
                MineralOutBlock.Add();
                break;

            default:
                break;
            }
        }