• R/O
  • HTTP
  • SSH
  • HTTPS

bytom: Commit

Official Go implementation of the Bytom protocol


Commit MetaInfo

Revision31e046c9d38fb2017a3ed023212fc8c7680992b7 (tree)
Time2019-10-16 16:18:30
AuthorHAOYUatHZ <haoyu@prot...>
CommiterHAOYUatHZ

Log Message

update

Change Summary

Incremental Difference

--- a/account/builder.go
+++ b/account/builder.go
@@ -15,13 +15,6 @@ import (
1515 "github.com/bytom/protocol/vm/vmutil"
1616 )
1717
18-var (
19- //chainTxUtxoNum maximum utxo quantity in a tx
20- chainTxUtxoNum = 5
21- //chainTxMergeGas chain tx gas
22- chainTxMergeGas = uint64(10000000)
23-)
24-
2518 //DecodeSpendAction unmarshal JSON-encoded data of spend action
2619 func (m *Manager) DecodeSpendAction(data []byte) (txbuilder.Action, error) {
2720 a := &spendAction{accounts: m}
@@ -62,20 +55,10 @@ func MergeSpendAction(actions []txbuilder.Action) []txbuilder.Action {
6255 return resultActions
6356 }
6457
65-//calcMergeGas calculate the gas required that n utxos are merged into one
66-func calcMergeGas(num int) uint64 {
67- gas := uint64(0)
68- for num > 1 {
69- gas += chainTxMergeGas
70- num -= chainTxUtxoNum - 1
71- }
72- return gas
73-}
74-
7558 func (m *Manager) reserveBtmUtxoChain(builder *txbuilder.TemplateBuilder, accountID string, amount uint64, useUnconfirmed bool) ([]*UTXO, error) {
7659 reservedAmount := uint64(0)
7760 utxos := []*UTXO{}
78- for gasAmount := uint64(0); reservedAmount < gasAmount+amount; gasAmount = calcMergeGas(len(utxos)) {
61+ for gasAmount := uint64(0); reservedAmount < gasAmount+amount; gasAmount = txbuilder.CalcMergeGas(len(utxos)) {
7962 reserveAmount := amount + gasAmount - reservedAmount
8063 res, err := m.utxoKeeper.Reserve(accountID, consensus.BTMAssetID, reserveAmount, useUnconfirmed, builder.MaxTime())
8164 if err != nil {
@@ -117,11 +100,11 @@ func (m *Manager) buildBtmTxChain(utxos []*UTXO, signer *signers.Signer) ([]*txb
117100 }
118101
119102 buildAmount += input.Amount()
120- if builder.InputCount() != chainTxUtxoNum && index != len(utxos)-1 {
103+ if builder.InputCount() != txbuilder.ChainTxUtxoNum && index != len(utxos)-1 {
121104 continue
122105 }
123106
124- outAmount := buildAmount - chainTxMergeGas
107+ outAmount := buildAmount - txbuilder.ChainTxMergeGas
125108 output := types.NewTxOutput(*consensus.BTMAssetID, outAmount, acp.ControlProgram)
126109 if err := builder.AddOutput(output); err != nil {
127110 return nil, nil, err
--- a/blockchain/txbuilder/estimate.go
+++ b/blockchain/txbuilder/estimate.go
@@ -1,8 +1,6 @@
11 package txbuilder
22
33 import (
4- "fmt"
5-
64 "github.com/bytom/consensus"
75 "github.com/bytom/consensus/segwit"
86 "github.com/bytom/protocol/bc/types"
@@ -11,33 +9,45 @@ import (
119
1210 // EstimateTxGasInfo estimate transaction consumed gas
1311 type EstimateTxGasInfo struct {
14- TotalNeu int64 `json:"total_neu"`
15- FlexibleNeu int64 `json:"flexible_neu"`
16- StorageNeu int64 `json:"storage_neu"`
17- VMNeu int64 `json:"vm_neu"`
12+ TotalNeu int64 `json:"total_neu"`
13+ FlexibleNeu int64 `json:"flexible_neu"`
14+ StorageNeu int64 `json:"storage_neu"`
15+ VMNeu int64 `json:"vm_neu"`
16+ ChainTxGrossNeu int64 `json:"chain_tx_gross_neu,omitempty"`
1817 }
1918
2019 const (
2120 baseSize = int64(176) // inputSize(112) + outputSize(64)
2221 baseP2WPKHSize = int64(98)
2322 baseP2WPKHGas = int64(1409)
23+
24+ //ChainTxUtxoNum maximum utxo quantity in a tx
25+ ChainTxUtxoNum = 5
26+ //ChainTxMergeGas chain tx gas
27+ ChainTxMergeGas = uint64(10000000)
2428 )
2529
26-func EstimateChainTxGas(templates []Template) (*EstimateTxGasInfo, error) {
27- chainResult := &EstimateTxGasInfo{}
28- for i, template := range templates {
29- result, err := EstimateTxGas(template)
30- if err != nil {
31- return nil, fmt.Errorf("estimate gas error for tx %d", i)
32- }
30+//calcMergeGas calculate the gas required that n utxos are merged into one
31+func CalcMergeGas(num int) uint64 {
32+ gas := uint64(0)
33+ for num > 1 {
34+ gas += ChainTxMergeGas
35+ num -= ChainTxUtxoNum - 1
36+ }
37+ return gas
38+}
3339
34- chainResult.TotalNeu += result.TotalNeu
35- chainResult.FlexibleNeu += result.FlexibleNeu
36- chainResult.StorageNeu += result.StorageNeu
37- chainResult.VMNeu += result.VMNeu
40+func EstimateChainTxGas(templates []Template) (*EstimateTxGasInfo, error) {
41+ estimated, err := EstimateTxGas(templates[len(templates)-1])
42+ if err != nil {
43+ return nil, err
3844 }
3945
40- return chainResult, nil
46+ for i := 0; i < len(templates)-1; i++ {
47+ mergeGas := CalcMergeGas(len(templates[i].Transaction.TxData.Inputs))
48+ estimated.ChainTxGrossNeu += int64(mergeGas) * consensus.VMGasRate
49+ }
50+ return estimated, nil
4151 }
4252
4353 // EstimateTxGas estimate consumed neu for transaction
Show on old repository browser