Example #1
0
func (gui *Gui) readPreviousTransactions() {
	it := gui.txDb.Db().NewIterator(nil, nil)
	addr := gui.address()
	for it.Next() {
		tx := ethchain.NewTransactionFromBytes(it.Value())

		var inout string
		if bytes.Compare(tx.Sender(), addr) == 0 {
			inout = "send"
		} else {
			inout = "recv"
		}

		gui.win.Root().Call("addTx", ethpub.NewPTx(tx), inout)

	}
	it.Release()
}
Example #2
0
// Simple go routine function that updates the list of peers in the GUI
func (gui *Gui) update() {
	reactor := gui.eth.Reactor()

	var (
		blockChan     = make(chan ethutil.React, 1)
		txChan        = make(chan ethutil.React, 1)
		objectChan    = make(chan ethutil.React, 1)
		peerChan      = make(chan ethutil.React, 1)
		chainSyncChan = make(chan ethutil.React, 1)
		miningChan    = make(chan ethutil.React, 1)
	)

	reactor.Subscribe("newBlock", blockChan)
	reactor.Subscribe("newTx:pre", txChan)
	reactor.Subscribe("newTx:post", txChan)
	reactor.Subscribe("chainSync", chainSyncChan)
	reactor.Subscribe("miner:start", miningChan)
	reactor.Subscribe("miner:stop", miningChan)

	nameReg := ethpub.EthereumConfig(gui.eth.StateManager()).NameReg()
	if nameReg != nil {
		reactor.Subscribe("object:"+string(nameReg.Address()), objectChan)
	}
	reactor.Subscribe("peerList", peerChan)

	peerUpdateTicker := time.NewTicker(5 * time.Second)
	generalUpdateTicker := time.NewTicker(1 * time.Second)

	state := gui.eth.StateManager().TransState()

	unconfirmedFunds := new(big.Int)
	gui.win.Root().Call("setWalletValue", fmt.Sprintf("%v", ethutil.CurrencyToString(state.GetAccount(gui.address()).Amount)))
	gui.getObjectByName("syncProgressIndicator").Set("visible", !gui.eth.IsUpToDate())

	lastBlockLabel := gui.getObjectByName("lastBlockLabel")

	for {
		select {
		case b := <-blockChan:
			block := b.Resource.(*ethchain.Block)
			gui.processBlock(block, false)
			if bytes.Compare(block.Coinbase, gui.address()) == 0 {
				gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Amount, nil)
			}

		case txMsg := <-txChan:
			tx := txMsg.Resource.(*ethchain.Transaction)

			if txMsg.Event == "newTx:pre" {
				object := state.GetAccount(gui.address())

				if bytes.Compare(tx.Sender(), gui.address()) == 0 {
					gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "send")
					gui.txDb.Put(tx.Hash(), tx.RlpEncode())

					unconfirmedFunds.Sub(unconfirmedFunds, tx.Value)
				} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
					gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv")
					gui.txDb.Put(tx.Hash(), tx.RlpEncode())

					unconfirmedFunds.Add(unconfirmedFunds, tx.Value)
				}

				gui.setWalletValue(object.Amount, unconfirmedFunds)
			} else {
				object := state.GetAccount(gui.address())
				if bytes.Compare(tx.Sender(), gui.address()) == 0 {
					object.SubAmount(tx.Value)
				} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
					object.AddAmount(tx.Value)
				}

				gui.setWalletValue(object.Amount, nil)

				state.UpdateStateObject(object)
			}
		case msg := <-chainSyncChan:
			sync := msg.Resource.(bool)
			gui.win.Root().ObjectByName("syncProgressIndicator").Set("visible", sync)

		case <-objectChan:
			gui.loadAddressBook()
		case <-peerChan:
			gui.setPeerInfo()
		case <-peerUpdateTicker.C:
			gui.setPeerInfo()
		case msg := <-miningChan:
			if msg.Event == "miner:start" {
				gui.miner = msg.Resource.(*ethminer.Miner)
			} else {
				gui.miner = nil
			}

		case <-generalUpdateTicker.C:
			statusText := "#" + gui.eth.BlockChain().CurrentBlock.Number.String()
			if gui.miner != nil {
				pow := gui.miner.GetPow()
				if pow.GetHashrate() != 0 {
					statusText = "Mining @ " + strconv.FormatInt(pow.GetHashrate(), 10) + "Khash - " + statusText
				}
			}
			lastBlockLabel.Set("text", statusText)
		}
	}
}