OSDN Git Service

update
authorChengcheng Zhang <943420582@qq.com>
Wed, 3 Jul 2019 14:46:04 +0000 (22:46 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 3 Jul 2019 14:46:04 +0000 (22:46 +0800)
database/wallet_store.go
test/mock/wallet_store.go
wallet/utxo.go
wallet/utxo_test.go

index be84b3d..ff25b4d 100644 (file)
@@ -96,14 +96,16 @@ func AccountIDKey(accountID string) []byte {
 
 // StandardUTXOKey makes an account unspent outputs key to store
 func StandardUTXOKey(id bc.Hash) []byte {
-       name := id.String()
-       return append(UTXOPrefix, []byte(name)...)
+       // name := id.String()
+       // return append(UTXOPrefix, []byte(name)...)
+       return append(UTXOPrefix, id.Bytes()...)
 }
 
 // ContractUTXOKey makes a smart contract unspent outputs key to store
 func ContractUTXOKey(id bc.Hash) []byte {
-       name := id.String()
-       return append(SUTXOPrefix, []byte(name)...)
+       // name := id.String()
+       // return append(SUTXOPrefix, []byte(name)...)
+       return append(SUTXOPrefix, id.Bytes()...)
 }
 
 func calcDeleteKey(blockHeight uint64) []byte {
@@ -362,6 +364,7 @@ func (store *WalletStore) GetWalletInfo() []byte {
 
 // ListAccountUTXOs get all account unspent outputs
 func (store *WalletStore) ListAccountUTXOs(key string) ([]*acc.UTXO, error) {
+       fmt.Println("ListAccountUTXOs []byte(key):", []byte(key))
        accountUtxoIter := store.walletDB.IteratorPrefix([]byte(key))
        defer accountUtxoIter.Release()
 
index 9b53340..69d62f0 100644 (file)
@@ -18,7 +18,6 @@ import (
 )
 
 const (
-       colon            = 0x3a
        utxoPrefix  byte = iota //UTXOPrefix is StandardUTXOKey prefix
        sutxoPrefix             //SUTXOPrefix is ContractUTXOKey prefix
        contractPrefix
@@ -38,8 +37,9 @@ const (
 
 // leveldb key prefix
 var (
-       UTXOPrefix  = []byte{utxoPrefix, colon}
-       SUTXOPrefix = []byte{sutxoPrefix, colon}
+       colon       byte = 0x3a
+       UTXOPrefix       = []byte{utxoPrefix, colon}
+       SUTXOPrefix      = []byte{sutxoPrefix, colon}
        // ContractPrefix = []byte{contractPrefix, contractPrefix, colon}
        ContractPrefix      = "Contract:"
        ContractIndexPrefix = []byte{contractIndexPrefix, colon}
@@ -97,14 +97,16 @@ func AccountIDKey(accountID string) []byte {
 
 // StandardUTXOKey makes an account unspent outputs key to store
 func StandardUTXOKey(id bc.Hash) []byte {
-       name := id.String()
-       return append(UTXOPrefix, []byte(name)...)
+       // name := id.String()
+       // return append(UTXOPrefix, []byte(name)...)
+       return append(UTXOPrefix, id.Bytes()...)
 }
 
 // ContractUTXOKey makes a smart contract unspent outputs key to store
 func ContractUTXOKey(id bc.Hash) []byte {
-       name := id.String()
-       return append(SUTXOPrefix, []byte(name)...)
+       //      name := id.String()
+       //      return append(SUTXOPrefix, []byte(name)...)
+       return append(SUTXOPrefix, id.Bytes()...)
 }
 
 func calcDeleteKey(blockHeight uint64) []byte {
@@ -363,6 +365,7 @@ func (store *MockWalletStore) GetWalletInfo() []byte {
 
 // ListAccountUTXOs get all account unspent outputs
 func (store *MockWalletStore) ListAccountUTXOs(key string) ([]*acc.UTXO, error) {
+       fmt.Println("ListAccountUTXOs []byte(key):", []byte(key))
        accountUtxoIter := store.walletDB.IteratorPrefix([]byte(key))
        defer accountUtxoIter.Release()
 
index 45210f5..ed17049 100644 (file)
@@ -1,6 +1,8 @@
 package wallet
 
 import (
+       "encoding/hex"
+
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/account"
@@ -23,8 +25,10 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma
        if unconfirmed {
                accountUtxos = w.AccountMgr.ListUnconfirmedUtxo(accountID, isSmartContract)
        }
+       idBytes, _ := hex.DecodeString(id)
 
-       confirmedUTXOs, err := w.store.ListAccountUTXOs(string(prefix) + id)
+       // confirmedUTXOs, err := w.store.ListAccountUTXOs(string(prefix) + id)
+       confirmedUTXOs, err := w.store.ListAccountUTXOs(string(append(prefix, idBytes...)))
        if err != nil {
                log.WithFields(log.Fields{"module": logModule, "err": err}).Error("GetAccountUtxos fail.")
        }
index 616d7ee..30c55c8 100644 (file)
@@ -95,18 +95,31 @@ func TestGetAccountUtxos(t *testing.T) {
                },
                {
                        dbUtxos: map[string]*account.UTXO{
-                               string(database.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
+                               string(mock.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
                                        OutputID: bc.Hash{V0: 1},
                                },
-                               string(database.StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{
+                               string(mock.StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{
                                        OutputID: bc.Hash{V0: 1, V1: 2},
                                },
-                               string(database.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
+                               string(mock.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
                                        OutputID: bc.Hash{V0: 2},
                                },
-                               string(database.StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{
+                               string(mock.StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{
                                        OutputID: bc.Hash{V0: 2, V1: 2},
                                },
+
+                               // hex.EncodeToString(mock.StandardUTXOKey(bc.Hash{V0: 1})): &account.UTXO{
+                               //      OutputID: bc.Hash{V0: 1},
+                               // },
+                               // hex.EncodeToString(mock.StandardUTXOKey(bc.Hash{V0: 1, V1: 2})): &account.UTXO{
+                               //      OutputID: bc.Hash{V0: 1, V1: 2},
+                               // },
+                               // hex.EncodeToString(mock.StandardUTXOKey(bc.Hash{V0: 2})): &account.UTXO{
+                               //      OutputID: bc.Hash{V0: 2},
+                               // },
+                               // hex.EncodeToString(mock.StandardUTXOKey(bc.Hash{V0: 2, V1: 2})): &account.UTXO{
+                               //      OutputID: bc.Hash{V0: 2, V1: 2},
+                               // },
                        },
                        unconfirmedUtxos: []*account.UTXO{
                                &account.UTXO{
@@ -195,6 +208,10 @@ func TestGetAccountUtxos(t *testing.T) {
                        if err != nil {
                                t.Error(err)
                        }
+                       fmt.Println("k:", []byte(k))
+                       // newKey, _ := hex.DecodeString(k)
+                       // fmt.Println("newkey:", newKey)
+                       // testDB.Set([]byte(k), data)
                        testDB.Set([]byte(k), data)
                }
 
@@ -207,6 +224,8 @@ func TestGetAccountUtxos(t *testing.T) {
                }
 
                for k := range c.dbUtxos {
+                       // newKey, _ := hex.DecodeString(k)
+                       // testDB.Delete([]byte(k))
                        testDB.Delete([]byte(k))
                }
        }