Skip to content

Commit

Permalink
v0.2.6, use jsoff instead of deprecated jlib
Browse files Browse the repository at this point in the history
  • Loading branch information
superisaac committed Dec 12, 2023
1 parent 7edc381 commit ce0ce93
Show file tree
Hide file tree
Showing 28 changed files with 171 additions and 146 deletions.
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.19
go 1.21

toolchain go1.21.3

use (
./nodemux
Expand Down
4 changes: 3 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/superisaac/jlib v0.4.2/go.mod h1:mr1uMAKhYPhxvtdKGLksNuVEWzd4WhbT+Z/VW8CVcd8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
20 changes: 10 additions & 10 deletions nodemux/chains/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package chains
import (
"context"
"fmt"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
"time"
Expand Down Expand Up @@ -37,7 +37,7 @@ func NewBitcoinChain() *BitcoinChain {
}

func (self BitcoinChain) GetClientVersion(ctx context.Context, ep *nodemuxcore.Endpoint) (string, error) {
reqmsg := jlib.NewRequestMessage(1, "getnetworkinfo", nil)
reqmsg := jsoff.NewRequestMessage(1, "getnetworkinfo", nil)
var info bitcoinNetworkInfo
err := ep.UnwrapCallRPC(ctx, reqmsg, &info)
if err != nil {
Expand All @@ -57,7 +57,7 @@ func (self BitcoinChain) updateMempoolPresenceCache(ctx context.Context, m *node
if !ok {
return
}
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "getrawmempool", nil)

var txids []string
Expand All @@ -79,7 +79,7 @@ func (self BitcoinChain) updateBlockPresenceCache(ctx context.Context, m *nodemu
if !ok {
return
}
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "getblock", []interface{}{blockHash})

var blk bitcoinBlock
Expand All @@ -97,7 +97,7 @@ func (self BitcoinChain) updateBlockPresenceCache(ctx context.Context, m *nodemu
}

func (self *BitcoinChain) GetBlockhead(ctx context.Context, m *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "getblockchaininfo", nil)

var chainInfo bitcoinBlockchainInfo
Expand All @@ -113,7 +113,7 @@ func (self *BitcoinChain) GetBlockhead(ctx context.Context, m *nodemuxcore.Multi
return block, nil
}

func (self *BitcoinChain) DelegateRPC(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *BitcoinChain) DelegateRPC(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
//useCache := reqmsg.Method == "gettransaction" || reqmsg.Method == "getrawtransaction" || reqmsg.Method == "decoderawtransaction"
useCache, resmsgFromCache := jsonrpcCacheFetchForMethods(
ctx, m, chain, reqmsg,
Expand All @@ -131,7 +131,7 @@ func (self *BitcoinChain) DelegateRPC(ctx context.Context, m *nodemuxcore.Multip
"getrawtransaction"); ok {
retmsg, err := ep.CallRPC(ctx, reqmsg)
if err == nil && useCache && retmsg.IsResult() {
jsonrpcCacheUpdate(ctx, m, chain, reqmsg, retmsg.(*jlib.ResultMessage), time.Minute*10)
jsonrpcCacheUpdate(ctx, m, chain, reqmsg, retmsg.(*jsoff.ResultMessage), time.Minute*10)
}
return retmsg, err
}
Expand All @@ -147,17 +147,17 @@ func (self *BitcoinChain) DelegateRPC(ctx context.Context, m *nodemuxcore.Multip

retmsg, err := m.DefaultRelayRPC(ctx, chain, reqmsg, -1)
if err == nil && useCache && retmsg.IsResult() {
jsonrpcCacheUpdate(ctx, m, chain, reqmsg, retmsg.(*jlib.ResultMessage), time.Minute*10)
jsonrpcCacheUpdate(ctx, m, chain, reqmsg, retmsg.(*jsoff.ResultMessage), time.Minute*10)
}
return retmsg, nil
}

func (self *BitcoinChain) findBlockHeight(reqmsg *jlib.RequestMessage) (int, bool) {
func (self *BitcoinChain) findBlockHeight(reqmsg *jsoff.RequestMessage) (int, bool) {
// the first argument is a integer number
var bh struct {
Height int
}
if err := jlib.DecodeParams(reqmsg.Params, &bh); err == nil && bh.Height > 0 {
if err := jsoff.DecodeParams(reqmsg.Params, &bh); err == nil && bh.Height > 0 {
return bh.Height, true
}
return 0, false
Expand Down
6 changes: 3 additions & 3 deletions nodemux/chains/casper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package chains

import (
"context"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand Down Expand Up @@ -32,7 +32,7 @@ func (self CasperChain) StartSync(context context.Context, m *nodemuxcore.Multip
}

func (self *CasperChain) GetBlockhead(context context.Context, b *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "chain_get_block", nil)

var bt casperBlock
Expand All @@ -48,7 +48,7 @@ func (self *CasperChain) GetBlockhead(context context.Context, b *nodemuxcore.Mu
return block, nil
}

func (self *CasperChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *CasperChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
// Custom relay methods can be defined here
return b.DefaultRelayRPC(rootCtx, chain, reqmsg, -3)
}
6 changes: 3 additions & 3 deletions nodemux/chains/conflux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chains

import (
"context"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand All @@ -23,7 +23,7 @@ func (self ConfluxChain) StartSync(context context.Context, m *nodemuxcore.Multi
}

func (self *ConfluxChain) GetBlockhead(context context.Context, b *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "cfx_epochNumber",
[]interface{}{"latest_mined"})
var height int
Expand All @@ -38,7 +38,7 @@ func (self *ConfluxChain) GetBlockhead(context context.Context, b *nodemuxcore.M
return block, nil
}

func (self *ConfluxChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *ConfluxChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
// Custom relay methods can be defined here
return b.DefaultRelayRPC(rootCtx, chain, reqmsg, -5)
}
6 changes: 3 additions & 3 deletions nodemux/chains/eosrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chains

import (
"context"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand Down Expand Up @@ -32,7 +32,7 @@ func (self EOSRPC) StartSync(context context.Context, m *nodemuxcore.Multiplexer
}

func (self *EOSRPC) GetBlockhead(context context.Context, b *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "get_info", nil)

var info eosrpcChainInfo
Expand All @@ -48,7 +48,7 @@ func (self *EOSRPC) GetBlockhead(context context.Context, b *nodemuxcore.Multipl
return block, nil
}

func (self *EOSRPC) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *EOSRPC) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
// Custom relay methods can be defined here
return b.DefaultRelayRPC(rootCtx, chain, reqmsg, -300)
}
6 changes: 3 additions & 3 deletions nodemux/chains/filecoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chains

import (
"context"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand All @@ -27,7 +27,7 @@ func (self FilecoinChain) StartSync(context context.Context, m *nodemuxcore.Mult
}

func (self *FilecoinChain) GetBlockhead(context context.Context, b *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "Filecoin.ChainHead", nil)

var bt filecoinBlock
Expand All @@ -43,7 +43,7 @@ func (self *FilecoinChain) GetBlockhead(context context.Context, b *nodemuxcore.
return block, nil
}

func (self *FilecoinChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *FilecoinChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
// Custom relay methods can be defined here
return b.DefaultRelayRPC(rootCtx, chain, reqmsg, -3)
}
8 changes: 4 additions & 4 deletions nodemux/chains/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package chains

import (
"context"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand Down Expand Up @@ -34,7 +34,7 @@ func NewHandshakeChain() *HandshakeChain {
}

func (self HandshakeChain) GetClientVersion(ctx context.Context, ep *nodemuxcore.Endpoint) (string, error) {
reqmsg := jlib.NewRequestMessage(1, "getinfo", nil)
reqmsg := jsoff.NewRequestMessage(1, "getinfo", nil)
var info handshakeInfo
err := ep.UnwrapCallRPC(ctx, reqmsg, &info)
if err != nil {
Expand All @@ -48,7 +48,7 @@ func (self HandshakeChain) StartSync(context context.Context, m *nodemuxcore.Mul
}

func (self *HandshakeChain) GetBlockhead(ctx context.Context, m *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "getchaintips", nil)

var chaintips []handshakeBlockhead
Expand All @@ -69,6 +69,6 @@ func (self *HandshakeChain) GetBlockhead(ctx context.Context, m *nodemuxcore.Mul
return nil, nil
}

func (self *HandshakeChain) DelegateRPC(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *HandshakeChain) DelegateRPC(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
return m.DefaultRelayRPC(ctx, chain, reqmsg, -2)
}
10 changes: 5 additions & 5 deletions nodemux/chains/jsonrpc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"strings"
"time"
)

func jsonrpcCacheGet(ctx context.Context, c *redis.Client, chain nodemuxcore.ChainRef, req *jlib.RequestMessage) (interface{}, bool) {
func jsonrpcCacheGet(ctx context.Context, c *redis.Client, chain nodemuxcore.ChainRef, req *jsoff.RequestMessage) (interface{}, bool) {
cacheKey := req.CacheKey(fmt.Sprintf("CC:%s", chain))
data, err := c.Get(ctx, cacheKey).Result()
if err != nil {
Expand All @@ -33,7 +33,7 @@ func jsonrpcCacheGet(ctx context.Context, c *redis.Client, chain nodemuxcore.Cha
return res, true
}

func jsonrpcCacheUpdate(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, req *jlib.RequestMessage, res *jlib.ResultMessage, expiration time.Duration) {
func jsonrpcCacheUpdate(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, req *jsoff.RequestMessage, res *jsoff.ResultMessage, expiration time.Duration) {
if c, ok := m.RedisClientExact(jsonrpcCacheRedisSelector(chain)); ok {
cacheKey := req.CacheKey(fmt.Sprintf("CC:%s", chain))
data, err := json.Marshal(res.Result)
Expand All @@ -54,7 +54,7 @@ func jsonrpcCacheRedisSelector(chain nodemuxcore.ChainRef) string {
return fmt.Sprintf("jsonrpc-cache-%s-%s", chain.Namespace, chain.Network)
}

func jsonrpcCacheFetchForMethods(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, methods ...string) (bool, *jlib.ResultMessage) {
func jsonrpcCacheFetchForMethods(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, methods ...string) (bool, *jsoff.ResultMessage) {
useCache := false
for _, method := range methods {
if reqmsg.Method == method {
Expand All @@ -66,7 +66,7 @@ func jsonrpcCacheFetchForMethods(ctx context.Context, m *nodemuxcore.Multiplexer
}
if c, ok := m.RedisClientExact(jsonrpcCacheRedisSelector(chain)); ok {
if resFromCache, ok := jsonrpcCacheGet(ctx, c, chain, reqmsg); ok {
return useCache, jlib.NewResultMessage(reqmsg, resFromCache)
return useCache, jsoff.NewResultMessage(reqmsg, resFromCache)
} else {
return useCache, nil
}
Expand Down
6 changes: 3 additions & 3 deletions nodemux/chains/monero.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package chains

import (
"context"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand All @@ -29,7 +29,7 @@ func (self MoneroChain) StartSync(context context.Context, m *nodemuxcore.Multip
}

func (self *MoneroChain) GetBlockhead(context context.Context, b *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "get_height", nil)

var bt moneroBlock
Expand All @@ -45,7 +45,7 @@ func (self *MoneroChain) GetBlockhead(context context.Context, b *nodemuxcore.Mu
return block, nil
}

func (self *MoneroChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *MoneroChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
// Custom relay methods can be defined here
return b.DefaultRelayRPC(rootCtx, chain, reqmsg, -3)
}
6 changes: 3 additions & 3 deletions nodemux/chains/near.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package chains

import (
"context"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand Down Expand Up @@ -34,7 +34,7 @@ func (self NearChain) StartSync(context context.Context, m *nodemuxcore.Multiple

func (self *NearChain) GetBlockhead(context context.Context, b *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
params := map[string]interface{}{"finality": "final"}
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "block", params)

var bt nearBlock
Expand All @@ -50,7 +50,7 @@ func (self *NearChain) GetBlockhead(context context.Context, b *nodemuxcore.Mult
return block, nil
}

func (self *NearChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *NearChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
// Custom relay methods can be defined here
return b.DefaultRelayRPC(rootCtx, chain, reqmsg, -3)
}
6 changes: 3 additions & 3 deletions nodemux/chains/polkadot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package chains
import (
"context"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"net/http"
)
Expand Down Expand Up @@ -42,7 +42,7 @@ func (self PolkadotChain) StartSync(context context.Context, m *nodemuxcore.Mult
}

func (self *PolkadotChain) GetBlockhead(context context.Context, b *nodemuxcore.Multiplexer, ep *nodemuxcore.Endpoint) (*nodemuxcore.Block, error) {
reqmsg := jlib.NewRequestMessage(
reqmsg := jsoff.NewRequestMessage(
1, "chain_getHeader", []interface{}{})

var bt polkadotBlock
Expand All @@ -58,7 +58,7 @@ func (self *PolkadotChain) GetBlockhead(context context.Context, b *nodemuxcore.
return block, nil
}

func (self *PolkadotChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, r *http.Request) (jlib.Message, error) {
func (self *PolkadotChain) DelegateRPC(rootCtx context.Context, b *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, r *http.Request) (jsoff.Message, error) {
// Custom relay methods can be defined here
return b.DefaultRelayRPC(rootCtx, chain, reqmsg, -2)
}
6 changes: 3 additions & 3 deletions nodemux/chains/presence_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"github.com/go-redis/redis/v8"
log "github.com/sirupsen/logrus"
"github.com/superisaac/jlib"
"github.com/superisaac/jsoff"
"github.com/superisaac/nodemux/core"
"math/rand"
"time"
Expand Down Expand Up @@ -69,7 +69,7 @@ func presenceCacheGetEndpoint(ctx context.Context, m *nodemuxcore.Multiplexer, c
// match a request message against a given methods list, if matched
// and the firsst param is txid then query the cache for an
// endpoint that has the txid.
func presenceCacheMatchRequest(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jlib.RequestMessage, methods ...string) (*nodemuxcore.Endpoint, bool) {
func presenceCacheMatchRequest(ctx context.Context, m *nodemuxcore.Multiplexer, chain nodemuxcore.ChainRef, reqmsg *jsoff.RequestMessage, methods ...string) (*nodemuxcore.Endpoint, bool) {
found := false
for _, mth := range methods {
if reqmsg.Method == mth {
Expand All @@ -86,7 +86,7 @@ func presenceCacheMatchRequest(ctx context.Context, m *nodemuxcore.Multiplexer,
Txid string
// Other params are skiped
}
err := jlib.DecodeParams(reqmsg.Params, &txidExtractor)
err := jsoff.DecodeParams(reqmsg.Params, &txidExtractor)
if err != nil {
reqmsg.Log().Warnf("error decoding params for txid: %s", err)
} else if txidExtractor.Txid != "" {
Expand Down
Loading

0 comments on commit ce0ce93

Please sign in to comment.