Golang implemented sidechain for Bytom
Révision | 2c87853e67a7aec657e465c4e82477c1e7a2ee85 (tree) |
---|---|
l'heure | 2020-10-29 20:44:03 |
Auteur | paladz <colt@Colt...> |
Commiter | paladz |
add performance test point
@@ -14,6 +14,7 @@ import ( | ||
14 | 14 | "github.com/bytom/vapor/protocol" |
15 | 15 | "github.com/bytom/vapor/protocol/bc" |
16 | 16 | "github.com/bytom/vapor/protocol/bc/types" |
17 | + "github.com/bytom/vapor/toolbar/measure" | |
17 | 18 | ) |
18 | 19 | |
19 | 20 | var ( |
@@ -53,6 +54,9 @@ func NewCoreWithDB(store *database.LevelDBMovStore, startBlockHeight uint64) *Co | ||
53 | 54 | // ApplyBlock parse pending order and cancel from the the transactions of block |
54 | 55 | // and add pending order to the dex db, remove cancel order from dex db. |
55 | 56 | func (m *Core) ApplyBlock(block *types.Block) error { |
57 | + measure.Start() | |
58 | + defer measure.End() | |
59 | + | |
56 | 60 | if block.Height < m.startBlockHeight { |
57 | 61 | return nil |
58 | 62 | } |
@@ -175,6 +179,9 @@ func (m *Core) StartHeight() uint64 { | ||
175 | 179 | // ValidateBlock no need to verify the block header, because the first module has been verified. |
176 | 180 | // just need to verify the transactions in the block. |
177 | 181 | func (m *Core) ValidateBlock(block *types.Block, verifyResults []*bc.TxVerifyResult) error { |
182 | + measure.Start() | |
183 | + defer measure.End() | |
184 | + | |
178 | 185 | for i, tx := range block.Transactions { |
179 | 186 | if err := m.ValidateTx(tx, verifyResults[i], block.Height); err != nil { |
180 | 187 | return err |
@@ -16,6 +16,7 @@ import ( | ||
16 | 16 | "github.com/bytom/vapor/protocol/bc" |
17 | 17 | "github.com/bytom/vapor/protocol/bc/types" |
18 | 18 | "github.com/bytom/vapor/protocol/state" |
19 | + "github.com/bytom/vapor/toolbar/measure" | |
19 | 20 | ) |
20 | 21 | |
21 | 22 | const ( |
@@ -285,6 +286,9 @@ func (s *Store) GetStoreStatus() *protocol.BlockStoreState { | ||
285 | 286 | |
286 | 287 | // GetTransactionsUtxo will return all the utxo that related to the input txs |
287 | 288 | func (s *Store) GetTransactionsUtxo(view *state.UtxoViewpoint, txs []*bc.Tx) error { |
289 | + measure.Start() | |
290 | + defer measure.End() | |
291 | + | |
288 | 292 | return getTransactionsUtxo(s.db, view, txs) |
289 | 293 | } |
290 | 294 |
@@ -314,6 +318,9 @@ func (s *Store) GetConsensusResult(seq uint64) (*state.ConsensusResult, error) { | ||
314 | 318 | |
315 | 319 | // SaveBlock persists a new block in the protocol. |
316 | 320 | func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error { |
321 | + measure.Start() | |
322 | + defer measure.End() | |
323 | + | |
317 | 324 | startTime := time.Now() |
318 | 325 | binaryBlockHeader, err := block.MarshalTextForBlockHeader() |
319 | 326 | if err != nil { |
@@ -17,6 +17,7 @@ import ( | ||
17 | 17 | "github.com/bytom/vapor/protocol/state" |
18 | 18 | "github.com/bytom/vapor/protocol/validation" |
19 | 19 | "github.com/bytom/vapor/protocol/vm/vmutil" |
20 | + "github.com/bytom/vapor/toolbar/measure" | |
20 | 21 | ) |
21 | 22 | |
22 | 23 | const ( |
@@ -49,6 +50,9 @@ type blockBuilder struct { | ||
49 | 50 | } |
50 | 51 | |
51 | 52 | func newBlockBuilder(chain *protocol.Chain, accountManager *account.Manager, timestamp uint64, warnDuration, criticalDuration time.Duration) *blockBuilder { |
53 | + measure.Start() | |
54 | + defer measure.End() | |
55 | + | |
52 | 56 | preBlockHeader := chain.BestBlockHeader() |
53 | 57 | block := &types.Block{ |
54 | 58 | BlockHeader: types.BlockHeader{ |
@@ -96,6 +100,9 @@ func (b *blockBuilder) applyCoinbaseTransaction() error { | ||
96 | 100 | } |
97 | 101 | |
98 | 102 | func (b *blockBuilder) applyTransactions(txs []*types.Tx, timeoutStatus uint8) error { |
103 | + measure.Start() | |
104 | + defer measure.End() | |
105 | + | |
99 | 106 | tempTxs := []*types.Tx{} |
100 | 107 | for i := 0; i < len(txs); i++ { |
101 | 108 | if tempTxs = append(tempTxs, txs[i]); len(tempTxs) < batchApplyNum && i != len(txs)-1 { |
@@ -127,6 +134,9 @@ func (b *blockBuilder) applyTransactions(txs []*types.Tx, timeoutStatus uint8) e | ||
127 | 134 | } |
128 | 135 | |
129 | 136 | func (b *blockBuilder) applyTransactionFromPool() error { |
137 | + measure.Start() | |
138 | + defer measure.End() | |
139 | + | |
130 | 140 | txDescList := b.chain.GetTxPool().GetTransactions() |
131 | 141 | sort.Sort(byTime(txDescList)) |
132 | 142 |
@@ -139,6 +149,9 @@ func (b *blockBuilder) applyTransactionFromPool() error { | ||
139 | 149 | } |
140 | 150 | |
141 | 151 | func (b *blockBuilder) applyTransactionFromSubProtocol() error { |
152 | + measure.Start() | |
153 | + defer measure.End() | |
154 | + | |
142 | 155 | isTimeout := func() bool { |
143 | 156 | return b.getTimeoutStatus() > timeoutOk |
144 | 157 | } |
@@ -162,6 +175,9 @@ func (b *blockBuilder) applyTransactionFromSubProtocol() error { | ||
162 | 175 | } |
163 | 176 | |
164 | 177 | func (b *blockBuilder) build() (*types.Block, error) { |
178 | + measure.Start() | |
179 | + defer measure.End() | |
180 | + | |
165 | 181 | if err := b.applyCoinbaseTransaction(); err != nil { |
166 | 182 | return nil, err |
167 | 183 | } |
@@ -186,6 +202,9 @@ func (b *blockBuilder) build() (*types.Block, error) { | ||
186 | 202 | } |
187 | 203 | |
188 | 204 | func (b *blockBuilder) calcBlockCommitment() (err error) { |
205 | + measure.Start() | |
206 | + defer measure.End() | |
207 | + | |
189 | 208 | var txEntries []*bc.Tx |
190 | 209 | for _, tx := range b.block.Transactions { |
191 | 210 | txEntries = append(txEntries, tx.Tx) |
@@ -290,6 +309,9 @@ type validateTxResult struct { | ||
290 | 309 | } |
291 | 310 | |
292 | 311 | func (b *blockBuilder) preValidateTxs(txs []*types.Tx, chain *protocol.Chain, view *state.UtxoViewpoint, gasLeft int64) ([]*validateTxResult, int64) { |
312 | + measure.Start() | |
313 | + defer measure.End() | |
314 | + | |
293 | 315 | var results []*validateTxResult |
294 | 316 | bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: chain.BestBlockHeight() + 1}} |
295 | 317 | bcTxs := make([]*bc.Tx, len(txs)) |
@@ -335,6 +357,9 @@ func (b *blockBuilder) preValidateTxs(txs []*types.Tx, chain *protocol.Chain, vi | ||
335 | 357 | } |
336 | 358 | |
337 | 359 | func (b *blockBuilder) validateBySubProtocols(tx *types.Tx, statusFail bool, subProtocols []protocol.SubProtocol) error { |
360 | + measure.Start() | |
361 | + defer measure.End() | |
362 | + | |
338 | 363 | for _, subProtocol := range subProtocols { |
339 | 364 | verifyResult := &bc.TxVerifyResult{StatusFail: statusFail} |
340 | 365 | if err := subProtocol.ValidateTx(tx, verifyResult, b.block.Height); err != nil { |
@@ -13,6 +13,7 @@ import ( | ||
13 | 13 | "github.com/bytom/vapor/protocol/bc" |
14 | 14 | "github.com/bytom/vapor/protocol/bc/types" |
15 | 15 | "github.com/bytom/vapor/protocol/state" |
16 | + "github.com/bytom/vapor/toolbar/measure" | |
16 | 17 | ) |
17 | 18 | |
18 | 19 | const ( |
@@ -116,6 +117,9 @@ func (c *Chain) updateBlockSignature(blockHeader *types.BlockHeader, nodeOrder u | ||
116 | 117 | // if some signature is invalid, they will be reset to nil |
117 | 118 | // if the block does not have the signature of blocker, it will return error |
118 | 119 | func (c *Chain) validateSign(block *types.Block) error { |
120 | + measure.Start() | |
121 | + defer measure.End() | |
122 | + | |
119 | 123 | consensusNodeMap, err := c.getConsensusNodes(&block.PreviousBlockHash) |
120 | 124 | if err != nil { |
121 | 125 | return err |
@@ -215,6 +219,9 @@ func (c *Chain) SignBlockHeader(blockHeader *types.BlockHeader) error { | ||
215 | 219 | } |
216 | 220 | |
217 | 221 | func (c *Chain) applyBlockSign(blockHeader *types.BlockHeader) error { |
222 | + measure.Start() | |
223 | + defer measure.End() | |
224 | + | |
218 | 225 | signature, err := c.signBlockHeader(blockHeader) |
219 | 226 | if err != nil { |
220 | 227 | return err |
@@ -8,6 +8,7 @@ import ( | ||
8 | 8 | "github.com/bytom/vapor/protocol/bc/types" |
9 | 9 | "github.com/bytom/vapor/protocol/state" |
10 | 10 | "github.com/bytom/vapor/protocol/validation" |
11 | + "github.com/bytom/vapor/toolbar/measure" | |
11 | 12 | ) |
12 | 13 | |
13 | 14 | var ( |
@@ -87,6 +88,9 @@ func (c *Chain) calcReorganizeChain(beginAttach *types.BlockHeader, beginDetach | ||
87 | 88 | } |
88 | 89 | |
89 | 90 | func (c *Chain) connectBlock(block *types.Block) (err error) { |
91 | + measure.Start() | |
92 | + defer measure.End() | |
93 | + | |
90 | 94 | bcBlock := types.MapBlock(block) |
91 | 95 | if bcBlock.TransactionStatus, err = c.store.GetTransactionStatus(&bcBlock.ID); err != nil { |
92 | 96 | return err |
@@ -362,6 +366,9 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error { | ||
362 | 366 | |
363 | 367 | // SaveBlock will validate and save block into storage |
364 | 368 | func (c *Chain) saveBlock(block *types.Block) error { |
369 | + measure.Start() | |
370 | + defer measure.End() | |
371 | + | |
365 | 372 | if err := c.validateSign(block); err != nil { |
366 | 373 | return errors.Sub(ErrBadBlock, err) |
367 | 374 | } |
@@ -453,6 +460,9 @@ func (c *Chain) blockProcesser() { | ||
453 | 460 | |
454 | 461 | // ProcessBlock is the entry for handle block insert |
455 | 462 | func (c *Chain) processBlock(block *types.Block) (bool, error) { |
463 | + measure.Start() | |
464 | + defer measure.End() | |
465 | + | |
456 | 466 | blockHash := block.Hash() |
457 | 467 | if c.BlockExist(&blockHash) { |
458 | 468 | log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Debug("block has been processed") |
@@ -6,6 +6,7 @@ import ( | ||
6 | 6 | "github.com/bytom/vapor/protocol/bc" |
7 | 7 | "github.com/bytom/vapor/protocol/bc/types" |
8 | 8 | "github.com/bytom/vapor/protocol/state" |
9 | + "github.com/bytom/vapor/toolbar/measure" | |
9 | 10 | ) |
10 | 11 | |
11 | 12 | var ( |
@@ -14,6 +15,9 @@ var ( | ||
14 | 15 | ) |
15 | 16 | |
16 | 17 | func (c *Chain) getBestConsensusResult() (*state.ConsensusResult, error) { |
18 | + measure.Start() | |
19 | + defer measure.End() | |
20 | + | |
17 | 21 | bestBlockHeader := c.bestBlockHeader |
18 | 22 | seq := state.CalcVoteSeq(bestBlockHeader.Height) |
19 | 23 | return c.getConsensusResult(seq, bestBlockHeader) |
@@ -12,6 +12,7 @@ import ( | ||
12 | 12 | "github.com/bytom/vapor/protocol/bc" |
13 | 13 | "github.com/bytom/vapor/protocol/bc/types" |
14 | 14 | "github.com/bytom/vapor/protocol/state" |
15 | + "github.com/bytom/vapor/toolbar/measure" | |
15 | 16 | ) |
16 | 17 | |
17 | 18 | const ( |
@@ -279,6 +280,9 @@ func (c *Chain) syncProtocolStatus(subProtocol SubProtocol) error { | ||
279 | 280 | |
280 | 281 | // This function must be called with mu lock in above level |
281 | 282 | func (c *Chain) setState(blockHeader, irrBlockHeader *types.BlockHeader, mainBlockHeaders []*types.BlockHeader, view *state.UtxoViewpoint, consensusResults []*state.ConsensusResult) error { |
283 | + measure.Start() | |
284 | + defer measure.End() | |
285 | + | |
282 | 286 | if err := c.store.SaveChainStatus(blockHeader, irrBlockHeader, mainBlockHeaders, view, consensusResults); err != nil { |
283 | 287 | return err |
284 | 288 | } |
@@ -5,6 +5,7 @@ import ( | ||
5 | 5 | "github.com/bytom/vapor/database/storage" |
6 | 6 | "github.com/bytom/vapor/errors" |
7 | 7 | "github.com/bytom/vapor/protocol/bc" |
8 | + "github.com/bytom/vapor/toolbar/measure" | |
8 | 9 | ) |
9 | 10 | |
10 | 11 | // UtxoViewpoint represents a view into the set of unspent transaction outputs |
@@ -32,6 +33,9 @@ func (view *UtxoViewpoint) ApplyTransaction(block *bc.Block, tx *bc.Tx, statusFa | ||
32 | 33 | } |
33 | 34 | |
34 | 35 | func (view *UtxoViewpoint) ApplyBlock(block *bc.Block, txStatus *bc.TransactionStatus) error { |
36 | + measure.Start() | |
37 | + defer measure.End() | |
38 | + | |
35 | 39 | for i, tx := range block.Transactions { |
36 | 40 | statusFail, err := txStatus.GetStatus(i) |
37 | 41 | if err != nil { |
@@ -12,6 +12,7 @@ import ( | ||
12 | 12 | "github.com/bytom/vapor/protocol/bc" |
13 | 13 | "github.com/bytom/vapor/protocol/bc/types" |
14 | 14 | "github.com/bytom/vapor/protocol/state" |
15 | + "github.com/bytom/vapor/toolbar/measure" | |
15 | 16 | ) |
16 | 17 | |
17 | 18 | const logModule = "leveldb" |
@@ -88,6 +89,9 @@ func ValidateBlockHeader(b *bc.Block, parent *types.BlockHeader) error { | ||
88 | 89 | |
89 | 90 | // ValidateBlock validates a block and the transactions within. |
90 | 91 | func ValidateBlock(b *bc.Block, parent *types.BlockHeader, rewards []state.CoinbaseReward) error { |
92 | + measure.Start() | |
93 | + defer measure.End() | |
94 | + | |
91 | 95 | startTime := time.Now() |
92 | 96 | if err := ValidateBlockHeader(b, parent); err != nil { |
93 | 97 | return err |
@@ -13,6 +13,7 @@ import ( | ||
13 | 13 | "github.com/bytom/vapor/math/checked" |
14 | 14 | "github.com/bytom/vapor/protocol/bc" |
15 | 15 | "github.com/bytom/vapor/protocol/vm" |
16 | + "github.com/bytom/vapor/toolbar/measure" | |
16 | 17 | ) |
17 | 18 | |
18 | 19 | // validate transaction error |
@@ -664,6 +665,9 @@ func validateTxWorker(workCh chan *validateTxWork, resultCh chan *ValidateTxResu | ||
664 | 665 | |
665 | 666 | // ValidateTxs validates txs in async mode |
666 | 667 | func ValidateTxs(txs []*bc.Tx, block *bc.Block) []*ValidateTxResult { |
668 | + measure.Start() | |
669 | + defer measure.End() | |
670 | + | |
667 | 671 | txSize := len(txs) |
668 | 672 | validateWorkerNum := runtime.NumCPU() |
669 | 673 | //init the goroutine validate worker |