OSDN Git Service

add TestGetAccountIndex
[bytom/vapor.git] / database / account_store_test.go
index 44b3c02..8d52dcd 100644 (file)
@@ -4,8 +4,14 @@ import (
        "os"
        "testing"
 
+       "github.com/vapor/testutil"
+
+       "github.com/vapor/blockchain/signers"
+       "github.com/vapor/crypto/ed25519/chainkd"
+
        acc "github.com/vapor/account"
        dbm "github.com/vapor/database/leveldb"
+       "github.com/vapor/protocol/bc"
 )
 
 func TestDeleteAccount(t *testing.T) {
@@ -106,8 +112,8 @@ func TestDeleteAccount(t *testing.T) {
                },
        }
 
+       accountStore := NewAccountStore(testDB)
        for i, c := range cases {
-               accountStore := NewAccountStore(testDB)
                as := accountStore.InitBatch()
                // store mock accounts
                for _, a := range c.accounts {
@@ -131,50 +137,158 @@ func TestDeleteAccount(t *testing.T) {
                }
 
                for _, a := range c.want {
-                       if _, err := as.GetAccountByID(a.ID); err == acc.ErrFindAccount {
+                       if _, err := as.GetAccountByID(a.ID); err != nil {
                                t.Errorf("case %v: cann't find account, err: %v", i, err)
                        }
 
-                       if _, err := as.GetAccountByAlias(a.Alias); err == acc.ErrFindAccount {
+                       if _, err := as.GetAccountByAlias(a.Alias); err != nil {
                                t.Errorf("case %v: cann't find account, err: %v", i, err)
                        }
                }
        }
 }
 
-// func TestDeleteStandardUTXO(t *testing.T) {
-//     testDB := dbm.NewDB("testdb", "leveldb", "temp")
-//     defer func() {
-//             testDB.Close()
-//             os.RemoveAll("temp")
-//     }()
-
-//     cases := []struct {
-//             utxos      []*acc.UTXO
-//             deleteUTXO *acc.UTXO
-//             want       []*acc.UTXO
-//     }{
-//             {
-//                     utxos:      []*acc.UTXO{},
-//                     deleteUTXO: &acc.UTXO{},
-//                     want:       []*acc.UTXO{},
-//             },
-//             {
-//                     utxos: []*acc.UTXO{
-//                             &acc.UTXO{
-//                                     OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
-//                             },
-//                     },
-//                     deleteUTXO: &acc.UTXO{
-//                             OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
-//                     },
-//                     want: []*acc.UTXO{},
-//             },
-//     }
-
-//     accountStore := NewAccountStore(testDB)
-
-//     for i, c := range cases {
-//             accountStore.
-//     }
-// }
+func TestDeleteStandardUTXO(t *testing.T) {
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
+
+       cases := []struct {
+               utxos      []*acc.UTXO
+               deleteUTXO *acc.UTXO
+               want       []*acc.UTXO
+       }{
+               {
+                       utxos:      []*acc.UTXO{},
+                       deleteUTXO: &acc.UTXO{},
+                       want:       []*acc.UTXO{},
+               },
+               {
+                       utxos: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                       },
+                       deleteUTXO: &acc.UTXO{
+                               OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                       },
+                       want: []*acc.UTXO{},
+               },
+               {
+                       utxos: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                       },
+                       deleteUTXO: &acc.UTXO{},
+                       want: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                       },
+               },
+               {
+                       utxos: []*acc.UTXO{},
+                       deleteUTXO: &acc.UTXO{
+                               OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                       },
+                       want: []*acc.UTXO{},
+               },
+               {
+                       utxos: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x0e, 0x04, 0x50, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x00, 0x01, 0x02, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x02, 0x39, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                       },
+                       deleteUTXO: &acc.UTXO{
+                               OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x02, 0x39, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                       },
+                       want: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x0e, 0x04, 0x50, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x00, 0x01, 0x02, 0x35, 0x70, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               },
+                       },
+               },
+       }
+
+       accountStore := NewAccountStore(testDB)
+       for _, c := range cases {
+               as := accountStore.InitBatch()
+               // store mock utxos
+               for _, utxo := range c.utxos {
+                       if err := as.SetStandardUTXO(utxo.OutputID, utxo); err != nil {
+                               t.Fatal(err)
+                       }
+               }
+
+               // delete utxo
+               as.DeleteStandardUTXO(c.deleteUTXO.OutputID)
+               if err := as.CommitBatch(); err != nil {
+                       t.Fatal(err)
+               }
+
+               // get utxo by outputID
+               for _, utxo := range c.want {
+                       if _, err := as.GetUTXO(utxo.OutputID); err != nil {
+                               t.Fatal(err)
+                       }
+               }
+       }
+}
+
+func TestGetAccountIndex(t *testing.T) {
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
+
+       cases := []struct {
+               account      *acc.Account
+               currentIndex uint64
+               want         uint64
+       }{
+               {
+                       account: &acc.Account{
+                               Signer: &signers.Signer{
+                                       XPubs: []chainkd.XPub{
+                                               [64]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
+                                               [64]byte{0x09, 0x09, 0x09, 0x01, 0x01, 0x00, 0x04, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c, 0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c},
+                                       },
+                               },
+                       },
+                       currentIndex: uint64(0),
+                       want:         uint64(0),
+               },
+       }
+
+       accountStore := NewAccountStore(testDB)
+       for i, c := range cases {
+               as := accountStore.InitBatch()
+               as.SetAccountIndex(c.account)
+               if err := as.CommitBatch(); err != nil {
+                       t.Fatal(err)
+               }
+               gotIndex := as.GetAccountIndex(c.account.XPubs)
+               if !testutil.DeepEqual(gotIndex, c.want) {
+                       t.Errorf("case %v: got incorrect account index, got: %v, want: %v.", i, gotIndex, c.want)
+               }
+       }
+}