Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from Bytom/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gguoss authored Oct 21, 2017
2 parents 94fa4ba + d331652 commit 1243faa
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 93 deletions.
47 changes: 24 additions & 23 deletions blockchain/account/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"context"
"encoding/json"

"github.com/bytom/blockchain/query"
"github.com/bytom/blockchain/signers"
"github.com/bytom/crypto/sha3pool"
"github.com/bytom/errors"
"github.com/bytom/protocol/bc"
"github.com/bytom/crypto/sha3pool"
"github.com/bytom/blockchain/query"
"github.com/bytom/protocol/bc/legacy"
"github.com/bytom/blockchain/signers"

chainjson "github.com/bytom/encoding/json"
)
Expand Down Expand Up @@ -128,14 +128,13 @@ func (m *Manager) ProcessBlocks(ctx context.Context) {
func (m *Manager) deleteSpentOutputs(ctx context.Context, b *legacy.Block) error {
// Delete consumed account UTXOs.
delOutputIDs := prevoutDBKeys(b.Transactions...)
for _,delOutputID := range delOutputIDs{
m.pinStore.DB.Delete(json.RawMessage("acu"+string(delOutputID.Bytes())))
for _, delOutputID := range delOutputIDs {
m.pinStore.DB.Delete(json.RawMessage("acu" + string(delOutputID.Bytes())))
}

return errors.Wrap(nil, "deleting spent account utxos")
}


func (m *Manager) indexAccountUTXOs(ctx context.Context, b *legacy.Block) error {
// Upsert any UTXOs belonging to accounts managed by this Core.
outs := make([]*rawOutput, 0, len(b.Transactions))
Expand Down Expand Up @@ -167,7 +166,6 @@ func (m *Manager) indexAccountUTXOs(ctx context.Context, b *legacy.Block) error
return errors.Wrap(err, "upserting confirmed account utxos")
}


func prevoutDBKeys(txs ...*legacy.Tx) (outputIDs []bc.Hash) {
for _, tx := range txs {
for _, inpID := range tx.Tx.InputIDs {
Expand All @@ -179,11 +177,10 @@ func prevoutDBKeys(txs ...*legacy.Tx) (outputIDs []bc.Hash) {
return
}


// loadAccountInfo turns a set of output IDs into a set of
// outputs by adding account annotations. Outputs that can't be
// annotated are excluded from the result.
func (m *Manager) loadAccountInfo(ctx context.Context, outs []*rawOutput) ([]*accountOutput) {
func (m *Manager) loadAccountInfo(ctx context.Context, outs []*rawOutput) []*accountOutput {
outsByScript := make(map[string][]*rawOutput, len(outs))
for _, out := range outs {
scriptStr := string(out.ControlProgram)
Expand All @@ -202,7 +199,7 @@ func (m *Manager) loadAccountInfo(ctx context.Context, outs []*rawOutput) ([]*ac
var b32 [32]byte
for s := range outsByScript {
sha3pool.Sum256(b32[:], []byte(s))
bytes := m.db.Get(json.RawMessage("acp"+string(b32[:])))
bytes := m.db.Get(json.RawMessage("acp" + string(b32[:])))
if bytes == nil {
continue
}
Expand All @@ -212,6 +209,12 @@ func (m *Manager) loadAccountInfo(ctx context.Context, outs []*rawOutput) ([]*ac
continue
}

//filte the accounts which exists in accountdb with wallet enabled
isExist := m.db.Get(json.RawMessage(cp.AccountID))
if isExist == nil {
continue
}

for _, out := range outsByScript[s] {
newOut := &accountOutput{
rawOutput: *out,
Expand All @@ -226,7 +229,6 @@ func (m *Manager) loadAccountInfo(ctx context.Context, outs []*rawOutput) ([]*ac
return result
}


// upsertConfirmedAccountOutputs records the account data for confirmed utxos.
// If the account utxo already exists (because it's from a local tx), the
// block confirmation data will in the row will be updated.
Expand All @@ -237,17 +239,17 @@ func (m *Manager) upsertConfirmedAccountOutputs(ctx context.Context,

var au *AccountUTXOs
for _, out := range outs {
au = &AccountUTXOs{OutputID: out.OutputID.Bytes(),
AssetID: out.AssetId.Bytes(),
Amount: int64(out.Amount),
AccountID: out.AccountID,
CpIndex: int64(out.keyIndex),
Program: out.ControlProgram,
Confirmed: int64(block.Height),
SourceID: out.sourceID.Bytes(),
SourcePos: int64(out.sourcePos),
RefData: out.refData.Bytes(),
Change: out.change}
au = &AccountUTXOs{OutputID: out.OutputID.Bytes(),
AssetID: out.AssetId.Bytes(),
Amount: int64(out.Amount),
AccountID: out.AccountID,
CpIndex: int64(out.keyIndex),
Program: out.ControlProgram,
Confirmed: int64(block.Height),
SourceID: out.sourceID.Bytes(),
SourcePos: int64(out.sourcePos),
RefData: out.refData.Bytes(),
Change: out.change}

accountutxo, err := json.Marshal(au)
if err != nil {
Expand All @@ -262,4 +264,3 @@ func (m *Manager) upsertConfirmedAccountOutputs(ctx context.Context,

return nil
}

8 changes: 6 additions & 2 deletions blockchain/hsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package blockchain

import (
"context"

"github.com/bytom/blockchain/pseudohsm"
"github.com/bytom/blockchain/txbuilder"
"github.com/bytom/crypto/ed25519/chainkd"
"github.com/bytom/net/http/httperror"
"github.com/bytom/net/http/httpjson"
log "github.com/sirupsen/logrus"
)

func init() {
Expand Down Expand Up @@ -55,17 +57,19 @@ func (a *BlockchainReactor) pseudohsmSignTemplates(ctx context.Context, x struct
Auth string
Txs []*txbuilder.Template `json:"transactions"`
XPubs []chainkd.XPub `json:"xpubs"`
}) []interface{} {
resp := make([]interface{}, 0, len(x.Txs))
}) interface{} {
resp := make([]interface{}, len(x.Txs))
for _, tx := range x.Txs {
err := txbuilder.Sign(ctx, tx, x.XPubs, x.Auth, a.pseudohsmSignTemplate)
log.WithFields(log.Fields{"tx": tx, "build err": err}).Info("After sign transaction.")
if err != nil {
info := errorFormatter.Format(err)
resp = append(resp, info)
} else {
resp = append(resp, tx)
}
}
log.WithField("resp", resp).Info("Sign Transaction complete.")
return resp
}

Expand Down
10 changes: 3 additions & 7 deletions blockchain/pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *Store) LoadAll(ctx context.Context) error {
}

s.pins[block_processor.Name] = newPin(s.DB,
block_processor.Name[3:],
block_processor.Name,
block_processor.Height)

}
Expand Down Expand Up @@ -242,12 +242,8 @@ func (p *pin) complete(ctx context.Context, height uint64) error {
}
}

block_processor = struct {
Name string
Height uint64
}{
Name: p.name,
Height: max}
block_processor.Name = p.name
block_processor.Height = max

bytes, err = json.Marshal(&block_processor)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions cmd/bytomcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,11 +984,12 @@ func signTransactions(client *rpc.Client, args []string) {
file, _ := os.Open(args[0])
tpl_byte := make([]byte, 10000)
file.Read(tpl_byte)
fmt.Printf("tpl:%v\n", string(tpl_byte))
stdjson.Unmarshal(tpl_byte, &tpl)
fmt.Printf("tpl_byte:%v\n", string(tpl_byte))
err = stdjson.Unmarshal(bytes.Trim(tpl_byte, "\x00"), &tpl)
fmt.Printf("tpl:%v, err:%v\n", tpl, err)
in.Txs = []*txbuilder.Template{&tpl}

var response map[string][]interface{}
var response []interface{} = make([]interface{}, 1)
client.Call(context.Background(), "/sign-transactions", &in, &response)
fmt.Printf("sign response:%v\n", response)
}
Expand Down
44 changes: 44 additions & 0 deletions cmd/bytomd/log_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
log "github.com/sirupsen/logrus"
"os"
"path"
"runtime"
"strings"
)

type ContextHook struct{}

func (hook ContextHook) Levels() []log.Level {
return log.AllLevels
}

func (hook ContextHook) Fire(entry *log.Entry) error {
pc := make([]uintptr, 3, 3)
cnt := runtime.Callers(6, pc)

for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !strings.Contains(name, "github.com/Sirupsen/log") {
file, line := fu.FileLine(pc[i] - 1)
entry.Data["file"] = path.Base(file)
entry.Data["func"] = path.Base(name)
entry.Data["line"] = line
break
}
}
return nil
}

func init() {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})

// If environment variable BYTOM_DEBUG is not empty,
// then add the hook to logrus and set the log level to DEBUG
if os.Getenv("BYTOM_DEBUG") != "" {
log.AddHook(ContextHook{})
log.SetLevel(log.DebugLevel)
}
}
33 changes: 0 additions & 33 deletions cmd/bytomd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,11 @@ package main

import (
"github.com/bytom/cmd/bytomd/commands"
"github.com/sirupsen/logrus"
"github.com/tendermint/tmlibs/cli"
"os"
"path"
"runtime"
"strings"
)

type ContextHook struct{}

func (hook ContextHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (hook ContextHook) Fire(entry *logrus.Entry) error {
pc := make([]uintptr, 3, 3)
cnt := runtime.Callers(6, pc)

for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !strings.Contains(name, "github.com/Sirupsen/logrus") {
file, line := fu.FileLine(pc[i] - 1)
entry.Data["file"] = path.Base(file)
entry.Data["func"] = path.Base(name)
entry.Data["line"] = line
break
}
}
return nil
}

func init() {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
}

func main() {
logrus.AddHook(ContextHook{})
cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv("./.bytomd"))
cmd.Execute()
}
2 changes: 1 addition & 1 deletion net/http/httperror/httperror.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (f Formatter) Log(ctx context.Context, err error) {
resp := f.Format(err)
keyvals := []interface{}{
"status", resp.HTTPStatus,
"chaincode", resp.ChainCode,
"bytomcode", resp.ChainCode,
"path", reqid.PathFromContext(ctx),
log.KeyError, errorMessage,
}
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func (n *Node) makeNodeInfo() *p2p.NodeInfo {
nodeInfo := &p2p.NodeInfo{
PubKey: n.privKey.PubKey().Unwrap().(crypto.PubKeyEd25519),
Moniker: n.config.Moniker,
Network: "chain0",
Network: "bytom",
Version: version.Version,
Other: []string{
cmn.Fmt("wire_version=%v", wire.Version),
Expand Down
1 change: 0 additions & 1 deletion p2p/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ FOR_LOOP:
break FOR_LOOP
}
if msgBytes != nil {
c.Logger.Debug("Received bytes", "chID", pkt.ChannelID, "msgBytes", msgBytes)
log.WithFields(log.Fields{
"channelID": pkt.ChannelID,
"msgBytes": msgBytes,
Expand Down
Loading

0 comments on commit 1243faa

Please sign in to comment.