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 #89 from Bytom/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gguoss authored Nov 6, 2017
2 parents fc6922b + 8bf6d5c commit 41dc248
Show file tree
Hide file tree
Showing 5,570 changed files with 2,662,307 additions and 4,396 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# Folders
_obj
_test
vendor

# Architecture specific extensions/prefixes
*.[568vq]
Expand All @@ -22,8 +21,10 @@ _cgo_export.*
_testmain.go

cmd/bytomd/bytomd
cmd/bytomd/.bytomd
cmd/bytomcli/bytomcli
cmd/bytom/.bytom
.bytomd

*.exe

Expand All @@ -39,3 +40,8 @@ profile.cov
profile.tmp

.DS_Store
cmd/bytomd/bytomd_error/config.toml
cmd/bytomd/bytomd_error/genesis.json
blockchain/pseudohsm/testdata/pseudo/UTC--2017-11-01T09-24-31.848287391Z--931de33f-e62e-4de7-8036-f82ad5efa77e
blockchain/pseudohsm/testdata/pseudo/UTC--2017-11-01T09-24-31.953012475Z--36b63ec7-b585-4a0e-95ff-e3f71b4ccb8b
blockchain/pseudohsm/UTC--2017-11-01T09-24-30.596579759Z--2178d885-aca6-4436-a865-e7e439e4bd9c
28 changes: 11 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
GOTOOLS = \
github.com/mitchellh/gox \
github.com/Masterminds/glide
PACKAGES = $(shell go list ./... | grep -v '/vendor/' | grep -v '/rpc/')
PACKAGES = $(shell go list ./... | grep -v '/vendor/')

all: install test
all: bytomd bytomcli test

install: get_vendor_deps
@go install --ldflags '-extldflags "-static"' \
--ldflags "-X github.com/Bytom/blockchain/version.GitCommit=`git rev-parse HEAD`" ./node/
@echo "====> Done!"
bytomd:
@echo "Building bytomd to cmd/bytomd/bytomd"
@go build -ldflags "-X github.com/bytom/version.GitCommit=`git rev-parse HEAD`" \
-o cmd/bytomd/bytomd cmd/bytomd/main.go

get_vendor_deps: ensure_tools
@rm -rf vendor/
@echo "====> Running glide install"
@glide install

ensure_tools:
go get $(GOTOOLS)
bytomcli:
@echo "Building bytomcli to cmd/bytomcli/bytomcli"
@go build -ldflags "-X github.com/bytom/version.GitCommit=`git rev-parse HEAD`" \
-o cmd/bytomcli/bytomcli cmd/bytomcli/main.go

test:
@echo "====> Running go test"
@go test $(PACKAGES)

.PHONY: install get_vendor_deps ensure_tools test
.PHONY: test
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@ $ git clone https://github.com/Bytom/bytom $GOPATH/src/github.com/bytom
``` bash
$ cd $GOPATH/src/github.com/bytom
$ make install
$ cd ./cmd/bytomd
$ go build
$ make bytomd
```

- Bytomcli

```go
$ cd $GOPATH/src/github.com/bytom/cmd/bytomcli
$ go build
$ make bytomcli
```

## Example
Expand Down
2 changes: 0 additions & 2 deletions blockchain/accesstoken/accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ package accesstoken
import (
"context"
"crypto/rand"
// "database/sql"
"fmt"
"regexp"
"time"

"github.com/bytom/crypto/sha3pool"
// "chain/database/pg"
"github.com/bytom/errors"
)

Expand Down
191 changes: 191 additions & 0 deletions blockchain/block_keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package blockchain

import (
"errors"
"sync"

log "github.com/sirupsen/logrus"

"github.com/bytom/p2p"
"github.com/bytom/protocol"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/legacy"
)

type blockKeeperPeer struct {
mtx sync.RWMutex
height uint64
hash *bc.Hash
}

func newBlockKeeperPeer(height uint64, hash *bc.Hash) *blockKeeperPeer {
return &blockKeeperPeer{
height: height,
hash: hash,
}
}

func (p *blockKeeperPeer) GetStatus() (height uint64, hash *bc.Hash) {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.height, p.hash
}

func (p *blockKeeperPeer) SetStatus(height uint64, hash *bc.Hash) {
p.mtx.Lock()
defer p.mtx.Unlock()

p.height = height
p.hash = hash
}

type pendingResponse struct {
block *legacy.Block
peerID string
}

//TODO: add retry mechanism
type blockKeeper struct {
mtx sync.RWMutex
chainHeight uint64
maxPeerHeight uint64
chainUpdateCh <-chan struct{}
peerUpdateCh chan struct{}

chain *protocol.Chain
sw *p2p.Switch
peers map[string]*blockKeeperPeer
pendingProcessCh chan *pendingResponse
}

func newBlockKeeper(chain *protocol.Chain, sw *p2p.Switch) *blockKeeper {
chainHeight := chain.Height()
bk := &blockKeeper{
chainHeight: chainHeight,
maxPeerHeight: uint64(0),
chainUpdateCh: chain.BlockWaiter(chainHeight + 1),
peerUpdateCh: make(chan struct{}, 1000),

chain: chain,
sw: sw,
peers: make(map[string]*blockKeeperPeer),
pendingProcessCh: make(chan *pendingResponse),
}
go bk.blockProcessWorker()
go bk.blockRequestWorker()
return bk
}

func (bk *blockKeeper) AddBlock(block *legacy.Block, peerID string) {
bk.pendingProcessCh <- &pendingResponse{block: block, peerID: peerID}
}

func (bk *blockKeeper) IsCaughtUp() bool {
bk.mtx.RLock()
defer bk.mtx.RUnlock()
return bk.chainHeight >= bk.maxPeerHeight
}

func (bk *blockKeeper) RemovePeer(peerID string) {
bk.mtx.Lock()
delete(bk.peers, peerID)
bk.mtx.Unlock()
log.WithField("ID", peerID).Info("Delete peer from blockKeeper")
}

func (bk *blockKeeper) requestBlockByHash(peerID string, hash *bc.Hash) error {
peer := bk.sw.Peers().Get(peerID)
if peer == nil {
return errors.New("can't find peer in peer pool")
}
msg := &BlockRequestMessage{RawHash: hash.Byte32()}
peer.TrySend(BlockchainChannel, struct{ BlockchainMessage }{msg})
return nil
}

func (bk *blockKeeper) requestBlockByHeight(peerID string, height uint64) error {
peer := bk.sw.Peers().Get(peerID)
if peer == nil {
return errors.New("can't find peer in peer pool")
}
msg := &BlockRequestMessage{Height: height}
peer.TrySend(BlockchainChannel, struct{ BlockchainMessage }{msg})
return nil
}

func (bk *blockKeeper) SetPeerHeight(peerID string, height uint64, hash *bc.Hash) {
bk.mtx.Lock()
defer bk.mtx.Unlock()

if height > bk.maxPeerHeight {
bk.maxPeerHeight = height
bk.peerUpdateCh <- struct{}{}
}

if peer, ok := bk.peers[peerID]; ok {
peer.SetStatus(height, hash)
return
}
peer := newBlockKeeperPeer(height, hash)
bk.peers[peerID] = peer
log.WithFields(log.Fields{"ID": peerID, "Height": height}).Info("Add new peer to blockKeeper")
}

func (bk *blockKeeper) RequestBlockByHeight(height uint64) {
bk.mtx.RLock()
defer bk.mtx.RUnlock()

for peerID, peer := range bk.peers {
if peerHeight, _ := peer.GetStatus(); peerHeight > bk.chainHeight {
bk.requestBlockByHeight(peerID, height)
}
}
}

func (bk *blockKeeper) blockRequestWorker() {
for {
select {
case <-bk.chainUpdateCh:
chainHeight := bk.chain.Height()
bk.mtx.Lock()
if bk.chainHeight < chainHeight {
bk.chainHeight = chainHeight
}
bk.chainUpdateCh = bk.chain.BlockWaiter(bk.chainHeight + 1)
bk.mtx.Unlock()

case <-bk.peerUpdateCh:
bk.mtx.RLock()
chainHeight := bk.chainHeight
maxPeerHeight := bk.maxPeerHeight
bk.mtx.RUnlock()

for i := chainHeight + 1; i <= maxPeerHeight; i++ {
bk.RequestBlockByHeight(i)
waiter := bk.chain.BlockWaiter(i)
<-waiter
}
}
}
}

func (bk *blockKeeper) blockProcessWorker() {
for pendingResponse := range bk.pendingProcessCh {
block := pendingResponse.block
blockHash := block.Hash()
isOrphan, err := bk.chain.ProcessBlock(block)
if err != nil {
log.WithField("hash", blockHash.String()).Errorf("blockKeeper fail process block %v", err)
continue
}
log.WithFields(log.Fields{
"height": block.Height,
"hash": blockHash.String(),
"isOrphan": isOrphan,
}).Info("blockKeeper processed block")

if isOrphan {
bk.requestBlockByHash(pendingResponse.peerID, &block.PreviousBlockHash)
}
}
}
23 changes: 9 additions & 14 deletions blockchain/hsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
)

func init() {
//Error code 050 represents alias of key duplicated
errorFormatter.Errors[pseudohsm.ErrDuplicateKeyAlias] = httperror.Info{400, "BTM050", "Alias already exists"}
//Error code 801 represents query request format error
errorFormatter.Errors[pseudohsm.ErrInvalidAfter] = httperror.Info{400, "BTM801", "Invalid `after` in query"}
//Error code 802 represents query reponses too many
errorFormatter.Errors[pseudohsm.ErrTooManyAliasesToList] = httperror.Info{400, "BTM802", "Too many aliases to list"}
}

func (a *BlockchainReactor) pseudohsmCreateKey(ctx context.Context, in struct{ Alias, Password string }) (result *pseudohsm.XPub, err error) {
return a.hsm.XCreate(in.Password, in.Alias)
return a.hsm.XCreate(in.Alias, in.Password)
}

func (a *BlockchainReactor) pseudohsmListKeys(ctx context.Context, query requestQuery) (page, error) {
Expand Down Expand Up @@ -54,10 +57,10 @@ func (a *BlockchainReactor) pseudohsmDeleteKey(ctx context.Context, x struct {
}

func (a *BlockchainReactor) pseudohsmSignTemplates(ctx context.Context, x struct {
Auth string
Txs []*txbuilder.Template `json:"transactions"`
XPub chainkd.XPub `json:"xpubs"`
XPrv chainkd.XPrv `json:"xprv"`
Auth string
Txs []*txbuilder.Template `json:"transactions"`
XPub chainkd.XPub `json:"xpubs"`
XPrv chainkd.XPrv `json:"xprv"`
}) interface{} {
resp := make([]interface{}, len(x.Txs))
var err error
Expand All @@ -67,7 +70,7 @@ func (a *BlockchainReactor) pseudohsmSignTemplates(ctx context.Context, x struct
derived := x.XPrv.Derive(path)
return derived.Sign(data[:]), nil
})
}else{
} else {
err = txbuilder.Sign(ctx, tx, []chainkd.XPub{x.XPub}, x.Auth, a.pseudohsmSignTemplate)
}

Expand Down Expand Up @@ -98,11 +101,3 @@ func (a *BlockchainReactor) pseudohsmResetPassword(ctx context.Context, x struct
}) error {
return a.hsm.ResetPassword(x.XPub, x.OldPassword, x.NewPassword)
}

func (a *BlockchainReactor) pseudohsmUpdateAlias(ctx context.Context, x struct {
Password string
NewAlias string
XPub chainkd.XPub `json:"xpubs"`
}) error {
return a.hsm.UpdateAlias(x.XPub, x.Password, x.NewAlias)
}
Loading

0 comments on commit 41dc248

Please sign in to comment.