OSDN Git Service

update
authorChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 18:30:50 +0000 (02:30 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 18:30:50 +0000 (02:30 +0800)
account/store.go
account/utxo_keeper.go
database/account_store.go
test/mock/UTXO.go
test/utxo_keeper_test.go

index 0fb3dcc..edf2e80 100644 (file)
@@ -34,7 +34,6 @@ type AccountStorer interface {
        SetContractIndex(string, uint64)
        SetBip44ContractIndex(string, bool, uint64)
        GetUTXOs() []*UTXO
-       GetStandardUTXO(bc.Hash) []byte
-       GetContractUTXO(bc.Hash) []byte
-       SetStandardUTXO(bc.Hash, []byte)
+       GetUTXO(bc.Hash) (*UTXO, error)
+       SetStandardUTXO(bc.Hash, *UTXO) error
 }
index 41e645a..57d5d68 100644 (file)
@@ -3,7 +3,6 @@ package account
 import (
        "bytes"
        "container/list"
-       "encoding/json"
        "sort"
        "sync"
        "sync/atomic"
@@ -239,15 +238,7 @@ func (uk *utxoKeeper) findUtxo(outHash bc.Hash, useUnconfirmed bool) (*UTXO, err
        if u, ok := uk.unconfirmed[outHash]; useUnconfirmed && ok {
                return u, nil
        }
-
-       u := &UTXO{}
-       if data := uk.store.GetStandardUTXO(outHash); data != nil {
-               return u, json.Unmarshal(data, u)
-       }
-       if data := uk.store.GetContractUTXO(outHash); data != nil {
-               return u, json.Unmarshal(data, u)
-       }
-       return nil, ErrMatchUTXO
+       return uk.store.GetUTXO(outHash)
 }
 
 func (uk *utxoKeeper) optUTXOs(utxos []*UTXO, amount uint64) ([]*UTXO, uint64, uint64) {
index 22cc0b6..8326167 100644 (file)
@@ -345,21 +345,28 @@ func (store *AccountStore) GetUTXOs() []*acc.UTXO {
        return utxos
 }
 
-// GetStandardUTXO get standard utxo by id
-func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
-       return store.accountDB.Get(StandardUTXOKey(outid))
-}
-
-// GetContractUTXO get contract utxo
-func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
-       return store.accountDB.Get(ContractUTXOKey(outid))
+// GetUTXO get standard utxo by id
+func (store *AccountStore) GetUTXO(outid bc.Hash) (*acc.UTXO, error) {
+       u := new(acc.UTXO)
+       if data := store.accountDB.Get(StandardUTXOKey(outid)); data != nil {
+               return u, json.Unmarshal(data, u)
+       }
+       if data := store.accountDB.Get(ContractUTXOKey(outid)); data != nil {
+               return u, json.Unmarshal(data, u)
+       }
+       return nil, acc.ErrMatchUTXO
 }
 
 // SetStandardUTXO set standard utxo
-func (store *AccountStore) SetStandardUTXO(outputID bc.Hash, data []byte) {
+func (store *AccountStore) SetStandardUTXO(outputID bc.Hash, utxo *acc.UTXO) error {
+       data, err := json.Marshal(utxo)
+       if err != nil {
+               return err
+       }
        if store.batch == nil {
                store.accountDB.Set(StandardUTXOKey(outputID), data)
        } else {
                store.batch.Set(StandardUTXOKey(outputID), data)
        }
+       return nil
 }
index 4ec94ed..b7e1e15 100644 (file)
@@ -3,7 +3,6 @@ package mock
 import (
        "bytes"
        "container/list"
-       "encoding/json"
        "sort"
        "sync"
        "sync/atomic"
@@ -148,15 +147,7 @@ func (uk *UTXOKeeper) FindUtxo(outHash bc.Hash, useUnconfirmed bool) (*acc.UTXO,
        if u, ok := uk.Unconfirmed[outHash]; useUnconfirmed && ok {
                return u, nil
        }
-
-       u := &acc.UTXO{}
-       if data := uk.Store.GetStandardUTXO(outHash); data != nil {
-               return u, json.Unmarshal(data, u)
-       }
-       if data := uk.Store.GetContractUTXO(outHash); data != nil {
-               return u, json.Unmarshal(data, u)
-       }
-       return nil, acc.ErrMatchUTXO
+       return uk.Store.GetUTXO(outHash)
 }
 
 func (uk *UTXOKeeper) FindUtxos(accountID string, assetID *bc.AssetID, useUnconfirmed bool, vote []byte) ([]*acc.UTXO, uint64) {
index 980cd64..6f85c2f 100644 (file)
@@ -669,11 +669,9 @@ func TestFindUtxos(t *testing.T) {
 
        for i, c := range cases {
                for _, u := range c.dbUtxos {
-                       data, err := json.Marshal(u)
-                       if err != nil {
+                       if err := c.uk.Store.SetStandardUTXO(u.OutputID, u); err != nil {
                                t.Error(err)
                        }
-                       c.uk.Store.SetStandardUTXO(u.OutputID, data)
                }
 
                gotUtxos, immatureAmount := c.uk.FindUtxos("testAccount", &bc.AssetID{}, c.useUnconfirmed, c.vote)