Skip to content

Commit

Permalink
fix url (#88)
Browse files Browse the repository at this point in the history
* fix url

* fix

* fix log

* fix

* getBlockHash
  • Loading branch information
Lawliet-Chan committed Aug 8, 2023
1 parent 78a3d88 commit f17df3c
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 215 deletions.
16 changes: 5 additions & 11 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,18 @@ 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"`
}

// 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
Expand All @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions core/context/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
57 changes: 27 additions & 30 deletions core/kernel/handle_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
//}
20 changes: 7 additions & 13 deletions core/kernel/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/kernel/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
41 changes: 22 additions & 19 deletions core/kernel/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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)
}
}

Expand Down
10 changes: 8 additions & 2 deletions core/tripod/tripod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -93,15 +101,13 @@ 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)
}
}

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)
}
}

Expand Down
Loading

0 comments on commit f17df3c

Please sign in to comment.