code

package
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: BSD-3-Clause Imports: 22 Imported by: 0

README

Code sync

An EVM account references its contract code by hash. The bytecode lives outside the account trie. As the account syncer discovers code hashes, it passes them to this package to resolve that code from peers. This package also defines how to respond to that same request for syncing peers.

sequenceDiagram
  participant A as Account syncer
  participant S as Syncer
  participant P as Peer
  A->>S: AddCode(hashes)
  S->>S: drop stored and in-flight hashes
  S->>S: mark the rest to-fetch
  S-->>A: return
  S->>P: GetCode, a batch of hashes
  P->>P: read each hash from its database
  P-->>S: bytecode
  S->>S: verify, commit code, clear markers

AddCode MUST NOT block on the network. It is called from the same message handlers that deliver the syncer's own responses, so a blocked add could deadlock the sync. To avoid this blocking, AddCode does not bound the amount of outstanding code to fetch.

Crash recovery

Once AddCode returns, the account syncer persists accounts that reference the code. The to-fetch marker records the pending download in case the node crashes before it completes.

Entry Meaning Written Deleted
To-fetch marker code is referenced but not yet downloaded by AddCode, before it returns in the same batch as the code write
Code verified bytecode by Sync, after verification never

NewSyncer re-queues any marked hash that still lacks code, resuming the download after a crash.

Duplicates

Many accounts carry identical bytecode, so AddCode sees the same hash repeatedly. Repeats are dropped by an in-memory claim, taken before the disk read and released only after the code is committed, so the read cannot miss code from a racing fetch.

Verification

A response is accepted only if it carries one entry per requested hash and every entry hashes to its request. Anything else de-scores the peer, and the batch is retried elsewhere.

Serving

RegisterHandler installs the peer side of the exchange, answering GetCode from the node's database. A request is answered in full or rejected, since the syncer discards partial responses. Both sides cap a request at the number of maximum-size contracts that fit in one response.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterHandler

func RegisterHandler(log logging.Logger, net *p2p.Network, codeReader ethdb.KeyValueReader) error

RegisterHandler serves code-by-hash requests at p2p.EVMCodeRequestHandlerID on net.

Types

type Client

Client sends code-by-hash requests.

func NewClient

func NewClient(n *p2p.Network, peers *p2p.PeerTracker) *Client

NewClient binds a Client at p2p.EVMCodeRequestHandlerID on n.

type Syncer

type Syncer struct {
	// contains filtered or unexported fields
}

Syncer resolves contract code by hash from peers.

func NewSyncer

func NewSyncer(log logging.Logger, c *Client, db ethdb.KeyValueStore) (*Syncer, error)

NewSyncer returns a Syncer that downloads contract code through c and writes it to db. A restarted sync resumes where the previous one left off.

func (*Syncer) AddCode

func (s *Syncer) AddCode(hashes []common.Hash) (retErr error)

AddCode marks code hashes to be fetched during syncing. If AddCode returns nil, the code will be populated by the time Syncer.Sync returns nil, even if the node crashes and the syncer restarts.

AddCode NEVER blocks on the network, so it is safe to call from the VM's app message handlers. If it did, those handlers could deadlock with the syncer's own code requests.

func (*Syncer) DoneAdding

func (s *Syncer) DoneAdding()

DoneAdding stops Syncer.AddCode from accepting new hashes and allows Syncer.Sync to return once it finishes fetching the queued hashes.

DoneAdding is idempotent and safe to call at any time.

func (*Syncer) Sync

func (s *Syncer) Sync(ctx context.Context) error

Sync runs until Syncer.DoneAdding has been called and every hash given to Syncer.AddCode has its code on disk, or until ctx is cancelled.

Sync MUST be called at most once. A cancelled Sync leaves hashes claimed but unfetched, and only the recovery in NewSyncer re-enqueues them, so each attempt needs a fresh Syncer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL