Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgnoah committed Dec 13, 2023
1 parent 07a3b76 commit 75b368d
Showing 1 changed file with 72 additions and 86 deletions.
158 changes: 72 additions & 86 deletions rpc/jsonrpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"time"

"github.com/Yiling-J/theine-go"
"github.com/ava-labs/avalanchego/ids"

"github.com/AnomalyFi/hypersdk/chain"
Expand All @@ -28,17 +29,18 @@ import (

ethhex "github.com/ethereum/go-ethereum/common/hexutil"

"github.com/dgraph-io/ristretto"
"github.com/tidwall/btree"
)

type JSONRPCServer struct {
c Controller
headers *ristretto.Cache
headers *theine.Cache[string, *chain.StatefulBlock]
//*ristretto.Cache
//*types.ShardedMap[string, *chain.StatefulBlock]
//map[ids.ID]*chain.StatefulBlock // Map block ID to block header

blocksWithValidTxs *ristretto.Cache
blocksWithValidTxs *theine.Cache[string, *types.SequencerBlock]
//*ristretto.Cache
//*types.ShardedMap[string, *types.SequencerBlock]
//map[ids.ID]*types.SequencerBlock // Map block ID to block header

Expand All @@ -52,38 +54,56 @@ func NewJSONRPCServer(c Controller) *JSONRPCServer {
//headers := types.NewShardedMap[string, *chain.StatefulBlock](10000, 10, types.HashString)
//blocksWithValidTxs := types.NewShardedMap[string, *types.SequencerBlock](10000, 10, types.HashString)

//blockCacheSize := 1
idsByHeight := btree.Map[uint64, ids.ID]{}

config := ristretto.Config{
NumCounters: 700000,
MaxCost: 2240000,
BufferItems: 64,
Metrics: false,
}
blocks := btree.Map[int64, uint64]{}

headersBuilders := theine.NewBuilder[string, *chain.StatefulBlock](1750)

headersBuilders.RemovalListener(func(key string, value *chain.StatefulBlock, reason theine.RemoveReason) {
_, found := idsByHeight.Delete(value.Hght)
if found {
fmt.Println("Dropped idsByHeight")
fmt.Println(reason)
}
_, hit := blocks.Delete(value.Tmstmp)
if hit {
fmt.Println("Dropped blocks")
fmt.Println(reason)
}
})

headersCache, err := ristretto.NewCache(&config)
headers, err := headersBuilders.Build()
if err != nil {
panic(err)
}

config_new := ristretto.Config{
NumCounters: 700000,
MaxCost: 2240000,
BufferItems: 64,
Metrics: false,
}
blocksWithValidTxsCachebuilder := theine.NewBuilder[string, *types.SequencerBlock](1750)

blocksWithValidTxsCache, err := ristretto.NewCache(&config_new)
blocksWithValidTxsCachebuilder.RemovalListener(func(key string, value *types.SequencerBlock, reason theine.RemoveReason) {
_, found := idsByHeight.Delete(value.Hght)
if found {
fmt.Println("Dropped idsByHeight blocksWithValidTxsCachebuilder")
fmt.Println(reason)
}
_, hit := blocks.Delete(value.Tmstmp)
if hit {
fmt.Println("Dropped blocks blocksWithValidTxsCachebuilder")
fmt.Println(reason)
}
})

blocksWithValidTxsCache, err := blocksWithValidTxsCachebuilder.Build()
if err != nil {
panic(err)
}

return &JSONRPCServer{
c: c,
headers: headersCache,
headers: headers,
blocksWithValidTxs: blocksWithValidTxsCache,
idsByHeight: btree.Map[uint64, ids.ID]{},
blocks: btree.Map[int64, uint64]{},
idsByHeight: idsByHeight,
blocks: blocks,
}
}

Expand Down Expand Up @@ -353,17 +373,13 @@ func (j *JSONRPCServer) GetBlockHeadersByHeight(req *http.Request, args *GetBloc
if !found {
return errors.New("Could not find Block")
}
b, ok := blk.(*chain.StatefulBlock)
if !ok {
return errors.New("Could not decode Block")
}
l1Head, err := ethhex.DecodeBig(b.L1Head)
l1Head, err := ethhex.DecodeBig(blk.L1Head)
if err != nil {
return err
}
Prev = BlockInfo{
BlockId: prevBlkId.String(),
Timestamp: b.Tmstmp,
Timestamp: blk.Tmstmp,
L1Head: l1Head.Uint64(),
}
}
Expand All @@ -378,18 +394,14 @@ func (j *JSONRPCServer) GetBlockHeadersByHeight(req *http.Request, args *GetBloc
if !found {
return false
}
b, ok := blk.(*chain.StatefulBlock)
if !ok {
return false
}

l1Head, err := ethhex.DecodeBig(b.L1Head)
l1Head, err := ethhex.DecodeBig(blk.L1Head)
if err != nil {
return false
}

if b.Tmstmp >= args.End {
tmp := b.Tmstmp / 1000
if blk.Tmstmp >= args.End {
tmp := blk.Tmstmp / 1000

Next = BlockInfo{
BlockId: id.String(),
Expand All @@ -399,8 +411,8 @@ func (j *JSONRPCServer) GetBlockHeadersByHeight(req *http.Request, args *GetBloc
return false
}

if b.Hght == heightKey {
tmp := b.Tmstmp / 1000
if blk.Hght == heightKey {
tmp := blk.Tmstmp / 1000

blocks = append(blocks, BlockInfo{
BlockId: id.String(),
Expand Down Expand Up @@ -435,12 +447,8 @@ func (j *JSONRPCServer) GetBlockHeadersID(req *http.Request, args *GetBlockHeade
if !found {
return errors.New("Could not find Block")
}
b, ok := block.(*chain.StatefulBlock)
if !ok {
return errors.New("Could not decode Block")
}

firstBlock = b.Hght
firstBlock = block.Hght
// Handle hash parameter
// ...
} else {
Expand Down Expand Up @@ -468,17 +476,14 @@ func (j *JSONRPCServer) GetBlockHeadersID(req *http.Request, args *GetBlockHeade
if !found {
return errors.New("Could not find Previous Block")
}
b, ok := blk.(*chain.StatefulBlock)
if !ok {
return errors.New("Could not decode Block")
}
l1Head, err := ethhex.DecodeBig(b.L1Head)

l1Head, err := ethhex.DecodeBig(blk.L1Head)
if err != nil {
return err
}
Prev = BlockInfo{
BlockId: prevBlkId.String(),
Timestamp: b.Tmstmp,
Timestamp: blk.Tmstmp,
L1Head: l1Head.Uint64(),
}
} else {
Expand All @@ -496,11 +501,8 @@ func (j *JSONRPCServer) GetBlockHeadersID(req *http.Request, args *GetBlockHeade
if !found {
return false
}
b, ok := blk.(*chain.StatefulBlock)
if !ok {
return false
}
l1Head, err := ethhex.DecodeBig(b.L1Head)

l1Head, err := ethhex.DecodeBig(blk.L1Head)
if err != nil {
// TODO add error potentially
return false
Expand All @@ -523,8 +525,8 @@ func (j *JSONRPCServer) GetBlockHeadersID(req *http.Request, args *GetBlockHeade
// return false
// }

if b.Tmstmp >= args.End {
tmp := b.Tmstmp / 1000
if blk.Tmstmp >= args.End {
tmp := blk.Tmstmp / 1000

Next = BlockInfo{
BlockId: id.String(),
Expand All @@ -534,8 +536,8 @@ func (j *JSONRPCServer) GetBlockHeadersID(req *http.Request, args *GetBlockHeade
return false
}

if b.Hght == heightKey {
tmp := b.Tmstmp / 1000
if blk.Hght == heightKey {
tmp := blk.Tmstmp / 1000

blocks = append(blocks, BlockInfo{
BlockId: id.String(),
Expand Down Expand Up @@ -601,15 +603,12 @@ func (j *JSONRPCServer) GetBlockHeadersByStart(req *http.Request, args *GetBlock
if !found {
return fmt.Errorf("Could not find Previous Block: %d ", firstBlock)
}
b, ok := blk.(*chain.StatefulBlock)
if !ok {
return errors.New("Could not decode Block")
}
l1Head, err := ethhex.DecodeBig(b.L1Head)

l1Head, err := ethhex.DecodeBig(blk.L1Head)
if err != nil {
return err
}
tmp := b.Tmstmp / 1000
tmp := blk.Tmstmp / 1000
Prev = BlockInfo{
BlockId: prevBlkId.String(),
Timestamp: tmp,
Expand All @@ -631,26 +630,23 @@ func (j *JSONRPCServer) GetBlockHeadersByStart(req *http.Request, args *GetBlock
j.idsByHeight.Ascend(firstBlock, func(heightKey uint64, id ids.ID) bool {
// Does heightKey match the given block's height for the id
blk, found := j.headers.Get(id.String())
b, ok := blk.(*chain.StatefulBlock)
if !ok {
return false
}
fmt.Println("Block SEQ: ", b.StateRoot.String(), "Block L1:", b.L1Head)

fmt.Println("Block SEQ: ", blk.StateRoot.String(), "Block L1:", blk.L1Head)

if !found {
return false
}
l1Head, err := ethhex.DecodeBig(b.L1Head)
l1Head, err := ethhex.DecodeBig(blk.L1Head)
if err != nil {
// TODO add error potentially
return false
}

if b.Tmstmp >= args.End {
if blk.Tmstmp >= args.End {
fmt.Println("Next Found")
fmt.Println("Blocks Length After Next:", len(blocks))

tmp := b.Tmstmp / 1000
tmp := blk.Tmstmp / 1000

Next = BlockInfo{
BlockId: id.String(),
Expand All @@ -660,8 +656,8 @@ func (j *JSONRPCServer) GetBlockHeadersByStart(req *http.Request, args *GetBlock
return false
}

if b.Hght == heightKey {
tmp := b.Tmstmp / 1000
if blk.Hght == heightKey {
tmp := blk.Tmstmp / 1000

blocks = append(blocks, BlockInfo{
BlockId: id.String(),
Expand Down Expand Up @@ -710,12 +706,7 @@ func (j *JSONRPCServer) GetBlockTransactions(req *http.Request, args *GetBlockTr
return errors.New("Txs Not Found")
}

b, ok := block.(*chain.StatefulBlock)
if !ok {
return errors.New("Could not decode Block")
}

reply.Txs = b.Txs
reply.Txs = block.Txs
reply.BlockId = args.ID

return nil
Expand All @@ -733,12 +724,7 @@ func (j *JSONRPCServer) GetBlockTransactionsByNamespace(req *http.Request, args
return fmt.Errorf("Could not find Block: %s ", BlkId.String())
}

b, ok := block.(*types.SequencerBlock)
if !ok {
return errors.New("Could not decode Block")
}

txs := b.Txs[args.Namespace]
txs := block.Txs[args.Namespace]

reply.Txs = txs
reply.BlockId = BlkId.String()
Expand All @@ -757,7 +743,7 @@ func (j *JSONRPCServer) AcceptBlock(blk *chain.StatelessBlock) error {
block, results, _, id, err := rpc.UnpackBlockMessage(msg, parser)

//TODO I should experiment with TTL
j.headers.SetWithTTL(id.String(), block, int64(block.Size()), 4*time.Hour)
j.headers.SetWithTTL(id.String(), block, 1, 1*time.Hour)
// j.headers[id.String()] = block
j.idsByHeight.Set(block.Hght, *id)
j.blocks.Set(block.Tmstmp, block.Hght)
Expand Down Expand Up @@ -797,7 +783,7 @@ func (j *JSONRPCServer) AcceptBlock(blk *chain.StatelessBlock) error {
}

// TODO need to fix this
j.blocksWithValidTxs.SetWithTTL(id.String(), sequencerBlock, int64(len(sequencerBlock.Txs)), 4*time.Hour)
j.blocksWithValidTxs.SetWithTTL(id.String(), sequencerBlock, 1, 1*time.Hour)

return nil
}
Expand Down

0 comments on commit 75b368d

Please sign in to comment.