Determine the Input Stack Format in Miniscript: A Guide
Miniscript is a popular programming language used to build decentralized applications on the Polkadot network. When creating or compiling Miniscript scripts, one of the key aspects to understand is how the input stack format is determined.
In this article, we will delve into the concept of input stack formats in Miniscript and explore the implications of different calling conventions when building a policy.
What is an input stack format?
An input stack format refers to the structure used by the compiler to represent the arguments passed to a function call. In other words, it is how a function takes its inputs from the environment and passes them back as results.
In Miniscript, the input stack format is determined by the calling convention of the called function. The calling convention defines how the function receives its inputs and passes them.
Types of Calling Conventions
There are two main types of calling conventions in Miniscript:
- C-style: This is the most common calling convention, where each argument is passed as a separate value on the stack.
- Packed: In this convention, multiple arguments can be packed into a single stack slot.
Determine the input stack format
When compiling a policy, the compiler must perform its own validation to ensure that the input stack format matches the expected calling convention for each function call. The compiler uses this information to validate the function inputs and outputs.
However, there is no single “exact” or “universal” way to determine the input stack format. Instead, the compiler must consult a variety of sources, including:
- Function Documentation: Many Miniscript functions have a “documentation” field in their specification that can provide information about the expected input and output formats.
- Compiler Settings
: The command and compilation settings can also affect the format of the input stack.
Usage Example
Suppose we want to compile a policy that calls the miniscript-minical
function with two arguments:
function main() {
var arg1 = "Hello";
var arg2 = 42;
}
If we run the following command in our editor, we will see a summary of the input stack format:
> miniscript main
Miniscript Compiler: v0.11.6
Input Stack Format:
arg1 int
arg2 uint8
result int
From this output, we can determine that the minicall
function expects two arguments on the stack, where arg1
is an integer and arg2
is a 256-bit unsigned integer.
Conclusion
Determining the format of the input stack in Miniscript is a complex process involving a variety of factors, including function documentation, compiler settings, and user-supplied values. While there is no single “one-size-fits-all” way to determine the format of the input stack, understanding these sources of information can help you write more robust and efficient code.
As developers, we need to ensure that our policies follow the expected calling conventions for each function call to avoid potential input validation issues, data corruption, or other problems.