From f17df3c4c20a4ddee17b60c4e95b9a90083af561 Mon Sep 17 00:00:00 2001 From: Xinran <1576710154@qq.com> Date: Tue, 8 Aug 2023 13:27:45 +0800 Subject: [PATCH] fix url (#88) * fix url * fix * fix log * fix * getBlockHash --- common/types.go | 16 ++-- core/context/read.go | 9 ++- core/kernel/handle_input.go | 57 +++++++------ core/kernel/http.go | 20 ++--- core/kernel/run.go | 6 +- core/kernel/websocket.go | 41 +++++----- core/tripod/tripod.go | 10 ++- core/txdb/txdb_scheme.go | 8 +- core/types/txn.go | 14 ++-- core/types/txn_test.go | 6 +- core/url.go | 67 +++++++++------- example/client/asset/transfer.go | 24 +++--- example/client/callchain/callchain.go | 111 ++++++++------------------ 13 files changed, 174 insertions(+), 215 deletions(-) diff --git a/common/types.go b/common/types.go index ceeede2b..d326a315 100644 --- a/common/types.go +++ b/common/types.go @@ -39,9 +39,9 @@ type ( // WrCall from clients, it is an instance of an 'Writing'. WrCall struct { - TripodName string `json:"tripod_name"` - WritingName string `json:"writing_name"` - Params string `json:"params"` + TripodName string `json:"tripod_name"` + FuncName string `json:"func_name"` + Params string `json:"params"` // TODO: make LeiPrice and Tips as a sortable interface. LeiPrice uint64 `json:"lei_price"` Tips uint64 `json:"tips"` @@ -49,10 +49,8 @@ type ( // RdCall from clients, it is an instance of an 'Read'. RdCall struct { - TripodName string `json:"tripod_name"` - ReadingName string `json:"reading_name"` - BlockHash Hash `json:"block_hash,omitempty"` - Params string `json:"params"` + TripodName string `json:"tripod_name"` + FuncName string `json:"func_name"` } // CallType is Writing or Reading CallType int @@ -71,10 +69,6 @@ func (e *WrCall) Hash() ([]byte, error) { return hash[:], nil } -func (q *RdCall) BindJsonParams(v interface{}) error { - return BindJsonParams(q.Params, v) -} - func BindJsonParams(params string, v interface{}) error { d := json.NewDecoder(bytes.NewReader([]byte(params))) d.UseNumber() diff --git a/core/context/read.go b/core/context/read.go index f9627bc2..9d0ff485 100644 --- a/core/context/read.go +++ b/core/context/read.go @@ -17,8 +17,13 @@ func NewReadContext(ctx *gin.Context) (*ReadContext, error) { }, nil } -func (rc *ReadContext) GetBlockHash() common.Hash { - return common.HexToHash(rc.Query(core.BlockHashKey)) +func (rc *ReadContext) GetBlockHash() *common.Hash { + blockHashHex := rc.Query(core.BlockHashKey) + if blockHashHex == "" { + return nil + } + blockHash := common.HexToHash(blockHashHex) + return &blockHash } func (rc *ReadContext) JsonOk(v any) { diff --git a/core/kernel/handle_input.go b/core/kernel/handle_input.go index 406b8ac7..a9817ef6 100644 --- a/core/kernel/handle_input.go +++ b/core/kernel/handle_input.go @@ -2,17 +2,14 @@ package kernel import ( "github.com/sirupsen/logrus" - . "github.com/yu-org/yu/common" - . "github.com/yu-org/yu/core" . "github.com/yu-org/yu/core/types" - "net/http" ) // HandleTxn handles txn from outside. // You can also self-define your input by calling HandleTxn (not only by default http and ws) func (k *Kernel) HandleTxn(stxn *SignedTxn) error { wrCall := stxn.Raw.WrCall - _, err := k.land.GetWriting(wrCall.TripodName, wrCall.WritingName) + _, err := k.land.GetWriting(wrCall.TripodName, wrCall.FuncName) if err != nil { return err } @@ -44,35 +41,35 @@ func (k *Kernel) HandleTxn(stxn *SignedTxn) error { // blockHash := GetBlockHash(req) // rdCall = &RdCall{ // TripodName: tripodName, -// ReadingName: rdName, +// FuncName: rdName, // Params: params, // BlockHash: blockHash, // } // return //} -func getWrFromHttp(req *http.Request, params string) (stxn *SignedTxn, err error) { - tripodName, wrName, urlErr := GetTripodCallName(req) - if err != nil { - return nil, urlErr - } - leiPrice, err := GetLeiPrice(req) - if err != nil { - return - } - tips, err := GetTips(req) - wrCall := &WrCall{ - TripodName: tripodName, - WritingName: wrName, - Params: params, - LeiPrice: leiPrice, - Tips: tips, - } - sig := GetSignature(req) - pubkey, err := GetPubkey(req) - if err != nil { - return - } - stxn, err = NewSignedTxn(wrCall, pubkey, sig) - return -} +//func getWrFromHttp(req *http.Request, params string) (stxn *SignedTxn, err error) { +// tripodName, wrName, urlErr := GetTripodCallName(req) +// if err != nil { +// return nil, urlErr +// } +// leiPrice, err := GetLeiPrice(req) +// if err != nil { +// return +// } +// tips, err := GetTips(req) +// wrCall := &WrCall{ +// TripodName: tripodName, +// FuncName: wrName, +// Params: params, +// LeiPrice: leiPrice, +// Tips: tips, +// } +// sig := GetSignature(req) +// pubkey, err := GetPubkey(req) +// if err != nil { +// return +// } +// stxn, err = NewSignedTxn(wrCall, pubkey, sig) +// return +//} diff --git a/core/kernel/http.go b/core/kernel/http.go index 88143f42..a644bf6c 100644 --- a/core/kernel/http.go +++ b/core/kernel/http.go @@ -7,22 +7,18 @@ import ( . "github.com/yu-org/yu/core" "github.com/yu-org/yu/core/context" "github.com/yu-org/yu/core/types" - "io" "net/http" - "path/filepath" ) func (k *Kernel) HandleHttp() { r := gin.Default() // POST request - wrPath := filepath.Join(WrApiPath, "*path") - r.POST(wrPath, func(c *gin.Context) { + r.POST(WrApiPath, func(c *gin.Context) { k.handleHttpWr(c) }) // GET request - rdPath := filepath.Join(RdApiPath, "*path") - r.GET(rdPath, func(c *gin.Context) { + r.GET(RdApiPath, func(c *gin.Context) { k.handleHttpRd(c) }) @@ -33,20 +29,19 @@ func (k *Kernel) HandleHttp() { } func (k *Kernel) handleHttpWr(c *gin.Context) { - params, err := io.ReadAll(c.Request.Body) + rawWrCall, err := GetRawWrCall(c) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } - stxn, err := getWrFromHttp(c.Request, string(params)) + _, err = k.land.GetWriting(rawWrCall.Call.TripodName, rawWrCall.Call.FuncName) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return } - wrCall := stxn.Raw.WrCall - _, err = k.land.GetWriting(wrCall.TripodName, wrCall.WritingName) + stxn, err := types.NewSignedTxn(rawWrCall.Call, rawWrCall.Pubkey, rawWrCall.Signature) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return @@ -72,12 +67,11 @@ func (k *Kernel) handleHttpWr(c *gin.Context) { err = k.txPool.Insert(stxn) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) - return } } func (k *Kernel) handleHttpRd(c *gin.Context) { - tripodName, rdName, err := GetTripodCallName(c.Request) + rdCall, err := GetRdCall(c) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return @@ -91,7 +85,7 @@ func (k *Kernel) handleHttpRd(c *gin.Context) { return } - rd, err := k.land.GetReading(tripodName, rdName) + rd, err := k.land.GetReading(rdCall.TripodName, rdCall.FuncName) if err != nil { c.AbortWithError(http.StatusBadRequest, err) return diff --git a/core/kernel/run.go b/core/kernel/run.go index 91c7f5a0..042e52ce 100644 --- a/core/kernel/run.go +++ b/core/kernel/run.go @@ -100,7 +100,7 @@ func (k *Kernel) OrderedExecute(block *Block) error { return err } - writing, err := k.land.GetWriting(wrCall.TripodName, wrCall.WritingName) + writing, err := k.land.GetWriting(wrCall.TripodName, wrCall.FuncName) if err != nil { k.handleError(err, ctx, block, stxn) continue @@ -193,7 +193,7 @@ func (k *Kernel) handleError(err error, ctx *context.WriteContext, block *Block, ctx.Error.Caller = stxn.GetCallerAddr() ctx.Error.BlockStage = ExecuteTxnsStage ctx.Error.TripodName = wrCall.TripodName - ctx.Error.WritingName = wrCall.WritingName + ctx.Error.WritingName = wrCall.FuncName ctx.Error.BlockHash = block.Hash ctx.Error.Height = block.Height @@ -210,7 +210,7 @@ func (k *Kernel) handleEvent(ctx *context.WriteContext, block *Block, stxn *Sign event.Height = block.Height event.BlockHash = block.Hash - event.WritingName = wrCall.WritingName + event.WritingName = wrCall.FuncName event.TripodName = wrCall.TripodName event.LeiCost = ctx.LeiCost event.BlockStage = ExecuteTxnsStage diff --git a/core/kernel/websocket.go b/core/kernel/websocket.go index 1cf9d72c..d40d8f2c 100644 --- a/core/kernel/websocket.go +++ b/core/kernel/websocket.go @@ -8,21 +8,20 @@ import ( . "github.com/yu-org/yu/core" "github.com/yu-org/yu/core/types" "net/http" - "path/filepath" ) func (k *Kernel) HandleWS() { r := gin.Default() - r.POST(filepath.Join(WrApiPath, "*path"), func(ctx *gin.Context) { - k.handleWS(ctx.Writer, ctx.Request, writing) + r.POST(WrApiPath, func(ctx *gin.Context) { + k.handleWS(ctx, writing) }) - r.GET(filepath.Join(RdApiPath, "*path"), func(ctx *gin.Context) { - k.handleWS(ctx.Writer, ctx.Request, reading) + r.GET(RdApiPath, func(ctx *gin.Context) { + k.handleWS(ctx, reading) }) r.GET(SubResultsPath, func(ctx *gin.Context) { - k.handleWS(ctx.Writer, ctx.Request, subscription) + k.handleWS(ctx, subscription) }) err := r.Run(k.wsPort) if err != nil { @@ -36,9 +35,9 @@ const ( subscription ) -func (k *Kernel) handleWS(w http.ResponseWriter, req *http.Request, typ int) { +func (k *Kernel) handleWS(ctx *gin.Context, typ int) { upgrade := websocket.Upgrader{} - c, err := upgrade.Upgrade(w, req, nil) + c, err := upgrade.Upgrade(ctx.Writer, ctx.Request, nil) if err != nil { k.errorAndClose(c, err.Error()) return @@ -56,24 +55,29 @@ func (k *Kernel) handleWS(w http.ResponseWriter, req *http.Request, typ int) { } switch typ { case writing: - k.handleWsWr(c, req, string(params)) + k.handleWsWr(ctx, string(params)) //case reading: // k.handleWsRd(c, req, string(params)) } } -func (k *Kernel) handleWsWr(c *websocket.Conn, req *http.Request, params string) { - stxn, err := getWrFromHttp(req, params) +func (k *Kernel) handleWsWr(ctx *gin.Context, params string) { + rawWrCall, err := GetRawWrCall(ctx) if err != nil { - k.errorAndClose(c, fmt.Sprintf("get Writing info from websocket error: %v", err)) + ctx.AbortWithError(http.StatusBadRequest, err) return } - wrCall := stxn.Raw.WrCall - _, err = k.land.GetWriting(wrCall.TripodName, wrCall.WritingName) + _, err = k.land.GetWriting(rawWrCall.Call.TripodName, rawWrCall.Call.FuncName) if err != nil { - k.errorAndClose(c, err.Error()) + ctx.AbortWithError(http.StatusBadRequest, err) + return + } + + stxn, err := types.NewSignedTxn(rawWrCall.Call, rawWrCall.Pubkey, rawWrCall.Signature) + if err != nil { + ctx.AbortWithError(http.StatusBadRequest, err) return } @@ -83,21 +87,20 @@ func (k *Kernel) handleWsWr(c *websocket.Conn, req *http.Request, params string) err = k.txPool.CheckTxn(stxn) if err != nil { - k.errorAndClose(c, err.Error()) + ctx.AbortWithError(http.StatusBadRequest, err) return } go func() { err = k.pubUnpackedTxns(types.FromArray(stxn)) if err != nil { - k.errorAndClose(c, fmt.Sprintf("publish Unpacked txn(%s) error: %v", stxn.TxnHash.String(), err)) + ctx.AbortWithError(http.StatusInternalServerError, err) } }() err = k.txPool.Insert(stxn) if err != nil { - k.errorAndClose(c, err.Error()) - return + ctx.AbortWithError(http.StatusInternalServerError, err) } } diff --git a/core/tripod/tripod.go b/core/tripod/tripod.go index 8f08fa9d..de083d2f 100644 --- a/core/tripod/tripod.go +++ b/core/tripod/tripod.go @@ -58,6 +58,14 @@ func (t *Tripod) SetInstance(instance interface{}) { t.name = tripodName } + for name, _ := range t.writings { + logrus.Debugf("register Writing (%s) into Tripod(%s) \n", name, t.name) + } + + for name, _ := range t.readings { + logrus.Debugf("register Reading (%s) into Tripod(%s) \n", name, t.name) + } + t.Instance = instance } @@ -93,7 +101,6 @@ func (t *Tripod) SetWritings(wrs ...Writing) { for _, wr := range wrs { name := getFuncName(wr) t.writings[name] = wr - logrus.Debugf("register Writing(%s) into Tripod(%s) \n", name, t.name) } } @@ -101,7 +108,6 @@ func (t *Tripod) SetReadings(readings ...Reading) { for _, r := range readings { name := getFuncName(r) t.readings[name] = r - logrus.Debugf("register Reading(%s) into Tripod(%s) \n", name, t.name) } } diff --git a/core/txdb/txdb_scheme.go b/core/txdb/txdb_scheme.go index 37555776..107b4e88 100644 --- a/core/txdb/txdb_scheme.go +++ b/core/txdb/txdb_scheme.go @@ -83,7 +83,7 @@ package txdb // BlockHash: event.BlockHash.String(), // Height: event.Height, // TripodName: event.TripodName, -// ExecName: event.WritingName, +// ExecName: event.FuncName, // Value: event.Value, // }, nil //} @@ -95,7 +95,7 @@ package txdb // BlockHash: HexToHash(e.BlockHash), // Height: e.Height, // TripodName: e.TripodName, -// WritingName: e.ExecName, +// FuncName: e.ExecName, // Value: e.Value, // }, nil // @@ -123,7 +123,7 @@ package txdb // BlockHash: err.BlockHash.String(), // Height: err.Height, // TripodName: err.TripodName, -// ExecName: err.WritingName, +// ExecName: err.FuncName, // Error: err.Err, // } //} @@ -135,7 +135,7 @@ package txdb // BlockHash: HexToHash(e.BlockHash), // Height: e.Height, // TripodName: e.TripodName, -// WritingName: e.ExecName, +// FuncName: e.ExecName, // Err: e.Error, // } //} diff --git a/core/types/txn.go b/core/types/txn.go index d5318afb..d87d8d39 100644 --- a/core/types/txn.go +++ b/core/types/txn.go @@ -48,7 +48,7 @@ func (st *SignedTxn) TripodName() string { } func (st *SignedTxn) WrName() string { - return st.Raw.WrCall.WritingName + return st.Raw.WrCall.FuncName } func (st *SignedTxn) BindJsonParams(v interface{}) error { @@ -201,7 +201,7 @@ func (ut *UnsignedTxn) ToPb() *goproto.UnsignedTxn { return &goproto.UnsignedTxn{ Ecall: &goproto.Ecall{ TripodName: ut.WrCall.TripodName, - ExecName: ut.WrCall.WritingName, + ExecName: ut.WrCall.FuncName, Params: ut.WrCall.Params, LeiPrice: ut.WrCall.LeiPrice, Tips: ut.WrCall.Tips, @@ -213,11 +213,11 @@ func (ut *UnsignedTxn) ToPb() *goproto.UnsignedTxn { func UnsignedTxnFromPb(pb *goproto.UnsignedTxn) *UnsignedTxn { return &UnsignedTxn{ WrCall: &WrCall{ - TripodName: pb.Ecall.TripodName, - WritingName: pb.Ecall.ExecName, - Params: pb.Ecall.Params, - LeiPrice: pb.Ecall.LeiPrice, - Tips: pb.Ecall.Tips, + TripodName: pb.Ecall.TripodName, + FuncName: pb.Ecall.ExecName, + Params: pb.Ecall.Params, + LeiPrice: pb.Ecall.LeiPrice, + Tips: pb.Ecall.Tips, }, Timestamp: pb.Timestamp, } diff --git a/core/types/txn_test.go b/core/types/txn_test.go index 44669d45..c4f41d9b 100644 --- a/core/types/txn_test.go +++ b/core/types/txn_test.go @@ -25,9 +25,9 @@ func TestSignedTxns_Remove(t *testing.T) { for i := 0; i < 3; i++ { istr := strconv.Itoa(i) ecall := &WrCall{ - TripodName: istr, - WritingName: istr, - Params: string(istr), + TripodName: istr, + FuncName: istr, + Params: string(istr), } hash, err := ecall.Hash() if err != nil { diff --git a/core/url.go b/core/url.go index 4aa6b4e7..439c8ca8 100644 --- a/core/url.go +++ b/core/url.go @@ -1,17 +1,14 @@ package core import ( - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/pkg/errors" + "github.com/gin-gonic/gin" . "github.com/yu-org/yu/common" "github.com/yu-org/yu/core/keypair" - "net/http" "path/filepath" - "strings" ) -// A complete writing-call url is POST /api/writing/{tripod}/{writing_name} -// A complete reading-call url is GET /api/reading/{tripod}/{reading_name}?xx=yy +// A complete writing-call url is POST /api/writing +// A complete reading-call url is GET /api/reading?tripod={tripod}&func_name={func_name}?xx=yy const ( // RootApiPath For developers, every customized Writing and Read of tripods @@ -20,11 +17,9 @@ const ( WrCallType = "writing" RdCallType = "reading" + TripodKey = "tripod" + FuncNameKey = "func_name" BlockHashKey = "block_hash" - PubkeyKey = "pubkey" - SignatureKey = "signature" - LeiPriceKey = "lei_price" - TipsKey = "tips" ) var ( @@ -33,30 +28,42 @@ var ( SubResultsPath = "/subscribe/results" ) -// GetTripodCallName return (Tripod Name, Write/Read Name, error) -func GetTripodCallName(req *http.Request) (string, string, error) { - path := req.URL.Path - paths := strings.Split(path, "/") - if len(paths) < 5 { - return "", "", errors.New("URL path illegal") - } - return paths[3], paths[4], nil -} - -func GetPubkey(req *http.Request) (keypair.PubKey, error) { - pubkeyStr := req.URL.Query().Get(PubkeyKey) - return keypair.PubkeyFromStr(pubkeyStr) +type RawWrCall struct { + Pubkey keypair.PubKey `json:"pubkey"` + Signature []byte `json:"signature"` + Call *WrCall `json:"call"` } -func GetSignature(req *http.Request) []byte { - signStr := req.URL.Query().Get(SignatureKey) - return FromHex(signStr) +type WritingPostBody struct { + // hex string + Pubkey string `json:"pubkey"` + // hex string + Signature string `json:"signature"` + Call *WrCall `json:"call"` } -func GetLeiPrice(req *http.Request) (uint64, error) { - return hexutil.DecodeUint64(req.URL.Query().Get(LeiPriceKey)) +func GetRawWrCall(ctx *gin.Context) (*RawWrCall, error) { + wpb := new(WritingPostBody) + err := ctx.ShouldBindJSON(wpb) + if err != nil { + return nil, err + } + pubkey, err := keypair.PubkeyFromStr(wpb.Pubkey) + if err != nil { + return nil, err + } + return &RawWrCall{ + Pubkey: pubkey, + Signature: FromHex(wpb.Signature), + Call: wpb.Call, + }, err } -func GetTips(req *http.Request) (uint64, error) { - return hexutil.DecodeUint64(req.URL.Query().Get(TipsKey)) +func GetRdCall(ctx *gin.Context) (*RdCall, error) { + tri := ctx.Query(TripodKey) + fn := ctx.Query(FuncNameKey) + return &RdCall{ + TripodName: tri, + FuncName: fn, + }, nil } diff --git a/example/client/asset/transfer.go b/example/client/asset/transfer.go index fc1c37d4..ec413b08 100644 --- a/example/client/asset/transfer.go +++ b/example/client/asset/transfer.go @@ -20,10 +20,10 @@ func QueryAccount(pubkey PubKey) { panic("json encode qryAccount error: " + err.Error()) } qcall := &RdCall{ - TripodName: "asset", - ReadingName: "QueryBalance", - BlockHash: Hash{}, - Params: string(paramByt), + TripodName: "asset", + FuncName: "QueryBalance", + BlockHash: Hash{}, + Params: string(paramByt), } resp := CallChainByReading(Websocket, qcall) respMap := make(context.H) @@ -51,10 +51,10 @@ func CreateAccount(reqType int, privkey PrivKey, pubkey PubKey, amount uint64) { panic("create-account params marshal error: " + err.Error()) } wrCall := &WrCall{ - TripodName: "asset", - WritingName: "CreateAccount", - Params: string(paramsByt), - LeiPrice: 0, + TripodName: "asset", + FuncName: "CreateAccount", + Params: string(paramsByt), + LeiPrice: 0, } CallChainByWriting(reqType, privkey, pubkey, wrCall) } @@ -74,10 +74,10 @@ func TransferBalance(reqType int, privkey PrivKey, pubkey PubKey, to Address, am panic("TransferBalance params marshal error: " + err.Error()) } wrCall := &WrCall{ - TripodName: "asset", - WritingName: "Transfer", - Params: string(paramsByt), - LeiPrice: leiPrice, + TripodName: "asset", + FuncName: "Transfer", + Params: string(paramsByt), + LeiPrice: leiPrice, } CallChainByWriting(reqType, privkey, pubkey, wrCall) } diff --git a/example/client/callchain/callchain.go b/example/client/callchain/callchain.go index 0f8a653a..612005ea 100644 --- a/example/client/callchain/callchain.go +++ b/example/client/callchain/callchain.go @@ -1,8 +1,8 @@ package callchain import ( - "fmt" - "github.com/ethereum/go-ethereum/common/hexutil" + "bytes" + "encoding/json" "github.com/gorilla/websocket" "github.com/sirupsen/logrus" . "github.com/yu-org/yu/common" @@ -13,7 +13,6 @@ import ( "io" "net/http" "net/url" - "strings" ) const ( @@ -21,57 +20,32 @@ const ( Websocket ) -func CallChainByReading(reqTyp int, rdCall *RdCall) []byte { - var ( - scheme, port string - ) - switch reqTyp { - case Http: - scheme = "http" - port = "7999" - case Websocket: - scheme = "ws" - port = "8999" - } - u := url.URL{Scheme: scheme, Host: fmt.Sprintf("localhost:%s", port), Path: fmt.Sprintf("%s/%s/%s", RdApiPath, rdCall.TripodName, rdCall.ReadingName)} +func CallChainByReading(rdCall *RdCall, params map[string]string) []byte { + u := url.URL{Scheme: "http", Host: "localhost:7999", Path: RdApiPath} q := u.Query() - // q.Set(BlockHashKey, rdCall.BlockHash.String()) + q.Set(TripodKey, rdCall.TripodName) + q.Set(FuncNameKey, rdCall.FuncName) + for key, value := range params { + q.Set(key, value) + } u.RawQuery = q.Encode() logrus.Debug("rdCall: ", u.String()) - switch reqTyp { - case Http: - resp, err := http.Get(u.String()) - if err != nil { - panic("post rdCall message to chain error: " + err.Error()) - } - body, err := io.ReadAll(resp.Body) - if err != nil { - panic("read rdCall response body error: " + err.Error()) - } - return body - case Websocket: - c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) - if err != nil { - panic("rdCall dial chain error: " + err.Error()) - } - defer c.Close() - err = c.WriteMessage(websocket.TextMessage, []byte(rdCall.Params)) - if err != nil { - panic("write rdCall message to chain error: " + err.Error()) - } - _, resp, err := c.ReadMessage() - if err != nil { - panic("get rdCall response error: " + err.Error()) - } - return resp + resp, err := http.Get(u.String()) + if err != nil { + panic("post rdCall message to chain error: " + err.Error()) + } + body, err := io.ReadAll(resp.Body) + if err != nil { + panic("read rdCall response body error: " + err.Error()) } - return nil + return body + } -func CallChainByWriting(reqType int, privkey PrivKey, pubkey PubKey, wrCall *WrCall) { +func CallChainByWriting(privkey PrivKey, pubkey PubKey, wrCall *WrCall) { hash, err := wrCall.Hash() if err != nil { panic("wrCall hash error: " + err.Error()) @@ -81,45 +55,24 @@ func CallChainByWriting(reqType int, privkey PrivKey, pubkey PubKey, wrCall *WrC panic("sign data error: " + err.Error()) } - var ( - scheme, port string - ) - switch reqType { - case Http: - scheme = "http" - port = "7999" - case Websocket: - scheme = "ws" - port = "8999" + u := url.URL{Scheme: "http", Host: "localhost:7999", Path: WrApiPath} + postBody := WritingPostBody{ + Pubkey: pubkey.StringWithType(), + Signature: ToHex(signByt), + Call: wrCall, + } + bodyByt, err := json.Marshal(postBody) + if err != nil { + panic("marshal post body failed: " + err.Error()) } - - u := url.URL{Scheme: scheme, Host: fmt.Sprintf("localhost:%s", port), Path: fmt.Sprintf("%s/%s/%s", WrApiPath, wrCall.TripodName, wrCall.WritingName)} - q := u.Query() - q.Set(SignatureKey, ToHex(signByt)) - q.Set(PubkeyKey, pubkey.StringWithType()) - q.Set(LeiPriceKey, hexutil.EncodeUint64(wrCall.LeiPrice)) - - u.RawQuery = q.Encode() logrus.Debug("wrCall: ", u.String()) - switch reqType { - case Http: - _, err = http.Post(u.String(), "application/json", strings.NewReader(wrCall.Params)) - if err != nil { - panic("post wrCall message to chain error: " + err.Error()) - } - case Websocket: - c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) - if err != nil { - panic("wrCall dial chain error: " + err.Error()) - } - defer c.Close() - err = c.WriteMessage(websocket.TextMessage, []byte(wrCall.Params)) - if err != nil { - panic("write wrCall message to chain error: " + err.Error()) - } + _, err = http.Post(u.String(), "application/json", bytes.NewReader(bodyByt)) + if err != nil { + panic("post wrCall message to chain error: " + err.Error()) } + } type Subscriber struct {