summaryrefslogtreecommitdiff
path: root/cmd/tealdbg/remote.go
blob: b845c2d3a724b364ab489767755b545d8bd990ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"io"
	"net/http"

	"github.com/gorilla/mux"

	"github.com/algorand/go-algorand/data/transactions/logic"
	"github.com/algorand/go-algorand/protocol"
)

// RemoteHookAdapter provides HTTP transport for WebDebugger
type RemoteHookAdapter struct {
	debugger *Debugger
}

// MakeRemoteHook creates new RemoteHookAdapter
func MakeRemoteHook(debugger *Debugger) *RemoteHookAdapter {
	r := new(RemoteHookAdapter)
	r.debugger = debugger
	return r
}

// Setup adds HTTP handlers for remote WebDebugger
func (rha *RemoteHookAdapter) Setup(router *mux.Router) {
	router.HandleFunc("/exec/register", rha.registerHandler).Methods("POST")
	router.HandleFunc("/exec/update", rha.updateHandler).Methods("POST")
	router.HandleFunc("/exec/complete", rha.completeHandler).Methods("POST")
}

func (rha *RemoteHookAdapter) decodeState(body io.Reader) (state logic.DebugState, err error) {
	dec := protocol.NewJSONDecoder(body)
	err = dec.Decode(&state)
	return
}

func (rha *RemoteHookAdapter) registerHandler(w http.ResponseWriter, r *http.Request) {
	state, err := rha.decodeState(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Register, and wait for user to acknowledge registration
	rha.debugger.Register(&state)

	// Proceed!
	w.WriteHeader(http.StatusOK)
	return
}

func (rha *RemoteHookAdapter) updateHandler(w http.ResponseWriter, r *http.Request) {
	state, err := rha.decodeState(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Ask debugger to process and wait to continue
	err = rha.debugger.update(&state)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	w.WriteHeader(http.StatusOK)
	return
}

func (rha *RemoteHookAdapter) completeHandler(w http.ResponseWriter, r *http.Request) {
	state, err := rha.decodeState(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Ask debugger to process and wait to continue
	err = rha.debugger.complete(&state)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	// Proceed!
	w.WriteHeader(http.StatusOK)
	return
}