diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 1170cced..5eee3ecb 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -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"), "") diff --git a/config/version.go b/config/version.go index 2e966051..535aba2d 100644 --- a/config/version.go +++ b/config/version.go @@ -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") diff --git a/p2p/chain_sync.go b/p2p/chain_sync.go index 86390c97..3bffc67d 100644 --- a/p2p/chain_sync.go +++ b/p2p/chain_sync.go @@ -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; { @@ -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 @@ -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 diff --git a/p2p/connection_pool.go b/p2p/connection_pool.go index b762ea4f..26a569d6 100644 --- a/p2p/connection_pool.go +++ b/p2p/connection_pool.go @@ -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 } @@ -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 } @@ -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() @@ -714,8 +711,9 @@ func trigger_sync() { connection.bootstrap_chain() chain.Sync = true } + break } - break + } } diff --git a/p2p/rpc_handshake.go b/p2p/rpc_handshake.go index 4ae621c3..3c4be926 100644 --- a/p2p/rpc_handshake.go +++ b/p2p/rpc_handshake.go @@ -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[:]) }