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 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 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 is how the 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 takes its inputs and distributes 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 slot on the stack.
Determine the format of the input stack
When compiling a policy, the compiler must perform its own validity checking to ensure that the format of the input stack matches the expected calling convention for each function call. The compiler uses this information to validate function inputs and outputs.
However, there is no single “exact” or “universal” way to determine the format of the input stack. Instead, the compiler must consult various sources, including:
- Function documentation: Many Miniscript functions have a
documentationfield in their specifications, which can provide information about the expected input and output formats.
- Compiler settings: The command and compilation settings can also influence the format of the input stack.
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 will see a summary of the input stack format:
> minicall 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, 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 that involves various factors, including the function documentation, compiler settings, and user input. While there is no 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 need to ensure that our policies follow the expected calling conventions for each function call, to avoid potential issues with input validation, data corruption, or other problems.