Files
coreth/sync/handlers/code_request_test.go
zeekay dd02f9cc9b chore: remove local replace directive and update vm dependency
- Update github.com/luxfi/vm v1.0.1 → v1.0.2
- Remove local replace directive for vm
- Use published package versions for reproducible builds
2026-01-03 07:11:08 -08:00

119 lines
4.1 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package handlers
import (
"context"
"crypto/rand"
"testing"
"github.com/luxfi/coreth/params"
"github.com/luxfi/coreth/sync/handlers/stats/statstest"
"github.com/luxfi/coreth/plugin/evm/message"
"github.com/luxfi/crypto"
"github.com/luxfi/geth/common"
"github.com/luxfi/geth/core/rawdb"
"github.com/luxfi/geth/ethdb/memorydb"
"github.com/luxfi/ids"
"github.com/stretchr/testify/assert"
)
func TestCodeRequestHandler(t *testing.T) {
database := memorydb.New()
codeBytes := []byte("some code goes here")
cryptoCodeHash := crypto.Keccak256Hash(codeBytes)
codeHash := common.BytesToHash(cryptoCodeHash.Bytes())
rawdb.WriteCode(database, codeHash, codeBytes)
maxSizeCodeBytes := make([]byte, params.MaxCodeSize)
n, err := rand.Read(maxSizeCodeBytes)
assert.NoError(t, err)
assert.Equal(t, params.MaxCodeSize, n)
cryptoMaxSizeCodeHash := crypto.Keccak256Hash(maxSizeCodeBytes)
maxSizeCodeHash := common.BytesToHash(cryptoMaxSizeCodeHash.Bytes())
rawdb.WriteCode(database, maxSizeCodeHash, maxSizeCodeBytes)
testHandlerStats := &statstest.TestHandlerStats{}
codeRequestHandler := NewCodeRequestHandler(database, message.Codec, testHandlerStats)
tests := map[string]struct {
setup func() (request message.CodeRequest, expectedCodeResponse [][]byte)
verifyStats func(t *testing.T, stats *statstest.TestHandlerStats)
}{
"normal": {
setup: func() (request message.CodeRequest, expectedCodeResponse [][]byte) {
return message.CodeRequest{
Hashes: []common.Hash{codeHash},
}, [][]byte{codeBytes}
},
verifyStats: func(t *testing.T, stats *statstest.TestHandlerStats) {
assert.EqualValues(t, 1, testHandlerStats.CodeRequestCount)
assert.EqualValues(t, len(codeBytes), testHandlerStats.CodeBytesReturnedSum)
},
},
"duplicate hashes": {
setup: func() (request message.CodeRequest, expectedCodeResponse [][]byte) {
return message.CodeRequest{
Hashes: []common.Hash{codeHash, codeHash},
}, nil
},
verifyStats: func(t *testing.T, stats *statstest.TestHandlerStats) {
assert.EqualValues(t, 1, testHandlerStats.DuplicateHashesRequested)
},
},
"too many hashes": {
setup: func() (request message.CodeRequest, expectedCodeResponse [][]byte) {
return message.CodeRequest{
Hashes: []common.Hash{{1}, {2}, {3}, {4}, {5}, {6}},
}, nil
},
verifyStats: func(t *testing.T, stats *statstest.TestHandlerStats) {
assert.EqualValues(t, 1, testHandlerStats.TooManyHashesRequested)
},
},
"max size code handled": {
setup: func() (request message.CodeRequest, expectedCodeResponse [][]byte) {
return message.CodeRequest{
Hashes: []common.Hash{maxSizeCodeHash},
}, [][]byte{maxSizeCodeBytes}
},
verifyStats: func(t *testing.T, stats *statstest.TestHandlerStats) {
assert.EqualValues(t, 1, testHandlerStats.CodeRequestCount)
assert.EqualValues(t, params.MaxCodeSize, testHandlerStats.CodeBytesReturnedSum)
},
},
}
for name, test := range tests {
// Reset stats before each test
testHandlerStats.Reset()
t.Run(name, func(t *testing.T) {
request, expectedResponse := test.setup()
responseBytes, err := codeRequestHandler.OnCodeRequest(context.Background(), ids.GenerateTestNodeID(), 1, request)
assert.NoError(t, err)
// If the expected response is empty, assert that the handler returns an empty response and return early.
if len(expectedResponse) == 0 {
assert.Len(t, responseBytes, 0, "expected response to be empty")
return
}
var response message.CodeResponse
if _, err = message.Codec.Unmarshal(responseBytes, &response); err != nil {
t.Fatal("error unmarshalling CodeResponse", err)
}
if len(expectedResponse) != len(response.Data) {
t.Fatalf("Unexpected length of code data expected %d != %d", len(expectedResponse), len(response.Data))
}
for i, code := range expectedResponse {
// assert.True(t, bytes.Equal(code, response.Data[i]), "code bytes mismatch at index %d", i)
assert.Equal(t, code, response.Data[i], "code bytes mismatch at index %d", i)
}
test.verifyStats(t, testHandlerStats)
})
}
}