Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BiBounded{Vec,BTreeMap} #250

Open
benluelo opened this issue Sep 13, 2022 · 7 comments
Open

BiBounded{Vec,BTreeMap} #250

benluelo opened this issue Sep 13, 2022 · 7 comments
Labels
T1-FRAME This PR/Issue is related to core FRAME, the framework.

Comments

@benluelo
Copy link
Contributor

benluelo commented Sep 13, 2022

The current Bounded* types are incredibly useful, but sometimes it is necessary to have a minimum amount of items as well as a max. We currently maintain our own BiBoundedVec in-house, but I think that other people would find this useful as well.

Two ideas:

  • Two separate types, with the BiBounded* type a wrapper over the Bounded* type to reuse immutable methods:

    struct BiBoundedBTreeMap<K, V, Min: Get<u32>, Max: Get<u32>>(BoundedBTreeMap<K, V, Max>, PhantomData<fn() -> Min>>;
  • Only one type BiBounded* with a type alias filling in the minimum with 0:

    struct BiBoundedBTreeMap<K, V, Min: Get<u32>, Max: Get<u32>>(BTreeMap<K, V>, PhantomData<fn() -> (Min, Max)>>;
    
    type BoundedBTreeMap<K, V, Max> = BiBoundedBTreeMap<K, V, ConstU32<0>, Max>;

    An issue with this option is that infallible methods on the Bounded* type (like remove) would have to be fallible (if only specialization was stable 😢)

(I'm leaning towards the former personally)

Again, happy to take this on if it's accepted!

@shawntabrizi
Copy link
Member

shawntabrizi commented Sep 13, 2022

Maybe we should just use a third party bounded vec crate which already supports upper and lower bound?

https://docs.rs/bounded-vec/latest/bounded_vec/

In any case, I think we can support upper and lower bound, where the lower bound generic is default to 0, and then it would be completely backwards compatible with the existing bounded vec structure.

I dont like introducing a new type here.

@benluelo
Copy link
Contributor Author

benluelo commented Sep 13, 2022

I was going to suggest a third-party crate, but they all seem to use const generics (logically). There is #290, but it hasn't been touched since January; I think it could use some love.

There also doesn't seem to be a good bounded map crate (from what I could find), so I think keeping the current bounded structures would be good.

In any case, I think we can support upper and lower bound, where the lower bound generic is default to 0, and then it would be completely backwards compatible with the existing bounded vec structure.

I considered this, but it has the same issue as the second option I suggested - infallible methods on a Bounded<0, Max> (currently just Bounded<Max>) structure that remove elements (pop, remove, drain, retain, clear, etc) will have to be made fallible, so it isn't entirely backwards compatible to just add the lower bound to the existing types.

(Also, generic parameters with a default must be trailing, so the types would have to be defined as Bounded<Max, Min = 0> which would be somewhat counterintuitive in my opinion)

@kianenigma
Copy link
Contributor

I think unfortunately we have to re-implement new types. Reusing the same type for lower bound will be an issue, since operations that reduce the size are currently infallible.

What would be the main benefit of bringing the types here to substrate? I am not against it, but it can also be an independent crate, kinda like https://github.com/encointer/substrate-fixed/blob/master/RELEASES.md, which extends our "basic" arithmetic types, and is still usable by the ecosystem.

That being said, I am not against adding them to substrate either.

@benluelo
Copy link
Contributor Author

I think extracting this out into a separate crate would be a good move, could be done in tandem with #290 too. Would this be something kept under the paritytech organization?

@kianenigma
Copy link
Contributor

not necessarily, it can be under any org.

@shawntabrizi
Copy link
Member

@benluelo thanks for the thoughtfulness you put into this.

I think with your comments in mind, I would be open to creating a new type, and adding it to Substrate.

If we plan to migrate it somewhere else in the future or whatever else, we can deal with that, but if you are interested to make the crate, sounds good to me.

Could we then also make BoundedVec a BiBoundedVec<Max, Min = 0> and handle all the infallible cases?

@benluelo
Copy link
Contributor Author

Awesome! I don't think I can commit to maintaining this as a separate crate right now, but if it were to be made somewhere else I would absolutely contribute to it.

As for right now, Bounded*<Max> collections would have to be a wrapper around their BiBounded*<Max, Min = 0> counterparts, and just delegate the methods that are fallible for both to the wrapped type (this boilerplate could of course be reduced with a simple macro_rules! if it becomes too much).

Would we want to do #290 at the same time as well?

@juangirini juangirini transferred this issue from paritytech/substrate Aug 24, 2023
@the-right-joyce the-right-joyce added T1-FRAME This PR/Issue is related to core FRAME, the framework. and removed T1-runtime labels Aug 25, 2023
github-merge-queue bot pushed a commit that referenced this issue Nov 5, 2024
This PR updates litep2p to the latest release.

- `KademliaEvent::PutRecordSucess` is renamed to fix word typo
- `KademliaEvent::GetProvidersSuccess` and
`KademliaEvent::IncomingProvider` are needed for bootnodes on DHT work
and will be utilized later


### Added

- kad: Providers part 8: unit, e2e, and `libp2p` conformance tests
([#258](paritytech/litep2p#258))
- kad: Providers part 7: better types and public API, public addresses &
known providers ([#246](paritytech/litep2p#246))
- kad: Providers part 6: stop providing
([#245](paritytech/litep2p#245))
- kad: Providers part 5: `GET_PROVIDERS` query
([#236](paritytech/litep2p#236))
- kad: Providers part 4: refresh local providers
([#235](paritytech/litep2p#235))
- kad: Providers part 3: publish provider records (start providing)
([#234](paritytech/litep2p#234))

### Changed

- transport_service: Improve connection stability by downgrading
connections on substream inactivity
([#260](paritytech/litep2p#260))
- transport: Abort canceled dial attempts for TCP, WebSocket and Quic
([#255](paritytech/litep2p#255))
- kad/executor: Add timeout for writting frames
([#277](paritytech/litep2p#277))
- kad: Avoid cloning the `KademliaMessage` and use reference for
`RoutingTable::closest`
([#233](paritytech/litep2p#233))
- peer_state: Robust state machine transitions
([#251](paritytech/litep2p#251))
- address_store: Improve address tracking and add eviction algorithm
([#250](paritytech/litep2p#250))
- kad: Remove unused serde cfg
([#262](paritytech/litep2p#262))
- req-resp: Refactor to move functionality to dedicated methods
([#244](paritytech/litep2p#244))
- transport_service: Improve logs and move code from tokio::select macro
([#254](paritytech/litep2p#254))

### Fixed

- tcp/websocket/quic: Fix cancel memory leak
([#272](paritytech/litep2p#272))
- transport: Fix pending dials memory leak
([#271](paritytech/litep2p#271))
- ping: Fix memory leak of unremoved `pending_opens`
([#274](paritytech/litep2p#274))
- identify: Fix memory leak of unused `pending_opens`
([#273](paritytech/litep2p#273))
- kad: Fix not retrieving local records
([#221](paritytech/litep2p#221))

See release changelog for more details:
https://github.com/paritytech/litep2p/releases/tag/v0.8.0

cc @paritytech/networking

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
Status: Backlog
Development

No branches or pull requests

5 participants