Esempio n. 1
0
// NewPendingTransaction creates a subscription that is triggered each time a transaction enters the transaction pool
// and is send from one of the transactions this nodes manages.
func (s *PublicTransactionPoolAPI) NewPendingTransactions() (rpc.Subscription, error) {
	sub := s.eventMux.Subscribe(core.TxPreEvent{})

	accounts, err := s.am.Accounts()
	if err != nil {
		return rpc.Subscription{}, err
	}
	accountSet := set.New()
	for _, account := range accounts {
		accountSet.Add(account.Address)
	}
	accountSetLastUpdates := time.Now()

	output := func(transaction interface{}) interface{} {
		if time.Since(accountSetLastUpdates) > (time.Duration(2) * time.Second) {
			if accounts, err = s.am.Accounts(); err != nil {
				accountSet.Clear()
				for _, account := range accounts {
					accountSet.Add(account.Address)
				}
				accountSetLastUpdates = time.Now()
			}
		}

		tx := transaction.(core.TxPreEvent)
		if from, err := tx.Tx.From(); err == nil {
			if accountSet.Has(from) {
				return tx.Tx.Hash()
			}
		}
		return nil
	}

	return rpc.NewSubscriptionWithOutputFormat(sub, output), nil
}
Esempio n. 2
0
// NewBlocks triggers a new block event each time a block is appended to the chain. It accepts an argument which allows
// the caller to specify whether the output should contain transactions and in what format.
func (s *PublicBlockChainAPI) NewBlocks(args NewBlocksArgs) (rpc.Subscription, error) {
	sub := s.eventMux.Subscribe(core.ChainEvent{})

	output := func(rawBlock interface{}) interface{} {
		if event, ok := rawBlock.(core.ChainEvent); ok {
			notification, err := s.rpcOutputBlock(event.Block, args.IncludeTransactions, args.TransactionDetails)
			if err == nil {
				return notification
			}
		}
		return rawBlock
	}

	return rpc.NewSubscriptionWithOutputFormat(sub, output), nil
}
Esempio n. 3
0
// Syncing provides information when this nodes starts synchronising with the Ethereumn network and when it's finished.
func (s *PublicDownloaderAPI) Syncing() (rpc.Subscription, error) {
	sub := s.d.mux.Subscribe(StartEvent{}, DoneEvent{}, FailedEvent{})

	output := func(event interface{}) interface{} {
		switch event.(type) {
		case StartEvent:
			result := &SyncingResult{Syncing: true}
			result.Status.Origin, result.Status.Current, result.Status.Height = s.d.Progress()
			return result
		case DoneEvent, FailedEvent:
			return false
		}
		return nil
	}

	return rpc.NewSubscriptionWithOutputFormat(sub, output), nil
}