github.com/annchain/OG@v0.0.9/tests/fetchTest/fetch_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package fetchTest
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  )
    20  
    21  func CalculateRequestSpan(remoteHeight, localHeight uint64) (int64, int, int, uint64) {
    22  	var (
    23  		from     int
    24  		count    int
    25  		MaxCount = 192 / 16
    26  	)
    27  	// requestHead is the highest block that we will ask for. If requestHead is not offset,
    28  	// the highest block that we will get is 16 blocks back from head, which means we
    29  	// will fetch 14 or 15 blocks unnecessarily in the case the height difference
    30  	// between us and the peer is 1-2 blocks, which is most common
    31  	requestHead := int(remoteHeight) - 1
    32  	if requestHead < 0 {
    33  		requestHead = 0
    34  	}
    35  	// requestBottom is the lowest block we want included in the query
    36  	// Ideally, we want to include just below own head
    37  	requestBottom := int(localHeight - 1)
    38  	if requestBottom < 0 {
    39  		requestBottom = 0
    40  	}
    41  	totalSpan := requestHead - requestBottom
    42  	span := 1 + totalSpan/MaxCount
    43  	fmt.Println(span)
    44  	if span < 2 {
    45  		span = 2
    46  	}
    47  	if span > 16 {
    48  		span = 16
    49  	}
    50  	fmt.Println(span)
    51  	count = 1 + totalSpan/span
    52  	if count > MaxCount {
    53  		count = MaxCount
    54  	}
    55  	fmt.Println(count, MaxCount)
    56  	if count < 2 {
    57  		count = 2
    58  	}
    59  	fmt.Println(count, MaxCount)
    60  	from = requestHead - (count-1)*span
    61  	if from < 0 {
    62  		from = 0
    63  	}
    64  	max := from + (count-1)*span
    65  	return int64(from), count, span - 1, uint64(max)
    66  }
    67  
    68  func TestCalculateRequestSpan(t *testing.T) {
    69  	from, count, skip, max := CalculateRequestSpan(8, 5)
    70  	fmt.Println(from, count, skip, max)
    71  	from, count, skip, max = CalculateRequestSpan(10, 0)
    72  	fmt.Println(from, count, skip, max)
    73  	from, count, skip, max = CalculateRequestSpan(389, 241)
    74  	fmt.Println(from, count, skip, max)
    75  	from, count = calculate(8, 5)
    76  	fmt.Println(from, count, 15)
    77  	from, count = calculate(10, 0)
    78  	fmt.Println(from, count, 15)
    79  	from, count = calculate(389, 241)
    80  	fmt.Println(from, count, 15)
    81  }
    82  
    83  func calculate(remote, local uint64) (int64, int) {
    84  	// Request the topmost blocks to short circuit binary ancestor lookup
    85  	head := local
    86  	if head > remote {
    87  		head = remote
    88  	}
    89  	from := int64(head) - 192
    90  	if from < 0 {
    91  		from = 0
    92  	}
    93  	// Span out with 15 block gaps into the future to catch bad head reports
    94  	limit := 2 * 192 / 16
    95  	count := 1 + int((int64(local)-from)/16)
    96  	if count > limit {
    97  		count = limit
    98  	}
    99  	return from, count
   100  }
   101  
   102  func TestUint(t *testing.T) {
   103  	floor, ceil := int64(-1), uint64(50)
   104  	height := uint64(50)
   105  	// Request the topmost blocks to short circuit binary ancestor lookup
   106  	head := ceil
   107  	if head > height {
   108  		head = height
   109  	}
   110  	from := int64(head) - int64(192)
   111  	fmt.Println(uint64(from), from)
   112  	if from < 0 {
   113  		from = 0
   114  	}
   115  	// Span out with 15 block gaps into the future to catch bad head reports
   116  	limit := 2 * 192 / 16
   117  	count := 1 + int((int64(ceil)-from)/16)
   118  	if count > limit {
   119  		count = limit
   120  	}
   121  	fmt.Println(uint64(from), count, 15, false, floor)
   122  }