HPB Gas 机制

优质
小牛编辑
125浏览
2023-12-01

导读:本文档基于hpb-1.0.2.3 版本,由于在涉及到转账交易和智能合约运算时,都会涉及到Gas费用问题,这篇文章我们主要介绍HPB的Gas 机制。

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

交易费用 = Gas * Gas Price.

HPB 计量单位转换关系

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

Gas如何收取

无智能合约的普通交易

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

智能合约创建交易

基本费用: 默认Gas Units = 53000.

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

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

智能合约运行交易

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

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

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

 // 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
Gas Price说明

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

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主网的出块节点.

Q9:智能合约最少消耗多少交易费用?

A9: 智能合约的交易费用请参考上表中的数字计算,交易费用 = Gas * Gas Price.

另外关于交易费用,请根据公式计算 ,举个例子: 以当前Gas Price = 18Gwei 为例,普通合约运行交易交易费用 = 10 18Gwei,创建智能合约的最低交易费用= 53000 18Gwei,执行智能合约的最低交易费用 = 10 * 18Gwei.