800 Commits

Author SHA1 Message Date
kdeme
e973d0b8ef
Improvements to discv5 sessions (#773)
- Adjust Sessions API to make use of results.Opt instead of
var params
- Add loadReadKey for when only the readKey is required
- Add 32-bit counter to a Session and add nextNonce call that
creates the nonce to use for next encryption (using the 32-bit
counter)
- Use nextNonce in discv5 encryption
- Tests for Sessions / nextNonce
2025-02-14 19:22:25 +01:00
bhartnett
c640d3c444
Discv5 Protocol: Add support for banning nodes (#769)
* Add banned nodes to routing table.

* Filter out banned nodes in lookups and cleanup expired bans in refreshLoop.

* Don't respond to messages from banned nodes.

* Prevent sending messages to banned nodes.
2025-01-30 19:28:10 +08:00
andri lim
e589cc0288
Reduce declared but not used warnings (#770) 2025-01-29 12:32:58 +07:00
Chirag Parmar
254be32672
Optimize RLP serialization (#765)
* add a profiling test

* basic rlp profiler

* force expansion of memory by reducing allocation size

* optimize using two pass

* fix two pass

* add a chunked buffer implementation

* add a chunked buffer implementation

* multiple writers

* add api tests

* fixes

* fix double stack update problem

* using a length tracker

* remove code redundancy

* collect length in a different manner

* add all tests

* revert changes

* fix

* hash writer

* fix

* remove unwanted files

* conditional length writer execution

* perf improvements - remove adds

* remove delete operations

* do not use delete

* lint code

* fix build warnings

* new benchmark contradicting results

* use a static buffer for big endian

* make lengths a tuple

* compile time evaluation of types

* static and dynamic pending lists

* return hash32

* add block header hashing to profiler

* add a more reliable profiler

* add/update copyright

* took TOO long for TWO lines of code
2025-01-27 10:17:37 +07:00
bhartnett
c6c9dc7ae0
Discv5 Routing Table: Add support for banning nodes (#768)
* Add banned nodes to routing table.
2025-01-24 13:30:11 +08:00
Ivan FB
be17263b2a
add discv5_network_byte metric in discoveryv5 protocol (#767) 2025-01-06 22:48:42 +01:00
Jacek Sieka
c2d47ac20e
ecies: cleanup (#766)
* avoid memory allocation in kdf
* fix some (mostly harmless) off-by-one openArrays
* avoid packed object
* fix some missing burnMem
2024-12-30 14:09:20 +01:00
Jacek Sieka
dcfbc4291d
update version to 0.5.0 (#764)
1.0.0 was never an intentional release and the nim-eth repo is not yet
stable in terms of API, thus we start at 0.5.0 and go from there for the
first tagged release.

Most of the eth code in this repo now is aligned with various specs
meaning that most API can be considered "mostly" stable, but there are
still aspects being worked on as well as a potential future
reorganisation of the code turning nim-eth into a more "pure"
spec-driven core ethereum infrastructure repo, removing in the process
application-level stuff that happens to be common between nimbus-eth1
and eth2 and ended up in here for convenience.
2024-12-04 12:40:44 +01:00
Kim De Mey
aa92ad4f42
discv4: Fix assert on invalid RLP list in Neighbours message (#763)
The assert would occur when the rlp size of the a node in the
nodes rlp list is incorrectly set too high and then the next
`listElem` call for the next node will start from the
incorrect data. When that data is not a list the assert in
`listElem` will be triggered.

Fixed by adding a `listLen` call which checks if it is a list.
Added also more strictness by:
- Checking if that list is of len 4, which it must be
- raising immediatly on invalid IP length
- raising immediatly on invalid public key / node id

+ test cases
2024-12-02 09:38:00 +01:00
Kim De Mey
dc092ca393
Add missing rlp imports to properple decode/encode blocks/bodies (#761)
Without these added transanction/header import/exports decoding
blocks or block bodies would fail if one only had imported
blocks_rlp.

Also removes unused import + add copyright comments in rlp code.
2024-11-20 12:15:34 +01:00
Jacek Sieka
88e4be4dc4
devp2p: upgrade to v5 (EIP-706) (#760)
* drop support for v4 (obsolete, doesn't work with all clients since
they use chunking and other obsolete v4 features that we're missing or
don't support it at all)
* rework asyncraises
* always store generated p2p macro code (similar to eth2)
* preparation for chronos cancellation support (more to be done here)
* when peer is disconnected, ensure pending handshakes and requests are
notified (instead of waiting for timeout)
* disallow raising from `onPeerDisconnected` - this simplifies
disconnection coordination among async tasks
* introduce several warning logs for protocol breaches - these should be
removed eventually, pending q/a on the rlpx layer in general
* fix snappy compression - the payload without msgId should be
compressed
* remove strict checks on unused fields in RLPx message header (this
matches geth behavior and the spirit of EIP-8)
* add snappy dep
2024-11-08 03:44:04 +01:00
Chirag Parmar
034b7886de
clean up redundant code in eth/rlp/writer.nim (#755)
* cleanup macros

* add test cases and fix counting function

* add check statements

* remove deprecated support for Option

* replace some logic

* remove debug print
2024-11-06 11:46:22 +07:00
Jacek Sieka
1467b145ae
remove unusued rlpx features, tighten hello exchange and some error h… (#759)
* disconnect peers that send non-hello messages during initial hello
step
* fix devp2p protocol version - 4 because we don't implement snappy
(yet) - this is cosmetic since this particular version field is not
actually being used
* fix ack message length checking
* move RLPx transport code to separate module, annotate with asyncraises
* increase max RLPx message size to 16mb, per EIP-706
* make sure both accept/connect timeout after 10s
* aim to log every connection attempt once at debug level
* make capability-id/context-id check more accurate
* disallow random messages before hello
2024-11-05 16:30:41 +01:00
Jacek Sieka
ee845a1768
devp2p: drop pre-EIP8 support, fix snappy negotiation (#758)
Support for previous versions was
[removed](7194c847b6)
from geth in 2021 after other clients had migrated - should be safe to
remove here also.

* fix snappy detection - it should use the hello version, not the RLPx
handshake
* simplify generation of auth messages
* pad 100-300 bytes like spec suggests

TODO: error handling is all over the place - will be addressed in a
follow-up PR
2024-11-02 08:15:40 +01:00
Jacek Sieka
719c0dfd56
eth_hash: condition converter deprecation warning on nim version (#756)
The warning gives too many false positives - until things work properly,
locally one can just remove it to find instances where it gets used..

https://github.com/nim-lang/Nim/issues/24241
2024-10-30 09:10:10 +01:00
Jacek Sieka
5d78c6a879
discv4: prefer ipv6 (#751)
This allows bonding with ipv4-mapped nodes above all, which fixes a
bunch of warnings
2024-10-29 10:03:13 +01:00
Csaba Kiraly
30476de038
fix potential infinite loop in randomNodes (#754)
Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
2024-10-24 17:24:53 +02:00
andri lim
66297c5c0a
Remove obsolete EIP-6110-7002-7251 types, encoding, and checks (#752)
* Remove obsolete EIP-6110-7002-7251 types

* Keep blocks_rlp.nim
2024-10-22 14:00:06 +07:00
Etan Kissling
171531fbf1
Add helper for recovering authority (#750)
For EIP-7702 SetCode transaction, it is necessary to identify the
authority issuing an authorization. Its account's code is set to
proxy to the authorization address if successful.
2024-10-15 21:44:10 +07:00
andri lim
b736906dc7
Fix Authorization fields name to v, r, s (#749) 2024-10-15 14:09:16 +00:00
tersec
470baf82bd
update Sepolia and Holesky bootnodes (#747) 2024-10-09 15:23:58 +00:00
tersec
e59125acf7
rm Goerli bootnodes (#746)
* rm Goerli bootnodes

* rm tests mention
2024-10-09 10:34:47 +00:00
Miran
bb76daa6d9
add missing type, fix typo, meaningful deprecations (#745) 2024-10-08 20:21:16 +02:00
Jacek Sieka
00c91a1dca
Ordered trie for computing roots (#744)
Root encoding is on the hot path for block verification both in the
consensus (when syncing) and execution clients and oddly consititutes a
significant part of resource usage even though it is not that much work.

While the trie code is capable of producing a transaction root and
similar feats, it turns out that it is quite inefficient - even for
small work loads.

This PR brings in a helper for the specific use case of building tries
of lists of values whose key is the RLP-encoded index of the item.

As it happens, such keys follow a particular structure where items end
up "almost" sorted, with the exception for the item at index 0 which
gets encoded as `[0x80]`, ie the empty list, thus moving it to a new
location.

Armed with this knowledge and the understanding that inserting ordered
items into a trie easily can be done with a simple recursion, this PR
brings a ~100x improvement in CPU usage (360ms vs 33s) and a ~50x
reduction in memory usage (70mb vs >3gb!) for the simple test of
encoding 1000000 keys.

In part, the memory usage reduction is due to a trick where the hash of
the item is computed as the item is being added instead of storing it in
the value.

There are further reductions possible such as maintaining a hasher per
level instead of storing hash values as well as using a direct-to-hash
rlp encoder.
2024-10-08 20:02:58 +02:00
Kim De Mey
cea821df60
Add times_rlp and headers_rlp modules (#743) 2024-10-04 17:19:59 +02:00
Jacek Sieka
4ea11b9fb9
transaction signing helpers (#742)
Transaction signing is something that happens in a lot of places - this
PR introduces primitives for transaction signing in `transaction_utils`
such that we can use the same logic across web3/eth1/etc for this simple
operation.

`transaction_utils` also contains a few more "spec-derived" helpers for
working with transactions, such as the computation of a contract address
etc that cannot easily be introduced in `transactions` itself without
bringing in dependencies like secp and rlp, so they end up in a separate
module.

Finally, since these modules collect "versions" of these transaction
types across different eips, some tests are moved to follow the same
structure.
2024-10-04 13:46:58 +02:00
Jacek Sieka
84664b0fc0
discv5: migrate to minilru (#741)
As it happens, the two share the exact same interface (even the test
suite removed in this PR passes) - `minilru` has an edge on efficiency
however, avoiding the doubly linked list node allocations etc
2024-10-03 12:34:38 +02:00
Jacek Sieka
5ce3c4557f
prevRandao -> Bytes32 (#740)
This one is a bit of a mess because it has different names and types
across specs :/
2024-10-02 13:00:10 +02:00
Jacek Sieka
70b7519f02
avoid typed return in generic template (#739) 2024-10-02 09:33:22 +02:00
Jacek Sieka
086ac68c86
Change VersionedHash to Hash32 (#738)
This is closer to both the exeuction spec and api
2024-10-01 11:54:01 +02:00
Jacek Sieka
119c910a4e
remove remaining int holdouts in rlp (#737)
these were never well defined
2024-09-30 18:32:36 +02:00
Jacek Sieka
b49df0a71a
remove binary tries (#736)
Binary tries were an early research idea but are no longer part of any
plausible etheruem roadmap, hence we remove them.
2024-09-30 12:59:16 +02:00
Jacek Sieka
00f5fb1fe0
clean up json serialization (#735)
serialize fewer non-eth types and more eth types - this module is
fraught with issues however since there's no one good "canonical"
encoding to choose - this goes back to the json ser framework lacking
good isolation between projects.
2024-09-30 10:34:47 +02:00
Jacek Sieka
6bd6bae86c
Align core types with execution spec (#733)
Since these types were written, we've gained an executable spec:

https://github.com/ethereum/execution-specs

This PR aligns some of the types we use with this spec to simplify
comparisons and cross-referencing.

Using a `distinct` type is a tradeoff between nim ergonomics, type
safety and the ability to work around nim quirks and stdlib weaknesses.

In particular, it allows us to overload common functions such as `hash`
with correct and performant versions as well as maintain control over
string conversions etc at the cost of a little bit of ceremony when
instantiating them.

Apart from distinct byte types, `Hash32`, is introduced in lieu of the
existing `Hash256`, again aligning this commonly used type with the spec
which picks bytes rather than bits in the name.
2024-09-29 10:52:19 +02:00
Jacek Sieka
3d51887c15
move signal ignore out of eth/common (#734)
`net/nat` is the only place where it is used and it certainly doesn't
belong in `common`
2024-09-24 11:30:05 +02:00
Kim De Mey
aa1e738a97
Add discv5 constants to know allowed max talkresp message size (#732) 2024-09-19 17:53:11 +02:00
andri lim
4eecab27ef
Implement EIP-7702 Transaction and Receipt (#731)
* Implement EIP-7702 Transaction and Receipt
2024-09-13 13:37:43 +07:00
jangko
59715353db
Fix EIP-7865 types and RLP encoding 2024-09-10 20:31:07 +07:00
tersec
11eafac0f0
handle new UPnP return value (#728) 2024-09-10 05:10:08 +00:00
Kim De Mey
f2568a64c0
Fix potential uTP clock drift overflow (#726) 2024-09-03 16:48:24 +02:00
Jordan Hrycaj
92c7bea807
Reassign logging weights in RLPx handler (#725)
why:
  Too much noise, updating sort of guided by
  https://github.com/status-im/nimbus-eth2/blob/unstable/docs/logging.md
2024-08-30 18:00:11 +00:00
Jordan Hrycaj
5ecbcb5886
Using unsigned types for message type and requst IDs (#722)
* Using unsigned types for message type and requst IDs

why:
  Negative values are neither defined for RLP nor in the protocol specs
  which refer to the RLPs (see yellow paper app B clause (199).

* Fix `int` argument (must be `uint`) in fuzzing tests

why:
  Not part of all tests so it slipped through.
2024-08-30 17:27:09 +00:00
Kim De Mey
b874e12516
Differentiate log for invalid packet seqnr without reorder buffer and not (#723) 2024-08-29 18:15:26 +02:00
Jordan Hrycaj
98b2a34075
Fix rlpx hello crash caused by rlp decoder (#721)
* Restricting exception catcher

why:
  `CatchableError` is not needed here

* Check data length before converting to `openArray[]`

why:
  Getting the first entry of an `openArray[]` crashes with `IndexDefect`.
  This is particularly annoying when decoding messages in rlpx.

* Added unit test using rlpx message that causes this problem to detect
2024-08-19 11:38:46 +00:00
Etan Kissling
cc6d88962e
Treat putting empty data in hexary trie as deleting data (#720)
Other implementations of MPT delete entries when attempting to put empty
value, because empty value cannot exist in RLP. We should match the
behaviour.

- https://github.com/ethereum/py-trie/pull/109

Also cross-checked with Geth and Ethereumjs implementations.
2024-08-14 23:48:39 +00:00
Jordan Hrycaj
9511502273
Removed obsolete chunked rlpx message protocol extension (#719) 2024-08-13 15:10:47 +00:00
Jacek Sieka
56f72c7a66
Revert speedups (#718)
* Revert "hash compatibility fix (#717)"

This reverts commit 047de2b788b485cca83c6d77f9a2e5f1e56ab656.

* Revert "port eth2_digest speedups to eth_hash (#716)"

This reverts commit 864d54467e09c08d62b508149e9184001a813363.
2024-08-09 07:20:35 +02:00
Jacek Sieka
047de2b788
hash compatibility fix (#717) 2024-08-09 06:23:56 +02:00
Jacek Sieka
864d54467e
port eth2_digest speedups to eth_hash (#716)
* port eth2_digest speedups to eth_hash

* faster comparison
* `hash` integration
* lower-case printing
2024-08-08 05:44:15 +00:00
andri lim
ebfe63b9b6
Convert GasInt to uint64 (#713) 2024-07-06 20:24:14 +07:00