Skip to content

Commit

Permalink
chore: go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
endersonmaia committed Sep 10, 2024
1 parent 8038bc0 commit 9d7eb8f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 128 deletions.
133 changes: 66 additions & 67 deletions go/src/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,90 @@ package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"fmt"
"log"
"os"
"log"
"os"
"strconv"

"dapp/rollups"
"dapp/rollups"
)

var (
infolog = log.New(os.Stderr, "[ info ] ", log.Lshortfile)
errlog = log.New(os.Stderr, "[ error ] ", log.Lshortfile)
infolog = log.New(os.Stderr, "[ info ] ", log.Lshortfile)
errlog = log.New(os.Stderr, "[ error ] ", log.Lshortfile)
)

func HandleAdvance(data *rollups.AdvanceResponse) error {
dataMarshal, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("HandleAdvance: failed marshaling json: %w", err)
}
infolog.Println("Received advance request data", string(dataMarshal))
return nil
dataMarshal, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("HandleAdvance: failed marshaling json: %w", err)
}
infolog.Println("Received advance request data", string(dataMarshal))
return nil
}


func HandleInspect(data *rollups.InspectResponse) error {
dataMarshal, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("HandleInspect: failed marshaling json: %w", err)
}
infolog.Println("Received inspect request data", string(dataMarshal))
return nil
dataMarshal, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("HandleInspect: failed marshaling json: %w", err)
}
infolog.Println("Received inspect request data", string(dataMarshal))
return nil
}

func Handler(response *rollups.FinishResponse) error {
var err error
var err error

switch response.Type {
case "advance_state":
data := new(rollups.AdvanceResponse)
if err = json.Unmarshal(response.Data, data); err != nil {
return fmt.Errorf("Handler: Error unmarshaling advance:", err)
}
err = HandleAdvance(data)
case "inspect_state":
data := new(rollups.InspectResponse)
if err = json.Unmarshal(response.Data, data); err != nil {
return fmt.Errorf("Handler: Error unmarshaling inspect:", err)
}
err = HandleInspect(data)
}
return err
switch response.Type {
case "advance_state":
data := new(rollups.AdvanceResponse)
if err = json.Unmarshal(response.Data, data); err != nil {
return fmt.Errorf("Handler: Error unmarshaling advance:", err)
}
err = HandleAdvance(data)
case "inspect_state":
data := new(rollups.InspectResponse)
if err = json.Unmarshal(response.Data, data); err != nil {
return fmt.Errorf("Handler: Error unmarshaling inspect:", err)
}
err = HandleInspect(data)
}
return err
}

func main() {
finish := rollups.FinishRequest{"accept"}
finish := rollups.FinishRequest{"accept"}

for true {
infolog.Println("Sending finish")
res, err := rollups.SendFinish(&finish)
if err != nil {
errlog.Panicln("Error: error making http request: ", err)
}
infolog.Println("Received finish status ", strconv.Itoa(res.StatusCode))

for true {
infolog.Println("Sending finish")
res, err := rollups.SendFinish(&finish)
if err != nil {
errlog.Panicln("Error: error making http request: ", err)
}
infolog.Println("Received finish status ", strconv.Itoa(res.StatusCode))

if (res.StatusCode == 202){
infolog.Println("No pending rollup request, trying again")
} else {
if res.StatusCode == 202 {
infolog.Println("No pending rollup request, trying again")
} else {

resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
errlog.Panicln("Error: could not read response body: ", err)
}

var response rollups.FinishResponse
err = json.Unmarshal(resBody, &response)
if err != nil {
errlog.Panicln("Error: unmarshaling body:", err)
}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
errlog.Panicln("Error: could not read response body: ", err)
}

finish.Status = "accept"
err = Handler(&response)
if err != nil {
errlog.Println(err)
finish.Status = "reject"
}
}
}
}
var response rollups.FinishResponse
err = json.Unmarshal(resBody, &response)
if err != nil {
errlog.Panicln("Error: unmarshaling body:", err)
}

finish.Status = "accept"
err = Handler(&response)
if err != nil {
errlog.Println(err)
finish.Status = "reject"
}
}
}
}
88 changes: 44 additions & 44 deletions go/src/rollups/helpers.go
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
package rollups

import (
"encoding/json"
"bytes"
"encoding/hex"
"encoding/json"
"net/http"
"bytes"
"os"
"os"
)

var rollup_server = os.Getenv("ROLLUP_HTTP_SERVER_URL")

func SendPost(endpoint string, jsonData []byte) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, rollup_server + "/" + endpoint, bytes.NewBuffer(jsonData))
if err != nil {
return &http.Response{}, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req, err := http.NewRequest(http.MethodPost, rollup_server+"/"+endpoint, bytes.NewBuffer(jsonData))
if err != nil {
return &http.Response{}, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")

return http.DefaultClient.Do(req)
return http.DefaultClient.Do(req)
}

func SendFinish(finish *FinishRequest) (*http.Response, error) {
body, err := json.Marshal(finish)
if err != nil {
return &http.Response{}, err
}
return SendPost("finish", body)
body, err := json.Marshal(finish)
if err != nil {
return &http.Response{}, err
}

return SendPost("finish", body)
}

func SendReport(report *ReportRequest) (*http.Response, error) {
body, err := json.Marshal(report)
if err != nil {
return &http.Response{}, err
}
return SendPost("report", body)
body, err := json.Marshal(report)
if err != nil {
return &http.Response{}, err
}

return SendPost("report", body)
}

func SendNotice(notice *NoticeRequest) (*http.Response, error) {
body, err := json.Marshal(notice)
if err != nil {
return &http.Response{}, err
}
return SendPost("notice", body)
body, err := json.Marshal(notice)
if err != nil {
return &http.Response{}, err
}

return SendPost("notice", body)
}

func SendVoucher(voucher *VoucherRequest) (*http.Response, error) {
body, err := json.Marshal(voucher)
if err != nil {
return &http.Response{}, err
}
return SendPost("voucher", body)
body, err := json.Marshal(voucher)
if err != nil {
return &http.Response{}, err
}

return SendPost("voucher", body)
}

func SendException(exception *ExceptionRequest) (*http.Response, error) {
body, err := json.Marshal(exception)
if err != nil {
return &http.Response{}, err
}
return SendPost("exception", body)
body, err := json.Marshal(exception)
if err != nil {
return &http.Response{}, err
}

return SendPost("exception", body)
}

func Hex2Str(hx string) (string, error) {
str, err := hex.DecodeString(hx[2:])
str, err := hex.DecodeString(hx[2:])
if err != nil {
return string(str), err
return string(str), err
}
return string(str), nil
return string(str), nil
}

func Str2Hex(str string) string {
hx := hex.EncodeToString([]byte(str))
return "0x"+string(hx)
hx := hex.EncodeToString([]byte(str))
return "0x" + string(hx)
}
34 changes: 17 additions & 17 deletions go/src/rollups/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,48 @@ import (
)

type FinishRequest struct {
Status string `json:"status"`
Status string `json:"status"`
}

type FinishResponse struct {
Type string `json:"request_type"`
Data json.RawMessage `json:"data"`
Type string `json:"request_type"`
Data json.RawMessage `json:"data"`
}

type InspectResponse struct {
Payload string `json:"payload"`
Payload string `json:"payload"`
}

type AdvanceResponse struct {
Metadata Metadata `json:"metadata"`
Payload string `json:"payload"`
Metadata Metadata `json:"metadata"`
Payload string `json:"payload"`
}

type Metadata struct {
MsgSender string `json:"msg_sender"`
EpochIndex uint64 `json:"epoch_index"`
InputIndex uint64 `json:"input_index"`
BlockNumber uint64 `json:"block_number"`
Timestamp uint64 `json:"timestamp"`
MsgSender string `json:"msg_sender"`
EpochIndex uint64 `json:"epoch_index"`
InputIndex uint64 `json:"input_index"`
BlockNumber uint64 `json:"block_number"`
Timestamp uint64 `json:"timestamp"`
}

type ReportRequest struct {
Payload string `json:"payload"`
Payload string `json:"payload"`
}

type NoticeRequest struct {
Payload string `json:"payload"`
Payload string `json:"payload"`
}

type VoucherRequest struct {
Destination string `json:"destination"`
Payload string `json:"payload"`
Destination string `json:"destination"`
Payload string `json:"payload"`
}

type ExceptionRequest struct {
Payload string `json:"payload"`
Payload string `json:"payload"`
}

type IndexResponse struct {
Index uint64 `json:"index"`
Index uint64 `json:"index"`
}

0 comments on commit 9d7eb8f

Please sign in to comment.