1

I'm trying to avoid using tight coupling for my pallets as it seems to be a bad idea for later enhancements. I'm also facing issues when trying to create a tests.rs file for, but that's another issue and probably related to my inexperience with rust.

The thing is, I have two pallets (pallet1 and pallet2). Pallet1 models an item and pallet2 is another 'object' but it is expected to be attached to the item on pallet1. So for example, when I create an item on pallet1, it creates a call on pallet2 that creates an Object with the Item id, using its trait like:

/// Pallet1 lib.rs
#[pallet::config]
pub trait Config: frame_system::Config + pallet_pallet1::Config {
    type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
    type Pallet2: Pallet2Provider<Self>;
}

#[pallet::weight(0)]
pub fn create_item(origin: OriginFor<T>, id: Vec<u8>) -> DispatchResult {
    // Do some stuff
    T::Pallet2::create_object(id);
    Ok(())
}

/// Pallet2 lib.rs
#[pallet::storage]
pub(super) type Objects<T: Config> =
    StorageMap<_, Twox64Concat, id, Object<T>>;

impl Pallet2Provider<T> for Pallet<T> {
   fn create_object(id: Vec<u8>) {
      <Objects<T>>::insert(id, Object::new(id));
   }
}

The code above is just an example, and it doesn't have to be correct, but it's just to illustrate the issue. There is another function within the Pallet2 that modifies the object, but at the same time modifies some value of the Pallet1 item, so I'm tight coupling Pallet1 to Pallet2 so thats why I'm adding the pallet_pallet1:Config dependency to the Pallet2 Config.

If both pallets have to interact with each other, they can be merged on a single pallet right? but it will be a large lib.rs and make it hard to read and maintain. Splitting this logic is what im trying to achieve but I don't know if I'm supposed to do tight or loose coupling.

Besides this issue, is there any real example where two pallets interact with each other and the first one modifies the second storage and viceversa in a loose way? I think using tight coupling is the easiest way to do so.

3
  • 1
    So for the code example , you can split the logic using the same pallet, just add function.rs file and put the function which modifies the storage. But In a case of 2 pallet interacting, The loosely coupling way is to define a trait and make sure the other pallet implement that trait and in your pallet1 you can use the functionality. So for example how pallet balances is being coupled. But in your example is like you are trying to loosely couple and tight couple at once May 27, 2022 at 11:29
  • thats the conclusion I reached, I can create a single pallet with multiple structures and add the functions.rs file to remove some code from the lib.rs, but should that be the approach? If i'm handling two items that are related should I keep them on a single pallet or in different pallets?
    – andresvsm
    May 27, 2022 at 14:04
  • 1
    I really think you should use only one pallet, checkout even frame_system handles alot of things May 28, 2022 at 20:39

1 Answer 1

3

In your minimal example, making a single pallet makes sense.

To be honest, it is not clear what question you are asking, or what problem you are running into.

Loose coupling makes sense when there are specific kinds of APIs which you expect to be switched out or configured.

For example, imagine some kind of VoteOutcome interface. There a bunch of different ways someone could design a voting system, but your pallet only cares about the outcome of the vote. In this case, you keep the underlying voting system as a separate pallet, and just expose the one API you care about.

This would then allow you to seperate something like Treasury where a proposal is suggested from the underlying Democracy system where users vote to accept or reject that proposal.

If two pallets are exactly depending on the underlying logic and behavior of the other, then tight coupling, or even just making one pallet makes sense, and will be easier to maintain.

As for when you would want to do tight coupling vs a single pallet, the only reason I would see to do tight coupling is either to minimize how large a single pallet gets to maintain, or to enable you to explicitly disable some one way tight coupled dependency.

In general, development is simplified when a single pallet is used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.