Coverage Summary for Class: TransactionGateway (co.rsk.net)

Class Class, % Method, % Line, %
TransactionGateway 100% (1/1) 66.7% (2/3) 58.3% (7/12)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.net; 20  21 import org.ethereum.core.Transaction; 22 import org.ethereum.core.TransactionPool; 23 import org.ethereum.core.TransactionPoolAddResult; 24 import org.ethereum.net.server.ChannelManager; 25  26 import javax.annotation.Nonnull; 27 import java.util.*; 28  29 /** 30  * Centralizes receiving and relaying transactions, so we can only distribute information to nodes that don't already 31  * have it. 32  */ 33 public class TransactionGateway { 34  private final ChannelManager channelManager; 35  private final TransactionPool transactionPool; 36  37  public TransactionGateway( 38  ChannelManager channelManager, 39  TransactionPool transactionPool) { 40  this.channelManager = Objects.requireNonNull(channelManager); 41  this.transactionPool = Objects.requireNonNull(transactionPool); 42  } 43  44  /** 45  * Receives transactions from other node 46  */ 47  public void receiveTransactionsFrom(@Nonnull List<Transaction> txs, @Nonnull Set<NodeID> nodeIDS) { 48  List<Transaction> result = transactionPool.addTransactions(txs); 49  if(!result.isEmpty()) { 50  channelManager.broadcastTransactions(result, nodeIDS); 51  } 52  } 53  54  /** 55  * Receives transaction via JSON RPC 56  */ 57  public TransactionPoolAddResult receiveTransaction(Transaction transaction) { 58  TransactionPoolAddResult result = transactionPool.addTransaction(transaction); 59  if(result.pendingTransactionsWereAdded()) { 60  channelManager.broadcastTransactions(result.getPendingTransactionsAdded(), Collections.emptySet()); 61  } 62  return result; 63  } 64 }