diff options
-rw-r--r-- | ethchain/filter.go | 111 | ||||
-rw-r--r-- | ethchain/state_manager.go | 48 | ||||
-rw-r--r-- | ethpub/types.go | 9 | ||||
-rw-r--r-- | ethstate/manifest.go | 3 |
4 files changed, 112 insertions, 59 deletions
diff --git a/ethchain/filter.go b/ethchain/filter.go index c3b0a7f94..4f3160b90 100644 --- a/ethchain/filter.go +++ b/ethchain/filter.go @@ -14,7 +14,7 @@ type Filter struct { earliest []byte latest []byte skip int - from, to []byte + from, to [][]byte max int } @@ -24,6 +24,46 @@ func NewFilter(eth EthManager) *Filter { return &Filter{eth: eth} } +func NewFilterFromMap(object map[string]interface{}, eth EthManager) *Filter { + filter := NewFilter(eth) + + if object["earliest"] != nil { + earliest := object["earliest"] + if e, ok := earliest.(string); ok { + filter.SetEarliestBlock(ethutil.Hex2Bytes(e)) + } else { + filter.SetEarliestBlock(earliest) + } + } + + if object["latest"] != nil { + latest := object["latest"] + if l, ok := latest.(string); ok { + filter.SetLatestBlock(ethutil.Hex2Bytes(l)) + } else { + filter.SetLatestBlock(latest) + } + } + + if object["to"] != nil { + filter.AddTo(ethutil.Hex2Bytes(object["to"].(string))) + } + + if object["from"] != nil { + filter.AddFrom(ethutil.Hex2Bytes(object["from"].(string))) + } + + if object["max"] != nil { + filter.SetMax(object["max"].(int)) + } + + if object["skip"] != nil { + filter.SetSkip(object["skip"].(int)) + } + + return filter +} + // Set the earliest and latest block for filtering. // -1 = latest block (i.e., the current block) // hash = particular hash from-to @@ -53,14 +93,22 @@ func (self *Filter) SetLatestBlock(latest interface{}) { } } -func (self *Filter) SetFrom(addr []byte) { +func (self *Filter) SetFrom(addr [][]byte) { self.from = addr } -func (self *Filter) SetTo(addr []byte) { +func (self *Filter) AddFrom(addr []byte) { + self.from = append(self.from, addr) +} + +func (self *Filter) SetTo(addr [][]byte) { self.to = addr } +func (self *Filter) AddTo(addr []byte) { + self.from = append(self.to, addr) +} + func (self *Filter) SetMax(max int) { self.max = max } @@ -101,21 +149,37 @@ func (self *Filter) Find() []*ethstate.Message { break } - // Filter the messages for interesting stuff - for _, message := range msgs { - if len(self.to) > 0 && bytes.Compare(message.To, self.to) != 0 { - continue - } + messages = append(messages, self.FilterMessages(msgs)...) + } + + block = self.eth.BlockChain().GetBlock(block.PrevHash) + } - if len(self.from) > 0 && bytes.Compare(message.From, self.from) != 0 { - continue - } + return messages +} - messages = append(messages, message) +func (self *Filter) FilterMessages(msgs []*ethstate.Message) []*ethstate.Message { + var messages []*ethstate.Message + includes := func(addresses [][]byte, a []byte) (found bool) { + for _, addr := range addresses { + if bytes.Compare(addr, a) == 0 { + return true } } - block = self.eth.BlockChain().GetBlock(block.PrevHash) + return + } + // Filter the messages for interesting stuff + for _, message := range msgs { + if len(self.to) > 0 && !includes(self.to, message.To) { + continue + } + + if len(self.from) > 0 && !includes(self.from, message.From) { + continue + } + + messages = append(messages, message) } return messages @@ -130,17 +194,28 @@ func (self *Filter) bloomFilter(block *Block) bool { bloom := NewBloomFilter(bin) + var fromIncluded, toIncluded bool if len(self.from) > 0 { - if !bloom.Search(self.from) { - return false + for _, from := range self.from { + if bloom.Search(from) { + fromIncluded = true + break + } } + } else { + fromIncluded = true } if len(self.to) > 0 { - if !bloom.Search(self.to) { - return false + for _, to := range self.to { + if bloom.Search(to) { + toIncluded = true + break + } } + } else { + toIncluded = true } - return true + return fromIncluded && toIncluded } diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index a60b28b3f..1f47a2e0b 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -380,14 +380,18 @@ func (sm *StateManager) createBloomFilter(state *ethstate.State) *BloomFilter { sm.Ethereum.Reactor().Post("object:"+addr, stateObject) } - for stateObjectAddr, mappedObjects := range state.Manifest().StorageChanges { - for addr, value := range mappedObjects { - // Set the bloom filter's bin - bloomf.Set(ethcrypto.Sha3Bin([]byte(stateObjectAddr + addr))) + sm.Ethereum.Reactor().Post("messages", state.Manifest().Messages) - sm.Ethereum.Reactor().Post("storage:"+stateObjectAddr+":"+addr, ðstate.StorageState{[]byte(stateObjectAddr), []byte(addr), value}) + /* + for stateObjectAddr, mappedObjects := range state.Manifest().StorageChanges { + for addr, value := range mappedObjects { + // Set the bloom filter's bin + bloomf.Set(ethcrypto.Sha3Bin([]byte(stateObjectAddr + addr))) + + sm.Ethereum.Reactor().Post("storage:"+stateObjectAddr+":"+addr, ðstate.StorageState{[]byte(stateObjectAddr), []byte(addr), value}) + } } - } + */ return bloomf } @@ -406,37 +410,9 @@ func (sm *StateManager) GetMessages(block *Block) (messages []*ethstate.Message, defer state.Reset() - if ethutil.Config.Diff && ethutil.Config.DiffType == "all" { - fmt.Printf("## %x %x ##\n", block.Hash(), block.Number) - } + sm.ApplyDiff(state, parent, block) - receipts, err := sm.ApplyDiff(state, parent, block) - if err != nil { - return nil, err - } - - txSha := CreateTxSha(receipts) - if bytes.Compare(txSha, block.TxSha) != 0 { - return nil, fmt.Errorf("Error validating tx sha. Received %x, got %x", block.TxSha, txSha) - } - - // Block validation - if err = sm.ValidateBlock(block); err != nil { - statelogger.Errorln("Error validating block:", err) - return nil, err - } - - // I'm not sure, but I don't know if there should be thrown - // any errors at this time. - if err = sm.AccumelateRewards(state, block); err != nil { - statelogger.Errorln("Error accumulating reward", err) - return nil, err - } - - if !block.State().Cmp(state) { - err = fmt.Errorf("Invalid merkle root.\nrec: %x\nis: %x", block.State().Trie.Root, state.Trie.Root) - return nil, err - } + sm.AccumelateRewards(state, block) return state.Manifest().Messages, nil } diff --git a/ethpub/types.go b/ethpub/types.go index faf75bbe1..159f7d9a7 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -221,15 +221,16 @@ func (self *PStateObject) EachStorage(cb ethtrie.EachCallback) { } type KeyVal struct { - Key string - Value string + Key string `json:"key"` + Value string `json:"value"` } func (c *PStateObject) StateKeyVal(asJson bool) interface{} { var values []KeyVal if c.object != nil { c.object.EachStorage(func(name string, value *ethutil.Value) { - values = append(values, KeyVal{name, ethutil.Bytes2Hex(value.Bytes())}) + value.Decode() + values = append(values, KeyVal{ethutil.Bytes2Hex([]byte(name)), ethutil.Bytes2Hex(value.Bytes())}) }) } @@ -238,7 +239,7 @@ func (c *PStateObject) StateKeyVal(asJson bool) interface{} { if err != nil { return nil } - fmt.Println(string(valuesJson)) + return string(valuesJson) } diff --git a/ethstate/manifest.go b/ethstate/manifest.go index b771127a6..88ce673b9 100644 --- a/ethstate/manifest.go +++ b/ethstate/manifest.go @@ -17,7 +17,7 @@ type Manifest struct { ObjectChanges map[string]*StateObject StorageChanges map[string]map[string]*big.Int - Messages []*Message + Messages Messages } func NewManifest() *Manifest { @@ -50,6 +50,7 @@ func (self *Manifest) AddMessage(msg *Message) *Message { return msg } +type Messages []*Message type Message struct { To, From []byte Input []byte |