func (u *unApprovedTransactions) Add(txadd *structures.Transaction) error {
	conflicts, err := u.DetectConflictsForNew(txadd)
	if err != nil {
		return err
	}
	if conflicts != nil {
		return errors.New(fmt.Sprintf("The transaction conflicts with other prepared transaction: %x", conflicts.ID))
	}
	utdb, err := u.DB.GetUnapprovedTransactionsObject()
	if err != nil {
		return err
	}
	u.Logger.Trace.Printf("adding TX to unappr %x", txadd.ID)
	txser, err := txadd.Serialize()
	if err != nil {
		return err
	}
	err = utdb.PutTransaction(txadd.ID, txser)
	if err != nil {
		return errors.New("Adding new transaction to unapproved cache: " + err.Error())
	}
	return nil
}
Add new transaction for the list of unapproved
Before to call this function we checked that transaction is valid
Now we need to check if there are no conflicts with other transactions in the cache
package transactions
import (
	"bytes"
	"encoding/hex"
	"errors"
	"fmt"
	"sort"
	"github.com/gelembjuk/democoin/lib/utils"
	"github.com/gelembjuk/democoin/node/database"
	"github.com/gelembjuk/democoin/node/structures"
)
type unApprovedTransactions struct {
	DB     database.DBManager
	Logger *utils.LoggerMan
}
func (u *unApprovedTransactions) CheckInputsArePrepared(inputs map[int]structures.TXInput, inputTXs map[int]*structures.Transaction) error {
	checked := map[string][]int{}
	for vinInd, vin := range inputs {
		txstr := hex.EncodeToString(vin.Txid)
		if outs, ok := checked[txstr]; ok {
			for _, out := range outs {
				if out == vin.Vout {
					return errors.New(fmt.Sprintf("Duplicate usage of transaction output: %s - %d", txstr, out))
				}
			}
		}
		tx, err := u.GetIfExists(vin.Txid)
		if err != nil {
			return err
		}
		if tx == nil {
			return NewTXVerifyError("Input transaction is not found in prepared to approve", TXVerifyErrorNoInput, vin.Txid)
		}
		inputTXs[vinInd] = tx
		checked[txstr] = append(checked[txstr], vin.Vout)
	}
	return nil
}
func (u *unApprovedTransactions) CheckInputsWereBefore(
	inputs map[int]structures.TXInput, prevTXs []*structures.Transaction,
	inputTXs map[int]*structures.Transaction) (map[int]*structures.Transaction, error) {
	checked := map[string][]int{}
	for vind, vin := range inputs {
		txstr := hex.EncodeToString(vin.Txid)
		if outs, ok := checked[txstr]; ok {
			for _, out := range outs {
				if out == vin.Vout {
					return inputTXs, errors.New("Duplicate usage of transaction output")
				}
			}
		}
		exists := false
		for _, tx := range prevTXs {
			if bytes.Compare(vin.Txid, tx.ID) == 0 {
				inputTXs[vind] = tx
				exists = true
				break
			}
		}
		if !exists {
			return inputTXs, NewTXVerifyError("Input transaction is not found in prepared to approve", TXVerifyErrorNoInput, vin.Txid)
		}
		checked[txstr] = append(checked[txstr], vin.Vout)
	}
	return inputTXs, nil
}
func (u *unApprovedTransactions) GetPreparedBy(PubKeyHash []byte) ([]structures.TXInput,
	[]*structures.TXOutputIndependent, []structures.TXInput, error) {
	inputs := []structures.TXInput{}
	outputs := []*structures.TXOutputIndependent{}
	utdb, err := u.DB.GetUnapprovedTransactionsObject()
	if err != nil {
		return nil, nil, nil, err
	}
	err = utdb.ForEach(func(k, txBytes []byte) error {
		tx := structures.Transaction{}
		err = tx.DeserializeTransaction(txBytes)
		if err != nil {
			return err
		}
		sender := []byte{}
		if !tx.IsCoinbase() {
			sender = tx.Vin[0].PubKey
			for _, vin := range tx.Vin {
				if vin.UsesKey(PubKeyHash) {
					inputs = append(inputs, vin)
				}
			}
		}
		for indV, vout := range tx.Vout {
			if vout.IsLockedWithKey(PubKeyHash) {
				voutind := structures.TXOutputIndependent{}
				voutind.LoadFromSimple(vout, tx.ID, indV, sender, tx.IsCoinbase(), txBytes)
				outputs = append(outputs, &voutind)
			}
		}
		return nil
	})
	if err != nil {
		return nil, nil, nil, err
	}
	realoutputs := []*structures.TXOutputIndependent{}
	approvedinputs := []structures.TXInput{}
	for _, vout := range outputs {
		used := false
		for _, vin := range inputs {
			if bytes.Compare(vin.Txid, vout.TXID) == 0 && vin.Vout == vout.OIndex {
				used = true
				break
			}
		}
		if !used {
			realoutputs = append(realoutputs, vout)
		}
	}
	for _, vin := range inputs {
		pendingout := false
		for _, vout := range outputs {
			if bytes.Compare(vin.Txid, vout.TXID) == 0 && vin.Vout == vout.OIndex {
				pendingout = true
				break
			}
		}
		if !pendingout {
			approvedinputs = append(approvedinputs, vin)
		}
	}
	return inputs, realoutputs, approvedinputs, nil
}
func (u *unApprovedTransactions) GetInputValue(input structures.TXInput) (float64, error) {
	u.Logger.Trace.Printf("Find TX %x in unapproved", input.Txid)
	tx, err := u.GetIfExists(input.Txid)
	if err != nil {
		return 0, err
	}
	if tx == nil {
		return 0, errors.New("TX not found in cache of unapproved")
	}
	return tx.Vout[input.Vout].Value, nil
}
func (u *unApprovedTransactions) GetIfExists(txid []byte) (*structures.Transaction, error) {
	utdb, err := u.DB.GetUnapprovedTransactionsObject()
	if err != nil {
		return nil, err
	}
	txBytes, err := utdb.GetTransaction(txid)
	if err != nil {
		return nil, err
	}
	if len(txBytes) == 0 {
		return nil, nil
	}
	tx := structures.Transaction{}
	err = tx.DeserializeTransaction(txBytes)
	if err != nil {
		return nil, err
	}
	return &tx, nil
}
func (u *unApprovedTransactions) GetTransactions(number int) ([]*structures.Transaction, error) {
	utdb, err := u.DB.GetUnapprovedTransactionsObject()
	if err != nil {
		return nil, err
	}
	txset := []*structures.Transaction{}
	totalnumber := 0
	err = utdb.ForEach(func(k, txBytes []byte) error {
		tx := structures.Transaction{}
		err = tx.DeserializeTransaction(txBytes)
		if err != nil {
			return err
		}
		txset = append(txset, &tx)
		totalnumber++
		if totalnumber >= number {
			return database.NewDBCursorStopError()
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	sort.Sort(structures.Transactions(txset))
	return txset, nil
}
func (u *unApprovedTransactions) GetCount() (int, error) {
	utdb, err := u.DB.GetUnapprovedTransactionsObject()
	if err != nil {
		return 0, err
	}
	return utdb.GetCount()
}