• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision7667ab494e054d87261e98e9a77256715255088c (tree)
Time2020-02-24 19:29:10
Authoripqhjjybj <ipqhjjybj@qq.c...>
Commiteripqhjjybj

Log Message

update test

Change Summary

Incremental Difference

--- a/consensus/general.go
+++ b/consensus/general.go
@@ -102,7 +102,7 @@ type Params struct {
102102 // ProducerSubsidys defines the producer subsidy by block height
103103 ProducerSubsidys []ProducerSubsidy
104104
105- SoftForkPoint map[uint64]uint64
105+ SoftForkPoint map[uint64]uint64
106106 MovStartHeight uint64
107107 }
108108
--- a/database/utxo_view.go
+++ b/database/utxo_view.go
@@ -1,12 +1,14 @@
11 package database
22
33 import (
4- "github.com/golang/protobuf/proto"
4+ "fmt"
5+
56 dbm "github.com/bytom/vapor/database/leveldb"
67 "github.com/bytom/vapor/database/storage"
78 "github.com/bytom/vapor/errors"
89 "github.com/bytom/vapor/protocol/bc"
910 "github.com/bytom/vapor/protocol/state"
11+ "github.com/golang/protobuf/proto"
1012 )
1113
1214 const utxoPreFix = "UT:"
@@ -16,18 +18,28 @@ func calcUtxoKey(hash *bc.Hash) []byte {
1618 }
1719
1820 func getTransactionsUtxo(db dbm.DB, view *state.UtxoViewpoint, txs []*bc.Tx) error {
21+ fmt.Println("[very important]")
1922 for _, tx := range txs {
23+ fmt.Println("[very important] getTransactionsUtxo tx.string()", tx.String())
2024 for _, prevout := range tx.SpentOutputIDs {
25+ fmt.Println("[very important] getTransactionsUtxo prevout", prevout.String())
2126 if view.HasUtxo(&prevout) {
2227 continue
2328 }
2429
30+ utxoEntry, err := GetUtxo(db, &prevout)
31+ fmt.Println("[why really important] prevout", prevout.String())
32+ fmt.Println("[why really important] utxoEntry:", utxoEntry.String(), "err", err)
33+
34+ fmt.Println("calcKey:", calcUtxoKey(&prevout))
2535 data := db.Get(calcUtxoKey(&prevout))
2636 if data == nil {
37+ fmt.Println("why data is not here")
2738 continue
2839 }
2940
3041 var utxo storage.UtxoEntry
42+ fmt.Println("Unmarshal data", data)
3143 if err := proto.Unmarshal(data, &utxo); err != nil {
3244 return errors.Wrap(err, "unmarshaling utxo entry")
3345 }
@@ -36,6 +48,7 @@ func getTransactionsUtxo(db dbm.DB, view *state.UtxoViewpoint, txs []*bc.Tx) err
3648 }
3749
3850 for _, prevout := range tx.MainchainOutputIDs {
51+ fmt.Println("MainchainOutputIDs preout", prevout.String())
3952 if view.HasUtxo(&prevout) {
4053 continue
4154 }
@@ -70,14 +83,22 @@ func getUtxo(db dbm.DB, hash *bc.Hash) (*storage.UtxoEntry, error) {
7083 return &utxo, nil
7184 }
7285
86+func GetUtxo(db dbm.DB, hash *bc.Hash) (*storage.UtxoEntry, error) {
87+ return getUtxo(db, hash)
88+}
89+
7390 func saveUtxoView(batch dbm.Batch, view *state.UtxoViewpoint) error {
91+ fmt.Println("[important] now go to saveUtxoView len entries:", len(view.Entries))
7492 for key, entry := range view.Entries {
93+ fmt.Println("[important] saveUtxoView key:", key.String(), " entry:", entry.String())
7594 if entry.Type == storage.CrosschainUTXOType && !entry.Spent {
95+ fmt.Println("[important] delete key:", calcUtxoKey(&key))
7696 batch.Delete(calcUtxoKey(&key))
7797 continue
7898 }
7999
80100 if entry.Type == storage.NormalUTXOType && entry.Spent {
101+ fmt.Println("[important] delete key:", calcUtxoKey(&key))
81102 batch.Delete(calcUtxoKey(&key))
82103 continue
83104 }
@@ -86,6 +107,7 @@ func saveUtxoView(batch dbm.Batch, view *state.UtxoViewpoint) error {
86107 if err != nil {
87108 return errors.Wrap(err, "marshaling utxo entry")
88109 }
110+ fmt.Println("[important set] calcUtxoKey(&key)", calcUtxoKey(&key))
89111 batch.Set(calcUtxoKey(&key), b)
90112 }
91113 return nil
--- a/protocol/block.go
+++ b/protocol/block.go
@@ -89,18 +89,20 @@ func (c *Chain) calcReorganizeChain(beginAttach *types.BlockHeader, beginDetach
8989 }
9090
9191 func (c *Chain) connectBlock(block *types.Block) (err error) {
92+ prevOut := bc.Hash{V0: 13401662211606668461, V1: 16715925272045549698, V2: 14986839123336726356, V3: 599535074188477126}
93+ entry, err := c.store.GetUtxo(&prevOut)
94+ fmt.Println("connectBlock entry:", entry, "err:", err)
95+
9296 bcBlock := types.MapBlock(block)
9397 if bcBlock.TransactionStatus, err = c.store.GetTransactionStatus(&bcBlock.ID); err != nil {
9498 return err
9599 }
96100
97- fmt.Println("connectBlock abcd")
98101 utxoView := state.NewUtxoViewpoint()
99102 if err := c.store.GetTransactionsUtxo(utxoView, bcBlock.Transactions); err != nil {
100103 return err
101104 }
102105
103- fmt.Println("utxoView.ApplyBlock")
104106 //fmt.Println("bcBlock.TransactionStatus", bcBlock.TransactionStatus)
105107 if err := utxoView.ApplyBlock(bcBlock, bcBlock.TransactionStatus); err != nil {
106108 return err
@@ -388,23 +390,19 @@ func (c *Chain) saveBlock(block *types.Block) error {
388390 }
389391
390392 bcBlock := types.MapBlock(block)
391- fmt.Println("validation.ValidateBlock")
392393 if err := validation.ValidateBlock(bcBlock, parent, rewards); err != nil {
393394 return errors.Sub(ErrBadBlock, err)
394395 }
395396
396- fmt.Println("block.go _, p := range c.subProtocols")
397397 for _, p := range c.subProtocols {
398398 if err := p.ValidateBlock(block, bcBlock.TransactionStatus.GetVerifyStatus()); err != nil {
399399 return errors.Wrap(err, "sub protocol save block")
400400 }
401401 }
402- fmt.Println("end validation")
403402
404403 if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
405404 return err
406405 }
407- fmt.Println("end save block")
408406
409407 c.orphanManage.Delete(&bcBlock.ID)
410408 return nil
@@ -463,6 +461,11 @@ func (c *Chain) blockProcesser() {
463461
464462 // ProcessBlock is the entry for handle block insert
465463 func (c *Chain) processBlock(block *types.Block) (bool, error) {
464+ fmt.Println("[processBlock]")
465+ prevOut := bc.Hash{V0: 13401662211606668461, V1: 16715925272045549698, V2: 14986839123336726356, V3: 599535074188477126}
466+ entry, err := c.store.GetUtxo(&prevOut)
467+ fmt.Println("processBlock entry:", entry, "err:", err)
468+
466469 blockHash := block.Hash()
467470 if c.BlockExist(&blockHash) {
468471 log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Debug("block has been processed")
@@ -480,13 +483,9 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) {
480483 return false, err
481484 }
482485
483- fmt.Println("saveBlock")
484-
485486 bestBlock := c.saveSubBlock(block)
486487 bestBlockHeader := &bestBlock.BlockHeader
487488
488- fmt.Println("end save sub block")
489-
490489 c.cond.L.Lock()
491490 defer c.cond.L.Unlock()
492491 if bestBlockHeader.PreviousBlockHash == c.bestBlockHeader.Hash() {
@@ -495,7 +494,6 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) {
495494 return false, c.connectBlock(bestBlock)
496495 }
497496
498- fmt.Println("doing")
499497 if bestBlockHeader.Height > c.bestBlockHeader.Height {
500498 log.WithFields(log.Fields{"module": logModule}).Debug("start to reorganize chain")
501499 return false, c.reorganizeChain(bestBlockHeader)
--- a/protocol/state/utxo_view.go
+++ b/protocol/state/utxo_view.go
@@ -22,12 +22,10 @@ func NewUtxoViewpoint() *UtxoViewpoint {
2222 }
2323
2424 func (view *UtxoViewpoint) ApplyTransaction(block *bc.Block, tx *bc.Tx, statusFail bool) error {
25- fmt.Println("UtxoViewpoint ApplyTransaction")
2625 if err := view.applyCrossChainUtxo(block, tx); err != nil {
2726 return err
2827 }
2928
30- fmt.Println("view.applySpendUtxo")
3129 if err := view.applySpendUtxo(block, tx, statusFail); err != nil {
3230 return err
3331 }
@@ -42,7 +40,6 @@ func (view *UtxoViewpoint) ApplyBlock(block *bc.Block, txStatus *bc.TransactionS
4240 return err
4341 }
4442
45- fmt.Println("UtxoViewpoint ApplyBlock", i, tx)
4643 if err := view.ApplyTransaction(block, tx, statusFail); err != nil {
4744 return err
4845 }
@@ -159,8 +156,8 @@ func (view *UtxoViewpoint) applySpendUtxo(block *bc.Block, tx *bc.Tx, statusFail
159156 continue
160157 }
161158
162- fmt.Println("assetID:", assetID)
163- fmt.Println("prevout", prevout)
159+ fmt.Println("[fuck error ]assetID:", assetID.String())
160+ fmt.Println("[fuck error ]prevout", prevout, prevout.String())
164161 entry, ok := view.Entries[prevout]
165162 if !ok {
166163 return errors.New("fail to find utxo entry")
--- a/protocol/validation/block.go
+++ b/protocol/validation/block.go
@@ -3,7 +3,6 @@ package validation
33 import (
44 "bytes"
55 "encoding/hex"
6- "fmt"
76 "time"
87
98 log "github.com/sirupsen/logrus"
@@ -123,19 +122,15 @@ func ValidateBlock(b *bc.Block, parent *types.BlockHeader, rewards []state.Coinb
123122 return errors.WithDetailf(errMismatchedMerkleRoot, "transaction id merkle root. compute: %v, given: %v", txMerkleRoot, *b.TransactionsRoot)
124123 }
125124
126- fmt.Println("b.TransactionStatus.VerifyStatus", b.TransactionStatus.VerifyStatus, len(b.TransactionStatus.VerifyStatus))
127- for i := 0; i < len(b.TransactionStatus.VerifyStatus); i++ {
128- fmt.Println("txStatus", i, b.TransactionStatus.VerifyStatus[i])
129- }
125+ // fmt.Println("b.TransactionStatus.VerifyStatus", b.TransactionStatus.VerifyStatus, len(b.TransactionStatus.VerifyStatus))
126+ // for i := 0; i < len(b.TransactionStatus.VerifyStatus); i++ {
127+ // fmt.Println("txStatus", i, b.TransactionStatus.VerifyStatus[i])
128+ // }
130129 txStatusHash, err := types.TxStatusMerkleRoot(b.TransactionStatus.VerifyStatus)
131130 if err != nil {
132131 return errors.Wrap(err, "computing transaction status merkle root")
133132 }
134133 if txStatusHash != *b.TransactionStatusHash {
135- fmt.Println("!!! here !!")
136- fmt.Println("txStatusHash", txStatusHash)
137- fmt.Println("TransactionStatusHash", b.TransactionStatusHash)
138- fmt.Println("TransactionStatusHash", *b.TransactionStatusHash)
139134 return errors.WithDetailf(errMismatchedMerkleRoot, "transaction status merkle root. compute: %v, given: %v", txStatusHash, *b.TransactionStatusHash)
140135 }
141136
--- a/protocol/validation/tx.go
+++ b/protocol/validation/tx.go
@@ -597,11 +597,9 @@ func applySoftFork001(vs *validationState, err error) {
597597 // ValidateTx validates a transaction.
598598 func ValidateTx(tx *bc.Tx, block *bc.Block) (*GasState, error) {
599599 gasStatus := &GasState{GasValid: false}
600- fmt.Println("block.Version, tx.Version", block.Version, tx.Version)
601600 if block.Version == 1 && tx.Version != 1 {
602601 return gasStatus, errors.WithDetailf(ErrTxVersion, "block version %d, transaction version %d", block.Version, tx.Version)
603602 }
604- fmt.Println("???")
605603 if tx.SerializedSize == 0 {
606604 return gasStatus, ErrWrongTransactionSize
607605 }
--- a/test/rollback_test.go
+++ b/test/rollback_test.go
@@ -20,7 +20,6 @@ import (
2020 dbm "github.com/bytom/vapor/database/leveldb"
2121 "github.com/bytom/vapor/errors"
2222 "github.com/bytom/vapor/event"
23- "github.com/bytom/vapor/proposal"
2423 "github.com/bytom/vapor/protocol"
2524 "github.com/bytom/vapor/protocol/bc"
2625 "github.com/bytom/vapor/protocol/bc/types"
@@ -152,6 +151,10 @@ func getConsensusResult(c *protocol.Chain, store *database.Store, seq uint64, bl
152151 }
153152
154153 func TestRollback(t *testing.T) {
154+ fmt.Println("\n\n\n\n")
155+ fmt.Println("consensus.ActiveNetParams.name", consensus.ActiveNetParams.Name)
156+ consensus.ActiveNetParams.VotePendingBlockNumber = 0
157+ fmt.Println("consensus.ActiveNetParams.VotePendingBlockNumber:", consensus.ActiveNetParams.VotePendingBlockNumber)
155158 // genesisBlock := config.GenesisBlock()
156159 testXpub, _ := hex.DecodeString("4d6f710dae8094c111450ca20e054c3aed59dfcb2d29543c29901a5903755e69273c2d8f2642a7baf94ebac88f1625af9f5eaf3b13a90de27eec3de78b9fb9ca")
157160
@@ -206,7 +209,7 @@ func TestRollback(t *testing.T) {
206209 {
207210 desc: "first round block",
208211 startRunNum: 1,
209- runBlockNum: 0,
212+ runBlockNum: 1,
210213 },
211214 // {
212215 // desc: "second add blocks",
@@ -246,23 +249,11 @@ func TestRollback(t *testing.T) {
246249 t.Fatal(err)
247250 }
248251
249- block, err := NewBlockTemplate(chain, accounts, timeStamp, warnDuration, criticalDuration)
252+ block, err := NewBlockTemplate(chain, db, accounts, timeStamp, warnDuration, criticalDuration)
250253 if err != nil {
251254 t.Fatal(err)
252255 }
253256
254- // bcBlock := types.MapBlock(block)
255- // verifyStatus := bcBlock.TransactionStatus.VerifyStatus
256- // fmt.Println("why", verifyStatus)
257- // fmt.Println("rollback_test, verifyStatus", len(verifyStatus))
258- // for index := 0; index < len(verifyStatus); index++ {
259- // fmt.Println("verifyStatus[", index, "]", verifyStatus[index])
260- // }
261- // fmt.Println("i:", i, len(block.Transactions))
262-
263- // sleep(1)
264- // block.Transactions = append(block.Transactions, transactions...)
265-
266257 if _, err := chain.ProcessBlock(block); err != nil {
267258 t.Fatal(err)
268259 }
@@ -297,7 +288,7 @@ func TestRollback(t *testing.T) {
297288 }
298289
299290 //block, err := proposal.NewBlockTemplate(chain, txPool, nil, timeStamp)
300- block, err := proposal.NewBlockTemplate(chain, accounts, timeStamp, warnDuration, criticalDuration)
291+ block, err := NewBlockTemplate(chain, db, accounts, timeStamp, warnDuration, criticalDuration)
301292 //block.Transactions = transactions
302293
303294 // bcBlock := types.MapBlock(block)
--- a/test/rollback_util.go
+++ b/test/rollback_util.go
@@ -12,6 +12,9 @@ import (
1212 "github.com/bytom/vapor/account"
1313 "github.com/bytom/vapor/blockchain/txbuilder"
1414 "github.com/bytom/vapor/consensus"
15+ "github.com/bytom/vapor/database"
16+ dbm "github.com/bytom/vapor/database/leveldb"
17+ "github.com/bytom/vapor/database/storage"
1518 "github.com/bytom/vapor/errors"
1619 "github.com/bytom/vapor/protocol"
1720 "github.com/bytom/vapor/protocol/bc"
@@ -37,13 +40,15 @@ func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
3740 func (a byTime) Less(i, j int) bool { return a[i].Added.Before(a[j].Added) }
3841
3942 // NewBlockTemplate returns a new block template that is ready to be solved
40-func NewBlockTemplate(chain *protocol.Chain, accountManager *account.Manager, timestamp uint64, warnDuration, criticalDuration time.Duration) (*types.Block, error) {
41- builder := newBlockBuilder(chain, accountManager, timestamp, warnDuration, criticalDuration)
43+func NewBlockTemplate(chain *protocol.Chain, db dbm.DB, accountManager *account.Manager, timestamp uint64, warnDuration, criticalDuration time.Duration) (*types.Block, error) {
44+ fmt.Println("NewBlockTemplate")
45+ builder := newBlockBuilder(chain, db, accountManager, timestamp, warnDuration, criticalDuration)
4246 return builder.build()
4347 }
4448
4549 type blockBuilder struct {
4650 chain *protocol.Chain
51+ db dbm.DB
4752 accountManager *account.Manager
4853
4954 block *types.Block
@@ -56,7 +61,7 @@ type blockBuilder struct {
5661 gasLeft int64
5762 }
5863
59-func newBlockBuilder(chain *protocol.Chain, accountManager *account.Manager, timestamp uint64, warnDuration, criticalDuration time.Duration) *blockBuilder {
64+func newBlockBuilder(chain *protocol.Chain, db dbm.DB, accountManager *account.Manager, timestamp uint64, warnDuration, criticalDuration time.Duration) *blockBuilder {
6065 preBlockHeader := chain.BestBlockHeader()
6166 block := &types.Block{
6267 BlockHeader: types.BlockHeader{
@@ -71,6 +76,7 @@ func newBlockBuilder(chain *protocol.Chain, accountManager *account.Manager, tim
7176
7277 builder := &blockBuilder{
7378 chain: chain,
79+ db: db,
7480 accountManager: accountManager,
7581 block: block,
7682 txStatus: bc.NewTransactionStatus(),
@@ -105,6 +111,7 @@ func (b *blockBuilder) applyCoinbaseTransaction() error {
105111
106112 func (b *blockBuilder) applyVoteTransaction() error {
107113 tx, err := b.createVoteTx(b.accountManager, b.block.Height)
114+
108115 if err != nil {
109116 return errors.Wrap(err, "fail on create vote tx")
110117 }
@@ -120,11 +127,32 @@ func (b *blockBuilder) applyVoteTransaction() error {
120127 }
121128 b.gasLeft -= gasState.GasUsed
122129
130+ batch := b.db.NewBatch()
131+ view := &state.UtxoViewpoint{
132+ Entries: map[bc.Hash]*storage.UtxoEntry{
133+ tx.Tx.SpentOutputIDs[0]: &storage.UtxoEntry{Type: storage.VoteUTXOType, BlockHeight: 0, Spent: false},
134+ },
135+ }
136+ //fmt.Println("[important] applyVoteTransaction go to save SpentOutputIDs", tx.Tx.SpentOutputIDs[0].String())
137+ if err := database.SaveUtxoView(batch, view); err != nil {
138+ return err
139+ }
140+ batch.Write()
141+ //fmt.Println("[important] batch write")
142+
143+ // prevout := tx.Tx.SpentOutputIDs[0]
144+ // utxoEntry, err := database.GetUtxo(b.db, &prevout)
145+ // fmt.Println("[important] utxoEntry:", utxoEntry.String(), "err", err)
146+ // txs := []*types.Tx{}
147+ // txs = append(txs, tx)
148+ // if err := b.applyTransactions(txs, timeoutWarn); err != nil {
149+ // return err
150+ // }
151+
123152 return err
124153 }
125154
126155 func (b *blockBuilder) applyTransactions(txs []*types.Tx, timeoutStatus uint8) error {
127- fmt.Println("test.applyTransactions")
128156 tempTxs := []*types.Tx{}
129157 for i := 0; i < len(txs); i++ {
130158 if tempTxs = append(tempTxs, txs[i]); len(tempTxs) < batchApplyNum && i != len(txs)-1 {
@@ -236,11 +264,6 @@ func (b *blockBuilder) calcBlockCommitment() (err error) {
236264 }
237265
238266 b.block.BlockHeader.BlockCommitment.TransactionStatusHash, err = types.TxStatusMerkleRoot(b.txStatus.VerifyStatus)
239- for i := 0; i < len(b.txStatus.VerifyStatus); i++ {
240- fmt.Println("txStatus", i, b.txStatus.VerifyStatus[i])
241- }
242- fmt.Println("b.txStatus.VerifyStatus", b.txStatus.VerifyStatus, len(b.txStatus.VerifyStatus))
243- fmt.Println("b.block.BlockHeader.BlockCommitment.TransactionStatusHash", b.block.BlockHeader.BlockCommitment.TransactionStatusHash)
244267 return err
245268 }
246269
@@ -282,11 +305,17 @@ func (b *blockBuilder) createVoteTx(accountManager *account.Manager, blockHeight
282305 }
283306
284307 builder := txbuilder.NewBuilder(time.Now())
285- if err = builder.AddInput(types.NewVetoInput(nil, bc.NewHash([32]byte{0xff}), *consensus.BTMAssetID, 100000000, 0, []byte{0x51}, testXpub), &txbuilder.SigningInstruction{}); err != nil {
308+ // if err = builder.AddInput(types.NewVetoInput(nil, bc.NewHash([32]byte{0xff}), *consensus.BTMAssetID, 100000000, 0, []byte{0x51}, testXpub), &txbuilder.SigningInstruction{}); err != nil {
309+ // return nil, err
310+ // }
311+ // if err = builder.AddOutput(types.NewIntraChainOutput(*consensus.BTMAssetID, 100000000, script)); err != nil {
312+ // return nil, err
313+ // }
314+
315+ if err := builder.AddInput(types.NewSpendInput(nil, bc.NewHash([32]byte{0, 1}), *consensus.BTMAssetID, 100000000, 0, script), &txbuilder.SigningInstruction{}); err != nil {
286316 return nil, err
287317 }
288-
289- if err = builder.AddOutput(types.NewIntraChainOutput(*consensus.BTMAssetID, 100000000, script)); err != nil {
318+ if err := builder.AddOutput(types.NewVoteOutput(*consensus.BTMAssetID, 100000000, script, testXpub)); err != nil {
290319 return nil, err
291320 }
292321
@@ -305,6 +334,13 @@ func (b *blockBuilder) createVoteTx(accountManager *account.Manager, blockHeight
305334 TxData: *txData,
306335 Tx: types.MapTx(txData),
307336 }
337+
338+ fmt.Println("tx.Tx.String", tx.Tx.String())
339+ //a := tx.Tx.SpentOutputIDs
340+ for _, prevout := range tx.Tx.SpentOutputIDs {
341+ fmt.Println("createVoteTx SpentOutputIDs", prevout.String())
342+ }
343+
308344 return tx, nil
309345 }
310346
@@ -333,7 +369,7 @@ func createCoinbaseTxByReward(accountManager *account.Manager, blockHeight uint6
333369 script, err = accountManager.GetCoinbaseControlProgram()
334370 arbitrary = append(arbitrary, accountManager.GetCoinbaseArbitrary()...)
335371 }
336- fmt.Println("blockHeight", blockHeight, "script:", script)
372+
337373 if err != nil {
338374 return nil, err
339375 }
Show on old repository browser