This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from Bytom/dev
Dev
- Loading branch information
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.