Exemplo n.º 1
0
func SendLoop() {
	var LastBlockIndex uint64 = 0
	for {
		time.Sleep(1 * time.Second)
		BlockIndex, err := bitcoinchain.GetBlockCount()
		if err != nil {
			fmt.Printf("Get block count failed continue:%v\r\n", err)
			continue
		}
		if BlockIndex <= LastBlockIndex {
			fmt.Printf("blockindex[%v] < LastBlockIndex[%d]\r\n", BlockIndex, LastBlockIndex)
			continue
		}
		UnspentList, err := wallet.UnspentList(3)
		if err != nil {
			fmt.Printf("Failed to get unspendlist %v\r\n", err)
			continue
		}
		UnSendList, err := dbutil.GetAllUnsentMessage(24 * time.Hour)
		if err != nil {
			fmt.Printf("Failed to get unsendlist %v\r\n", err)
			continue
		}
		//fmt.Printf("found unspentlist %v\r\n ", UnspentList)
		//fmt.Printf("found unsendlist %v\r\n", UnSendList)
		for _, spent := range UnspentList {
			for _, send := range UnSendList {
				//fmt.Printf("spent addr %v, send addr %v\r\n", spent.Address, send.RelayAddr)

				if spent.Address == send.RelayAddr {

					amount := uint64(spent.Amount * float64(conf.COIN))
					//fmt.Printf("amount is %v, Minmoney is %v\r\n", amount, send.MinMoney)
					if amount >= send.MinMoney {
						err = SendMessage(send)
						if err != nil {
							fmt.Printf("faield to send message %v\r\n", err)
						}
					}
				}
			}
		}
	}

}
Exemplo n.º 2
0
func Follow() {
	var block_index uint64
	block_index, err := dbutil.LastBlockIndex()
	if err != nil {
		fmt.Println("Get lastblockindex failed")
		//return
		block_index = 0
	}
	if block_index == 0 {
		fmt.Println("block table in database is empty")
	}
	block_index++
	fmt.Print("The last block_index++ in db is ", block_index)
	if block_index < startblockindex {
		block_index = startblockindex
	}
	fmt.Printf("block_index is %v\r\n", block_index)

	var dbtran conf.DB_transaction
	dbtran, err = dbutil.GetLastTran()
	if err != nil {
		fmt.Println("GetLastTran error")
	}

	tx_index := dbtran.Tx_index + 1
	for {
		tempblockcount, err := bitcoinchain.GetBlockCount()
		GlobalLastBlockIndex = tempblockcount
		if err != nil {
			fmt.Printf("get tempblockcount failed %v\r\n", err)
			continue
		}
		//fmt.Printf("block_index is %v, templockcount is %v\r\n", block_index, tempblockcount)
		GlobalParsedBlockIndex = block_index
		if block_index <= tempblockcount {
			c := block_index
			requires_rollback := false
			for {
				if c == startblockindex {
					fmt.Printf("Reach the start...")
					break
				}
				c_block, _ := bitcoinchain.GetBlockByIndex(c)
				bitcoind_parent := c_block.PreviousHash
				block, err := dbutil.GetBlock(c - 1)
				if err != nil {
					fmt.Printf("dbutil.Getblock failed [%d]\r\n", c-1)
					break
				}
				db_parent := block.Block_hash
				if db_parent == bitcoind_parent {
					break
				} else {
					c -= 1
					requires_rollback = true
				}
			}
			if requires_rollback {
				fmt.Printf("status:Blockchain reorganisation at block %v\n", c)
				Reparse(c - 1)
				block_index = c
				continue
			}
			block_hash, _ := bitcoinchain.GetBlockHashString(block_index)
			block, _ := bitcoinchain.GetBlockByIndex(block_index)
			block_time := block.Time

			rawblock, _ := bitcoinchain.GetRawBlock(block_index)
			all_transaction_in_block := rawblock.Transactions()
			fmt.Printf("Insert block[%d] into db\r\n", block_index)

			for _, tx := range all_transaction_in_block {
				fmt.Printf("Try to parse tx %v\r\n", tx.Sha())
				_, err := dbutil.GetTran(tx.Sha().String())
				if err == nil {
					tx_index += 1
					fmt.Printf("the tx_index[%d] already in db, next\r\n", tx_index-1)
					continue
				}
				msgtx := tx.MsgTx()

				source, destination, btc_amount, fee, data := get_tx_info(msgtx, block_index)
				for _, value := range data {
					if source != "" && (value != "" || destination == conf.WISHINGWALLADDRESS) {

						dbutil.InsertTran(tx_index, tx.Sha().String(), block_index, block_hash, uint64(block_time), source, destination, btc_amount, fee, value)
					}
				}
				tx_index += 1
			}
			dbutil.InsertBlock(block_index, block_hash, uint64(block_time))
			Parse_block(block_index, uint64(block_time), "", "")
			block_index++
		}
		if block_index >= GlobalLastBlockIndex-2 {
			//fmt.Printf("sleep 1 second\r\n")
			time.Sleep(1 * time.Second)
		}
	}

}