cache_test.go 13.8 KB
Newer Older
inphi's avatar
inphi committed
1 2 3 4 5 6 7 8 9 10 11
package proxyd

import (
	"context"
	"math"
	"strconv"
	"testing"

	"github.com/stretchr/testify/require"
)

12 13
const numBlockConfirmations = 10

inphi's avatar
inphi committed
14
func TestRPCCacheImmutableRPCs(t *testing.T) {
inphi's avatar
inphi committed
15 16 17
	const blockHead = math.MaxUint64
	ctx := context.Background()

inphi's avatar
inphi committed
18
	getBlockNum := func(ctx context.Context) (uint64, error) {
inphi's avatar
inphi committed
19 20
		return blockHead, nil
	}
21
	cache := newRPCCache(newMemoryCache(), getBlockNum, nil, numBlockConfirmations)
inphi's avatar
inphi committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	ID := []byte(strconv.Itoa(1))

	rpcs := []struct {
		req  *RPCReq
		res  *RPCRes
		name string
	}{
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_chainId",
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  "0xff",
				ID:      ID,
			},
			name: "eth_chainId",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "net_version",
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  "9999",
				ID:      ID,
			},
			name: "net_version",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockByNumber",
				Params:  []byte(`["0x1", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `{"difficulty": "0x1", "number": "0x1"}`,
				ID:      ID,
			},
			name: "eth_getBlockByNumber",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockByNumber",
				Params:  []byte(`["earliest", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `{"difficulty": "0x1", "number": "0x1"}`,
				ID:      ID,
			},
			name: "eth_getBlockByNumber earliest",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				Params:  []byte(`["0x1", "0x2", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
				ID:      ID,
			},
			name: "eth_getBlockRange",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				Params:  []byte(`["earliest", "0x2", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
				ID:      ID,
			},
			name: "eth_getBlockRange earliest",
		},
	}

	for _, rpc := range rpcs {
		t.Run(rpc.name, func(t *testing.T) {
115
			err := cache.PutRPC(ctx, rpc.req, rpc.res)
inphi's avatar
inphi committed
116 117 118 119 120 121 122 123 124
			require.NoError(t, err)

			cachedRes, err := cache.GetRPC(ctx, rpc.req)
			require.NoError(t, err)
			require.Equal(t, rpc.res, cachedRes)
		})
	}
}

inphi's avatar
inphi committed
125 126 127 128 129 130 131 132 133 134 135 136
func TestRPCCacheBlockNumber(t *testing.T) {
	var blockHead uint64 = 0x1000
	var gasPrice uint64 = 0x100
	ctx := context.Background()
	ID := []byte(strconv.Itoa(1))

	getGasPrice := func(ctx context.Context) (uint64, error) {
		return gasPrice, nil
	}
	getBlockNum := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
137
	cache := newRPCCache(newMemoryCache(), getBlockNum, getGasPrice, numBlockConfirmations)
inphi's avatar
inphi committed
138 139 140 141 142 143 144 145 146 147 148 149

	req := &RPCReq{
		JSONRPC: "2.0",
		Method:  "eth_blockNumber",
		ID:      ID,
	}
	res := &RPCRes{
		JSONRPC: "2.0",
		Result:  `0x1000`,
		ID:      ID,
	}

150
	err := cache.PutRPC(ctx, req, res)
inphi's avatar
inphi committed
151 152 153 154 155
	require.NoError(t, err)

	cachedRes, err := cache.GetRPC(ctx, req)
	require.NoError(t, err)
	require.Equal(t, res, cachedRes)
156 157 158 159 160

	blockHead = 0x1001
	cachedRes, err = cache.GetRPC(ctx, req)
	require.NoError(t, err)
	require.Equal(t, &RPCRes{JSONRPC: "2.0", Result: `0x1001`, ID: ID}, cachedRes)
inphi's avatar
inphi committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

func TestRPCCacheGasPrice(t *testing.T) {
	var blockHead uint64 = 0x1000
	var gasPrice uint64 = 0x100
	ctx := context.Background()
	ID := []byte(strconv.Itoa(1))

	getGasPrice := func(ctx context.Context) (uint64, error) {
		return gasPrice, nil
	}
	getBlockNum := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
175
	cache := newRPCCache(newMemoryCache(), getBlockNum, getGasPrice, numBlockConfirmations)
inphi's avatar
inphi committed
176 177 178 179 180 181 182 183 184 185 186 187

	req := &RPCReq{
		JSONRPC: "2.0",
		Method:  "eth_gasPrice",
		ID:      ID,
	}
	res := &RPCRes{
		JSONRPC: "2.0",
		Result:  `0x100`,
		ID:      ID,
	}

188
	err := cache.PutRPC(ctx, req, res)
inphi's avatar
inphi committed
189 190 191 192 193
	require.NoError(t, err)

	cachedRes, err := cache.GetRPC(ctx, req)
	require.NoError(t, err)
	require.Equal(t, res, cachedRes)
194 195 196 197 198

	gasPrice = 0x101
	cachedRes, err = cache.GetRPC(ctx, req)
	require.NoError(t, err)
	require.Equal(t, &RPCRes{JSONRPC: "2.0", Result: `0x101`, ID: ID}, cachedRes)
inphi's avatar
inphi committed
199 200
}

inphi's avatar
inphi committed
201 202 203 204 205 206 207
func TestRPCCacheUnsupportedMethod(t *testing.T) {
	const blockHead = math.MaxUint64
	ctx := context.Background()

	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
208
	cache := newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations)
inphi's avatar
inphi committed
209 210 211 212
	ID := []byte(strconv.Itoa(1))

	req := &RPCReq{
		JSONRPC: "2.0",
inphi's avatar
inphi committed
213
		Method:  "eth_syncing",
inphi's avatar
inphi committed
214 215 216 217
		ID:      ID,
	}
	res := &RPCRes{
		JSONRPC: "2.0",
inphi's avatar
inphi committed
218
		Result:  false,
inphi's avatar
inphi committed
219 220 221
		ID:      ID,
	}

222
	err := cache.PutRPC(ctx, req, res)
inphi's avatar
inphi committed
223 224 225 226 227 228 229
	require.NoError(t, err)

	cachedRes, err := cache.GetRPC(ctx, req)
	require.NoError(t, err)
	require.Nil(t, cachedRes)
}

inphi's avatar
inphi committed
230 231 232
func TestRPCCacheEthGetBlockByNumber(t *testing.T) {
	ctx := context.Background()

233
	var blockHead uint64
inphi's avatar
inphi committed
234 235 236
	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
237
	makeCache := func() RPCCache { return newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations) }
inphi's avatar
inphi committed
238 239 240 241 242
	ID := []byte(strconv.Itoa(1))

	req := &RPCReq{
		JSONRPC: "2.0",
		Method:  "eth_getBlockByNumber",
243
		Params:  []byte(`["0xa", false]`),
inphi's avatar
inphi committed
244 245 246 247 248 249 250 251 252 253
		ID:      ID,
	}
	res := &RPCRes{
		JSONRPC: "2.0",
		Result:  `{"difficulty": "0x1", "number": "0x1"}`,
		ID:      ID,
	}
	req2 := &RPCReq{
		JSONRPC: "2.0",
		Method:  "eth_getBlockByNumber",
254
		Params:  []byte(`["0xb", false]`),
inphi's avatar
inphi committed
255 256 257 258 259 260 261 262
		ID:      ID,
	}
	res2 := &RPCRes{
		JSONRPC: "2.0",
		Result:  `{"difficulty": "0x2", "number": "0x2"}`,
		ID:      ID,
	}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	t.Run("set multiple finalized blocks", func(t *testing.T) {
		blockHead = 100
		cache := makeCache()
		require.NoError(t, cache.PutRPC(ctx, req, res))
		require.NoError(t, cache.PutRPC(ctx, req2, res2))
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Equal(t, res, cachedRes)
		cachedRes, err = cache.GetRPC(ctx, req2)
		require.NoError(t, err)
		require.Equal(t, res2, cachedRes)
	})

	t.Run("unconfirmed block", func(t *testing.T) {
		blockHead = 0xc
		cache := makeCache()
		require.NoError(t, cache.PutRPC(ctx, req, res))
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Nil(t, cachedRes)
	})
inphi's avatar
inphi committed
284 285
}

inphi's avatar
inphi committed
286 287 288 289 290 291 292
func TestRPCCacheEthGetBlockByNumberForRecentBlocks(t *testing.T) {
	ctx := context.Background()

	var blockHead uint64 = 2
	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
293
	cache := newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations)
inphi's avatar
inphi committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	ID := []byte(strconv.Itoa(1))

	rpcs := []struct {
		req  *RPCReq
		res  *RPCRes
		name string
	}{
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockByNumber",
				Params:  []byte(`["latest", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `{"difficulty": "0x1", "number": "0x1"}`,
				ID:      ID,
			},
			name: "latest block",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockByNumber",
				Params:  []byte(`["pending", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `{"difficulty": "0x1", "number": "0x1"}`,
				ID:      ID,
			},
			name: "pending block",
		},
	}

	for _, rpc := range rpcs {
		t.Run(rpc.name, func(t *testing.T) {
333
			err := cache.PutRPC(ctx, rpc.req, rpc.res)
inphi's avatar
inphi committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
			require.NoError(t, err)

			cachedRes, err := cache.GetRPC(ctx, rpc.req)
			require.NoError(t, err)
			require.Nil(t, cachedRes)
		})
	}
}

func TestRPCCacheEthGetBlockByNumberInvalidRequest(t *testing.T) {
	ctx := context.Background()

	const blockHead = math.MaxUint64
	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
350
	cache := newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations)
inphi's avatar
inphi committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364
	ID := []byte(strconv.Itoa(1))

	req := &RPCReq{
		JSONRPC: "2.0",
		Method:  "eth_getBlockByNumber",
		Params:  []byte(`["0x1"]`), // missing required boolean param
		ID:      ID,
	}
	res := &RPCRes{
		JSONRPC: "2.0",
		Result:  `{"difficulty": "0x1", "number": "0x1"}`,
		ID:      ID,
	}

365
	err := cache.PutRPC(ctx, req, res)
inphi's avatar
inphi committed
366
	require.Error(t, err)
inphi's avatar
inphi committed
367 368

	cachedRes, err := cache.GetRPC(ctx, req)
inphi's avatar
inphi committed
369
	require.Error(t, err)
inphi's avatar
inphi committed
370 371 372
	require.Nil(t, cachedRes)
}

373 374 375
func TestRPCCacheEthGetBlockRange(t *testing.T) {
	ctx := context.Background()

376
	var blockHead uint64
377 378 379
	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
380
	makeCache := func() RPCCache { return newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations) }
381 382
	ID := []byte(strconv.Itoa(1))

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	t.Run("finalized block", func(t *testing.T) {
		req := &RPCReq{
			JSONRPC: "2.0",
			Method:  "eth_getBlockRange",
			Params:  []byte(`["0x1", "0x10", false]`),
			ID:      ID,
		}
		res := &RPCRes{
			JSONRPC: "2.0",
			Result:  `[{"number": "0x1"}, {"number": "0x10"}]`,
			ID:      ID,
		}
		blockHead = 0x1000
		cache := makeCache()
		require.NoError(t, cache.PutRPC(ctx, req, res))
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Equal(t, res, cachedRes)
	})

	t.Run("unconfirmed block", func(t *testing.T) {
		cache := makeCache()
		req := &RPCReq{
			JSONRPC: "2.0",
			Method:  "eth_getBlockRange",
			Params:  []byte(`["0x1", "0x1000", false]`),
			ID:      ID,
		}
		res := &RPCRes{
			JSONRPC: "2.0",
			Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
			ID:      ID,
		}
		require.NoError(t, cache.PutRPC(ctx, req, res))
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Nil(t, cachedRes)
	})
421 422
}

inphi's avatar
inphi committed
423 424 425 426 427 428 429
func TestRPCCacheEthGetBlockRangeForRecentBlocks(t *testing.T) {
	ctx := context.Background()

	var blockHead uint64 = 0x1000
	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
430
	cache := newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations)
inphi's avatar
inphi committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	ID := []byte(strconv.Itoa(1))

	rpcs := []struct {
		req  *RPCReq
		res  *RPCRes
		name string
	}{
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				Params:  []byte(`["0x1", "latest", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
				ID:      ID,
			},
			name: "latest block",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				Params:  []byte(`["0x1", "pending", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
				ID:      ID,
			},
			name: "pending block",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				Params:  []byte(`["latest", "0x1000", false]`),
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
				ID:      ID,
			},
			name: "latest block 2",
		},
	}

	for _, rpc := range rpcs {
		t.Run(rpc.name, func(t *testing.T) {
484
			err := cache.PutRPC(ctx, rpc.req, rpc.res)
inphi's avatar
inphi committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
			require.NoError(t, err)

			cachedRes, err := cache.GetRPC(ctx, rpc.req)
			require.NoError(t, err)
			require.Nil(t, cachedRes)
		})
	}
}

func TestRPCCacheEthGetBlockRangeInvalidRequest(t *testing.T) {
	ctx := context.Background()

	const blockHead = math.MaxUint64
	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
501
	cache := newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations)
inphi's avatar
inphi committed
502 503
	ID := []byte(strconv.Itoa(1))

inphi's avatar
inphi committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	rpcs := []struct {
		req  *RPCReq
		res  *RPCRes
		name string
	}{
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				Params:  []byte(`["0x1", "0x2"]`), // missing required boolean param
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
				ID:      ID,
			},
			name: "missing boolean param",
		},
		{
			req: &RPCReq{
				JSONRPC: "2.0",
				Method:  "eth_getBlockRange",
				Params:  []byte(`["abc", "0x2", true]`), // invalid block hex
				ID:      ID,
			},
			res: &RPCRes{
				JSONRPC: "2.0",
				Result:  `[{"number": "0x1"}, {"number": "0x2"}]`,
				ID:      ID,
			},
			name: "invalid block hex",
		},
inphi's avatar
inphi committed
537 538
	}

inphi's avatar
inphi committed
539
	for _, rpc := range rpcs {
inphi's avatar
inphi committed
540
		t.Run(rpc.name, func(t *testing.T) {
541
			err := cache.PutRPC(ctx, rpc.req, rpc.res)
inphi's avatar
inphi committed
542
			require.Error(t, err)
inphi's avatar
inphi committed
543

inphi's avatar
inphi committed
544 545 546 547 548
			cachedRes, err := cache.GetRPC(ctx, rpc.req)
			require.Error(t, err)
			require.Nil(t, cachedRes)
		})
	}
inphi's avatar
inphi committed
549
}
inphi's avatar
inphi committed
550 551 552 553

func TestRPCCacheEthCall(t *testing.T) {
	ctx := context.Background()

554
	var blockHead uint64
inphi's avatar
inphi committed
555 556 557
	fn := func(ctx context.Context) (uint64, error) {
		return blockHead, nil
	}
558 559

	makeCache := func() RPCCache { return newRPCCache(newMemoryCache(), fn, nil, numBlockConfirmations) }
inphi's avatar
inphi committed
560 561 562 563 564
	ID := []byte(strconv.Itoa(1))

	req := &RPCReq{
		JSONRPC: "2.0",
		Method:  "eth_call",
565
		Params:  []byte(`[{"to": "0xDEADBEEF", "data": "0x1"}, "0x10"]`),
inphi's avatar
inphi committed
566 567 568 569 570 571 572 573
		ID:      ID,
	}
	res := &RPCRes{
		JSONRPC: "2.0",
		Result:  `0x0`,
		ID:      ID,
	}

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	t.Run("finalized block", func(t *testing.T) {
		blockHead = 0x100
		cache := makeCache()
		err := cache.PutRPC(ctx, req, res)
		require.NoError(t, err)
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Equal(t, res, cachedRes)
	})

	t.Run("unconfirmed block", func(t *testing.T) {
		blockHead = 0x10
		cache := makeCache()
		require.NoError(t, cache.PutRPC(ctx, req, res))
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Nil(t, cachedRes)
	})

	t.Run("latest block", func(t *testing.T) {
		blockHead = 0x100
		req := &RPCReq{
			JSONRPC: "2.0",
			Method:  "eth_call",
			Params:  []byte(`[{"to": "0xDEADBEEF", "data": "0x1"}, "latest"]`),
			ID:      ID,
		}
		cache := makeCache()
		require.NoError(t, cache.PutRPC(ctx, req, res))
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Nil(t, cachedRes)
	})

	t.Run("pending block", func(t *testing.T) {
		blockHead = 0x100
		req := &RPCReq{
			JSONRPC: "2.0",
			Method:  "eth_call",
			Params:  []byte(`[{"to": "0xDEADBEEF", "data": "0x1"}, "pending"]`),
			ID:      ID,
		}
		cache := makeCache()
		require.NoError(t, cache.PutRPC(ctx, req, res))
		cachedRes, err := cache.GetRPC(ctx, req)
		require.NoError(t, err)
		require.Nil(t, cachedRes)
	})
inphi's avatar
inphi committed
622
}