Show / Hide Table of Contents

Transaction

Transaction is the basic operation model of the whole NEO network. Wallets, smart contracts and accounts interact with NEO network through transactions. In NEO's P2P network, information is packed as InvPayload for transferring (Inv is abbreviation of Inventory). Different payloads have their special data, thus three types of inventory types are created. InventoryType = 0x01 means transaction data is packed into InvPayload . Besides, there are block data package ( InventoryType = 0x02 ) and consensus data package ( InventoryType = 0x03 ).

Structure

The basic data structure of a transaction is as following:

SizeFieldTypeDescription
1TypebyteType of transaction
1VersionbyteTrading version, currently 0
?--Data specific to transaction types
?*?Attributestx_attr[]Additional features that the transaction has
34*?Inputstx_in[]Input
60 * ?Outputstx_out[]Output
?*?ScriptsWitness[]List of scripts used to validate the transaction

Input

Input specifies the source of assets. There can be zero or multiple Inputs in each transaction. The Input of MinerTransaction is empty. The data structure of Input is shown as follows:

SizeFieldTypeDescription
32PrevHashUInt256Previous transaction's hash
2PrevIndexushortPrevious transaction's indexPrevious transaction's index

The combination of PrevHash and PrevIndex are called a coin reference. It reference to a previous Unspent Transaction Output(UTXO). For more details, read "UTXO" section.

Output

Each transaction can have up to 65536 outputs. An output represents the transfer result of any asset. The data structure of output is shown as follows:

SizeFieldTypeDescription
32AssetIdUIntBaseAsset id
?ValueBigDecimalValue
20ScriptHashUInt160Address of remittee

Attribute

SizeFieldTypeDescription
1UsagebyteUsage
0|1lengthuint8Length of data (It will be omitted in specific circumstances)
?Databyte[length]External data

TransactionAttributeUsage, each transaction attribute has different usages:

FieldValueDescription
ContractHash0x00Hash value of contract
ECDH020x02Public key for ECDH key exchange
ECDH030x03Public key for ECDH key exchange
Script0x20Additional validation of transactions
Vote0x30For voting
DescriptionUrl0x81Url address of description
Description0x90Brief description
Hash1 - Hash150xa1-0xafUsed to store custom hash values
Remark-Remark150xf0-0xffRemarks

For ContractHash, ECDH series, Hash series, data length is fixed to 32 bytes and length field is omitted;
For Script, data length is fixed to 20 bytes and length field is omitted;
For DescriptionUrl, the data length must be clearly defined, and the length should not exceed 255;
For Description, Remark series, the data length must be clearly defined, and the length should not exceed 65535.

Witness

Before each transaction is added into a block, it needs to be digitally signed to ensure that it will not be modified during transmission. NEO uses ECDSA digital signature method. The script hash of the transaction output is the public key used for ECDSA signature. NEO does not use SegWit in Bitcoin. Each transaction contains its own Script.Witness , while the Script.Witness is actually a smart contract.

Witness is an executable verification script. The InvocationScript provides the parameters for the VerificationScript to execute. Verification succeeds only when the script execution returns true.

SizeFieldTypeDescription
?InvocationScriptbyte[]Invocation script
?VerificationScriptbyte[]Verification script

Invocation script performs stack operation instructions, provides parameters for verification script (eg, signatures). The script interpreter executes the invocation script code first, and then the verification script code.

Block.NextConsensus represents the script hash of multi-signature contract ( Opt.CHECKMULTISIG ), which needs the signatures of the consensus nodes, shown in the following figure. When executed in NVM internally, it completes the verification of signatures and public keys.

nextconsensus_witness

Transaction Type

In NEO, there are 9 types of transaction.

TypeValueSystem Fee (GAS)Description
MinerTransaction0x000The first transaction of a block, used to allocate byte fees
RegisterTransaction0x4010000/0(Deprecated) Registers assets
IssueTransaction0x01500/0Issues assets
ClaimTransaction0x020Claims GAS
StateTransaction0x901000/0Enrolls as a validator candidate or votes for consensus nodes
EnrollmentTransaction0x201000(Deprecated) Enroll as validator candidate
ContractTransaction0x800The most commonly used transaction
PublishTransaction0xd0500*n(Deprecated) Publishes smart contracts
InvocationTransaction0xd1Fees vary with execution instructions.Invokes smart contracts

Transaction system fee: Different transactions have different system fees. The detail is defined in the configuration file protocol.json . Collected system fees are distributed to NEO holders, how this is done will be explained later in the guide.

Transaction network fee: NetworkFee = tx.inputs.GAS - tx.outputs.GAS - tx.SystemFee . In consensus activity, the network fee will be the reward for the Speaker, who packages transactions into a block. The network fee is stored in the first transaction ( MinerTransaction ) of the block. The higher the network fee is, the easier the transaction will be packaged into the new created block.

How to use transaction

The following example of Genesis Block generation shows the usage of a transaction.

Eg-1. Create Genesis Block

Genesis Block is the first block in the blockchain by default in hard code, and the block index is 0. NEO and GAS assets are registered in Genesis Block, and NEO asset are distributed. Note that global asset such as NEO and GAS are registered with RegisterTransaction , NEP5 tokens are generated through InvocationTransaction .

The block head of Genesis block is as follows:

SizeFieldTypeValue
4VersionuintVersion is 0
32PrevHashUInt256 0x0000000000000000000000000000000000000000000000000000000000000000
32MerkleRootUInt256 0x803ff4abe3ea6533bcc0be574efa02f83ae8fdc651c879056b0d9be336c01bf4
4Timestampuint 1531667301
4Indexuint 0
8ConsensusDataulong 2083236893 , the nonce value of the Bitcoin Genesis Block, as a respect to Bitcoin
20NextConsensusUInt160The script hash of consensus nodes' multi-signature contract in the next round.
1--uint8It's fixed as 1
?WitnessWitness 0x51 , respresenting OptCode.PUSHT , always return TRUE.
?*? Transactions Transaction[]It stored four txs as following.

The first transaction of each block must be MinerTransaction , which is used for distribution of transaction's network fees in the block.

SizeFieldTypeValue
1Typeuint8 0x00
1Versionuint8 0
8Nonceulong 2083236893
?*?Attributestx_attr[]Empty
34*?Inputstx_in[]Empty
60*?Outputstx_out[]Empty
?*?ScriptsWitness[]Empty

The second transaction is a RegisterTransaction , which registers NEO asset.

SizeFieldTypeValue
1Typebyte 0x40
1Versionbyte 0
1AssetTypebyte 0x00
?NamestringThe name of NEO
8AmountFix8 100000000
1Precisionbyte 0
?OwnerECPoint
32AdminUInt160 0x51 .toScriptHash
?*?Attributestx_attr[]Empty
34*?Inputstx_in[]Empty
60*?Outputstx_out[]Empty
?*?ScriptsWitness[]Empty

The name of NEO = [{"lang":"zh-CN","name":"小蚁股"},{"lang":"en","name":"AntShare"}]

The third transaction is another RegisterTransaction , which registers GAS asset.

SizeFieldTypeValue
1Typebyte 0x40
1Versionbyte 0
1AssetTypebyte 0x01
?NamestringThe name of GAS
8AmountFix8 100000000
1Precisionbyte 8
?OwnerECPoint
32AdminUInt160 0x00 .toScriptHash, representing OpCode.PUSHF script
?*?Attributestx_attr[]Empty
34*?Inputstx_in[]Empty
60*?Outputstx_out[]Empty
?*?ScriptsWitness[]Empty

The name of GAS = [{"lang":"zh-CN","name":"小蚁币"},{"lang":"en","name":"AntCoin"}]

The fourth transaction is an IssueTransaction , which issues NEO to contract address.

SizeFieldTypeValue
1Typebyte 0x01
1Versionbyte 0
?*?Attributestx_attr[]Empty
34*?Inputstx_in[]Empty
60*?Outputstx_out[]has one output, see the below table
?*?ScriptsWitness[] 0x51 , representing OpCode.PUSHT

The output defines the transfer of all NEO tokens to the multi-signature contract address of the standby validators(consensus nodes). The scripts are empty, meaning that the transactions do not need to be validated because it is a Genesis block transaction.

SizeFieldTypeValue
1AssetIdbyte 0x00 , representing NEO token.
8ValueFix8 100000000
20ScriptHashUInt160The script hash of standby consensus nodes' multi-signature contract.