Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit 1fc2b75

Browse files
authored
Merge pull request #1730 from Bytom/prod
Prod
2 parents d2815df + 7f08c11 commit 1fc2b75

File tree

142 files changed

+23933
-487
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+23933
-487
lines changed

account/accounts.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ var (
4747

4848
// pre-define errors for supporting bytom errorFormatter
4949
var (
50-
ErrDuplicateAlias = errors.New("duplicate account alias")
51-
ErrDuplicateIndex = errors.New("duplicate account with same xPubs and index")
52-
ErrFindAccount = errors.New("fail to find account")
53-
ErrMarshalAccount = errors.New("failed marshal account")
54-
ErrInvalidAddress = errors.New("invalid address")
55-
ErrFindCtrlProgram = errors.New("fail to find account control program")
56-
ErrDeriveRule = errors.New("invalid key derive rule")
57-
ErrContractIndex = errors.New("exceed the maximum addresses per account")
58-
ErrAccountIndex = errors.New("exceed the maximum accounts per xpub")
59-
ErrFindTransaction = errors.New("no transaction")
50+
ErrDuplicateAlias = errors.New("Duplicate account alias")
51+
ErrDuplicateIndex = errors.New("Duplicate account with same xPubs and index")
52+
ErrFindAccount = errors.New("Failed to find account")
53+
ErrMarshalAccount = errors.New("Failed to marshal account")
54+
ErrInvalidAddress = errors.New("Invalid address")
55+
ErrFindCtrlProgram = errors.New("Failed to find account control program")
56+
ErrDeriveRule = errors.New("Invalid key derivation rule")
57+
ErrContractIndex = errors.New("Exceeded maximum addresses per account")
58+
ErrAccountIndex = errors.New("Exceeded maximum accounts per xpub")
59+
ErrFindTransaction = errors.New("No transaction")
6060
)
6161

6262
// ContractKey account control promgram store prefix

api/api_test.go

+1-72
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ package api
22

33
import (
44
"context"
5-
"encoding/json"
6-
"math"
75
"net/http/httptest"
86
"os"
97
"testing"
108

119
"github.com/bytom/accesstoken"
1210
"github.com/bytom/blockchain/rpc"
13-
"github.com/bytom/blockchain/txbuilder"
14-
"github.com/bytom/consensus"
15-
"github.com/bytom/testutil"
1611
dbm "github.com/bytom/database/leveldb"
12+
"github.com/bytom/testutil"
1713
)
1814

1915
func TestAPIHandler(t *testing.T) {
@@ -99,70 +95,3 @@ func TestAPIHandler(t *testing.T) {
9995
}
10096
}
10197
}
102-
103-
func TestEstimateTxGas(t *testing.T) {
104-
tmplStr := `{"allow_additional_actions":false,"raw_transaction":"070100010161015ffe8a1209937a6a8b22e8c01f056fd5f1730734ba8964d6b79de4a639032cecddffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8099c4d59901000116001485eb6eee8023332da85df60157dc9b16cc553fb2010002013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80afa08b4f011600142b4fd033bc76b4ddf5cb00f625362c4bc7b10efa00013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8090dfc04a011600146eea1ce6cfa5b718ae8094376be9bc1a87c9c82700","signing_instructions":[{"position":0,"witness_components":[{"keys":[{"derivation_path":["010100000000000000","0100000000000000"],"xpub":"cb4e5932d808ee060df9552963d87f60edac42360b11d4ad89558ef2acea4d4aaf4818f2ebf5a599382b8dfce0a0c798c7e44ec2667b3a1d34c61ba57609de55"}],"quorum":1,"signatures":null,"type":"raw_tx_signature"},{"type":"data","value":"1c9b5c1db7f4afe31fd1b7e0495a8bb042a271d8d7924d4fc1ff7cf1bff15813"}]}]}`
105-
template := txbuilder.Template{}
106-
err := json.Unmarshal([]byte(tmplStr), &template)
107-
if err != nil {
108-
t.Fatal(err)
109-
}
110-
111-
estimateResult, err := EstimateTxGas(template)
112-
if err != nil {
113-
t.Fatal(err)
114-
}
115-
116-
baseRate := float64(100000)
117-
totalNeu := float64(estimateResult.StorageNeu+estimateResult.VMNeu+flexibleGas*consensus.VMGasRate) / baseRate
118-
roundingNeu := math.Ceil(totalNeu)
119-
estimateNeu := int64(roundingNeu) * int64(baseRate)
120-
121-
if estimateResult.TotalNeu != estimateNeu {
122-
t.Errorf(`got=%#v; want=%#v`, estimateResult.TotalNeu, estimateNeu)
123-
}
124-
}
125-
126-
func TestEstimateTxGasRange(t *testing.T) {
127-
128-
cases := []struct {
129-
path string
130-
tmplStr string
131-
respWant *EstimateTxGasResp
132-
}{
133-
{
134-
path: "/estimate-transaction-gas",
135-
tmplStr: `{"raw_transaction":"070100010160015e9a4e2bbae57dd71b6a827fb50aaeb744ce3ae6f45c4aec7494ad097213220e8affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0cea1bc5800011600144a6322008c5424251c7502c7d7d55f6389c3c358010001013dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe086f29b3301160014fa61b0629e5f2da2bb8b08e7fc948dbd265234f700","signing_instructions":[{"position":0,"witness_components":[{"type":"raw_tx_signature","quorum":1,"keys":[{"xpub":"19204fe9172cb0eeae86b39ec7a61ddc556656c8df08fd43ef6074296f32b347349722316972e382c339b79b7e1d83a565c6b3e7cf46847733a47044ae493257","derivation_path":["010100000000000000","0700000000000000"]}],"signatures":null},{"type":"data","value":"a527a92a7488c010bc42b39d6b50f0822183e51efab228af8ca8ca81ca459237"}]}],"allow_additional_actions":false}`,
136-
respWant: &EstimateTxGasResp{
137-
TotalNeu: (flexibleGas + 2095) * consensus.VMGasRate,
138-
},
139-
},
140-
{
141-
path: "/estimate-transaction-gas",
142-
tmplStr: `{"raw_transaction":"07010001016d016bcf24f1471d67c25a01ac84482ecdd8550229180171cae22321f87fe43d4f6a13ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8086a6d5f2020001220020713ef71e6087a58d6055ce81e8a8ea8a60ca19aef77923859e53a1fa9df0042989010240844b99bab9f393e89ca3bb272b1ba146852124f13a2d37fc47da6a7320f5ae1a4b6df1322750906ad480796db663e35ef7fd9544718eea08e51c5388f9813d0446ae20bd609e953918ab2ce120c43486894ff38dc4b65c2c1b4e19f6b41265d76b062120508684f922c1e5eea3dcbd592b00d297b2ddf92d35d5acabea9ff491ef514abe5152ad02014affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80bef6b4cd0201220020dc794f041d19c67108a05d2a6d797a2b12029f31b2c91ec699c9477727f25315000149ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80fef9b123012200200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac6600","signing_instructions":[{"position":0,"witness_components":[{"type":"raw_tx_signature","quorum":1,"keys":[{"xpub":"5ff7f79f0fd4eb9ccb17191b0a1ac9bed5b4a03320a06d2ff8170dd51f9ad9089c4038ec7280b5eb6745ef3d36284e67f5cf2ed2a0177d462d24abf53c0399ed","derivationPath":["010200000000000000","0400000000000000"]},{"xpub":"d0e7607bec7f68ea9135fbb9e3e94ef05a034d28be847070740fcba9454a749f6e21942cfef90f1437184cb70775beb290c13852c1497631dbcb137f74788e4f","derivationPath":["010200000000000000","0400000000000000"]}],"signatures":["","844b99bab9f393e89ca3bb272b1ba146852124f13a2d37fc47da6a7320f5ae1a4b6df1322750906ad480796db663e35ef7fd9544718eea08e51c5388f9813d04"]},{"type":"data","value":"ae20bd609e953918ab2ce120c43486894ff38dc4b65c2c1b4e19f6b41265d76b062120508684f922c1e5eea3dcbd592b00d297b2ddf92d35d5acabea9ff491ef514abe5152ad"}]}],"allow_additional_actions":false}`,
143-
respWant: &EstimateTxGasResp{
144-
TotalNeu: (flexibleGas + 3305) * consensus.VMGasRate,
145-
},
146-
},
147-
{
148-
path: "/estimate-transaction-gas",
149-
tmplStr: `{"raw_transaction":"07010002016c016acf24f1471d67c25a01ac84482ecdd8550229180171cae22321f87fe43d4f6a13ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80b4c4c32101012200200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66ef020440f5baa1530bd7ded5c37f1c91360e28e736c91a7933eff961d68eebf90bdce63eb4361689759a8aa420256af565e38921985026de8d27dd7b66f0d01c90170a0440b23b44f62f3e97bcbd5f80cb9bb3d63cb154c62d402851e5b4d5d89849fef74271c8c38f594b944b75222d06ef18bddec4b6278ad3185f72ac5321ce5948e90940a00b096eef5b3bed5c6a2843d29e1820ef1413947d3e278c21cc70976c47976d1159468f071bf853b244be8f6cc55d78615ea6594c946f1a6e6622d8e9d42206a901ae20d441b6f375659325a04eede4fc3b74579bb08ccd05b41b99776501e22d6dca7320af6d98ca2c3cd10bf0affbfa6e86609b750523cfadb662ec963c164f05798a49209820b9f1553b03aaebe7e3f9e9222ed7db73b5079b18564042fd3b2cef74156a20271b52de5f554aa4a6f1358f1c2193617bfb3fed4546d13c4af773096a429f9420eeb4a78d8b5cb8283c221ca2d3fd96b8946b3cddee02b7ceffb8f605932588595355ad016c016a158f56c5673a52876bbbed4cd8724428b43a8d9ddd2a759c9df06b46898f101affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80fef9b12301012200200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66ef020440c3c4fdbe99f9266a42df767cf03c22d9d09096446a8882b9d0c0076d9c85da28add31320705452fb566a091515cedb1ea9966647201236a0da13a020f848b8084043e22fe631cee95e3185ecd0c6fc4a262689d674725abe7d7f3158d8d43c776338edeec76600776fc0dcee280bd7a1a8a2b23909c6cefa7fbb55c27522b6100640fefe403941035a66ba9b6d097dfe0ada68ae6d006272928fad2ba23341fe878690e9e2fa1d2d3992c16aa20125fb2da7f7687920c12a36e4964533ceeccd3602a901ae20d441b6f375659325a04eede4fc3b74579bb08ccd05b41b99776501e22d6dca7320af6d98ca2c3cd10bf0affbfa6e86609b750523cfadb662ec963c164f05798a49209820b9f1553b03aaebe7e3f9e9222ed7db73b5079b18564042fd3b2cef74156a20271b52de5f554aa4a6f1358f1c2193617bfb3fed4546d13c4af773096a429f9420eeb4a78d8b5cb8283c221ca2d3fd96b8946b3cddee02b7ceffb8f605932588595355ad020149ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80ea8ed51f01220020036f3d1665dc802fd36aded656c2f4b2b2c5b00e86c44f5352257b718941a4e9000149ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80fef9b12301220020e402787b2bf9749f8fcdcc132a44e86bacf36780ec5df2189a11020d590533ee00","signing_instructions":[{"position":0,"witness_components":[{"type":"raw_tx_signature","quorum":3,"keys":[{"xpub":"5ff7f79f0fd4eb9ccb17191b0a1ac9bed5b4a03320a06d2ff8170dd51f9ad9089c4038ec7280b5eb6745ef3d36284e67f5cf2ed2a0177d462d24abf53c0399ed","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"7d1c7a9094ab23f432e60afbbfe2791ba9ab3daba8aaa544634218243b8659985cb0ae9fe2b0f5da8a84c6b117c9491bf38f5e59b0d05642d90ba34cf7611eec","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"b0d2d90cdee01976d51b55963ae214493708d8db44f7516d2d4853a542cba4c07fbd0ad3e7a9ff4b6fbe6b71e66f4538a9424eaf15f538d958aa7025f5f752dc","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"d0e7607bec7f68ea9135fbb9e3e94ef05a034d28be847070740fcba9454a749f6e21942cfef90f1437184cb70775beb290c13852c1497631dbcb137f74788e4f","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"e18b9d219e960d761e8d03290acddb5211fea1140c87663908ea74f212763ca8d809bb0fe861884e662429564fa0f2725b5787175054c17685a83a68e160344d","derivationPath":["010300000000000000","0100000000000000"]}],"signatures":["","f5baa1530bd7ded5c37f1c91360e28e736c91a7933eff961d68eebf90bdce63eb4361689759a8aa420256af565e38921985026de8d27dd7b66f0d01c90170a04","b23b44f62f3e97bcbd5f80cb9bb3d63cb154c62d402851e5b4d5d89849fef74271c8c38f594b944b75222d06ef18bddec4b6278ad3185f72ac5321ce5948e909","a00b096eef5b3bed5c6a2843d29e1820ef1413947d3e278c21cc70976c47976d1159468f071bf853b244be8f6cc55d78615ea6594c946f1a6e6622d8e9d42206",""]},{"type":"data","value":"ae20d441b6f375659325a04eede4fc3b74579bb08ccd05b41b99776501e22d6dca7320af6d98ca2c3cd10bf0affbfa6e86609b750523cfadb662ec963c164f05798a49209820b9f1553b03aaebe7e3f9e9222ed7db73b5079b18564042fd3b2cef74156a20271b52de5f554aa4a6f1358f1c2193617bfb3fed4546d13c4af773096a429f9420eeb4a78d8b5cb8283c221ca2d3fd96b8946b3cddee02b7ceffb8f605932588595355ad"}]},{"position":1,"witness_components":[{"type":"raw_tx_signature","quorum":3,"keys":[{"xpub":"5ff7f79f0fd4eb9ccb17191b0a1ac9bed5b4a03320a06d2ff8170dd51f9ad9089c4038ec7280b5eb6745ef3d36284e67f5cf2ed2a0177d462d24abf53c0399ed","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"7d1c7a9094ab23f432e60afbbfe2791ba9ab3daba8aaa544634218243b8659985cb0ae9fe2b0f5da8a84c6b117c9491bf38f5e59b0d05642d90ba34cf7611eec","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"b0d2d90cdee01976d51b55963ae214493708d8db44f7516d2d4853a542cba4c07fbd0ad3e7a9ff4b6fbe6b71e66f4538a9424eaf15f538d958aa7025f5f752dc","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"d0e7607bec7f68ea9135fbb9e3e94ef05a034d28be847070740fcba9454a749f6e21942cfef90f1437184cb70775beb290c13852c1497631dbcb137f74788e4f","derivationPath":["010300000000000000","0100000000000000"]},{"xpub":"e18b9d219e960d761e8d03290acddb5211fea1140c87663908ea74f212763ca8d809bb0fe861884e662429564fa0f2725b5787175054c17685a83a68e160344d","derivationPath":["010300000000000000","0100000000000000"]}],"signatures":["","c3c4fdbe99f9266a42df767cf03c22d9d09096446a8882b9d0c0076d9c85da28add31320705452fb566a091515cedb1ea9966647201236a0da13a020f848b808","43e22fe631cee95e3185ecd0c6fc4a262689d674725abe7d7f3158d8d43c776338edeec76600776fc0dcee280bd7a1a8a2b23909c6cefa7fbb55c27522b61006","fefe403941035a66ba9b6d097dfe0ada68ae6d006272928fad2ba23341fe878690e9e2fa1d2d3992c16aa20125fb2da7f7687920c12a36e4964533ceeccd3602",""]},{"type":"data","value":"ae20d441b6f375659325a04eede4fc3b74579bb08ccd05b41b99776501e22d6dca7320af6d98ca2c3cd10bf0affbfa6e86609b750523cfadb662ec963c164f05798a49209820b9f1553b03aaebe7e3f9e9222ed7db73b5079b18564042fd3b2cef74156a20271b52de5f554aa4a6f1358f1c2193617bfb3fed4546d13c4af773096a429f9420eeb4a78d8b5cb8283c221ca2d3fd96b8946b3cddee02b7ceffb8f605932588595355ad"}]}],"allow_additional_actions":false}`,
150-
respWant: &EstimateTxGasResp{
151-
TotalNeu: (flexibleGas + 13556) * consensus.VMGasRate,
152-
},
153-
},
154-
}
155-
for _, c := range cases {
156-
template := txbuilder.Template{}
157-
err := json.Unmarshal([]byte(c.tmplStr), &template)
158-
if err != nil {
159-
t.Fatal(err)
160-
}
161-
estimateTxGasResp, err := EstimateTxGas(template)
162-
realTotalNeu := float64(c.respWant.TotalNeu)
163-
rate := math.Abs((float64(estimateTxGasResp.TotalNeu) - realTotalNeu) / float64(estimateTxGasResp.TotalNeu))
164-
if rate > 0.2 {
165-
t.Errorf(`the estimateNeu over realNeu 20%%`)
166-
}
167-
}
168-
}

api/errors.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ var respErrFormatter = map[error]httperror.Info{
5959
account.ErrInsufficient: {400, "BTM700", "Funds of account are insufficient"},
6060
account.ErrImmature: {400, "BTM701", "Available funds of account are immature"},
6161
account.ErrReserved: {400, "BTM702", "Available UTXOs of account have been reserved"},
62-
account.ErrMatchUTXO: {400, "BTM703", "Not found UTXO with given hash"},
62+
account.ErrMatchUTXO: {400, "BTM703", "UTXO with given hash not found"},
6363
ErrBadActionType: {400, "BTM704", "Invalid action type"},
6464
ErrBadAction: {400, "BTM705", "Invalid action object"},
6565
ErrBadActionConstruction: {400, "BTM706", "Invalid action construction"},
6666
txbuilder.ErrMissingFields: {400, "BTM707", "One or more fields are missing"},
6767
txbuilder.ErrBadAmount: {400, "BTM708", "Invalid asset amount"},
68-
account.ErrFindAccount: {400, "BTM709", "Not found account"},
69-
asset.ErrFindAsset: {400, "BTM710", "Not found asset"},
68+
account.ErrFindAccount: {400, "BTM709", "Account not found"},
69+
asset.ErrFindAsset: {400, "BTM710", "Asset not found"},
7070
txbuilder.ErrBadContractArgType: {400, "BTM711", "Invalid contract argument type"},
71-
txbuilder.ErrOrphanTx: {400, "BTM712", "Not found transaction input utxo"},
72-
txbuilder.ErrExtTxFee: {400, "BTM713", "Transaction fee exceed max limit"},
71+
txbuilder.ErrOrphanTx: {400, "BTM712", "Transaction input UTXO not found"},
72+
txbuilder.ErrExtTxFee: {400, "BTM713", "Transaction fee exceeded max limit"},
7373
txbuilder.ErrNoGasInput: {400, "BTM714", "Transaction has no gas input"},
7474

7575
// Submit transaction error namespace (73x ~ 79x)

api/miner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (a *API) submitBlock(ctx context.Context, req *SubmitBlockReq) Response {
8585
return NewErrorResponse(errors.New("block submitted is orphan"))
8686
}
8787

88-
if err = a.eventDispatcher.Post(event.NewMinedBlockEvent{Block: req.Block}); err != nil {
88+
if err = a.eventDispatcher.Post(event.NewMinedBlockEvent{Block: *req.Block}); err != nil {
8989
return NewErrorResponse(err)
9090
}
9191

api/query.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ func (a *API) listTransactions(ctx context.Context, filter struct {
144144
transaction, err = a.wallet.GetTransactionByTxID(filter.ID)
145145
if err != nil && filter.Unconfirmed {
146146
transaction, err = a.wallet.GetUnconfirmedTxByTxID(filter.ID)
147-
if err != nil {
148-
return NewErrorResponse(err)
149-
}
147+
}
148+
149+
if err != nil {
150+
return NewErrorResponse(err)
150151
}
151152
transactions = []*query.AnnotatedTx{transaction}
152153
} else {

api/transact.go

+1-110
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ package api
33
import (
44
"context"
55
"encoding/json"
6-
"math"
76
"strings"
87
"time"
98

109
log "github.com/sirupsen/logrus"
1110

1211
"github.com/bytom/account"
1312
"github.com/bytom/blockchain/txbuilder"
14-
"github.com/bytom/consensus"
15-
"github.com/bytom/consensus/segwit"
1613
"github.com/bytom/errors"
17-
"github.com/bytom/math/checked"
1814
"github.com/bytom/net/http/reqid"
1915
"github.com/bytom/protocol/bc"
2016
"github.com/bytom/protocol/bc/types"
@@ -23,7 +19,6 @@ import (
2319
var (
2420
defaultTxTTL = 30 * time.Minute
2521
defaultBaseRate = float64(100000)
26-
flexibleGas = int64(1800)
2722
)
2823

2924
func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, error), bool) {
@@ -223,115 +218,11 @@ func (a *API) submitTxs(ctx context.Context, ins struct {
223218
return NewSuccessResponse(&submitTxsResp{TxID: txHashs})
224219
}
225220

226-
// EstimateTxGasResp estimate transaction consumed gas
227-
type EstimateTxGasResp struct {
228-
TotalNeu int64 `json:"total_neu"`
229-
StorageNeu int64 `json:"storage_neu"`
230-
VMNeu int64 `json:"vm_neu"`
231-
}
232-
233-
// EstimateTxGas estimate consumed neu for transaction
234-
func EstimateTxGas(template txbuilder.Template) (*EstimateTxGasResp, error) {
235-
// base tx size and not include sign
236-
data, err := template.Transaction.TxData.MarshalText()
237-
if err != nil {
238-
return nil, err
239-
}
240-
baseTxSize := int64(len(data))
241-
242-
// extra tx size for sign witness parts
243-
signSize := estimateSignSize(template.SigningInstructions)
244-
245-
// total gas for tx storage
246-
totalTxSizeGas, ok := checked.MulInt64(baseTxSize+signSize, consensus.StorageGasRate)
247-
if !ok {
248-
return nil, errors.New("calculate txsize gas got a math error")
249-
}
250-
251-
// consume gas for run VM
252-
totalP2WPKHGas := int64(0)
253-
totalP2WSHGas := int64(0)
254-
baseP2WPKHGas := int64(1419)
255-
// flexible Gas is used for handle need extra utxo situation
256-
257-
for pos, inpID := range template.Transaction.Tx.InputIDs {
258-
sp, err := template.Transaction.Spend(inpID)
259-
if err != nil {
260-
continue
261-
}
262-
263-
resOut, err := template.Transaction.Output(*sp.SpentOutputId)
264-
if err != nil {
265-
continue
266-
}
267-
268-
if segwit.IsP2WPKHScript(resOut.ControlProgram.Code) {
269-
totalP2WPKHGas += baseP2WPKHGas
270-
} else if segwit.IsP2WSHScript(resOut.ControlProgram.Code) {
271-
sigInst := template.SigningInstructions[pos]
272-
totalP2WSHGas += estimateP2WSHGas(sigInst)
273-
}
274-
}
275-
276-
// total estimate gas
277-
totalGas := totalTxSizeGas + totalP2WPKHGas + totalP2WSHGas + flexibleGas
278-
279-
// rounding totalNeu with base rate 100000
280-
totalNeu := float64(totalGas*consensus.VMGasRate) / defaultBaseRate
281-
roundingNeu := math.Ceil(totalNeu)
282-
estimateNeu := int64(roundingNeu) * int64(defaultBaseRate)
283-
284-
// TODO add priority
285-
286-
return &EstimateTxGasResp{
287-
TotalNeu: estimateNeu,
288-
StorageNeu: totalTxSizeGas * consensus.VMGasRate,
289-
VMNeu: (totalP2WPKHGas + totalP2WSHGas) * consensus.VMGasRate,
290-
}, nil
291-
}
292-
293-
// estimate p2wsh gas.
294-
// OP_CHECKMULTISIG consume (984 * a - 72 * b - 63) gas,
295-
// where a represent the num of public keys, and b represent the num of quorum.
296-
func estimateP2WSHGas(sigInst *txbuilder.SigningInstruction) int64 {
297-
P2WSHGas := int64(0)
298-
baseP2WSHGas := int64(738)
299-
300-
for _, witness := range sigInst.WitnessComponents {
301-
switch t := witness.(type) {
302-
case *txbuilder.SignatureWitness:
303-
P2WSHGas += baseP2WSHGas + (984*int64(len(t.Keys)) - 72*int64(t.Quorum) - 63)
304-
case *txbuilder.RawTxSigWitness:
305-
P2WSHGas += baseP2WSHGas + (984*int64(len(t.Keys)) - 72*int64(t.Quorum) - 63)
306-
}
307-
}
308-
return P2WSHGas
309-
}
310-
311-
// estimate signature part size.
312-
// if need multi-sign, calculate the size according to the length of keys.
313-
func estimateSignSize(signingInstructions []*txbuilder.SigningInstruction) int64 {
314-
signSize := int64(0)
315-
baseWitnessSize := int64(300)
316-
317-
for _, sigInst := range signingInstructions {
318-
for _, witness := range sigInst.WitnessComponents {
319-
switch t := witness.(type) {
320-
case *txbuilder.SignatureWitness:
321-
signSize += int64(t.Quorum) * baseWitnessSize
322-
case *txbuilder.RawTxSigWitness:
323-
signSize += int64(t.Quorum) * baseWitnessSize
324-
}
325-
}
326-
}
327-
return signSize
328-
}
329-
330221
// POST /estimate-transaction-gas
331222
func (a *API) estimateTxGas(ctx context.Context, in struct {
332223
TxTemplate txbuilder.Template `json:"transaction_template"`
333224
}) Response {
334-
txGasResp, err := EstimateTxGas(in.TxTemplate)
225+
txGasResp, err := txbuilder.EstimateTxGas(in.TxTemplate)
335226
if err != nil {
336227
return NewErrorResponse(err)
337228
}

0 commit comments

Comments
 (0)