OSDN Git Service

opt code fix_utxo_valid_height
authorshenao78 <shenao.78@163.com>
Thu, 13 Jun 2019 10:33:05 +0000 (18:33 +0800)
committershenao78 <shenao.78@163.com>
Thu, 13 Jun 2019 10:33:05 +0000 (18:33 +0800)
wallet/unconfirmed.go
wallet/utxo.go
wallet/utxo_test.go

index 5641565..4eec6a2 100644 (file)
@@ -39,7 +39,7 @@ func (w *Wallet) AddUnconfirmedTx(txD *protocol.TxDesc) {
                log.WithFields(log.Fields{"module": logModule, "err": err}).Error("wallet fail on saveUnconfirmedTx")
        }
 
-       utxos := txOutToUtxos(txD.Tx, txD.StatusFail, 0, 0)
+       utxos := txOutToUtxos(txD.Tx, txD.StatusFail, 0)
        utxos = w.filterAccountUtxo(utxos)
        w.AccountMgr.AddUnconfirmedUtxo(utxos)
 }
index 2fea723..aa083dc 100644 (file)
@@ -67,11 +67,7 @@ func (w *Wallet) attachUtxos(batch dbm.Batch, b *types.Block, txStatus *bc.Trans
                }
 
                //hand update the transaction output utxos
-               validHeight := uint64(0)
-               if txIndex == 0 {
-                       validHeight = b.Height + consensus.CoinbasePendingBlockNumber
-               }
-               outputUtxos := txOutToUtxos(tx, statusFail, validHeight, b.Height)
+               outputUtxos := txOutToUtxos(tx, statusFail, b.Height)
                utxos := w.filterAccountUtxo(outputUtxos)
                if err := batchSaveUtxos(utxos, batch); err != nil {
                        log.WithFields(log.Fields{"module": logModule, "err": err}).Error("attachUtxos fail on batchSaveUtxos")
@@ -225,7 +221,12 @@ func txInToUtxos(tx *types.Tx, statusFail bool) []*account.UTXO {
        return utxos
 }
 
-func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight, blockHeight uint64) []*account.UTXO {
+func txOutToUtxos(tx *types.Tx, statusFail bool, blockHeight uint64) []*account.UTXO {
+       validHeight := uint64(0)
+       if tx.Inputs[0].InputType() == types.CoinbaseInputType {
+               validHeight = blockHeight + consensus.CoinbasePendingBlockNumber
+       }
+
        utxos := []*account.UTXO{}
        for i, out := range tx.Outputs {
                entryOutput, err := tx.Entry(*tx.ResultIds[i])
@@ -247,7 +248,7 @@ func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight, blockHeight uint64
                                ControlProgram: out.ControlProgram(),
                                SourceID:       *bcOut.Source.Ref,
                                SourcePos:      bcOut.Source.Position,
-                               ValidHeight:    vaildHeight,
+                               ValidHeight:    validHeight,
                        }
 
                case *bc.VoteOutput:
@@ -256,8 +257,8 @@ func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight, blockHeight uint64
                        }
 
                        voteValidHeight := blockHeight + consensus.VotePendingBlockNumber
-                       if vaildHeight < voteValidHeight {
-                               vaildHeight = voteValidHeight
+                       if validHeight < voteValidHeight {
+                               validHeight = voteValidHeight
                        }
 
                        utxo = &account.UTXO{
@@ -267,7 +268,7 @@ func txOutToUtxos(tx *types.Tx, statusFail bool, vaildHeight, blockHeight uint64
                                ControlProgram: out.ControlProgram(),
                                SourceID:       *bcOut.Source.Ref,
                                SourcePos:      bcOut.Source.Position,
-                               ValidHeight:    vaildHeight,
+                               ValidHeight:    validHeight,
                                Vote:           bcOut.Vote,
                        }
 
index e016633..61d8e3b 100644 (file)
@@ -524,7 +524,7 @@ func TestTxOutToUtxos(t *testing.T) {
        cases := []struct {
                tx          *types.Tx
                statusFail  bool
-               vaildHeight uint64
+               blockHeight uint64
                wantUtxos   []*account.UTXO
        }{
                {
@@ -537,7 +537,7 @@ func TestTxOutToUtxos(t *testing.T) {
                                },
                        }),
                        statusFail:  false,
-                       vaildHeight: 98,
+                       blockHeight: 98,
                        wantUtxos: []*account.UTXO{
                                &account.UTXO{
                                        OutputID:       bc.Hash{V0: 1728735075694344097, V1: 884766857607786922, V2: 12293210594955921685, V3: 11109045974561998790},
@@ -546,7 +546,7 @@ func TestTxOutToUtxos(t *testing.T) {
                                        ControlProgram: []byte{0x51},
                                        SourceID:       bc.NewHash([32]byte{0xb4, 0x7e, 0x94, 0x31, 0x88, 0xfe, 0xd3, 0xe9, 0xac, 0x99, 0x7c, 0xfc, 0x99, 0x6d, 0xd7, 0x4d, 0x04, 0x10, 0x77, 0xcb, 0x1c, 0xf8, 0x95, 0x14, 0x00, 0xe3, 0x42, 0x00, 0x8d, 0x05, 0xec, 0xdc}),
                                        SourcePos:      0,
-                                       ValidHeight:    98,
+                                       ValidHeight:    198,
                                },
                        },
                },
@@ -564,7 +564,7 @@ func TestTxOutToUtxos(t *testing.T) {
                                },
                        }),
                        statusFail:  false,
-                       vaildHeight: 0,
+                       blockHeight: 0,
                        wantUtxos: []*account.UTXO{
                                &account.UTXO{
                                        OutputID:       bc.Hash{V0: 8675398163687045889, V1: 7549510466747714094, V2: 13693077838209211470, V3: 6878568403630757599},
@@ -614,7 +614,7 @@ func TestTxOutToUtxos(t *testing.T) {
                                },
                        }),
                        statusFail:  true,
-                       vaildHeight: 0,
+                       blockHeight: 0,
                        wantUtxos: []*account.UTXO{
                                &account.UTXO{
                                        OutputID:       bc.Hash{V0: 7067560744282869147, V1: 8991714784298240423, V2: 2595857933262917893, V3: 11490631006811252506},
@@ -648,7 +648,7 @@ func TestTxOutToUtxos(t *testing.T) {
                                },
                        }),
                        statusFail:  false,
-                       vaildHeight: 0,
+                       blockHeight: 0,
                        wantUtxos: []*account.UTXO{
                                &account.UTXO{
                                        OutputID:       bc.Hash{V0: 8675398163687045889, V1: 7549510466747714094, V2: 13693077838209211470, V3: 6878568403630757599},
@@ -681,7 +681,7 @@ func TestTxOutToUtxos(t *testing.T) {
                                },
                        }),
                        statusFail:  false,
-                       vaildHeight: 0,
+                       blockHeight: 0,
                        wantUtxos: []*account.UTXO{
                                &account.UTXO{
                                        OutputID:       bc.Hash{V0: 396952592194652166, V1: 9806684391645699244, V2: 484243382648745315, V3: 16988093808435014689},
@@ -720,7 +720,7 @@ func TestTxOutToUtxos(t *testing.T) {
        }
 
        for i, c := range cases {
-               if gotUtxos := txOutToUtxos(c.tx, c.statusFail, c.vaildHeight, 0); !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
+               if gotUtxos := txOutToUtxos(c.tx, c.statusFail, c.blockHeight); !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
                        t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
 
                        for j, u := range gotUtxos {