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 #1627 from Bytom/prod
Browse files Browse the repository at this point in the history
Prod
  • Loading branch information
Paladz authored Mar 4, 2019
2 parents 5330797 + 8db7fe7 commit 56443ac
Show file tree
Hide file tree
Showing 256 changed files with 4,144 additions and 30,285 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ available flags for `bytomd node`:
--auth.disable Disable rpc access authenticate
--chain_id string Select network type
-h, --help help for node
--mining.enable Enable mining
--mining.recommit_interval Set mining pool recomit interval in seconds (default 15)
--mining Enable mining
--p2p.dial_timeout int Set dial timeout (default 3)
--p2p.handshake_timeout int Set handshake timeout (default 30)
--p2p.laddr string Node listen address.
Expand Down
1 change: 1 addition & 0 deletions account/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
// HardenedKeyStart bip32 hierarchical deterministic wallets
// keys with index ≥ 0x80000000 are hardened keys
HardenedKeyStart = 0x80000000
logModule = "account"
)

var (
Expand Down
7 changes: 4 additions & 3 deletions account/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bytom/crypto/ed25519/chainkd"
"github.com/bytom/database/leveldb"
"github.com/bytom/errors"
"github.com/bytom/event"
"github.com/bytom/protocol"
"github.com/bytom/testutil"
)
Expand Down Expand Up @@ -211,11 +212,11 @@ func mockAccountManager(t *testing.T) *Manager {
}
defer os.RemoveAll(dirPath)

testDB := dbm.NewDB("testdb", "memdb", "temp")
defer os.RemoveAll("temp")
testDB := dbm.NewDB("testdb", "memdb", dirPath)
dispatcher := event.NewDispatcher()

store := leveldb.NewStore(testDB)
txPool := protocol.NewTxPool(store)
txPool := protocol.NewTxPool(store, dispatcher)
chain, err := protocol.NewChain(store, txPool)
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 3 additions & 2 deletions account/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ func (m *Manager) Restore(image *Image) error {
for _, slice := range image.Slice {
if existed := m.db.Get(Key(slice.Account.ID)); existed != nil {
log.WithFields(log.Fields{
"alias": slice.Account.Alias,
"id": slice.Account.ID,
"module": logModule,
"alias": slice.Account.Alias,
"id": slice.Account.ID,
}).Warning("skip restore account due to already existed")
continue
}
Expand Down
2 changes: 1 addition & 1 deletion account/utxo_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (uk *utxoKeeper) findUtxos(accountID string, assetID *bc.AssetID, useUnconf
for utxoIter.Next() {
u := &UTXO{}
if err := json.Unmarshal(utxoIter.Value(), u); err != nil {
log.WithField("err", err).Error("utxoKeeper findUtxos fail on unmarshal utxo")
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("utxoKeeper findUtxos fail on unmarshal utxo")
continue
}
appendUtxo(u)
Expand Down
37 changes: 21 additions & 16 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/bytom/net/http/static"
"github.com/bytom/net/websocket"
"github.com/bytom/netsync"
"github.com/bytom/p2p"
"github.com/bytom/protocol"
"github.com/bytom/wallet"
)
Expand All @@ -40,7 +41,8 @@ const (
// SUCCESS indicates the rpc calling is successful.
SUCCESS = "success"
// FAIL indicated the rpc calling is failed.
FAIL = "fail"
FAIL = "fail"
logModule = "api"
)

// Response describes the response standard.
Expand Down Expand Up @@ -105,7 +107,7 @@ func (wh *waitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

// API is the scheduling center for server
type API struct {
sync *netsync.SyncManager
sync NetSync
wallet *wallet.Wallet
accessTokens *accesstoken.CredentialStore
chain *protocol.Chain
Expand Down Expand Up @@ -152,7 +154,7 @@ func (a *API) initServer(config *cfg.Config) {

// StartServer start the server
func (a *API) StartServer(address string) {
log.WithField("api address:", address).Info("Rpc listen")
log.WithFields(log.Fields{"module": logModule, "api address:": address}).Info("Rpc listen")
listener, err := net.Listen("tcp", address)
if err != nil {
cmn.Exit(cmn.Fmt("Failed to register tcp port: %v", err))
Expand All @@ -163,13 +165,24 @@ func (a *API) StartServer(address string) {
// we call it.
go func() {
if err := a.server.Serve(listener); err != nil {
log.WithField("error", errors.Wrap(err, "Serve")).Error("Rpc server")
log.WithFields(log.Fields{"module": logModule, "error": errors.Wrap(err, "Serve")}).Error("Rpc server")
}
}()
}

type NetSync interface {
IsListening() bool
IsCaughtUp() bool
PeerCount() int
GetNetwork() string
BestPeer() *netsync.PeerInfo
DialPeerWithAddress(addr *p2p.NetAddress) error
GetPeerInfos() []*netsync.PeerInfo
StopPeer(peerID string) error
}

// NewAPI create and initialize the API
func NewAPI(sync *netsync.SyncManager, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, cpuMiner *cpuminer.CPUMiner, miningPool *miningpool.MiningPool, chain *protocol.Chain, config *cfg.Config, token *accesstoken.CredentialStore, dispatcher *event.Dispatcher, notificationMgr *websocket.WSNotificationManager) *API {
func NewAPI(sync NetSync, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, cpuMiner *cpuminer.CPUMiner, miningPool *miningpool.MiningPool, chain *protocol.Chain, config *cfg.Config, token *accesstoken.CredentialStore, dispatcher *event.Dispatcher, notificationMgr *websocket.WSNotificationManager) *API {
api := &API{
sync: sync,
wallet: wallet,
Expand Down Expand Up @@ -198,7 +211,6 @@ func (a *API) buildHandler() {
m := http.NewServeMux()
if a.wallet != nil {
walletEnable = true

m.Handle("/create-account", jsonHandler(a.createAccount))
m.Handle("/update-account-alias", jsonHandler(a.updateAccountAlias))
m.Handle("/list-accounts", jsonHandler(a.listAccounts))
Expand Down Expand Up @@ -303,10 +315,9 @@ func (a *API) buildHandler() {

m.HandleFunc("/websocket-subscribe", a.websocketHandler)

handler := latencyHandler(m, walletEnable)
handler := walletHandler(m, walletEnable)
handler = webAssetsHandler(handler)
handler = gzip.Handler{Handler: handler}

a.handler = handler
}

Expand Down Expand Up @@ -347,7 +358,7 @@ func AuthHandler(handler http.Handler, accessTokens *accesstoken.CredentialStore
// TODO(tessr): check that this path exists; return early if this path isn't legit
req, err := authenticator.Authenticate(req)
if err != nil {
log.WithField("error", errors.Wrap(err, "Serve")).Error("Authenticate fail")
log.WithFields(log.Fields{"module": logModule, "error": errors.Wrap(err, "Serve")}).Error("Authenticate fail")
err = errors.WithDetail(errNotAuthenticated, err.Error())
errorFormatter.Write(req.Context(), rw, err)
return
Expand All @@ -367,14 +378,8 @@ func RedirectHandler(next http.Handler) http.Handler {
})
}

// latencyHandler take latency for the request url path, and redirect url path to wait-disable when wallet is closed
func latencyHandler(m *http.ServeMux, walletEnable bool) http.Handler {
func walletHandler(m *http.ServeMux, walletEnable bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// latency for the request url path
if l := latency(m, req); l != nil {
defer l.RecordSince(time.Now())
}

// when the wallet is not been opened and the url path is not been found, modify url path to error,
// and redirect handler to error
if _, pattern := m.Handler(req); pattern != req.URL.Path && !walletEnable {
Expand Down
1 change: 1 addition & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var respErrFormatter = map[error]httperror.Info{
txbuilder.ErrBadContractArgType: {400, "BTM711", "Invalid contract argument type"},
txbuilder.ErrOrphanTx: {400, "BTM712", "Not found transaction input utxo"},
txbuilder.ErrExtTxFee: {400, "BTM713", "Transaction fee exceed max limit"},
txbuilder.ErrNoGasInput: {400, "BTM714", "Transaction has no gas input"},

// Submit transaction error namespace (73x ~ 79x)
// Validation error (73x ~ 75x)
Expand Down
32 changes: 0 additions & 32 deletions api/metrics.go

This file was deleted.

10 changes: 0 additions & 10 deletions api/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/bytom/protocol/bc/types"
)

var ErrEmptyWorkSubmission = errors.New("empty work submission")

// BlockHeaderJSON struct provides support for get work in json format, when it also follows
// BlockHeader structure
type BlockHeaderJSON struct {
Expand Down Expand Up @@ -101,10 +99,6 @@ type SubmitWorkReq struct {

// submitWork submits work in compressed protobuf format
func (a *API) submitWork(ctx context.Context, req *SubmitWorkReq) Response {
if req.BlockHeader == nil {
return NewErrorResponse(ErrEmptyWorkSubmission)
}

if err := a.SubmitWork(req.BlockHeader); err != nil {
return NewErrorResponse(err)
}
Expand All @@ -118,10 +112,6 @@ type SubmitWorkJSONReq struct {

// submitWorkJSON submits work in json format
func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkJSONReq) Response {
if req.BlockHeader == nil {
return NewErrorResponse(ErrEmptyWorkSubmission)
}

bh := &types.BlockHeader{
Version: req.BlockHeader.Version,
Height: req.BlockHeader.Height,
Expand Down
9 changes: 4 additions & 5 deletions api/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type NetInfo struct {
// GetNodeInfo return net information
func (a *API) GetNodeInfo() *NetInfo {
info := &NetInfo{
Listening: a.sync.Switch().IsListening(),
Listening: a.sync.IsListening(),
Syncing: !a.sync.IsCaughtUp(),
Mining: a.cpuMiner.IsMining(),
PeerCount: len(a.sync.Switch().Peers().List()),
PeerCount: a.sync.PeerCount(),
CurrentBlock: a.chain.BestBlockHeight(),
NetWorkID: a.sync.NodeInfo().Network,
NetWorkID: a.sync.GetNetwork(),
Version: &VersionInfo{
Version: version.Version,
Update: version.Status.VersionStatus(),
Expand Down Expand Up @@ -76,9 +76,8 @@ func (a *API) connectPeerByIpAndPort(ip string, port uint16) (*netsync.PeerInfo,
}

addr := p2p.NewNetAddressIPPort(netIp, port)
sw := a.sync.Switch()

if err := sw.DialPeerWithAddress(addr); err != nil {
if err := a.sync.DialPeerWithAddress(addr); err != nil {
return nil, errors.Wrap(err, "can not connect to the address")
}
peer := a.getPeerInfoByAddr(addr.String())
Expand Down
2 changes: 1 addition & 1 deletion api/transact.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

var (
defaultTxTTL = 5 * time.Minute
defaultTxTTL = 30 * time.Minute
defaultBaseRate = float64(100000)
flexibleGas = int64(1800)
)
Expand Down
4 changes: 2 additions & 2 deletions api/txfeeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (a *API) createTxFeed(ctx context.Context, in struct {
Filter string `json:"filter"`
}) Response {
if err := a.txFeedTracker.Create(ctx, in.Alias, in.Filter); err != nil {
log.WithField("error", err).Error("Add TxFeed Failed")
log.WithFields(log.Fields{"module": logModule, "error": err}).Error("Add TxFeed Failed")
return NewErrorResponse(err)
}
return NewSuccessResponse(nil)
Expand Down Expand Up @@ -58,7 +58,7 @@ func (a *API) updateTxFeed(ctx context.Context, in struct {
return NewErrorResponse(err)
}
if err := a.txFeedTracker.Create(ctx, in.Alias, in.Filter); err != nil {
log.WithField("error", err).Error("Update TxFeed Failed")
log.WithFields(log.Fields{"module": logModule, "error": err}).Error("Update TxFeed Failed")
return NewErrorResponse(err)
}
return NewSuccessResponse(nil)
Expand Down
4 changes: 3 additions & 1 deletion asset/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/bytom/consensus"
"github.com/bytom/crypto/ed25519/chainkd"
"github.com/bytom/database/leveldb"
"github.com/bytom/event"
"github.com/bytom/protocol"
"github.com/bytom/testutil"
)
Expand Down Expand Up @@ -152,7 +153,8 @@ func TestListAssets(t *testing.T) {

func mockChain(testDB dbm.DB) (*protocol.Chain, error) {
store := leveldb.NewStore(testDB)
txPool := protocol.NewTxPool(store)
dispatcher := event.NewDispatcher()
txPool := protocol.NewTxPool(store, dispatcher)
chain, err := protocol.NewChain(store, txPool)
if err != nil {
return nil, err
Expand Down
17 changes: 0 additions & 17 deletions blockchain/env.go

This file was deleted.

27 changes: 26 additions & 1 deletion blockchain/pseudohsm/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
package pseudohsm

import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"path/filepath"

log "github.com/sirupsen/logrus"

"github.com/bytom/crypto/ed25519/chainkd"
)

const logModule = "pseudohsm"

// KeyImage is the struct for hold export key data
type KeyImage struct {
XKeys []*encryptedKeyJSON `json:"xkeys"`
Expand Down Expand Up @@ -38,6 +45,23 @@ func (h *HSM) Restore(image *KeyImage) error {
defer h.cacheMu.Unlock()

for _, xKey := range image.XKeys {
data, err := hex.DecodeString(xKey.XPub)
if err != nil {
return ErrXPubFormat
}

var xPub chainkd.XPub
copy(xPub[:], data)
if h.cache.hasKey(xPub) {
log.WithFields(log.Fields{
"module": logModule,
"alias": xKey.Alias,
"id": xKey.ID,
"xPub": xKey.XPub,
}).Warning("skip restore key due to already existed")
continue
}

if ok := h.cache.hasAlias(xKey.Alias); ok {
return ErrDuplicateKeyAlias
}
Expand All @@ -52,7 +76,8 @@ func (h *HSM) Restore(image *KeyImage) error {
if err := writeKeyFile(file, rawKey); err != nil {
return err
}

h.cache.reload()
}
h.cache.maybeReload()
return nil
}
Loading

0 comments on commit 56443ac

Please sign in to comment.