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 #1786 from Bytom/dev
Browse files Browse the repository at this point in the history
update master
  • Loading branch information
Paladz authored Sep 11, 2019
2 parents aa9dc66 + a73e508 commit 43eb7d8
Show file tree
Hide file tree
Showing 23 changed files with 770 additions and 215 deletions.
1 change: 1 addition & 0 deletions blockchain/query/annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type AnnotatedInput struct {
Arbitrary chainjson.HexBytes `json:"arbitrary,omitempty"`
InputID bc.Hash `json:"input_id"`
WitnessArguments []chainjson.HexBytes `json:"witness_arguments"`
SignData bc.Hash `json:"sign_data,omitempty"`
}

//AnnotatedOutput means an annotated transaction output.
Expand Down
4 changes: 4 additions & 0 deletions consensus/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ var MainNetParams = Params{
{191000, bc.NewHash([32]byte{0x09, 0x4f, 0xe3, 0x23, 0x91, 0xb5, 0x11, 0x18, 0x68, 0xcc, 0x99, 0x9f, 0xeb, 0x95, 0xf9, 0xcc, 0xa5, 0x27, 0x6a, 0xf9, 0x0e, 0xda, 0x1b, 0xc6, 0x2e, 0x03, 0x29, 0xfe, 0x08, 0xdd, 0x2b, 0x01})},
{205000, bc.NewHash([32]byte{0x6f, 0xdd, 0x87, 0x26, 0x73, 0x3f, 0x0b, 0xc7, 0x58, 0x64, 0xa4, 0xdf, 0x45, 0xe4, 0x50, 0x27, 0x68, 0x38, 0x18, 0xb9, 0xa9, 0x44, 0x56, 0x20, 0x34, 0x68, 0xd8, 0x68, 0x72, 0xdb, 0x65, 0x6f})},
{219700, bc.NewHash([32]byte{0x98, 0x49, 0x8d, 0x4b, 0x7e, 0xe9, 0x44, 0x55, 0xc1, 0x07, 0xdd, 0x9a, 0xba, 0x6b, 0x49, 0x92, 0x61, 0x15, 0x03, 0x4f, 0x59, 0x42, 0x35, 0x74, 0xea, 0x3b, 0xdb, 0x2c, 0x53, 0x11, 0x75, 0x74})},
{240000, bc.NewHash([32]byte{0x35, 0x16, 0x65, 0x58, 0xf4, 0xef, 0x24, 0x82, 0x43, 0xbb, 0x15, 0x79, 0xd4, 0xfe, 0x1b, 0x14, 0x9f, 0xe9, 0xf0, 0xe0, 0x48, 0x72, 0x86, 0x68, 0xa7, 0xb9, 0xda, 0x58, 0x66, 0x3b, 0x1c, 0xcb})},
{270000, bc.NewHash([32]byte{0x9d, 0x6f, 0xcc, 0xd8, 0xb8, 0xe4, 0x8c, 0x17, 0x52, 0x9a, 0xe6, 0x1b, 0x40, 0x60, 0xe0, 0xe3, 0x6d, 0x1e, 0x89, 0xc0, 0x26, 0xdf, 0x1c, 0x28, 0x18, 0x0d, 0x29, 0x0c, 0x9b, 0x15, 0xcc, 0x97})},
{300000, bc.NewHash([32]byte{0xa2, 0x85, 0x84, 0x6c, 0xe0, 0x3e, 0x1d, 0x68, 0x98, 0x7d, 0x93, 0x21, 0xea, 0xcc, 0x1d, 0x07, 0x88, 0xd1, 0x4c, 0x77, 0xa3, 0xd7, 0x55, 0x8a, 0x2b, 0x4a, 0xf7, 0x4d, 0x50, 0x14, 0x53, 0x5d})},
},
}

Expand All @@ -152,6 +155,7 @@ var TestNetParams = Params{
{83200, bc.NewHash([32]byte{0xb4, 0x6f, 0xc5, 0xcf, 0xa3, 0x3d, 0xe1, 0x11, 0x71, 0x68, 0x40, 0x68, 0x0c, 0xe7, 0x4c, 0xaf, 0x5a, 0x11, 0xfe, 0x82, 0xbc, 0x36, 0x88, 0x0f, 0xbd, 0x04, 0xf0, 0xc4, 0x86, 0xd4, 0xd6, 0xd5})},
{93000, bc.NewHash([32]byte{0x6f, 0x4f, 0x37, 0x5f, 0xe9, 0xfb, 0xdf, 0x66, 0x60, 0x0e, 0xf0, 0x39, 0xb7, 0x18, 0x26, 0x75, 0xa0, 0x9a, 0xa5, 0x9b, 0x83, 0xc9, 0x9a, 0x25, 0x45, 0xb8, 0x7d, 0xd4, 0x99, 0x24, 0xa2, 0x8a})},
{113300, bc.NewHash([32]byte{0x7a, 0x69, 0x75, 0xa5, 0xf6, 0xb6, 0x94, 0xf3, 0x94, 0xa2, 0x63, 0x91, 0x28, 0xb6, 0xab, 0x7e, 0xf9, 0x71, 0x27, 0x5a, 0xe2, 0x59, 0xd3, 0xff, 0x70, 0x6e, 0xcb, 0xd8, 0xd8, 0x30, 0x9c, 0xc4})},
{235157, bc.NewHash([32]byte{0xfa, 0x76, 0x36, 0x3e, 0x9e, 0x58, 0xea, 0xe4, 0x7d, 0x26, 0x70, 0x7e, 0xf3, 0x8b, 0xfd, 0xad, 0x1a, 0x99, 0xf7, 0x4c, 0xac, 0xc6, 0x80, 0x99, 0x58, 0x10, 0x13, 0x66, 0x4b, 0x8c, 0x39, 0x4f})},
},
}

Expand Down
7 changes: 5 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func (hook *BtmHook) ioWrite(entry *logrus.Entry) error {
return err
}

_, err = writer.Write(msg)
return err
if _, err = writer.Write(msg); err != nil {
return err
}

return writer.Close()
}

func clearLockFiles(logPath string) error {
Expand Down
3 changes: 2 additions & 1 deletion netsync/block_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
log "github.com/sirupsen/logrus"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"

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

Expand Down Expand Up @@ -79,7 +80,7 @@ func (f *blockFetcher) insert(msg *blockMsg) {
return
}

f.peers.addBanScore(msg.peerID, 20, 0, err.Error())
f.peers.ProcessIllegal(msg.peerID, security.LevelMsgIllegal, err.Error())
return
}

Expand Down
6 changes: 4 additions & 2 deletions netsync/block_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/bytom/consensus"
"github.com/bytom/errors"
"github.com/bytom/mining/tensority"
"github.com/bytom/p2p/security"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
)
Expand All @@ -29,6 +30,7 @@ var (
errRequestTimeout = errors.New("request timeout")
errPeerDropped = errors.New("Peer dropped")
errPeerMisbehave = errors.New("peer is misbehave")
ErrPeerMisbehave = errors.New("peer is misbehave")
)

type blockMsg struct {
Expand Down Expand Up @@ -367,7 +369,7 @@ func (bk *blockKeeper) startSync() bool {
bk.syncPeer = peer
if err := bk.fastBlockSync(checkPoint); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Warning("fail on fastBlockSync")
bk.peers.errorHandler(peer.ID(), err)
bk.peers.ProcessIllegal(peer.ID(), security.LevelMsgIllegal, err.Error())
return false
}
return true
Expand All @@ -384,7 +386,7 @@ func (bk *blockKeeper) startSync() bool {

if err := bk.regularBlockSync(targetHeight); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Warning("fail on regularBlockSync")
bk.peers.errorHandler(peer.ID(), err)
bk.peers.ProcessIllegal(peer.ID(), security.LevelMsgIllegal, err.Error())
return false
}
return true
Expand Down
7 changes: 4 additions & 3 deletions netsync/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bytom/consensus"
"github.com/bytom/event"
"github.com/bytom/p2p"
"github.com/bytom/p2p/security"
core "github.com/bytom/protocol"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
Expand Down Expand Up @@ -44,14 +45,14 @@ type Chain interface {

type Switch interface {
AddReactor(name string, reactor p2p.Reactor) p2p.Reactor
AddBannedPeer(string) error
StopPeerGracefully(string)
NodeInfo() *p2p.NodeInfo
Start() (bool, error)
Stop() bool
IsListening() bool
DialPeerWithAddress(addr *p2p.NetAddress) error
Peers() *p2p.PeerSet
IsBanned(peerID string, level byte, reason string) bool
}

//SyncManager Sync Manager is responsible for the business layer information synchronization
Expand Down Expand Up @@ -336,12 +337,12 @@ func (sm *SyncManager) handleStatusResponseMsg(basePeer BasePeer, msg *StatusRes
func (sm *SyncManager) handleTransactionMsg(peer *peer, msg *TransactionMessage) {
tx, err := msg.GetTransaction()
if err != nil {
sm.peers.addBanScore(peer.ID(), 0, 10, "fail on get tx from message")
sm.peers.ProcessIllegal(peer.ID(), security.LevelConnException, "fail on get txs from message")
return
}

if isOrphan, err := sm.chain.ValidateTx(tx); err != nil && err != core.ErrDustTx && !isOrphan {
sm.peers.addBanScore(peer.ID(), 10, 0, "fail on validate tx transaction")
sm.peers.ProcessIllegal(peer.ID(), security.LevelMsgIllegal, "fail on validate tx transaction")
}
}

Expand Down
53 changes: 8 additions & 45 deletions netsync/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ import (

"github.com/bytom/consensus"
"github.com/bytom/errors"
"github.com/bytom/p2p/trust"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
)

const (
maxKnownTxs = 32768 // Maximum transactions hashes to keep in the known list (prevent DOS)
maxKnownBlocks = 1024 // Maximum block hashes to keep in the known list (prevent DOS)
defaultBanThreshold = uint32(100)
maxKnownTxs = 32768 // Maximum transactions hashes to keep in the known list (prevent DOS)
maxKnownBlocks = 1024 // Maximum block hashes to keep in the known list (prevent DOS)
)

//BasePeer is the interface for connection level peer
type BasePeer interface {
Addr() net.Addr
ID() string
RemoteAddrHost() string
ServiceFlag() consensus.ServiceFlag
TrafficStatus() (*flowrate.Status, *flowrate.Status)
TrySend(byte, interface{}) bool
Expand All @@ -35,8 +34,8 @@ type BasePeer interface {

//BasePeerSet is the intergace for connection level peer manager
type BasePeerSet interface {
AddBannedPeer(string) error
StopPeerGracefully(string)
IsBanned(ip string, level byte, reason string) bool
}

// PeerInfo indicate peer status snap
Expand All @@ -60,7 +59,6 @@ type peer struct {
services consensus.ServiceFlag
height uint64
hash *bc.Hash
banScore trust.DynamicBanScore
knownTxs *set.Set // Set of transaction hashes known to be known by this peer
knownBlocks *set.Set // Set of block hashes known to be known by this peer
filterAdds *set.Set // Set of addresses that the spv node cares about.
Expand All @@ -84,30 +82,6 @@ func (p *peer) Height() uint64 {
return p.height
}

func (p *peer) addBanScore(persistent, transient uint32, reason string) bool {
score := p.banScore.Increase(persistent, transient)
if score > defaultBanThreshold {
log.WithFields(log.Fields{
"module": logModule,
"address": p.Addr(),
"score": score,
"reason": reason,
}).Errorf("banning and disconnecting")
return true
}

warnThreshold := defaultBanThreshold >> 1
if score > warnThreshold {
log.WithFields(log.Fields{
"module": logModule,
"address": p.Addr(),
"score": score,
"reason": reason,
}).Warning("ban score increasing")
}
return false
}

func (p *peer) addFilterAddress(address []byte) {
p.mtx.Lock()
defer p.mtx.Unlock()
Expand Down Expand Up @@ -331,21 +305,18 @@ func newPeerSet(basePeerSet BasePeerSet) *peerSet {
}
}

func (ps *peerSet) addBanScore(peerID string, persistent, transient uint32, reason string) {
func (ps *peerSet) ProcessIllegal(peerID string, level byte, reason string) {
ps.mtx.Lock()
peer := ps.peers[peerID]
ps.mtx.Unlock()

if peer == nil {
return
}
if ban := peer.addBanScore(persistent, transient, reason); !ban {
return
}
if err := ps.AddBannedPeer(peer.Addr().String()); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("fail on add ban peer")
if banned := ps.IsBanned(peer.RemoteAddrHost(), level, reason); banned {
ps.removePeer(peerID)
}
ps.removePeer(peerID)
return
}

func (ps *peerSet) addPeer(peer BasePeer, height uint64, hash *bc.Hash) {
Expand Down Expand Up @@ -439,14 +410,6 @@ func (ps *peerSet) broadcastTx(tx *types.Tx) error {
return nil
}

func (ps *peerSet) errorHandler(peerID string, err error) {
if errors.Root(err) == errPeerMisbehave {
ps.addBanScore(peerID, 20, 0, err.Error())
} else {
ps.removePeer(peerID)
}
}

// Peer retrieves the registered peer with the given id.
func (ps *peerSet) getPeer(id string) *peer {
ps.mtx.RLock()
Expand Down
11 changes: 9 additions & 2 deletions netsync/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (p *P2PPeer) IsLAN() bool {
return false
}

func (p *P2PPeer) RemoteAddrHost() string {
return ""
}

func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
return p.flag
}
Expand Down Expand Up @@ -89,8 +93,11 @@ func NewPeerSet() *PeerSet {
return &PeerSet{}
}

func (ps *PeerSet) AddBannedPeer(string) error { return nil }
func (ps *PeerSet) StopPeerGracefully(string) {}
func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
return false
}

func (ps *PeerSet) StopPeerGracefully(string) {}

type NetWork struct {
nodes map[*SyncManager]P2PPeer
Expand Down
10 changes: 9 additions & 1 deletion p2p/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func (info *NodeInfo) CompatibleWith(other *NodeInfo) error {
return nil
}

func (info NodeInfo) DoFilter(ip string, pubKey string) error {
if info.PubKey.String() == pubKey {
return ErrConnectSelf
}

return nil
}

func (info *NodeInfo) getPubkey() crypto.PubKeyEd25519 {
return info.PubKey
}
Expand All @@ -70,7 +78,7 @@ func (info *NodeInfo) listenHost() string {
}

//RemoteAddrHost peer external ip address
func (info *NodeInfo) remoteAddrHost() string {
func (info *NodeInfo) RemoteAddrHost() string {
host, _, _ := net.SplitHostPort(info.RemoteAddr)
return host
}
Expand Down
8 changes: 8 additions & 0 deletions p2p/peer_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func (ps *PeerSet) Add(peer *Peer) error {
return nil
}

func (ps *PeerSet) DoFilter(ip string, pubKey string) error {
if ps.Has(pubKey) {
return ErrDuplicatePeer
}

return nil
}

// Get looks up a peer by the provided peerKey.
func (ps *PeerSet) Get(peerKey string) *Peer {
ps.mtx.Lock()
Expand Down
Loading

0 comments on commit 43eb7d8

Please sign in to comment.