func mapResponseToBlock(r *block.ResponseBlock, d *block.DBlock) {

	d.BlockLength = r.Size
	d.BlockHash = r.Hash
	d.TransactionCount = r.Ntx
	d.FormatVersion = r.Ver
	d.PreviousBlockHash = r.Prevblock
	d.MerkleRoot = r.Mrklroot
	d.TimeStamp = r.Time
	d.TargetValue = 0
	d.Nonce = r.Nonce

	d.Transactions = make([]block.DTransaction, d.TransactionCount)

	for t := 0; t < d.TransactionCount-1; t++ {
		d.Transactions[t].TransactionHash = r.Tx[t].Hash
		d.Transactions[t].TransactionVersionNumber = r.Tx[t].Ver
		d.Transactions[t].InputCount = r.Tx[t].Vinsz
		d.Transactions[t].TransactionIndex = r.Tx[t].Txindex
		d.Transactions[t].Time = r.Tx[t].Time

		d.Transactions[t].Inputs = make([]block.DInput, d.Transactions[t].InputCount)

		for i := 0; i < d.Transactions[t].InputCount-1; i++ {
			d.Transactions[t].Inputs[i].InputScript = r.Tx[t].Inputs[i].Script
			d.Transactions[t].Inputs[i].TransactionIndex = d.Transactions[t].TransactionIndex
			d.Transactions[t].Inputs[i].TransactionHash = ""
			d.Transactions[t].Inputs[i].InputScriptLength = len(d.Transactions[t].Inputs[i].InputScript)
			d.Transactions[t].Inputs[i].SequenceNumber = r.Tx[t].Inputs[i].Sequence
		}

		d.Transactions[t].OutputCount = r.Tx[t].Voutsz
		d.Transactions[t].Outputs = make([]block.DOutput, d.Transactions[t].OutputCount)

		for o := 0; o < d.Transactions[t].OutputCount-1; o++ {
			d.Transactions[t].Outputs[o].OutputValue = r.Tx[t].Out[o].Value
			d.Transactions[t].Outputs[o].TransactionIndex = r.Tx[t].Out[o].Txindex
			d.Transactions[t].Outputs[o].ChallengeScript = r.Tx[t].Out[o].Script
			d.Transactions[t].Outputs[o].ChallengeScriptLength = len(d.Transactions[t].Outputs[o].ChallengeScript)
			d.Transactions[t].Outputs[o].KeyType = ""
			d.Transactions[t].Outputs[o].NumAddresses = 1

			d.Transactions[t].Outputs[o].Addresses = make([]block.DAddress, 1)
			d.Transactions[t].Outputs[o].Addresses[0].Address = r.Tx[t].Out[o].Addr
		}

		d.Transactions[t].TransactionLockTime = r.Tx[t].Locktime

	}

}
//MapBlockToDBlock maps a block read from the .dat files to a database block
func MapBlockToDBlock(b *block.Block, d *block.DBlock) error {

	d.MagicNumber = int(b.MagicNumber)
	d.BlockLength = int(b.BlockLength)
	d.BlockHash = b.BlockHash
	d.FormatVersion = int(b.Header.FormatVersion)
	d.PreviousBlockHash = b.Header.PreviousBlockHash
	d.MerkleRoot = b.Header.MerkleRoot
	d.TimeStamp = int(b.Header.TimeStamp)
	d.TargetValue = int(b.Header.TargetValue)
	d.Nonce = int(b.Header.Nonce)
	d.TransactionCount = int(b.TransactionCount)

	var dTxs []block.DTransaction

	for t := 0; t < d.TransactionCount-1; t++ {

		var dIns []block.DInput
		var tx block.DTransaction

		tx.TransactionIndex = 0
		tx.Time = d.TimeStamp
		tx.TransactionHash = b.Transactions[t].TransactionHash
		tx.TransactionVersionNumber = int(b.Transactions[t].TransactionVersionNumber)
		tx.InputCount = int(b.Transactions[t].InputCount)

		for i := 0; i < tx.InputCount-1; i++ {

			var in block.DInput

			in.TransactionHash = b.Transactions[t].Inputs[i].TransactionHash
			in.TransactionIndex = int(b.Transactions[t].Inputs[i].TransactionIndex)
			in.InputScriptLength = int(b.Transactions[t].Inputs[i].InputScriptLength)
			in.InputScript = b.Transactions[t].Inputs[i].InputScript
			in.SequenceNumber = int(b.Transactions[t].Inputs[i].SequenceNumber)

			dIns = append(dIns, in)
		}

		tx.OutputCount = int(b.Transactions[t].OutputCount)
		var dOuts []block.DOutput

		for o := 0; o < tx.OutputCount-1; o++ {

			var out block.DOutput

			out.OutputValue = int(b.Transactions[t].Outputs[o].OutputValue)
			out.ChallengeScriptLength = int(b.Transactions[t].Outputs[o].ChallengeScriptLength)
			out.ChallengeScript = b.Transactions[t].Outputs[o].ChallengeScript
			out.KeyType = b.Transactions[t].Outputs[o].KeyType
			out.NumAddresses = len(b.Transactions[t].Outputs[o].Addresses)

			var dAdds []block.DAddress

			for a := 0; a < out.NumAddresses; a++ {

				var add block.DAddress

				add.Address = b.Transactions[t].Outputs[o].Addresses[a].Address

				dAdds = append(dAdds, add)
			}
			out.Addresses = dAdds
			dOuts = append(dOuts, out)
		}

		tx.TransactionLockTime = int(b.Transactions[t].TransactionLockTime)

		tx.Inputs = dIns
		tx.Outputs = dOuts
		dTxs = append(dTxs, tx)

	}
	d.Transactions = dTxs
	return nil
}