OSDN Git Service

fix integration
authorChengcheng Zhang <943420582@qq.com>
Wed, 29 May 2019 19:04:34 +0000 (03:04 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 29 May 2019 19:04:34 +0000 (03:04 +0800)
blockchain/pseudohsm/keycache.go
blockchain/pseudohsm/pseudohsm.go
test/integration/standard_transaction_test.go

index 92d9a36..8517815 100644 (file)
@@ -96,15 +96,20 @@ func (kc *keyCache) hasAlias(alias string) bool {
 func (kc *keyCache) add(newKey XPub) {
        fmt.Println("add start...")
        fmt.Println("add newKey:", newKey)
-       fmt.Println("add newKey type:", reflect.TypeOf(newKey))
-       fmt.Println("add newKey.XPub type:", reflect.TypeOf(newKey.XPub))
+       fmt.Println("add newKey type:", reflect.TypeOf(newKey).String())
+       fmt.Println("add newKey.XPub type:", reflect.TypeOf(newKey.XPub).String())
        kc.mu.Lock()
        defer kc.mu.Unlock()
 
+       // switch xpub := newKey.XPub.(type){
+       //      if reflect.TypeOf(xpub).
+       // }
+
        i := sort.Search(len(kc.all), func(i int) bool { return kc.all[i].File >= newKey.File })
        if i < len(kc.all) && kc.all[i] == newKey {
                return
        }
+       fmt.Println("add i:", i)
        fmt.Println("add kc.all 1:", kc.all)
        // newKey is not in the cache.
        kc.all = append(kc.all, XPub{})
@@ -112,20 +117,39 @@ func (kc *keyCache) add(newKey XPub) {
        copy(kc.all[i+1:], kc.all[i:])
        fmt.Println("add kc.all 3:", kc.all)
        kc.all[i] = newKey
+       fmt.Println("add newKey:", newKey.XPub)
+       fmt.Println("add newKey -4 type:", reflect.TypeOf(newKey.XPub).String())
        fmt.Println("add kc.all 4:", kc.all)
        kc.byPubs[newKey.XPub] = append(kc.byPubs[newKey.XPub], newKey)
        fmt.Println("add kc.byPubs:", kc.byPubs)
        fmt.Println("add kc.byPubs:", kc.byPubs[newKey.XPub])
-       fmt.Println("add kc.byPubs type:", reflect.TypeOf(kc.byPubs))
+       fmt.Println("add kc.byPubs type:", reflect.TypeOf(kc.byPubs).String())
        fmt.Println("add kc:", kc)
 }
 
 func (kc *keyCache) keys() []XPub {
+       fmt.Println("keys start...")
+       fmt.Println("keys kc:", kc)
+       for i, v := range kc.byPubs {
+               fmt.Println("keys i:", i)
+               fmt.Println("keys i type:", reflect.TypeOf(i))
+               fmt.Println("keys v:", v)
+               fmt.Println("keys v type:", reflect.TypeOf(v))
+       }
        kc.maybeReload()
        kc.mu.Lock()
        defer kc.mu.Unlock()
        cpy := make([]XPub, len(kc.all))
        copy(cpy, kc.all)
+       fmt.Println("keys end...")
+       fmt.Println("keys kc:", kc)
+       for i, v := range kc.byPubs {
+               fmt.Println("keys i:", i)
+               fmt.Println("keys i type:", reflect.TypeOf(i))
+               fmt.Println("keys v:", v)
+               fmt.Println("keys v type:", reflect.TypeOf(v))
+       }
+
        return cpy
 }
 
@@ -162,6 +186,19 @@ func (kc *keyCache) find(xpub XPub) (XPub, error) {
        // if (xpub.XPub != vcrypto.XPubKeyer{}) {
        // matches = kc.byPubs[xpub.XPub]
        // }
+       // if _, ok := xpub.XPub.(edchainkd.XPub); ok {
+       fmt.Println("find xpub.XPub:", xpub.XPub)
+       fmt.Println("find xpub.XPub type:", reflect.TypeOf(xpub.XPub))
+       matches = kc.byPubs[xpub.XPub]
+       fmt.Println("find matches:", matches)
+       // }
+
+       for i, v := range matches {
+               fmt.Println("find i:", i)
+               fmt.Println("find i type:", reflect.TypeOf(i))
+               fmt.Println("find v:", v)
+               fmt.Println("find v type:", reflect.TypeOf(v))
+       }
 
        // for i, c := range kc.byPubs {
        //      if reflect.TypeOf(i).String() == "string" {
@@ -201,9 +238,11 @@ func (kc *keyCache) find(xpub XPub) (XPub, error) {
                                return matches[i], nil
                        }
                }
-               // if (xpub.XPub == vcrypto.XPubKeyer{}) {
-               //      return XPub{}, ErrLoadKey
-               // }
+               if _, ok := xpub.XPub.(edchainkd.XPub); ok {
+                       return XPub{}, ErrLoadKey
+               }
+               // or other, e.g. sm2
+
                return XPub{}, ErrLoadKey
        }
        switch len(matches) {
@@ -230,19 +269,60 @@ func xpubToString(xpub vcrypto.XPubKeyer) (str string) {
 // reload caches addresses of existing key.
 // Callers must hold ac.mu.
 func (kc *keyCache) reload() {
+       fmt.Println("reload before scan.......")
+       fmt.Println("reload kc:", kc)
+       for i, v := range kc.byPubs {
+               fmt.Println("reload i:", i)
+               fmt.Println("reload i type:", reflect.TypeOf(i))
+               fmt.Println("reload v:", v)
+               fmt.Println("reload v type:", reflect.TypeOf(v))
+       }
+
        keys, err := kc.scan()
        if err != nil {
                log.WithFields(log.Fields{"module": logModule, "load keys error": err}).Error("can't load keys")
        }
        kc.all = keys
-       fmt.Println("reload.......")
+       fmt.Println("reload before sort.......")
+       fmt.Println("reload kc:", kc)
+       for i, v := range kc.byPubs {
+               fmt.Println("reload i:", i)
+               fmt.Println("reload i type:", reflect.TypeOf(i))
+               fmt.Println("reload v:", v)
+               fmt.Println("reload v type:", reflect.TypeOf(v))
+       }
        sort.Sort(kc.all)
        for k := range kc.byPubs {
                delete(kc.byPubs, k)
        }
+       fmt.Println("reload after sort.......")
+       fmt.Println("reload kc:", kc)
+       for i, v := range kc.byPubs {
+               fmt.Println("reload i:", i)
+               fmt.Println("reload i type:", reflect.TypeOf(i))
+               fmt.Println("reload v:", v)
+               fmt.Println("reload v type:", reflect.TypeOf(v))
+       }
        for _, k := range keys {
+               // xpub := edchainkd.XPub{}
+               switch kt := k.XPub.(type) {
+               case string:
+                       xpb, err := edchainkd.NewXPub(kt)
+                       if err != nil {
+                               panic(err)
+                       }
+                       k.XPub = *xpb
+               }
                kc.byPubs[k.XPub] = append(kc.byPubs[k.XPub], k)
        }
+       fmt.Println("reload end.......")
+       fmt.Println("reload kc:", kc)
+       for i, v := range kc.byPubs {
+               fmt.Println("reload i:", i)
+               fmt.Println("reload i type:", reflect.TypeOf(i))
+               fmt.Println("reload v:", v)
+               fmt.Println("reload v type:", reflect.TypeOf(v))
+       }
        log.WithFields(log.Fields{"module": logModule, "cache has keys:": len(kc.all)}).Debug("reloaded keys")
 }
 
index 7d3c6e2..7271ce2 100644 (file)
@@ -73,6 +73,8 @@ func (h *HSM) XCreate(alias string, auth string, language string) (*XPub, *strin
        if err != nil {
                return nil, nil, err
        }
+       fmt.Println("XCreate ...xpub:", xpub)
+       fmt.Println("XCreate ...xpub type:", reflect.TypeOf(xpub.XPub).String())
        h.cache.add(*xpub)
 
        fmt.Println("XCreate after add h.cache:", h.cache)
@@ -246,20 +248,20 @@ func (h *HSM) LoadChainKDKey(xpub vcrypto.XPubKeyer, auth string) (xprv vcrypto.
        //}
 
        fmt.Println("LoadChainKDKey h.cache:", h.cache)
-       for i, c := range h.cache.byPubs {
-               if reflect.TypeOf(i).String() == "string" {
-                       if xpb, err := edchainkd.NewXPub(reflect.ValueOf(i).String()); err != nil {
-                               panic(err)
-                       } else {
-                               h.cache.byPubs[*xpb] = c
-                               delete(h.cache.byPubs, i)
-                       }
-               }
-               fmt.Println("LoadChainKDKey i:", i)
-               fmt.Println("LoadChainKDKey i type:", reflect.TypeOf(i))
-               fmt.Println("LoadChainKDKey c:", c)
-               fmt.Println("LoadChainKDKey c type:", reflect.TypeOf(c[0]))
-       }
+       // for i, c := range h.cache.byPubs {
+       //      if reflect.TypeOf(i).String() == "string" {
+       //              if xpb, err := edchainkd.NewXPub(reflect.ValueOf(i).String()); err != nil {
+       //                      panic(err)
+       //              } else {
+       //                      h.cache.byPubs[*xpb] = c
+       //                      delete(h.cache.byPubs, i)
+       //              }
+       //      }
+       //      fmt.Println("LoadChainKDKey i:", i)
+       //      fmt.Println("LoadChainKDKey i type:", reflect.TypeOf(i))
+       //      fmt.Println("LoadChainKDKey c:", c)
+       //      fmt.Println("LoadChainKDKey c type:", reflect.TypeOf(c[0]))
+       // }
        _, xkey, err := h.loadDecryptedKey(xpub, auth)
        if err != nil {
                return xprv, ErrLoadKey
index c47bcca..ee066a3 100644 (file)
@@ -166,14 +166,14 @@ func TestP2SH(t *testing.T) {
                t.Fatal(err)
        }
 
-       xpubers := make([]vcrypto.XPubKeyer, 2)
-       xpubers[0] = xpub1.XPub
-       xpubers[1] = xpub2.XPub
+       // xpubers := make([]vcrypto.XPubKeyer, 2)
+       // xpubers[0] = xpub1.XPub
+       // xpubers[1] = xpub2.XPub
        fmt.Println("TestP2SH xpub1.XPub", xpub1.XPub)
-       // fmt.Println("TestP2SH xpub2.XPub", xpub2.XPub)
+       fmt.Println("TestP2SH xpub2.XPub", xpub2.XPub)
 
-       // testAccount, err := accountManager.Create([]vcrypto.XPubKeyer{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
-       testAccount, err := accountManager.Create(xpubers, 2, "testAccount", signers.BIP0044)
+       testAccount, err := accountManager.Create([]vcrypto.XPubKeyer{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
+       // testAccount, err := accountManager.Create(xpubers, 2, "testAccount", signers.BIP0044)
        if err != nil {
                t.Fatal(err)
        }