title |
---|
-chap-num- Extrinsics |
import Pseudocode from '@site/src/components/Pseudocode'; import encodeMortality from '!!raw-loader!@site/src/algorithms/encodeMortality.tex'; import decodeMortality from '!!raw-loader!@site/src/algorithms/decodeMortality.tex';
An extrinsic is a SCALE encoded array consisting of a version number, signature, and varying data types indicating the resulting Runtime function to be called, including the parameters required for that function to be executed.
:::definition
An extrinsic ,
$$ {t}{x}:={\left({T}{{v}},{T}{{b}}\right)} $$
The value of
:::
:::definition
${T}{{v}}$ is a 8-bit bitfield and defines the extrinsic version. The required format of an extrinsic body, ${T}{{b}}$, is dictated by the Runtime. Older or unsupported versions are rejected.
The most significant bit of ${T}{{v}}$ indicates whether the transaction is signed (${1}$) or unsigned (${0}$). The remaining 7-bits represent the version number. As an example, for extrinsic format version 4, a signed extrinsic represents ${T}{{v}}$ as 132
while an unsigned extrinsic represents it as 4
.
:::
Version 4 of the Polkadot extrinsic format is defined as follows:
$$ {T}{{b}}:={\left({A}{{i}},{Sig},{E},{M}{{i}},{F}{{i}}{\left({m}\right)}\right)} $$
where
-
${A}_{{i}}$ is the multi-address of the sender defined in Definition -def-num-ref-. -
${Sig}$ : the signature of the sender (Definition -def-num-ref-). -
${E}$ : the extra data for the extrinsic (Definition -def-num-ref-). -
${M}_{{i}}$ : the indicator of the Polkadot module (Definition -def-num-ref-). -
${F}_{{i}}{\left({m}\right)}$ : the indicator of the function of the Polkadot module (Definition -def-num-ref-).
:::definition
Account Id,
:::
:::definition
The signature,
Signature types vary in size, but each individual type is always fixed-size and therefore does not contain a length prefix. Ed25519
and Sr25519
signatures are 512-bit while Ecdsa
is 520-bit, where the last 8 bits are the recovery ID.
The signature is created by signing payload
where
-
${M}_{{i}}$ : the module indicator (Definition -def-num-ref-). -
${F}_{{i}}{\left({m}\right)}$ : the function indicator of the module (Definition -def-num-ref-). -
${E}$ : the extra data (Definition -def-num-ref-). -
${R}_{{v}}$ : a UINT32 containing the specification version (spec_version
) of the Runtime (Section -sec-num-ref-), which can be updated and is therefore subject to change. -
${F}_{{v}}$ : a UINT32 containing the transaction version (transaction_version
) of the Runtime (Section -sec-num-ref-), which can be updated and is therefore subject to change. -
${H}_{{h}}{\left({G}\right)}$ : a 32-byte array containing the genesis hash. -
${H}_{{h}}{\left({B}\right)}$ : a 32-byte array containing the hash of the block which starts the mortality period, as described in Definition -def-num-ref-.
:::
:::definition
Extra data,
$$ {E}:={\left({T}{mor},{N},{P}{{t}}\right)} $$
where
-
${T}_{mor}$ : contains the SCALE encoded mortality of the extrinsic (Definition -def-num-ref-). -
${N}$ : a compact integer containing the nonce of the sender. The nonce must be incremented by one for each extrinsic created, otherwise, the Polkadot network will reject the extrinsic. -
${P}_{{t}}$ : a compact integer containing the transactor pay including tip.
:::
:::definition
:::
:::definition
${F}{{i}}{\left({m}\right)}$ is a tuple which contains an indicator, ${m}{{i}}$, for the Runtime to which function within the Polkadot module,
$$ {F}{{i}}{\left({m}\right)}:={\left({m}{{i}},{p}{a}{r}{a}{m}{s}\right)} $$
The value of Balances
module has the following functions:
:::
:::definition
Extrinsic mortality is a mechanism which ensures that an extrinsic is only valid within a certain period of the ongoing Polkadot lifetime. Extrinsics can also be immortal, as clarified in Section -sec-num-ref-.
The mortality mechanism works with two related values:
-
${M}{{{per}}}$: the period of validity in terms of block numbers from the block hash specified as ${H}{{h}}{\left({B}\right)}$ in the payload (Definition -def-num-ref-). The requirement is ${M}{{{per}}}\geq{4}$ and ${M}{{{per}}}$ must be the power of two, such as
32
,64
,128
, etc. -
${M}{{{pha}}}$: the phase in the period that this extrinsic’s lifetime begins. This value is calculated with a formula, and validators can use this value in order to determine which block hash is included in the payload. The requirement is ${M}{{{pha}}}<{M}_{{{per}}}$.
In order to tie a transaction’s lifetime to a certain block (${H}{{i}}{\left({B}\right)}$) after it was issued, without wasting precious space for block hashes, block numbers are divided into regular periods and the lifetime is instead expressed as a "phase" (${M}{{{pha}}}$) from these regular boundaries:
$$ {M}{pha}={H}{{i}}{\left({B}\right)} ; mod ; {M}_{{{per}}} $$
${M}{{{per}}}$ and ${M}{{{pha}}}$ are then included in the extrinsic, as clarified in Definition -def-num-ref-, in the SCALE encoded form of ${T}{mor}$ (Section -sec-num-ref-). Polkadot validators can use ${M}{{{pha}}}$ to figure out the block hash included in the payload, which will therefore result in a valid signature if the extrinsic is within the specified period or an invalid signature if the extrinsic "died".
:::
The extrinsic author choses ${M}{{{per}}}={256}$ at block 10'000
, resulting with ${M}{{{pha}}}={16}$. The extrinsic is then valid for blocks ranging from 10'000
to 10'256
.
${T}{mor}$ refers to the SCALE encoded form of type ${M}{{{per}}}$ and ${M}{{{pha}}}$. ${T}{mor}$ is the size of two bytes if the extrinsic is considered mortal, or simply one bytes with a value equal to zero if the extrinsic is considered immortal.
$$ {T}{mor}:={E}{n}{c}{{{S}{C}}}{\left({M}{{{per}}},{M}{{{pha}}}\right)} $$
The SCALE encoded representation of mortality
If the extrinsic is immortal, specify a single byte with a value equal to zero.
:::algorithm
<Pseudocode content={encodeMortality} algID="encodeMortality" options={{ "lineNumber": true }} />
<Pseudocode content={decodeMortality} algID="decodeMortality" options={{ "lineNumber": true }} />
where
-
${T}^{{{b}{0}}}{\left\lbrace{mor}\right\rbrace}$: the first byte of ${T}{mor}$.
-
${T}^{{{b}{1}}}{\left\lbrace{mor}\right\rbrace}$: the second byte of ${T}{mor}$.
-
Limit(
${num}$ ,${min}$ ,${max}$ ): Ensures that${num}$ is between${min}$ and${max}$ . If${min}$ or${max}$ is defined as$\phi$ , then there is no requirement for the specified minimum/maximum. -
TZ(
${num}$ ): returns the number of trailing zeros in the binary representation of${num}$ . For example, the binary representation of40
is0010 1000
, which has three trailing zeros. -
$\gg$ : performs a binary right shift operation. -
$\ll$ : performs a binary left shift operation. -
${\mid}$ : performs a bitwise OR operation. :::