Official Go implementation of the Bytom protocol
Revision | 31e046c9d38fb2017a3ed023212fc8c7680992b7 (tree) |
---|---|
Time | 2019-10-16 16:18:30 |
Author | HAOYUatHZ <haoyu@prot...> |
Commiter | HAOYUatHZ |
update
@@ -15,13 +15,6 @@ import ( | ||
15 | 15 | "github.com/bytom/protocol/vm/vmutil" |
16 | 16 | ) |
17 | 17 | |
18 | -var ( | |
19 | - //chainTxUtxoNum maximum utxo quantity in a tx | |
20 | - chainTxUtxoNum = 5 | |
21 | - //chainTxMergeGas chain tx gas | |
22 | - chainTxMergeGas = uint64(10000000) | |
23 | -) | |
24 | - | |
25 | 18 | //DecodeSpendAction unmarshal JSON-encoded data of spend action |
26 | 19 | func (m *Manager) DecodeSpendAction(data []byte) (txbuilder.Action, error) { |
27 | 20 | a := &spendAction{accounts: m} |
@@ -62,20 +55,10 @@ func MergeSpendAction(actions []txbuilder.Action) []txbuilder.Action { | ||
62 | 55 | return resultActions |
63 | 56 | } |
64 | 57 | |
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 | - | |
75 | 58 | func (m *Manager) reserveBtmUtxoChain(builder *txbuilder.TemplateBuilder, accountID string, amount uint64, useUnconfirmed bool) ([]*UTXO, error) { |
76 | 59 | reservedAmount := uint64(0) |
77 | 60 | 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)) { | |
79 | 62 | reserveAmount := amount + gasAmount - reservedAmount |
80 | 63 | res, err := m.utxoKeeper.Reserve(accountID, consensus.BTMAssetID, reserveAmount, useUnconfirmed, builder.MaxTime()) |
81 | 64 | if err != nil { |
@@ -117,11 +100,11 @@ func (m *Manager) buildBtmTxChain(utxos []*UTXO, signer *signers.Signer) ([]*txb | ||
117 | 100 | } |
118 | 101 | |
119 | 102 | buildAmount += input.Amount() |
120 | - if builder.InputCount() != chainTxUtxoNum && index != len(utxos)-1 { | |
103 | + if builder.InputCount() != txbuilder.ChainTxUtxoNum && index != len(utxos)-1 { | |
121 | 104 | continue |
122 | 105 | } |
123 | 106 | |
124 | - outAmount := buildAmount - chainTxMergeGas | |
107 | + outAmount := buildAmount - txbuilder.ChainTxMergeGas | |
125 | 108 | output := types.NewTxOutput(*consensus.BTMAssetID, outAmount, acp.ControlProgram) |
126 | 109 | if err := builder.AddOutput(output); err != nil { |
127 | 110 | return nil, nil, err |
@@ -1,8 +1,6 @@ | ||
1 | 1 | package txbuilder |
2 | 2 | |
3 | 3 | import ( |
4 | - "fmt" | |
5 | - | |
6 | 4 | "github.com/bytom/consensus" |
7 | 5 | "github.com/bytom/consensus/segwit" |
8 | 6 | "github.com/bytom/protocol/bc/types" |
@@ -11,33 +9,45 @@ import ( | ||
11 | 9 | |
12 | 10 | // EstimateTxGasInfo estimate transaction consumed gas |
13 | 11 | 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"` | |
18 | 17 | } |
19 | 18 | |
20 | 19 | const ( |
21 | 20 | baseSize = int64(176) // inputSize(112) + outputSize(64) |
22 | 21 | baseP2WPKHSize = int64(98) |
23 | 22 | baseP2WPKHGas = int64(1409) |
23 | + | |
24 | + //ChainTxUtxoNum maximum utxo quantity in a tx | |
25 | + ChainTxUtxoNum = 5 | |
26 | + //ChainTxMergeGas chain tx gas | |
27 | + ChainTxMergeGas = uint64(10000000) | |
24 | 28 | ) |
25 | 29 | |
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 | +} | |
33 | 39 | |
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 | |
38 | 44 | } |
39 | 45 | |
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 | |
41 | 51 | } |
42 | 52 | |
43 | 53 | // EstimateTxGas estimate consumed neu for transaction |