From 24a6d87c3f4bc69fdd1c619b36f8b74a61fd8bae Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 26 May 2014 11:47:47 +0200 Subject: Don't handshake if there is no key yet; first start on Ethereal --- peer.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/peer.go b/peer.go index 7e505d680..d613bf6ff 100644 --- a/peer.go +++ b/peer.go @@ -531,13 +531,16 @@ func (p *Peer) Stop() { } func (p *Peer) pushHandshake() error { - pubkey := ethutil.GetKeyRing().Get(0).PublicKey + keyRing := ethutil.GetKeyRing().Get(0) + if keyRing != nil { + pubkey := keyRing.PublicKey - msg := ethwire.NewMessage(ethwire.MsgHandshakeTy, []interface{}{ - uint32(ProtocolVersion), uint32(0), p.Version, byte(p.caps), p.port, pubkey[1:], - }) + msg := ethwire.NewMessage(ethwire.MsgHandshakeTy, []interface{}{ + uint32(ProtocolVersion), uint32(0), p.Version, byte(p.caps), p.port, pubkey[1:], + }) - p.QueueMessage(msg) + p.QueueMessage(msg) + } return nil } -- cgit v1.2.3 From aefcb834199a64ff62daf7e685bbe5088c08a11e Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 26 May 2014 17:07:04 +0200 Subject: Updated Public Block and Transaction objects with more information --- ethpub/types.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/ethpub/types.go b/ethpub/types.go index e8a2164a7..edef7d50e 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -2,16 +2,20 @@ package ethpub import ( "encoding/hex" + "encoding/json" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" + _ "log" "strings" ) // Block interface exposed to QML type PBlock struct { - ref *ethchain.Block - Number int `json:"number"` - Hash string `json:"hash"` + ref *ethchain.Block + Number int `json:"number"` + Hash string `json:"hash"` + Transactions string `json:"transactions"` + Time int64 `json:"time"` } // Creates a new QML Block from a chain block @@ -19,8 +23,17 @@ func NewPBlock(block *ethchain.Block) *PBlock { if block == nil { return nil } + var ptxs []PTx + for _, tx := range block.Transactions() { + ptxs = append(ptxs, *NewPTx(tx)) + } + + b, err := json.Marshal(ptxs) + if err != nil { + return nil + } - return &PBlock{ref: block, Number: int(block.Number.Uint64()), Hash: ethutil.Hex(block.Hash())} + return &PBlock{ref: block, Number: int(block.Number.Uint64()), Hash: ethutil.Hex(block.Hash()), Transactions: string(b), Time: block.Time} } func (self *PBlock) ToString() string { @@ -43,16 +56,23 @@ func (self *PBlock) GetTransaction(hash string) *PTx { type PTx struct { ref *ethchain.Transaction - Value, Hash, Address string - Contract bool + Value string `json:"value"` + Gas string `json:"gas"` + GasPrice string `json:"gasPrice"` + Hash string `json:"hash"` + Address string `json:"address"` + Sender string `json:"sender"` + Data string `json:"data"` + Contract bool `json:"isContract"` } func NewPTx(tx *ethchain.Transaction) *PTx { hash := hex.EncodeToString(tx.Hash()) - sender := hex.EncodeToString(tx.Recipient) + receiver := hex.EncodeToString(tx.Recipient) + sender := hex.EncodeToString(tx.Sender()) isContract := len(tx.Data) > 0 - return &PTx{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value), Address: sender, Contract: isContract} + return &PTx{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value), Address: receiver, Contract: isContract, Gas: tx.Gas.String(), GasPrice: tx.GasPrice.String(), Data: hex.EncodeToString(tx.Data), Sender: sender} } func (self *PTx) ToString() string { -- cgit v1.2.3 From 2232974cda9ed73c62a370545742ef88332c5f1e Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 26 May 2014 17:18:51 +0200 Subject: Changed var names around --- ethpub/types.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ethpub/types.go b/ethpub/types.go index edef7d50e..af3cfe2cd 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -23,17 +23,18 @@ func NewPBlock(block *ethchain.Block) *PBlock { if block == nil { return nil } + var ptxs []PTx for _, tx := range block.Transactions() { ptxs = append(ptxs, *NewPTx(tx)) } - b, err := json.Marshal(ptxs) + txJson, err := json.Marshal(ptxs) if err != nil { return nil } - return &PBlock{ref: block, Number: int(block.Number.Uint64()), Hash: ethutil.Hex(block.Hash()), Transactions: string(b), Time: block.Time} + return &PBlock{ref: block, Number: int(block.Number.Uint64()), Hash: ethutil.Hex(block.Hash()), Transactions: string(txJson), Time: block.Time} } func (self *PBlock) ToString() string { -- cgit v1.2.3 From 5cdfee51437532ccfb49e874fdbbea2702c3d13f Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 27 May 2014 01:08:51 +0200 Subject: New Trie iterator --- ethutil/encoding.go | 15 +++++++++++++++ ethutil/trie.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ ethutil/trie_test.go | 22 ++++++++++------------ 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/ethutil/encoding.go b/ethutil/encoding.go index 1f661947a..9fcdf3edf 100644 --- a/ethutil/encoding.go +++ b/ethutil/encoding.go @@ -59,3 +59,18 @@ func CompactHexDecode(str string) []int { return hexSlice } + +func DecodeCompact(key []int) string { + base := "0123456789abcdef" + var str string + + for _, v := range key { + if v < 16 { + str += string(base[v]) + } + } + + res, _ := hex.DecodeString(str) + + return string(res) +} diff --git a/ethutil/trie.go b/ethutil/trie.go index c993e4d8f..18d0a5f0a 100644 --- a/ethutil/trie.go +++ b/ethutil/trie.go @@ -442,6 +442,8 @@ type TrieIterator struct { shas [][]byte values []string + + lastNode []byte } func (t *Trie) NewIterator() *TrieIterator { @@ -513,3 +515,47 @@ func (it *TrieIterator) Key() string { func (it *TrieIterator) Value() string { return "" } + +type EachCallback func(key string, node *Value) + +func (it *TrieIterator) Each(cb EachCallback) { + it.fetchNode(nil, NewValue(it.trie.Root).Bytes(), cb) +} + +func (it *TrieIterator) fetchNode(key []int, node []byte, cb EachCallback) { + it.iterateNode(key, it.trie.cache.Get(node), cb) +} + +func (it *TrieIterator) iterateNode(key []int, currentNode *Value, cb EachCallback) { + if currentNode.Len() == 2 { + k := CompactDecode(currentNode.Get(0).Str()) + + if currentNode.Get(1).Str() == "" { + it.iterateNode(key, currentNode.Get(1), cb) + } else { + pk := append(key, k...) + + if k[len(k)-1] == 16 { + cb(DecodeCompact(pk), currentNode.Get(1)) + } else { + it.fetchNode(pk, currentNode.Get(1).Bytes(), cb) + } + } + } else { + for i := 0; i < currentNode.Len(); i++ { + pk := append(key, i) + if i == 16 && currentNode.Get(i).Len() != 0 { + cb(DecodeCompact(pk), currentNode.Get(i)) + } else { + if currentNode.Get(i).Str() == "" { + it.iterateNode(pk, currentNode.Get(i), cb) + } else { + val := currentNode.Get(i).Str() + if val != "" { + it.fetchNode(pk, []byte(val), cb) + } + } + } + } + } +} diff --git a/ethutil/trie_test.go b/ethutil/trie_test.go index d74d129ac..c89f2fbb7 100644 --- a/ethutil/trie_test.go +++ b/ethutil/trie_test.go @@ -154,7 +154,7 @@ func TestTrieDeleteWithValue(t *testing.T) { } -func TestTrieIterator(t *testing.T) { +func TestTriePurge(t *testing.T) { _, trie := New() trie.Update("c", LONG_WORD) trie.Update("ca", LONG_WORD) @@ -171,16 +171,14 @@ func TestTrieIterator(t *testing.T) { } } -func TestHashes(t *testing.T) { +func TestTrieIt(t *testing.T) { _, trie := New() - trie.Update("cat", "dog") - trie.Update("ca", "dude") - trie.Update("doge", "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ") - trie.Update("dog", "test") - trie.Update("test", "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ") - fmt.Printf("%x\n", trie.Root) - trie.Delete("dog") - fmt.Printf("%x\n", trie.Root) - trie.Delete("test") - fmt.Printf("%x\n", trie.Root) + trie.Update("c", LONG_WORD) + trie.Update("ca", LONG_WORD) + trie.Update("cat", LONG_WORD) + + it := trie.NewIterator() + it.Each(func(key string, node *Value) { + fmt.Println(key, ":", node.Str()) + }) } -- cgit v1.2.3 From c1b09d639cc922c7aa322ac56cc4f400d76431b4 Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 27 May 2014 10:38:31 +0200 Subject: Disamble tx data before adding it to PTx object --- ethpub/types.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ethpub/types.go b/ethpub/types.go index af3cfe2cd..348ae3f25 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -71,9 +71,11 @@ func NewPTx(tx *ethchain.Transaction) *PTx { hash := hex.EncodeToString(tx.Hash()) receiver := hex.EncodeToString(tx.Recipient) sender := hex.EncodeToString(tx.Sender()) + data := strings.Join(ethchain.Disassemble(tx.Data), "\n") + isContract := len(tx.Data) > 0 - return &PTx{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value), Address: receiver, Contract: isContract, Gas: tx.Gas.String(), GasPrice: tx.GasPrice.String(), Data: hex.EncodeToString(tx.Data), Sender: sender} + return &PTx{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value), Address: receiver, Contract: isContract, Gas: tx.Gas.String(), GasPrice: tx.GasPrice.String(), Data: data, Sender: sender} } func (self *PTx) ToString() string { -- cgit v1.2.3 From aba3066658a32723543f9e4eda74a14ef140fc0e Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 27 May 2014 13:10:18 +0200 Subject: Changed debug hook and added state iterator --- ethchain/closure.go | 2 +- ethchain/state.go | 5 +++++ ethchain/vm.go | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ethchain/closure.go b/ethchain/closure.go index 59194e4e8..e65cfd252 100644 --- a/ethchain/closure.go +++ b/ethchain/closure.go @@ -72,7 +72,7 @@ func (c *Closure) Address() []byte { return c.object.Address() } -type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack) +type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack, stateObject *StateObject) func (c *Closure) Call(vm *Vm, args []byte, hook DebugHook) ([]byte, error) { c.Args = args diff --git a/ethchain/state.go b/ethchain/state.go index e209e0e2f..5af748e00 100644 --- a/ethchain/state.go +++ b/ethchain/state.go @@ -49,6 +49,11 @@ func (s *State) Purge() int { return s.trie.NewIterator().Purge() } +func (s *State) EachStorage(cb ethutil.EachCallback) { + it := s.trie.NewIterator() + it.Each(cb) +} + func (s *State) GetStateObject(addr []byte) *StateObject { data := s.trie.Get(string(addr)) if data == "" { diff --git a/ethchain/vm.go b/ethchain/vm.go index e025920f3..0d9e8599e 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -543,7 +543,7 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) (ret []byte, err erro pc.Add(pc, ethutil.Big1) if hook != nil { - hook(step-1, op, mem, stack) + hook(step-1, op, mem, stack, closure.Object()) } } } -- cgit v1.2.3 From 4eb1771e67ee2c66d5fb86c2b5961d84e40f7dd0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 27 May 2014 13:32:31 +0200 Subject: Hooks can now quit the vm --- ethchain/closure.go | 2 +- ethchain/vm.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ethchain/closure.go b/ethchain/closure.go index e65cfd252..c935ed50a 100644 --- a/ethchain/closure.go +++ b/ethchain/closure.go @@ -72,7 +72,7 @@ func (c *Closure) Address() []byte { return c.object.Address() } -type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack, stateObject *StateObject) +type DebugHook func(step int, op OpCode, mem *Memory, stack *Stack, stateObject *StateObject) bool func (c *Closure) Call(vm *Vm, args []byte, hook DebugHook) ([]byte, error) { c.Args = args diff --git a/ethchain/vm.go b/ethchain/vm.go index 0d9e8599e..9821a839a 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -543,7 +543,9 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) (ret []byte, err erro pc.Add(pc, ethutil.Big1) if hook != nil { - hook(step-1, op, mem, stack, closure.Object()) + if !hook(step-1, op, mem, stack, closure.Object()) { + return nil, nil + } } } } -- cgit v1.2.3 From a4285331601d9d77458d0d714dd110144c796023 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 27 May 2014 16:10:18 +0200 Subject: bump --- ethutil/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethutil/config.go b/ethutil/config.go index 40ab3aa69..a7c318e52 100644 --- a/ethutil/config.go +++ b/ethutil/config.go @@ -43,7 +43,7 @@ func ReadConfig(base string, logTypes LoggerType, id string) *config { } } - Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC9"} + Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC10"} Config.Identifier = id Config.Log = NewLogger(logTypes, LogLevelDebug) Config.SetClientString("/Ethereum(G)") -- cgit v1.2.3