<commit_before>/*
 * Copyright 2012 <NAME> <EMAIL>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bitsofproof.supernode.core;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bitsofproof.supernode.messages.BitcoinMessageListener;
import com.bitsofproof.supernode.messages.GetDataMessage;
import com.bitsofproof.supernode.messages.InvMessage;
import com.bitsofproof.supernode.messages.TxMessage;
import com.bitsofproof.supernode.model.BlockStore;
import com.bitsofproof.supernode.model.Tx;

public class TransactionHandler
{
	private static final Logger log = LoggerFactory.getLogger (TransactionHandler.class);

	private BlockStore store;
	private ChainLoader loader;

	private final Map<String, Tx> unconfirmed = new HashMap<String, Tx> ();

	public TransactionHandler (BitcoinNetwork network)
	{
		network.addListener ("inv", new BitcoinMessageListener<InvMessage> ()
		{
			@Override
			public void process (InvMessage im, BitcoinPeer peer) throws Exception
			{
				GetDataMessage get = (GetDataMessage) peer.createMessage ("getdata");
				for ( byte[] h : im.getTransactionHashes () )
				{
					String hash = new Hash (h).toString ();
					if ( unconfirmed.get (hash) == null )
					{
						log.trace ("heard about new transaction " + hash + " from " + peer.getAddress ());
						get.getTransactions ().add (h);
					}
				}
				if ( !loader.isBehind () && get.getTransactions ().size () > 0 )
				{
					log.trace ("asking for transaction details from " + peer.getAddress ());
					peer.send (get);
				}
			}
		});
		network.addListener ("tx", new BitcoinMessageListener<TxMessage> ()
		{

			@Override
			public void process (TxMessage txm, BitcoinPeer peer) throws Exception
			{
				log.trace ("received transaction details for " + txm.getTx ().getHash () + " from " + peer.getAddress ());
				store.validateTransaction (txm.getTx ());
				log.trace ("Caching unconfirmed transaction " + txm.getTx ().getHash ());
				unconfirmed.put (txm.getTx ().getHash (), txm.getTx ());
			}
		});
	}

	public BlockStore getStore ()
	{<commit_msg>transaction resend<commit_after>/*
 * Copyright 2012 <NAME> <EMAIL>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bitsofproof.supernode.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bitsofproof.supernode.core.BitcoinNetwork.PeerTask;
import com.bitsofproof.supernode.messages.BitcoinMessageListener;
import com.bitsofproof.supernode.messages.GetDataMessage;
import com.bitsofproof.supernode.messages.InvMessage;
import com.bitsofproof.supernode.messages.TxMessage;
import com.bitsofproof.supernode.model.BlockStore;
import com.bitsofproof.supernode.model.Tx;

public class TransactionHandler
{
	private static final Logger log = LoggerFactory.getLogger (TransactionHandler.class);

	private BlockStore store;
	private ChainLoader loader;

	private final Map<String, Tx> unconfirmed = Collections.synchronizedMap (new HashMap<String, Tx> ());

	public TransactionHandler (final BitcoinNetwork network)
	{
		network.addListener ("inv", new BitcoinMessageListener<InvMessage> ()
		{
			@Override
			public void process (InvMessage im, BitcoinPeer peer) throws Exception
			{
				GetDataMessage get = (GetDataMessage) peer.createMessage ("getdata");
				for ( byte[] h : im.getTransactionHashes () )
				{
					String hash = new Hash (h).toString ();
					if ( unconfirmed.get (hash) == null )
					{
						log.trace ("heard about new transaction " + hash + " from " + peer.getAddress ());
						get.getTransactions ().add (h);
					}
				}
				if ( !loader.isBehind () && get.getTransactions ().size () > 0 )
				{
					log.trace ("asking for transaction details from " + peer.getAddress ());
					peer.send (get);
				}
			}
		});
		network.addListener ("tx", new BitcoinMessageListener<TxMessage> ()
		{

			@Override
			public void process (final TxMessage txm, final BitcoinPeer peer) throws Exception
			{
				log.trace ("received transaction details for " + txm.getTx ().getHash () + " from " + peer.getAddress ());
				if ( !unconfirmed.containsKey (txm.getTx ().getHash ()) )
				{
					store.validateTransaction (txm.getTx ());
					log.trace ("Caching unconfirmed transaction " + txm.getTx ().getHash ());
					unconfirmed.put (txm.getTx ().getHash (), txm.getTx ());
					network.runForConnected (new PeerTask ()
					{
						@Override
						public void run (BitcoinPeer p) throws Exception
						{
							// tell others
							if ( p != peer )
							{
								TxMessage tm = (TxMessage) p.createMessage ("tx");
								tm.setTx (txm.getTx ());
								p.send (tm);
								log.trace ("Sent transaction " + txm.getTx ().getHash () + " to " + p.getAddress ());
							}
						}
					});
				}
			}
		});
	}

	public BlockStore getStore ()
	{
{'fraction_non_alphanumeric': 0.11557280242955964, 'fraction_numerical': 0.0037118272313143245, 'mean_word_length': 3.588235294117647, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4737860', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1733, 'n_words': 501}