mirror of
https://github.com/luxfi/evm.git
synced 2026-09-24 00:27:44 +00:00
Salvages an in-flight Wave 1C cascade on evm/ (the EVM fork separate
from coreth and evmgpu). 17 files touched. Mirrors the coreth
migration patterns:
- plugin/evm/message/codec.go: defines local Manager interface with
hand-rolled wrappers.Packer; drops codec.Manager polymorphic registry.
- plugin/evm/message/request.go: typed Marshaler boundary.
- sync/{client,handlers}/*.go: codec.Manager → message.Manager swap.
- plugin/evm/customrawdb/*: explicit big-endian for state sync schema.
- plugin/evm/validators/state/{codec,state,state_test}.go: hand-rolled.
- plugin/evm/upgrade/feewindow/window.go + plugin/evm/vm.go +
network/network.go + plugin/evm/network_handler.go: call-site fixups.
- precompile/registry/registry.go: codec-free registration path.
Builds clean: go build ./...
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/luxfi/ids"
|
|
|
|
"github.com/luxfi/evm/plugin/evm/message"
|
|
"github.com/luxfi/evm/sync/handlers/stats"
|
|
"github.com/luxfi/geth/common"
|
|
"github.com/luxfi/geth/core/rawdb"
|
|
"github.com/luxfi/geth/ethdb"
|
|
log "github.com/luxfi/log"
|
|
)
|
|
|
|
// CodeRequestHandler is a peer.RequestHandler for message.CodeRequest
|
|
// serving requested contract code bytes
|
|
type CodeRequestHandler struct {
|
|
codeReader ethdb.KeyValueReader
|
|
codec message.Manager
|
|
stats stats.CodeRequestHandlerStats
|
|
}
|
|
|
|
func NewCodeRequestHandler(codeReader ethdb.KeyValueReader, codec message.Manager, stats stats.CodeRequestHandlerStats) *CodeRequestHandler {
|
|
handler := &CodeRequestHandler{
|
|
codeReader: codeReader,
|
|
codec: codec,
|
|
stats: stats,
|
|
}
|
|
return handler
|
|
}
|
|
|
|
// OnCodeRequest handles request to retrieve contract code by its hash in message.CodeRequest
|
|
// Never returns error
|
|
// Returns nothing if code hash is not found
|
|
// Expects returned errors to be treated as FATAL
|
|
// Assumes ctx is active
|
|
func (n *CodeRequestHandler) OnCodeRequest(_ context.Context, nodeID ids.NodeID, requestID uint32, codeRequest message.CodeRequest) ([]byte, error) {
|
|
startTime := time.Now()
|
|
n.stats.IncCodeRequest()
|
|
|
|
// always report code read time metric
|
|
defer func() {
|
|
n.stats.UpdateCodeReadTime(time.Since(startTime))
|
|
}()
|
|
|
|
if len(codeRequest.Hashes) > message.MaxCodeHashesPerRequest {
|
|
n.stats.IncTooManyHashesRequested()
|
|
log.Debug("too many hashes requested, dropping request", "nodeID", nodeID, "requestID", requestID, "numHashes", len(codeRequest.Hashes))
|
|
return nil, nil
|
|
}
|
|
if !isUnique(codeRequest.Hashes) {
|
|
n.stats.IncDuplicateHashesRequested()
|
|
log.Debug("duplicate code hashes requested, dropping request", "nodeID", nodeID, "requestID", requestID)
|
|
return nil, nil
|
|
}
|
|
|
|
codeBytes := make([][]byte, len(codeRequest.Hashes))
|
|
totalBytes := 0
|
|
for i, hash := range codeRequest.Hashes {
|
|
codeBytes[i] = rawdb.ReadCode(n.codeReader, hash)
|
|
if len(codeBytes[i]) == 0 {
|
|
n.stats.IncMissingCodeHash()
|
|
log.Debug("requested code not found, dropping request", "nodeID", nodeID, "requestID", requestID, "hash", hash)
|
|
return nil, nil
|
|
}
|
|
totalBytes += len(codeBytes[i])
|
|
}
|
|
|
|
codeResponse := message.CodeResponse{Data: codeBytes}
|
|
responseBytes, err := n.codec.Marshal(message.Version, codeResponse)
|
|
if err != nil {
|
|
log.Error("could not marshal CodeResponse, dropping request", "nodeID", nodeID, "requestID", requestID, "request", codeRequest, "err", err)
|
|
return nil, nil
|
|
}
|
|
n.stats.UpdateCodeBytesReturned(uint32(totalBytes))
|
|
return responseBytes, nil
|
|
}
|
|
|
|
func isUnique(hashes []common.Hash) bool {
|
|
seen := make(map[common.Hash]struct{})
|
|
for _, hash := range hashes {
|
|
if _, found := seen[hash]; found {
|
|
return false
|
|
}
|
|
seen[hash] = struct{}{}
|
|
}
|
|
return true
|
|
}
|