<commit_before>package app

import (
	log "github.com/Sirupsen/logrus"
	"github.com/ubclaunchpad/cumulus/miner"
)

// nWorkers is how many workers this node has.
const nWorkers = 10

// Workers is a list of workers.
var workers [nWorkers]*AppWorker

// WorkerQueue is the channel of workers handling work.
var WorkerQueue chan AppWorker

// QuitChan is the channel we use to kill the workers.
var QuitChan chan int

// Worker is an interface for the basic app worker tasks.
type Worker interface {
	HandleTransaction(work TransactionWork)
	HandleBlock(work BlockWork)
}

// AppWorker implements the basic worker.
type AppWorker struct {
	ID int
	log.FieldLogger
}

// NewWorker returns a new AppWorker object.
func NewWorker(id int) AppWorker {
	return AppWorker{
		ID:          id,
		FieldLogger: log.WithField("id", id),
	}
}

// Start continually collects new work from existing work channels.
func (w AppWorker) Start() {
	go func() {
		for {
			// Wait for work.
			log.WithFields(log.Fields{
				"id": w.ID,
			}).Debug("Worker waiting for work")
			select {
			case work := <-TransactionWorkQueue:
				w.FieldLogger.Debug("Worker handling new transaction work")
				w.HandleTransaction(work)
			case work := <-BlockWorkQueue:
				w.FieldLogger.Debug("Worker handling new block work")
				w.HandleBlock(work)
			case <-QuitChan:
				w.FieldLogger.Debug("Worker quitting")
				return
			}
		}
	}()
}

// HandleTransaction handles new instance of TransactionWork.
func (w *AppWorker) HandleTransaction(work TransactionWork) {
	ok := tpool.Set(work.Transaction, chain)

	// Respond to the request if a response method was provided.
	if work.Responder != nil {
		work.Responder.Lock()
		defer work.Responder.Unlock()
		work.Responder.Send(ok)
	}
}

// HandleBlock handles new instance of BlockWork.
func (w *AppWorker) HandleBlock(work BlockWork) {
	ok := tpool.Update(work.Block, chain)

	if ok {
		// Append to the chain before requesting
		// the next block so that the block
		// numbers make sense.
		chain.AppendBlock(work.Block)
		blk := tpool.NextBlock(chain)
		if miner.IsMining() {
			miner.RestartMiner(chain, blk)
		}
	}

	// Respond to the request if a response method was provided.
	if work.Responder != nil {<commit_msg>validBlock and transaction variables<commit_after>package app

import (
	log "github.com/Sirupsen/logrus"
	"github.com/ubclaunchpad/cumulus/miner"
)

// nWorkers is how many workers this node has.
const nWorkers = 10

// Workers is a list of workers.
var workers [nWorkers]*AppWorker

// WorkerQueue is the channel of workers handling work.
var WorkerQueue chan AppWorker

// QuitChan is the channel we use to kill the workers.
var QuitChan chan int

// Worker is an interface for the basic app worker tasks.
type Worker interface {
	HandleTransaction(work TransactionWork)
	HandleBlock(work BlockWork)
}

// AppWorker implements the basic worker.
type AppWorker struct {
	ID int
	log.FieldLogger
}

// NewWorker returns a new AppWorker object.
func NewWorker(id int) AppWorker {
	return AppWorker{
		ID:          id,
		FieldLogger: log.WithField("id", id),
	}
}

// Start continually collects new work from existing work channels.
func (w AppWorker) Start() {
	go func() {
		for {
			// Wait for work.
			log.WithFields(log.Fields{
				"id": w.ID,
			}).Debug("Worker waiting for work")
			select {
			case work := <-TransactionWorkQueue:
				w.FieldLogger.Debug("Worker handling new transaction work")
				w.HandleTransaction(work)
			case work := <-BlockWorkQueue:
				w.FieldLogger.Debug("Worker handling new block work")
				w.HandleBlock(work)
			case <-QuitChan:
				w.FieldLogger.Debug("Worker quitting")
				return
			}
		}
	}()
}

// HandleTransaction handles new instance of TransactionWork.
func (w *AppWorker) HandleTransaction(work TransactionWork) {
	validTransaction := tpool.Set(work.Transaction, chain)

	// Respond to the request if a response method was provided.
	if work.Responder != nil {
		work.Responder.Lock()
		defer work.Responder.Unlock()
		work.Responder.Send(validTransaction)
	}
}

// HandleBlock handles new instance of BlockWork.
func (w *AppWorker) HandleBlock(work BlockWork) {
	validBlock := tpool.Update(work.Block, chain)

	if ok {
		// Append to the chain before requesting
		// the next block so that the block
		// numbers make sense.
		chain.AppendBlock(work.Block)
		blk := tpool.NextBlock(chain)
		if miner.IsMining() {
			miner.RestartMiner(chain, blk)
		}
	}

	// Respond to the request if a response method was provided.
	if work.Responder != nil {
{'fraction_non_alphanumeric': 0.10059563203176704, 'fraction_numerical': 0.0008824178248400617, 'mean_word_length': 3.5890688259109313, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '859611', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1395, 'n_words': 463}