Skip to content

Commit

Permalink
Add cache test
Browse files Browse the repository at this point in the history
  • Loading branch information
dkeysil committed Mar 8, 2024
1 parent a08bcc0 commit bbe7e33
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 26 deletions.
16 changes: 14 additions & 2 deletions services/json-rpc/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ func decodeBody(req *http.Request) (*jsonRpcReq, error) {
}

func decodeAndReplaceBody(req *http.Request) (*jsonRpcReq, error) {
b, _ := io.ReadAll(req.Body)
b, err := io.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("failed to read request body")
}
req.Body.Close()
req.Body = io.NopCloser(bytes.NewBuffer(b))

decodedBody, err := decodeBody(req)
if err != nil {
return nil, fmt.Errorf("failed to decode json-rpc request body")
}

req.Body.Close()

req.Body = io.NopCloser(bytes.NewBuffer(b))
return decodeBody(req)
return decodedBody, nil
}
1 change: 1 addition & 0 deletions services/json-rpc/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ func TestDecodeAndReplaceBody(t *testing.T) {

// still can read body because it was replaced
b, err := io.ReadAll(req.Body)
r.NoError(err)
r.Equal(bodyStr, string(b))
}
5 changes: 4 additions & 1 deletion services/json-rpc/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func TestTooManyReqsError(t *testing.T) {
var errResp errorResponse
r.NoError(json.NewDecoder(resp.Body).Decode(&errResp))
r.Equal("2.0", errResp.JSONRPC)
r.Equal(testRequestID, errResp.ID)
var id int
err = json.Unmarshal(errResp.ID, &id)
r.NoError(err)
r.Equal(testRequestID, id)
r.Equal(-32000, errResp.Error.Code)
r.Contains(errResp.Error.Message, "exceeds")
}
76 changes: 53 additions & 23 deletions services/json-rpc/json_rpc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ func (c *cache) collectGarbage(garbage map[uint64][]string) {
func (c *cache) Append(events *protocol.CombinedBlockEvents) {
garbage := make(map[uint64][]string, 0)
defer func() {
time.Sleep(c.cacheExpire)
c.collectGarbage(garbage)
go func() {
time.Sleep(c.cacheExpire)
c.collectGarbage(garbage)
}()
}()

for _, event := range events.Events {
Expand All @@ -212,7 +214,26 @@ func (c *cache) Append(events *protocol.CombinedBlockEvents) {
keys := make([]string, 0)

// eth_blockNumber
cc.put("eth_blockNumber", "", event.Block.Number)
val, ok := cc.get("eth_blockNumber", "")
if ok {
blockNumber := val.(string)
actualBlockNumber, err := strconv.ParseInt(strings.Replace(blockNumber, "0x", "", -1), 16, 64)
if err != nil {
continue
}

newBlockNumber, err := strconv.ParseInt(strings.Replace(event.Block.Number, "0x", "", -1), 16, 64)
if err != nil {
continue
}

if newBlockNumber > actualBlockNumber {
cc.put("eth_blockNumber", "", event.Block.Number)
}
} else {
cc.put("eth_blockNumber", "", event.Block.Number)
}

keys = append(keys, "eth_blockNumber")

// eth_getBlockByNumber
Expand Down Expand Up @@ -298,27 +319,8 @@ func (c *cache) Append(events *protocol.CombinedBlockEvents) {
intBlockNumber := int(blockNumber)
for i, trace := range event.Traces {
traceBlock[i] = domain.Trace{
Action: domain.TraceAction{
CallType: &trace.Action.CallType,
To: &trace.Action.To,
Input: &trace.Action.Input,
From: &trace.Action.From,
Gas: &trace.Action.Gas,
Value: &trace.Action.Value,
Init: &trace.Action.Init,
Address: &trace.Action.Address,
Balance: &trace.Action.Balance,
RefundAddress: &trace.Action.RefundAddress,
},
BlockHash: &event.Block.Hash,
BlockNumber: &intBlockNumber,
Result: &domain.TraceResult{
Output: &trace.Result.Output,
GasUsed: &trace.Result.GasUsed,
Address: &trace.Result.Address,
Code: &trace.Result.Code,
},
Subtraces: int(trace.Subtraces),
Subtraces: int(trace.Subtraces),
TraceAddress: func() []int {
traceAddress := make([]int, len(trace.TraceAddress))
for i, address := range trace.TraceAddress {
Expand All @@ -334,6 +336,34 @@ func (c *cache) Append(events *protocol.CombinedBlockEvents) {
Type: trace.Type,
Error: &trace.Error,
}

if event.Block != nil {
traceBlock[i].BlockHash = &event.Block.Hash
}

if trace.Action != nil {
traceBlock[i].Action = domain.TraceAction{
CallType: &trace.Action.CallType,
To: &trace.Action.To,
Input: &trace.Action.Input,
From: &trace.Action.From,
Gas: &trace.Action.Gas,
Value: &trace.Action.Value,
Init: &trace.Action.Init,
Address: &trace.Action.Address,
Balance: &trace.Action.Balance,
RefundAddress: &trace.Action.RefundAddress,
}
}

if trace.Result != nil {
traceBlock[i].Result = &domain.TraceResult{
Output: &trace.Result.Output,
GasUsed: &trace.Result.GasUsed,
Address: &trace.Result.Address,
Code: &trace.Result.Code,
}
}
}

cc.put("trace_block", fmt.Sprintf(`["%s"]`, event.Block.Number), traceBlock)
Expand Down
108 changes: 108 additions & 0 deletions services/json-rpc/json_rpc_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package json_rpc

import (
"testing"
"time"

"github.com/forta-network/forta-core-go/protocol"
"github.com/stretchr/testify/assert"
)

func TestCache(t *testing.T) {
cache := cache{
chains: make(map[uint64]*chainCache),
cacheExpire: time.Second,
}

cache.Append(events)

blockNumber, ok := cache.Get(1, "eth_blockNumber", "")
assert.True(t, ok)
assert.Equal(t, "1", blockNumber)

blockNumber, ok = cache.Get(2, "eth_blockNumber", "")
assert.True(t, ok)
assert.Equal(t, "101", blockNumber)

time.Sleep(time.Second)

blockNumber, ok = cache.Get(1, "eth_blockNumber", "")
assert.False(t, ok)
assert.Empty(t, blockNumber)
}

var events = &protocol.CombinedBlockEvents{
Events: []*protocol.CombinedBlockEvent{
{
ChainID: 1,
Block: &protocol.CombinedBlock{
Hash: "0xaaaa",
Number: "1",
Transactions: []*protocol.Transaction{
{
Hash: "0xbbbb",
From: "0xcccc",
},
},
Uncles: []string{"0xdddd"},
},
Logs: []*protocol.LogEntry{
{
Address: "0xcccc",
Topics: []string{"0xeeee"},
},
},
Traces: []*protocol.Trace{
{
Action: &protocol.TraceAction{
From: "0xcccc",
},
Result: &protocol.TraceResult{
Address: "0xcccc",
},
TraceAddress: []int64{1},
},
},
},
{
ChainID: 2,
Block: &protocol.CombinedBlock{
Hash: "0xffff",
Number: "100",
Transactions: []*protocol.Transaction{
{
Hash: "0x1111",
From: "0x2222",
},
},
Uncles: []string{"0x3333"},
},
Logs: []*protocol.LogEntry{},
Traces: []*protocol.Trace{
{
TraceAddress: []int64{2},
},
},
},
{
ChainID: 2,
Block: &protocol.CombinedBlock{
Hash: "0xfffd",
Number: "101",
Transactions: []*protocol.Transaction{
{
Hash: "0x1112",
From: "0x2223",
},
},
Uncles: []string{"0x3333"},
},
Logs: []*protocol.LogEntry{},
Traces: []*protocol.Trace{
{
TraceAddress: []int64{1},
},
},
},
},
}

0 comments on commit bbe7e33

Please sign in to comment.