Пример #1
0
func (self *XEth) AllLogs(earliest, latest int64, skip, max int, address []string, topics [][]string) vm.Logs {
	filter := filters.New(self.backend.ChainDb())
	filter.SetBeginBlock(earliest)
	filter.SetEndBlock(latest)
	filter.SetAddresses(cAddress(address))
	filter.SetTopics(cTopics(topics))

	return filter.Find()
}
Пример #2
0
// Start Go API. Not important for this version
func (c *Contract) Subscribe(key *ecdsa.PrivateKey, serviceId *big.Int, amount, price *big.Int, cb func(*Subscription)) (*types.Transaction, error) {
	from := crypto.PubkeyToAddress(key.PublicKey)

	data, err := c.abi.Pack("subscribe", serviceId)
	if err != nil {
		return nil, err
	}

	statedb, err := c.blockchain.State()
	if err != nil {
		return nil, err
	}

	transaction, err := types.NewTransaction(statedb.GetNonce(from), contractAddress, amount, big.NewInt(600000), big.NewInt(50000000000), data).SignECDSA(key)
	if err != nil {
		return nil, err
	}

	evId := c.abi.Events["NewSubscription"].Id()
	filter := filters.New(c.db)
	filter.SetAddresses([]common.Address{contractAddress})
	filter.SetTopics([][]common.Hash{ // TODO refactor, helper
		[]common.Hash{evId},
		[]common.Hash{from.Hash()},
		[]common.Hash{common.BigToHash(serviceId)},
	})
	filter.SetBeginBlock(0)
	filter.SetEndBlock(-1)
	filter.LogCallback = func(log *vm.Log, removed bool) {
		// TODO: do to and from validation here
		/*
			from := log.Topics[1]
			to := log.Topics[2]
		*/
		subscriptionId := common.BytesToHash(log.Data[0:31])
		nonce := common.BytesToBig(log.Data[31:])

		c.channelMu.Lock()
		defer c.channelMu.Unlock()

		channel, exist := c.subs[subscriptionId]
		if !exist {
			channel = NewSubscription(c, subscriptionId, from, serviceId, nonce)
			c.subs[subscriptionId] = channel
		}
		cb(channel)
	}

	c.filters.Add(filter, filters.PendingLogFilter)

	return transaction, nil
}
Пример #3
0
func (self *XEth) NewBlockFilter() int {
	self.blockMu.Lock()
	defer self.blockMu.Unlock()

	filter := filters.New(self.backend.ChainDb())
	id := self.filterManager.Add(filter)
	self.blockQueue[id] = &hashQueue{timeout: time.Now()}

	filter.BlockCallback = func(block *types.Block, logs vm.Logs) {
		self.blockMu.Lock()
		defer self.blockMu.Unlock()

		if queue := self.blockQueue[id]; queue != nil {
			queue.add(block.Hash())
		}
	}
	return id
}
Пример #4
0
func (self *XEth) NewTransactionFilter() int {
	self.transactionMu.Lock()
	defer self.transactionMu.Unlock()

	filter := filters.New(self.backend.ChainDb())
	id := self.filterManager.Add(filter)
	self.transactionQueue[id] = &hashQueue{timeout: time.Now()}

	filter.TransactionCallback = func(tx *types.Transaction) {
		self.transactionMu.Lock()
		defer self.transactionMu.Unlock()

		if queue := self.transactionQueue[id]; queue != nil {
			queue.add(tx.Hash())
		}
	}
	return id
}
Пример #5
0
func (self *XEth) NewLogFilter(earliest, latest int64, skip, max int, address []string, topics [][]string) int {
	self.logMu.Lock()
	defer self.logMu.Unlock()

	filter := filters.New(self.backend.ChainDb())
	id := self.filterManager.Add(filter)
	self.logQueue[id] = &logQueue{timeout: time.Now()}

	filter.SetBeginBlock(earliest)
	filter.SetEndBlock(latest)
	filter.SetAddresses(cAddress(address))
	filter.SetTopics(cTopics(topics))
	filter.LogsCallback = func(logs vm.Logs) {
		self.logMu.Lock()
		defer self.logMu.Unlock()

		if queue := self.logQueue[id]; queue != nil {
			queue.add(logs...)
		}
	}

	return id
}