OSDN Git Service

fix the spend-tx error (#108)
authorPaladz <yzhu101@uottawa.ca>
Thu, 16 Nov 2017 15:00:49 +0000 (23:00 +0800)
committerGitHub <noreply@github.com>
Thu, 16 Nov 2017 15:00:49 +0000 (23:00 +0800)
blockchain/account/accounts.go
blockchain/account/indexer.go
blockchain/query.go
cmd/bytomcli/main.go

index 791cbe7..80a60e4 100644 (file)
@@ -43,8 +43,8 @@ func accountKey(name string) []byte {
        return []byte(accountPreFix + name)
 }
 
-func accountCPKey(name string) []byte {
-       return []byte(accountCPPreFix + name)
+func accountCPKey(hash [32]byte) []byte {
+       return append([]byte(accountCPPreFix), hash[:]...)
 }
 
 // NewManager creates a new account manager
@@ -246,6 +246,7 @@ func (m *Manager) createControlProgram(ctx context.Context, accountID string, ch
        if err != nil {
                return nil, err
        }
+
        return &controlProgram{
                AccountID:      account.ID,
                KeyIndex:       idx,
@@ -278,15 +279,15 @@ type controlProgram struct {
 }
 
 func (m *Manager) insertAccountControlProgram(ctx context.Context, progs ...*controlProgram) error {
-       var hash []byte
+       var hash [32]byte
        for _, prog := range progs {
                accountCP, err := json.Marshal(prog)
                if err != nil {
                        return err
                }
 
-               sha3pool.Sum256(hash, prog.ControlProgram)
-               m.db.Set(accountCPKey(string(hash)), accountCP)
+               sha3pool.Sum256(hash[:], prog.ControlProgram)
+               m.db.Set(accountCPKey(hash), accountCP)
        }
        return nil
 }
index 5837dc5..a653814 100644 (file)
@@ -188,10 +188,10 @@ func (m *Manager) loadAccountInfo(ctx context.Context, outs []*rawOutput) []*acc
        result := make([]*accountOutput, 0, len(outs))
        cp := controlProgram{}
 
-       var hash []byte
+       var hash [32]byte
        for s := range outsByScript {
-               sha3pool.Sum256(hash, []byte(s))
-               bytes := m.db.Get(accountCPKey(string(hash)))
+               sha3pool.Sum256(hash[:], []byte(s))
+               bytes := m.db.Get(accountCPKey(hash))
                if bytes == nil {
                        continue
                }
index 617ff38..acac7ad 100644 (file)
@@ -7,6 +7,8 @@ import (
        "math"
        "sort"
 
+       log "github.com/sirupsen/logrus"
+
        "github.com/bytom/blockchain/account"
        "github.com/bytom/blockchain/query"
        "github.com/bytom/errors"
@@ -30,9 +32,10 @@ var (
 //
 // POST /list-accounts
 func (bcr *BlockchainReactor) listAccounts(ctx context.Context, in requestQuery) interface{} {
-
-       response, _ := bcr.accounts.QueryAll(ctx)
-
+       response, err := bcr.accounts.QueryAll(ctx)
+       if err != nil {
+               log.Errorf("listAccounts: %v", err)
+       }
        return response
 
 }
index 8f3aaf5..26e3027 100644 (file)
@@ -591,22 +591,24 @@ func submitSpendTransaction(client *rpc.Client, args []string) {
                fmt.Printf("xprv:%v\n", xprvAccount1)
        } else {
                fmt.Printf("xprv unmarshal error:%v\n", xprvAccount1)
+               os.Exit(1)
        }
        // Build Transaction-Spend_account
        fmt.Printf("To build transaction:\n")
        buildReqFmt := `
                {"actions": [
+                   {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":20000000, "account_id": "%s"},
                        {"type": "spend_account", "asset_id": "%s", "amount": %s, "account_id": "%s"},
-                       {"type": "spend_account", "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "amount":20000000, "account_id": "%s"},
                        {"type": "control_account", "asset_id": "%s", "amount": %s, "account_id": "%s"}
        ]}`
 
-       buildReqStr := fmt.Sprintf(buildReqFmt, args[2], args[4], args[0], args[0], args[2], args[4], args[1])
+       buildReqStr := fmt.Sprintf(buildReqFmt, args[0], args[2], args[4], args[0], args[2], args[4], args[1])
 
        var buildReq blockchain.BuildRequest
        err = stdjson.Unmarshal([]byte(buildReqStr), &buildReq)
        if err != nil {
                fmt.Println(err)
+               os.Exit(1)
        }
 
        tpl := make([]txbuilder.Template, 1)
@@ -620,12 +622,12 @@ func submitSpendTransaction(client *rpc.Client, args []string) {
        })
        if err != nil {
                fmt.Printf("sign-transaction error. err:%v\n", err)
-               os.Exit(0)
+               os.Exit(1)
        }
 
        fmt.Printf("sign tpl:%v\n", tpl[0])
-       fmt.Printf("sign tpl's SigningInstructions:%v\n", tpl[0].SigningInstructions[0])
-       fmt.Printf("SigningInstructions's SignatureWitnesses:%v\n", tpl[0].SigningInstructions[0].SignatureWitnesses[0])
+       //fmt.Printf("sign tpl's SigningInstructions:%v\n", tpl[0].SigningInstructions[0])
+       //fmt.Printf("SigningInstructions's SignatureWitnesses:%v\n", tpl[0].SigningInstructions[0].SignatureWitnesses[0])
 
        // submit-transaction-Spend_account
        var submitResponse interface{}