Determine the Input Stack Format in Miniscript: A Guide
Miniscript is a popular programming language used for building decentralized applications on the Polkadot network. When creating or compiling Miniscript scripts, one of the crucial aspects that need to be understood 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 compiling a policy.
What is an Input Stack Format?
The input stack format refers to the structure used by the compiler to represent the arguments passed to a function call. In other words, it’s how the function takes its inputs from the environment and passes them back out as results.
In Miniscript, the input stack format is determined by the calling convention of the function being called. The calling convention defines how the function takes its inputs and passes them out.
Types of Calling Conventions
There are two primary 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 slot on the stack.
Determine Input Stack Format
When compiling a policy, the compiler must perform its own validity check to ensure that the input stack format matches the expected calling convention for each function call. The compiler uses this information to validate the inputs and outputs of the functions.
However, there is no single “exact” or “universal” way to determine the input stack format. Instead, the compiler must consult various sources, including:
- Function documentation: Many Miniscript functions have a
documentation
field in their specification, which can provide information about the expected input and output formats.
- Compiler settings: The compilation command and settings can also influence the input stack format.
Example Use Case
Suppose we want to compile a policy that calls the miniscript-minicall
function with two arguments:
function main() {
var arg1 = "Hello";
var arg2 = 42;
}
If we run the following command in our editor, we’ll see a summary of the input stack format:
> minicall main
Miniscript compiler: v0.11.6
Input stack format:
arg1 you
arg2 uint8
result you
From this output, we can determine that the minicall
function expects two arguments on the stack, with arg1
being an integer and arg2
being a 256-bit unsigned integer.
Conclusion
Determining the input stack format in Miniscript is a complex process involving various factors, including function documentation, compiler settings, and user-input values. While there isn’t a single “universal” way to determine the input stack format, understanding these sources of information can help you write more robust and efficient code.
As developers, we must ensure that our policies adhere to the expected calling conventions for each function call to avoid potential issues with input validation, data corruption, or other problems.