Skip to content

Commit

Permalink
Merge pull request #1607 from 0chain/cache-sign
Browse files Browse the repository at this point in the history
hotfix sign cache
  • Loading branch information
dabasov authored Sep 7, 2024
2 parents 22a6f3a + c181372 commit e7d40e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
11 changes: 8 additions & 3 deletions zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,9 +1000,14 @@ func GetAllocation(allocationID string) (*Allocation, error) {
if err != nil {
return nil, errors.New("allocation_decode_error", "Error decoding the allocation: "+err.Error()+" "+string(allocationBytes))
}
sig, err := client.Sign(enc.Hash(allocationObj.Tx))
if err != nil {
return nil, err
hashdata := allocationObj.Tx
sig, ok := zboxutil.SignCache.Get(hashdata)
if !ok {
sig, err = client.Sign(enc.Hash(hashdata))
zboxutil.SignCache.Add(hashdata, sig)
if err != nil {
return nil, err
}
}

allocationObj.sig = sig
Expand Down
29 changes: 23 additions & 6 deletions zboxcore/zboxutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/0chain/gosdk/core/logger"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/0chain/gosdk/zboxcore/client"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/hashicorp/golang-lru/v2/simplelru"
"github.com/hitenjain14/fasthttp"
)

Expand Down Expand Up @@ -52,6 +54,7 @@ var (
Client HttpClient
FastHttpClient FastClient
log logger.Logger
SignCache simplelru.LRUCache[string, string]
)

const (
Expand Down Expand Up @@ -186,6 +189,11 @@ func init() {
fasthttp.SetBodySizePoolLimit(respBodyPoolLimit, respBodyPoolLimit)
envProxy.initialize()
log.Init(logger.DEBUG, "0box-sdk")
c, err := lru.New[string, string](1000)
if err != nil {
panic(err)
}
SignCache = c
}

func NewHTTPRequest(method string, url string, data []byte) (*http.Request, context.Context, context.CancelFunc, error) {
Expand Down Expand Up @@ -215,9 +223,14 @@ func setClientInfoWithSign(req *http.Request, sig, allocation, baseURL string) e
req.Header.Set(CLIENT_SIGNATURE_HEADER, sig)

hashData := allocation + baseURL
sig2, err := client.Sign(encryption.Hash(hashData))
if err != nil {
return err
sig2, ok := SignCache.Get(hashData)
if !ok {
var err error
sig2, err = client.Sign(encryption.Hash(hashData))
SignCache.Add(hashData, sig2)
if err != nil {
return err
}
}
req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sig2)
return nil
Expand Down Expand Up @@ -621,9 +634,13 @@ func setFastClientInfoWithSign(req *fasthttp.Request, allocation, baseURL string
}
req.Header.Set(CLIENT_SIGNATURE_HEADER, sign)
hashData := allocation + baseURL
sig2, err := client.Sign(encryption.Hash(hashData))
if err != nil {
return err
sig2, ok := SignCache.Get(hashData)
if !ok {
sig2, err = client.Sign(encryption.Hash(hashData))
SignCache.Add(hashData, sig2)
if err != nil {
return err
}
}
req.Header.Set(CLIENT_SIGNATURE_HEADER_V2, sig2)
return nil
Expand Down

0 comments on commit e7d40e3

Please sign in to comment.