Open lumtis opened 2 years ago
This is an excellent idea!
We should start writing helpers in gno.land/p/something
(not rules
, can be too many different things) and consider making a top-level package only after encountering limitations with this method.
BTW, I prefer the procedural over declarative.
My 2cts:
for listing a set of conditions and makes building a code parser easier
-> I disagree. It's straightforward to parse in both cases.If you're concerned about readability, we can use idiomatic go like this:
func Foo() {
{
rules.AssertPayable()
rules.AssertReadOnly()
rules.AssertPayableWith("...")
// ...
}
// ...
}
But I think keeping them at the top of functions with an empty line just after will be more than enough.
The good news is that we can manage to support both at the same time, but I'll prefer the procedural method for the official packages' implementation.
Sounds good to have a package in p
for this. I was wondering if it would possible to implement ReadOnly
through this method since it would be something quite useful.
For procedural vs declarative, I guess it's personal preference, using brackets is actually already visual.
I think a more go-ish approach would be to use comments to give more context to the clients, generate documentation, and so on.
It's the current way Go extends a source code for things that are for tools and humans.
Having a library for rules still makes sense for real assertions, i.e., I would not prevent people from making TXs for read-only stuff, a TX is more decentralized and uses the consensus + it will be IBC friendly. So, for the example of read-only, I would add comments to suggest people call it using RPC instead of TX but not prevent it.
Also, mixing actual code and magic is more obscure than having explicit code for code and explicit comments for everything else non-code.
I suggest that anyone starts a rules package that will make sense. And consider parsing comments instead of code for everything related to annotations/suggestions.
I'm opening this issue assuming this is not a planned feature. Maybe you already discussed privately about this.
Problem
With smart contracts, functions may have general conditions:
Proposed solution
I may see the method
std.AssertOriginCall()
that already exists and is apparent to an "external" condition. Therefore, a solution could be to extend the assert available methods from the standard package. Or defining a new package for this purpose. For example, a standard package named "rules"But a procedural style to define these rules might be less adapted than a declarative style. Declarative style is more visual for listing a set of conditions and makes building a code parser easier, for a client for example. So an even better solution IMO can be to define a method taking generic rules. For example, an
Apply
methodConditions can be defined as following
This example simplifies implementing a generic client that parse the AST of the uploaded smart contract and shows in the UI the methods differently, prevent calling a ReadOnly method in a tx for example.