Fixed messages to use proper numbers

This commit is contained in:
obscuren 2014-09-26 13:32:54 +02:00
parent 9ed8dc7384
commit 68119d0929
3 changed files with 54 additions and 55 deletions

View File

@ -236,6 +236,16 @@ func (self *BlockChain) GetBlockByNumber(num uint64) *Block {
return block return block
} }
func (self *BlockChain) GetBlockBack(num uint64) *Block {
block := self.CurrentBlock
for ; num != 0 && block != nil; num-- {
block = self.GetBlock(block.PrevHash)
}
return block
}
func (bc *BlockChain) BlockInfoByHash(hash []byte) BlockInfo { func (bc *BlockChain) BlockInfoByHash(hash []byte) BlockInfo {
bi := BlockInfo{} bi := BlockInfo{}
data, _ := ethutil.Config.Db.Get(append(hash, []byte("Info")...)) data, _ := ethutil.Config.Db.Get(append(hash, []byte("Info")...))

View File

@ -3,6 +3,7 @@ package ethchain
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math"
"github.com/ethereum/eth-go/ethstate" "github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethutil"
@ -16,8 +17,8 @@ type data struct {
// Filtering interface // Filtering interface
type Filter struct { type Filter struct {
eth EthManager eth EthManager
earliest []byte earliest int64
latest []byte latest int64
skip int skip int
from, to [][]byte from, to [][]byte
max int max int
@ -38,37 +39,33 @@ func NewFilterFromMap(object map[string]interface{}, eth EthManager) *Filter {
filter := NewFilter(eth) filter := NewFilter(eth)
if object["earliest"] != nil { if object["earliest"] != nil {
earliest := object["earliest"] val := ethutil.NewValue(object["earliest"])
if e, ok := earliest.(string); ok { filter.SetEarliestBlock(val.Int())
filter.SetEarliestBlock(ethutil.Hex2Bytes(e))
} else {
filter.SetEarliestBlock(earliest)
}
} }
if object["latest"] != nil { if object["latest"] != nil {
latest := object["latest"] val := ethutil.NewValue(object["latest"])
if l, ok := latest.(string); ok { filter.SetLatestBlock(val.Int())
filter.SetLatestBlock(ethutil.Hex2Bytes(l))
} else {
filter.SetLatestBlock(latest)
}
} }
if object["to"] != nil { if object["to"] != nil {
filter.AddTo(ethutil.Hex2Bytes(object["to"].(string))) val := ethutil.NewValue(object["to"])
filter.AddTo(ethutil.Hex2Bytes(val.Str()))
} }
if object["from"] != nil { if object["from"] != nil {
filter.AddFrom(ethutil.Hex2Bytes(object["from"].(string))) val := ethutil.NewValue(object["from"])
filter.AddFrom(ethutil.Hex2Bytes(val.Str()))
} }
if object["max"] != nil { if object["max"] != nil {
filter.SetMax(object["max"].(int)) val := ethutil.NewValue(object["max"])
filter.SetMax(int(val.Uint()))
} }
if object["skip"] != nil { if object["skip"] != nil {
filter.SetSkip(object["skip"].(int)) val := ethutil.NewValue(object["skip"])
filter.SetSkip(int(val.Uint()))
} }
if object["altered"] != nil { if object["altered"] != nil {
@ -85,30 +82,12 @@ func (self *Filter) AddAltered(id, address []byte) {
// Set the earliest and latest block for filtering. // Set the earliest and latest block for filtering.
// -1 = latest block (i.e., the current block) // -1 = latest block (i.e., the current block)
// hash = particular hash from-to // hash = particular hash from-to
func (self *Filter) SetEarliestBlock(earliest interface{}) { func (self *Filter) SetEarliestBlock(earliest int64) {
e := ethutil.NewValue(earliest) self.earliest = earliest
// Check for -1 (latest) otherwise assume bytes
if e.Int() == -1 {
self.earliest = self.eth.BlockChain().CurrentBlock.Hash()
} else if e.Len() > 0 {
self.earliest = e.Bytes()
} else {
panic(fmt.Sprintf("earliest has to be either -1 or a valid hash: %v (%T)", e, e.Val))
}
} }
func (self *Filter) SetLatestBlock(latest interface{}) { func (self *Filter) SetLatestBlock(latest int64) {
l := ethutil.NewValue(latest) self.latest = latest
// Check for -1 (latest) otherwise assume bytes
if l.Int() == -1 {
self.latest = self.eth.BlockChain().CurrentBlock.Hash()
} else if l.Len() > 0 {
self.latest = l.Bytes()
} else {
panic(fmt.Sprintf("latest has to be either -1 or a valid hash: %v", l))
}
} }
func (self *Filter) SetFrom(addr [][]byte) { func (self *Filter) SetFrom(addr [][]byte) {
@ -137,23 +116,27 @@ func (self *Filter) SetSkip(skip int) {
// Run filters messages with the current parameters set // Run filters messages with the current parameters set
func (self *Filter) Find() []*ethstate.Message { func (self *Filter) Find() []*ethstate.Message {
var messages []*ethstate.Message var earliestBlockNo uint64 = uint64(self.earliest)
if self.earliest == -1 {
block := self.eth.BlockChain().GetBlock(self.latest) earliestBlockNo = self.eth.BlockChain().CurrentBlock.Number.Uint64()
// skip N blocks (useful for pagination)
if self.skip > 0 {
for i := 0; i < i; i++ {
block = self.eth.BlockChain().GetBlock(block.PrevHash)
} }
var latestBlockNo uint64 = uint64(self.latest)
if self.latest == -1 {
latestBlockNo = self.eth.BlockChain().CurrentBlock.Number.Uint64()
} }
// Start block filtering var (
quit := false messages []*ethstate.Message
for i := 1; !quit && block != nil; i++ { block = self.eth.BlockChain().GetBlockByNumber(latestBlockNo)
// Mark last check quit bool
if self.max == i || (len(self.earliest) > 0 && bytes.Compare(block.Hash(), self.earliest) == 0) { )
for i := 0; !quit && block != nil; i++ {
// Quit on latest
switch {
case block.Number.Uint64() == earliestBlockNo:
quit = true quit = true
case self.max <= len(messages):
break
} }
// Use bloom filtering to see if this block is interesting given the // Use bloom filtering to see if this block is interesting given the
@ -173,7 +156,9 @@ func (self *Filter) Find() []*ethstate.Message {
block = self.eth.BlockChain().GetBlock(block.PrevHash) block = self.eth.BlockChain().GetBlock(block.PrevHash)
} }
return messages skip := int(math.Min(float64(len(messages)), float64(self.skip)))
return messages[skip:]
} }
func includes(addresses [][]byte, a []byte) (found bool) { func includes(addresses [][]byte, a []byte) (found bool) {

View File

@ -63,6 +63,10 @@ func (val *Value) Uint() uint64 {
return uint64(Val) return uint64(Val)
} else if Val, ok := val.Val.(uint64); ok { } else if Val, ok := val.Val.(uint64); ok {
return Val return Val
} else if Val, ok := val.Val.(float32); ok {
return uint64(Val)
} else if Val, ok := val.Val.(float64); ok {
return uint64(Val)
} else if Val, ok := val.Val.(int); ok { } else if Val, ok := val.Val.(int); ok {
return uint64(Val) return uint64(Val)
} else if Val, ok := val.Val.(uint); ok { } else if Val, ok := val.Val.(uint); ok {