diff options
Diffstat (limited to 'ethereal/ui')
-rw-r--r-- | ethereal/ui/debugger.go | 177 | ||||
-rw-r--r-- | ethereal/ui/gui.go | 20 | ||||
-rw-r--r-- | ethereal/ui/ui_lib.go | 64 |
3 files changed, 205 insertions, 56 deletions
diff --git a/ethereal/ui/debugger.go b/ethereal/ui/debugger.go new file mode 100644 index 000000000..8b27c2fe5 --- /dev/null +++ b/ethereal/ui/debugger.go @@ -0,0 +1,177 @@ +package ethui + +import ( + "fmt" + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethutil" + "github.com/go-qml/qml" + "math/big" + "strings" +) + +type DebuggerWindow struct { + win *qml.Window + engine *qml.Engine + lib *UiLib + Db *Debugger +} + +func NewDebuggerWindow(lib *UiLib) *DebuggerWindow { + engine := qml.NewEngine() + component, err := engine.LoadFile(lib.AssetPath("debugger/debugger.qml")) + if err != nil { + fmt.Println(err) + + return nil + } + + win := component.CreateWindow(nil) + db := &Debugger{win, make(chan bool), make(chan bool), true} + + return &DebuggerWindow{engine: engine, win: win, lib: lib, Db: db} +} + +func (self *DebuggerWindow) Show() { + context := self.engine.Context() + context.SetVar("dbg", self) + + go func() { + self.win.Show() + self.win.Wait() + }() +} + +func formatData(data string) []byte { + if len(data) == 0 { + return nil + } + // Simple stupid + d := new(big.Int) + if data[0:1] == "\"" && data[len(data)-1:] == "\"" { + d.SetBytes([]byte(data[1 : len(data)-1])) + } else if data[:2] == "0x" { + d.SetBytes(ethutil.FromHex(data[2:])) + } else { + d.SetString(data, 0) + } + + return ethutil.BigToBytes(d, 256) +} + +func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, dataStr string) { + if !self.Db.done { + self.Db.Q <- true + } + + dataSlice := strings.Split(dataStr, "\n") + var data []byte + for _, dataItem := range dataSlice { + d := formatData(dataItem) + data = append(data, d...) + } + + state := self.lib.eth.BlockChain().CurrentBlock.State() + + defer func() { + if r := recover(); r != nil { + fmt.Println(r) + } + }() + + script, err := ethutil.Compile(scriptStr) + if err != nil { + ethutil.Config.Log.Debugln(err) + + return + } + + dis := ethchain.Disassemble(script) + self.win.Root().Call("clearAsm") + + for _, str := range dis { + self.win.Root().Call("setAsm", str) + } + // Contract addr as test address + keyPair := ethutil.GetKeyRing().Get(0) + callerTx := ethchain.NewContractCreationTx(ethutil.Big(valueStr), ethutil.Big(gasStr), ethutil.Big(gasPriceStr), script) + callerTx.Sign(keyPair.PrivateKey) + + account := self.lib.eth.StateManager().TransState().GetAccount(keyPair.Address()) + contract := ethchain.MakeContract(callerTx, state) + callerClosure := ethchain.NewClosure(account, contract, script, state, ethutil.Big(gasStr), ethutil.Big(gasPriceStr)) + + block := self.lib.eth.BlockChain().CurrentBlock + vm := ethchain.NewVm(state, self.lib.eth.StateManager(), ethchain.RuntimeVars{ + Origin: account.Address(), + BlockNumber: block.BlockInfo().Number, + PrevHash: block.PrevHash, + Coinbase: block.Coinbase, + Time: block.Time, + Diff: block.Difficulty, + }) + + self.Db.done = false + go func() { + callerClosure.Call(vm, data, self.Db.halting) + + state.Reset() + + self.Db.done = true + }() +} + +func (self *DebuggerWindow) Next() { + self.Db.Next() +} + +type Debugger struct { + win *qml.Window + N chan bool + Q chan bool + done bool +} + +type storeVal struct { + Key, Value string +} + +func (d *Debugger) halting(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack, stateObject *ethchain.StateObject) bool { + d.win.Root().Call("setInstruction", pc) + d.win.Root().Call("clearMem") + d.win.Root().Call("clearStack") + d.win.Root().Call("clearStorage") + + addr := 0 + for i := 0; i+32 <= mem.Len(); i += 32 { + d.win.Root().Call("setMem", memAddr{fmt.Sprintf("%03d", addr), fmt.Sprintf("% x", mem.Data()[i:i+32])}) + addr++ + } + + for _, val := range stack.Data() { + d.win.Root().Call("setStack", val.String()) + } + + stateObject.State().EachStorage(func(key string, node *ethutil.Value) { + d.win.Root().Call("setStorage", storeVal{fmt.Sprintf("% x", key), fmt.Sprintf("% x", node.Str())}) + }) + +out: + for { + select { + case <-d.N: + break out + case <-d.Q: + d.done = true + + return false + } + } + + return true +} + +func (d *Debugger) Next() { + if !d.done { + d.N <- true + } +} diff --git a/ethereal/ui/gui.go b/ethereal/ui/gui.go index 8d6796ddb..63ab028ab 100644 --- a/ethereal/ui/gui.go +++ b/ethereal/ui/gui.go @@ -54,7 +54,7 @@ func New(ethereum *eth.Ethereum) *Gui { } func (gui *Gui) Start(assetPath string) { - const version = "0.5.0 RC9" + const version = "0.5.0 RC10" defer gui.txDb.Close() @@ -130,20 +130,19 @@ func (gui *Gui) createWindow(comp qml.Object) *qml.Window { gui.win = win gui.uiLib.win = win - db := &Debugger{gui.win, make(chan bool)} + db := &Debugger{gui.win, make(chan bool), make(chan bool), true} gui.lib.Db = db gui.uiLib.Db = db return gui.win } - func (gui *Gui) setInitialBlockChain() { - // Load previous 10 blocks - chain := gui.eth.BlockChain().GetChain(gui.eth.BlockChain().CurrentBlock.Hash(), 10) - for _, block := range chain { - gui.processBlock(block) + sBlk := gui.eth.BlockChain().LastBlockHash + blk := gui.eth.BlockChain().GetBlock(sBlk) + for ; blk != nil; blk = gui.eth.BlockChain().GetBlock(sBlk) { + sBlk = blk.PrevHash + gui.processBlock(blk, true) } - } func (gui *Gui) readPreviousTransactions() { @@ -164,8 +163,8 @@ func (gui *Gui) readPreviousTransactions() { it.Release() } -func (gui *Gui) processBlock(block *ethchain.Block) { - gui.win.Root().Call("addBlock", ethpub.NewPBlock(block)) +func (gui *Gui) processBlock(block *ethchain.Block, initial bool) { + gui.win.Root().Call("addBlock", ethpub.NewPBlock(block), initial) } func (gui *Gui) setWalletValue(amount, unconfirmedFunds *big.Int) { @@ -204,6 +203,7 @@ func (gui *Gui) update() { select { case b := <-blockChan: block := b.Resource.(*ethchain.Block) + gui.processBlock(block, false) if bytes.Compare(block.Coinbase, gui.addr) == 0 { gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.addr).Amount, nil) } diff --git a/ethereal/ui/ui_lib.go b/ethereal/ui/ui_lib.go index 1c88b0181..998392525 100644 --- a/ethereal/ui/ui_lib.go +++ b/ethereal/ui/ui_lib.go @@ -2,13 +2,10 @@ package ethui import ( "bitbucket.org/kardianos/osext" - "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" - "github.com/ethereum/go-ethereum/utils" "github.com/go-qml/qml" - "github.com/obscuren/mutan" "os" "path" "path/filepath" @@ -92,6 +89,12 @@ func (ui *UiLib) AssetPath(p string) string { return path.Join(ui.assetPath, p) } +func (self *UiLib) StartDebugger() { + dbWindow := NewDebuggerWindow(self) + + dbWindow.Show() +} + func DefaultAssetPath() string { var base string // If the current working directory is the go-ethereum dir @@ -121,27 +124,28 @@ func DefaultAssetPath() string { func (ui *UiLib) DebugTx(recipient, valueStr, gasStr, gasPriceStr, data string) { state := ui.eth.BlockChain().CurrentBlock.State() - mainInput, _ := mutan.PreParse(data) - callerScript, err := utils.Compile(mainInput) + script, err := ethutil.Compile(data) if err != nil { ethutil.Config.Log.Debugln(err) return } - dis := ethchain.Disassemble(callerScript) + dis := ethchain.Disassemble(script) ui.win.Root().Call("clearAsm") for _, str := range dis { ui.win.Root().Call("setAsm", str) } - callerTx := ethchain.NewContractCreationTx(ethutil.Big(valueStr), ethutil.Big(gasStr), ethutil.Big(gasPriceStr), nil) - // Contract addr as test address keyPair := ethutil.GetKeyRing().Get(0) + callerTx := + ethchain.NewContractCreationTx(ethutil.Big(valueStr), ethutil.Big(gasStr), ethutil.Big(gasPriceStr), script) + callerTx.Sign(keyPair.PrivateKey) + account := ui.eth.StateManager().TransState().GetStateObject(keyPair.Address()) - c := ethchain.MakeContract(callerTx, state) - callerClosure := ethchain.NewClosure(account, c, c.Script(), state, ethutil.Big(gasStr), ethutil.Big(gasPriceStr)) + contract := ethchain.MakeContract(callerTx, state) + callerClosure := ethchain.NewClosure(account, contract, contract.Init(), state, ethutil.Big(gasStr), ethutil.Big(gasPriceStr)) block := ui.eth.BlockChain().CurrentBlock vm := ethchain.NewVm(state, ui.eth.StateManager(), ethchain.RuntimeVars{ @@ -151,50 +155,18 @@ func (ui *UiLib) DebugTx(recipient, valueStr, gasStr, gasPriceStr, data string) Coinbase: block.Coinbase, Time: block.Time, Diff: block.Difficulty, - TxData: nil, }) + ui.Db.done = false go func() { - callerClosure.Call(vm, nil, ui.Db.halting) + callerClosure.Call(vm, contract.Init(), ui.Db.halting) state.Reset() + + ui.Db.done = true }() } func (ui *UiLib) Next() { ui.Db.Next() } - -type Debugger struct { - win *qml.Window - N chan bool -} - -func (d *Debugger) halting(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack) { - d.win.Root().Call("setInstruction", pc) - d.win.Root().Call("clearMem") - d.win.Root().Call("clearStack") - - addr := 0 - for i := 0; i+32 <= mem.Len(); i += 32 { - d.win.Root().Call("setMem", memAddr{fmt.Sprintf("%03d", addr), fmt.Sprintf("% x", mem.Data()[i:i+32])}) - addr++ - } - - for _, val := range stack.Data() { - d.win.Root().Call("setStack", val.String()) - } - -out: - for { - select { - case <-d.N: - break out - default: - } - } -} - -func (d *Debugger) Next() { - d.N <- true -} |