首先 1 HPB = 1,000,000,000,000,000,000 Wei 就是 1^18, 1 Gwei = 1,000,000,000 Wei。

交易费用 = Gas * Gas Price。

1、HPB 计量单位转换关系

单位WeiKweibabbagefemtohpbmweilovelacepicohpb
Wei1103103103106106106
单位gweishannonnanohpbnanoszabomicrohpbmicro
Wei109109109109101210121012
单位finneymillihpbmillihpbkhpbgrandmhpb
Wei1015101510151018102110211024
单位ghpbthpbphpbehpbzhpbyhpbnhpb
Wei1027103010331036103910421045
单位dhpbvhpbuhpb
Wei104810511054

2、Gas 如何收取

2.1 无智能合约的普通交易

在 HPB 主网里,交易费用在涉及普通交易和智能合约交易时并不相同,通常来说 HPB 普通交易的默认 Gas Units = 10. Gas price 根据 HPB 所有节点的竞争而变化,在此版本的源代码里默认是大于 1Wei,现实环境中,经验值为 Gas Price = 18Gwei.

2.2 智能合约创建交易

(1)基本费用:默认 Gas Units = 53000。

(2)数据费用:交易的 data 字段,每个字节 1 个。

(3)合约预编译费用:根据预编译函数返回结果,预编译函数评估时,不同的指令集,收取 Gas 的方法不同。简单指令(如 Add,MUL,AND,OR 等)收取固定 Gas,复杂指令(如内存操作 MLOAD,SLOAD)根据使用的内存进行收取。

(4)智能合约运行交易:

基本费用:默认 Gas Units = 10。

数据费用:交易的 data 字段,每个字节 1 个。

(5)合约预编译费用:根据所调用智能合约类型的不同,Gas 计算方法不同,请参照 HPB 源代码,如下所示:

[code]
// Precompiled contract gas prices EcrecoverGas uint64 = 3000 // Elliptic curve sender recovery gas price Sha256BaseGas uint64 = 60 // Base price for a SHA256 operation Sha256PerWordGas uint64 = 12 // Per-word price for a SHA256 operation Ripemd160BaseGas uint64 = 600 // Base price for a RIPEMD160 operation Ripemd160PerWordGas uint64 = 120 // Per-word price for a RIPEMD160 operation IdentityBaseGas uint64 = 15 // Base price for a data copy operation IdentityPerWordGas uint64 = 3 // Per-work price for a data copy operation ModExpQuadCoeffDiv uint64 = 20 // Divisor for the quadratic particle of the big int modular exponentiation Bn256AddGas uint64 = 500 // Gas needed for an elliptic curve addition Bn256ScalarMulGas uint64 = 40000 // Gas needed for an elliptic curve scalar multiplication Bn256PairingBaseGas uint64 = 100000 // Base price for an elliptic curve pairing check Bn256PairingPerPointGas uint64 = 80000 // Per-point price for an elliptic curve pairing check

const (         MaximumExtraDataSize  uint64 = 32    // Maximum size extra data may be after Genesis.        ExpByteGas            uint64 = 10    // Times ceil(log256(exponent)) for the EXP instruction.        SloadGas              uint64 = 50    // Multiplied by the number of 32-byte words that are copied (round up) for any *COPY operation and added.        CallValueTransferGas  uint64 = 9000  // Paid for CALL when the value transfer is non-zero.        CallNewAccountGas     uint64 = 25000 // Paid for CALL when the destination address didn't exist prior.        TxGas                 uint64 = 10    // Per transaction not creating a contract. NOTE: Not payable on data of calls between transactions. //for testnet        TxGasContractCreation uint64 = 53000 // Per transaction that creates a contract. NOTE: Not payable on data of calls between transactions.        TxDataZeroGas         uint64 = 1     // Per byte of data attached to a transaction that equals zero. NOTE: Not payable on data of calls between transactions. //for testnet        QuadCoeffDiv          uint64 = 512   // Divisor for the quadratic particle of the memory cost equation.        SstoreSetGas          uint64 = 20000 // Once per SLOAD operation.        LogDataGas            uint64 = 8     // Per byte in a LOG* operation's data.        CallStipend           uint64 = 2300  // Free gas given at beginning of call.                Sha3Gas          uint64 = 30    // Once per SHA3 operation.        Sha3WordGas      uint64 = 6     // Once per word of the SHA3 operation's data.        SstoreResetGas   uint64 = 5000  // Once per SSTORE operation if the zeroness changes from zero.        SstoreClearGas   uint64 = 5000  // Once per SSTORE operation if the zeroness doesn't change.        SstoreRefundGas  uint64 = 15000 // Once per SSTORE operation if the zeroness changes to zero.        JumpdestGas      uint64 = 1     // Refunded gas, once per SSTORE operation if the zeroness changes to zero.        EpochDuration    uint64 = 30000 // Duration between proof-of-work epochs.         CallGas          uint64 = 40    // Once per CALL operation & message call transaction.        CreateDataGas    uint64 = 200   //         CallCreateDepth  uint64 = 1024  // Maximum depth of call/create stack.        ExpGas           uint64 = 10    // Once per EXP instruction        LogGas           uint64 = 375   // Per LOG* operation.        CopyGas          uint64 = 3     //         StackLimit       uint64 = 1024  // Maximum size of VM stack allowed.        TierStepGas      uint64 = 0     // Once per operation, for a selection of them.        LogTopicGas      uint64 = 375   // Multiplied by the * of the LOG*, per LOG transaction. e.g. LOG0 incurs 0 * c_txLogTopicGas, LOG4 incurs 4 * c_txLogTopicGas.        CreateGas        uint64 = 32000 // Once per CREATE operation & contract-creation transaction.        SuicideRefundGas uint64 = 24000 // Refunded following a suicide operation.        MemoryGas        uint64 = 3     // Times the address of the (highest referenced byte in memory + 1). NOTE: referencing happens on read, write and in instructions such as RETURN and CALL.        TxDataNonZeroGas uint64 = 1     // Per byte of data attached to a transaction that is not equal to zero. NOTE: Not payable on data of calls between transactions. //for testnet                MaxCodeSize = 24576 // Maximum bytecode to permit for a contract

[/code]

(6) Gas Price 说明

在我们常见的 ETH 钱包中 Gas Price 在不同阶段是会有调整,通常来说都是钱包根据网络情况为用户设置,HPB Wallet 也 默认帮助用户设置 Gas Price,此版本源代码默认是只要大于 1Wei 即可以,现阶段的经验值为 18Gwei,同样 HPB 智能合约的 Gas Price 也遵循同样的机制,现阶段运行智能合约的 Gas Price 为 18Gwei。

(7) FAQ

Q1: Gas 如何计算?
A1: 请参照上表中的计算方式。

Q2: 一个交易最少消耗多少交易费用?
A2: 首先要确定属于哪一种交易,普通交易还是智能合约交易,然后根据上表中 Gas 的规定去计算,交易费用 =Gas * Gas Price,例如普通交易的 Gas 为固定值 10 ,没有最大和最小之分,智能合约部分也请参照上面的表去计算,每一种交易都有一个最低的 Gas 要求,另外交易费用是手动可设置的,例如用户想交易更快,可以调节参数 Gas Price 的值。

Q3: 普通交易最多消耗多少 Gas?
A3: 普通交易的 Gas 是固定值 10 。

Q4: 智能合约交易最多消耗多少 Gas?
A4: 智能合约的 Gas 没有上限 。

Q5: 搭建 Dapp 需要购买 HPB token 才能搭建到 HPB 主网上吗
A4: Yes。

Q6: Dapp 搭建是否需要锁定 HPB Token?
A6: 目前的版本不需要锁定,只是在部署过程中和运行中需要一定的交易费用。

Q7:Dapp 是否可以搭建在测试平台?
A7: 可以,测试链平台有智能合约功能,现在最新的测试链还没有完全释放出来给社区用,待准备好之后再提供给社区的爱好者使用,同时提供测试链的 token。

Q8: 交易费用如何分配?
A8: 交易费用会奖励给维护 HPB 主网的块节点。

本文首发于汪晓明博
http://wangxiaoming.com/

HPB 芯链官网
http://www.hpb.io/

汪晓明

HPB 芯链创始人,巴比特专栏作家。十余年金融大数据、区块链技术开发经验,曾参与创建银联大数据。主创区块链教学视频节目《明说》30 多期,编写了《以太坊官网文档中文版》,并作为主要作者编写了《区块链开发指南》,在中国区块链社区以 ID“蓝莲花”知名。

来源链接:mp.weixin.qq.com