Skip to content

Commit

Permalink
Release91: P2P and Synchronization fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainDero committed Jun 6, 2022
1 parent db7eba4 commit bb316a0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
12 changes: 12 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ func (chain *Blockchain) Add_Complete_Block(cbl *block.Complete_Block) (err erro
}
}



if bl.Height > uint64(chain.Get_Height()+2) {
return fmt.Errorf("advance Block"), false // block in future skipping it
}

if bl.Height > uint64(config.STABLE_LIMIT) {
if bl.Height < uint64(chain.Get_Height()-config.STABLE_LIMIT) {
return fmt.Errorf("previous Block"), false // block in past skipping it
}
}

// only 1 tips allowed in block
if len(bl.Tips) > 1 {
block_logger.V(1).Error(fmt.Errorf("More than 1 tips present in block rejecting"), "")
Expand Down
2 changes: 1 addition & 1 deletion config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ import "github.com/blang/semver/v4"

// right now it has to be manually changed
// do we need to include git commitsha??
var Version = semver.MustParse("3.4.141-78.DEROHE.STARGATE+26022022")
var Version = semver.MustParse("3.4.141-91.DEROHE.STARGATE+26022022")
8 changes: 6 additions & 2 deletions p2p/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func (connection *Connection) sync_chain() {

try_again:

request = Chain_Request_Struct{}
response = Chain_Response_Struct{}

// send our blocks, first 10 blocks directly, then decreasing in powers of 2
start_point := chain.Load_TOPO_HEIGHT()
for i := int64(0); i < start_point; {
Expand Down Expand Up @@ -169,6 +172,7 @@ try_again:
connection.logger.V(3).Info("rewinding status", "our topoheight", chain.Load_TOPO_HEIGHT(), "peer topoheight", response.Start_topoheight)
pop_count := chain.Load_TOPO_HEIGHT() - response.Start_topoheight
chain.Rewind_Chain(int(pop_count)) // pop as many blocks as necessary
pop_count = 0

// we should NOT queue blocks, instead we sent our chain request again
goto try_again
Expand Down Expand Up @@ -247,8 +251,8 @@ try_again:
return
}

// response only 128 blocks at a time
max_blocks_to_queue := 128
// response only 4096 blocks at a time
max_blocks_to_queue := 4096
// check whether the objects are in our db or not
// until we put in place a parallel object tracker, do it one at a time

Expand Down
24 changes: 11 additions & 13 deletions p2p/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,13 @@ func broadcast_Block_Coded(cbl *block.Complete_Block, PeerID uint64, first_seen
}
if atomic.LoadUint32(&v.State) != HANDSHAKE_PENDING && PeerID != v.Peer_ID && v.Peer_ID != GetPeerID() { // skip pre-handshake connections

// if the other end is > 2 blocks behind, do not broadcast block to hime
// if the other end is > 2 blocks behind, do not broadcast block to him
// this is an optimisation, since if the other end is syncing
// every peer will keep on broadcasting and thus making it more lagging
// due to overheads
// if the other end is > 2 blocks forwards, do not broadcast block to him
peer_height := atomic.LoadInt64(&v.Height)
if (our_height - peer_height) > 2 {
if (our_height-peer_height) > 2 || (peer_height - our_height) > 2 {
continue
}

Expand Down Expand Up @@ -496,7 +497,7 @@ func broadcast_Chunk(chunk *Block_Chunk, PeerID uint64, first_seen int64) { // i
// every peer will keep on broadcasting and thus making it more lagging
// due to overheads
peer_height := atomic.LoadInt64(&v.Height)
if (our_height - peer_height) > 25 {
if (our_height-peer_height) > 3 || (peer_height - our_height) > 3 {
continue
}

Expand Down Expand Up @@ -671,29 +672,25 @@ func trigger_sync() {

//connection.Lock() recursive mutex are not suported
// only choose highest available peers for syncing
if atomic.LoadUint32(&connection.State) != HANDSHAKE_PENDING && height < atomic.LoadInt64(&connection.Height) { // skip pre-handshake connections
if atomic.LoadUint32(&connection.State) != HANDSHAKE_PENDING && (height < atomic.LoadInt64(&connection.Height) || (connection.SyncNode && height > (atomic.LoadInt64(&connection.Height)+2)) ) { // skip pre-handshake connections
// check whether we are lagging with this connection
//connection.Lock()
islagging := height < atomic.LoadInt64(&connection.Height)
islagging := (height < atomic.LoadInt64(&connection.Height) || (connection.SyncNode && height > (atomic.LoadInt64(&connection.Height)+2)) )

//fmt.Printf("checking cdiff is lagging %+v topoheight %d peer topoheight %d \n", islagging, topoheight, connection.TopoHeight)

// islagging := true
//connection.Unlock()
if islagging {

if connection.Pruned > chain.Load_Block_Topological_order(chain.Get_Top_ID()) && chain.Get_Height() != 0 {
connection.logger.V(1).Info("We cannot resync with the peer, since peer chain is pruned", "height", connection.Height, "pruned", connection.Pruned)
continue
}

if connection.Height > chain.Get_Height() { // give ourselves one sec, maybe the block is just being written
time.Sleep(time.Second)
time.Sleep(time.Second)
height := chain.Get_Height()
islagging = height < atomic.LoadInt64(&connection.Height) // we only use topoheight, since pruned chain might not have full cdiff
} else {
continue
}
islagging = (height < atomic.LoadInt64(&connection.Height) || (connection.SyncNode && height > (atomic.LoadInt64(&connection.Height)+2)) )


if islagging {
//connection.Lock()
Expand All @@ -714,8 +711,9 @@ func trigger_sync() {
connection.bootstrap_chain()
chain.Sync = true
}
break
}
break

}
}

Expand Down
8 changes: 8 additions & 0 deletions p2p/rpc_handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ import "time"

import "github.com/deroproject/derohe/config"
import "github.com/deroproject/derohe/globals"
import "github.com/blang/semver/v4"

// verify incoming handshake for number of checks such as mainnet/testnet etc etc
func Verify_Handshake(handshake *Handshake_Struct) bool {
v := semver.MustParse(handshake.DaemonVersion)
var pre int
fmt.Sscanf(v.Pre[0].String(), "%d", &pre)
if pre < 78 {
return false
}

return bytes.Equal(handshake.Network_ID[:], globals.Config.Network_ID[:])
}

Expand Down

0 comments on commit bb316a0

Please sign in to comment.