Merge 0.6.0-dev into 0.6.0-libp2p (#824)

* [NOD-1079] Fix block rejects over "Already have block" (#783)

* [NOD-1079] Return regular error instead of ruleError on already-have-block in ProcessBlock.

* [NOD-1079] Fix bad implementation of IsSelectedTipKnown.

* [NOD-1079] In shouldQueryPeerSelectedTips use selected DAG tip timestamp instead of past median time.

* [NOD-1079] Remove redundant (and possibly buggy) clearing of sm.requestedBlocks.

* [NOD-684] change simnet block rate to block per ms (#782)

* [NOD-684] Get rid of dag.targetTimePerBlock and use finality duration in dag params

* [NOD-684] Fix regtest genesis block

* [NOD-684] Set simnet's TargetTimePerBlock to 1ms

* [NOD-684] Shorten simnet finality duration

* [NOD-684] Change isDAGCurrentMaxDiff to be written as number of blocks

* [NOD-684] Fix NextBlockMinimumTime to be add one millisecond after past median time

* [NOD-1004] Make AddrManager.getAddress use only 1 loop to check all address chances and pick one of them (#741)

* [NOD-1004] Remove code duplication in Good().

* [NOD-1004] Remove some more code duplication in Good().

* [NOD-1004] Remove some more code duplication in Good().

* [NOD-1004] Remove code duplication in GetAddress().

* [NOD-1004] Remove code duplication in updateAddress.

* [NOD-1004] Remove some more code duplication in updateAddress.

* [NOD-1004] Remove redundant check in expireNew.

* [NOD-1004] Remove superfluous existence check from updateAddress.

* [NOD-1004] Make triedBucket use a slice instead of a list.

* [NOD-1004] Remove code duplication in getAddress.

* [NOD-1004] Remove infinite loops out of getAddress.

* [NOD-1004] Made impossible branch panic.

* [NOD-1004] Remove a mystery comment.

* [NOD-1004] Remove an unnecessary sort.

* [NOD-1004] Make AddressKey a type alias.

* [NOD-1004] Added comment for AddressKey

* [NOD-1004] Fix merge errors.

* [NOD-1004] Fix merge errors.

* [NOD-1004] Do some renaming.

* [NOD-1004] Do some more renaming.

* [NOD-1004] Rename AddrManager to AddressManager.

* [NOD-1004] Rename AddrManager to AddressManager.

* [NOD-1004] Do some more renaming.

* [NOD-1004] Rename bucket to addressBucketArray.

* [NOD-1004] Fix a comment.

* [NOD-1004] Rename na to netAddress.

* [NOD-1004] Bring back an existence check.

* [NOD-1004] Fix an error message.

* [NOD-1004] Fix a comment.

* [NOD-1004] Use a boolean instead of -1.

* [NOD-1004] Use a boolean instead of -1 in another place.

Co-authored-by: Mike Zak <feanorr@gmail.com>

* Fix merge errors.

Co-authored-by: Ori Newman <orinewman1@gmail.com>
Co-authored-by: Mike Zak <feanorr@gmail.com>
This commit is contained in:
stasatdaglabs 2020-07-26 15:23:18 +03:00 committed by GitHub
parent b2901454d6
commit 24a09fb3df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 1570 additions and 1550 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package addrmgr
package addressmanager
import (
"fmt"
@ -26,7 +26,7 @@ import (
// method.
type naTest struct {
in wire.NetAddress
want string
want AddressKey
}
// naTests houses all of the tests to be performed against the NetAddressKey
@ -97,7 +97,7 @@ func addNaTests() {
addNaTest("fef3::4:4", 8336, "[fef3::4:4]:8336")
}
func addNaTest(ip string, port uint16, want string) {
func addNaTest(ip string, port uint16, want AddressKey) {
nip := net.ParseIP(ip)
na := *wire.NewNetAddressIPPort(nip, port, wire.SFNodeNetwork)
test := naTest{na, want}
@ -109,7 +109,7 @@ func lookupFuncForTest(host string) ([]net.IP, error) {
}
func newAddrManagerForTest(t *testing.T, testName string,
localSubnetworkID *subnetworkid.SubnetworkID) (addressManager *AddrManager, teardown func()) {
localSubnetworkID *subnetworkid.SubnetworkID) (addressManager *AddressManager, teardown func()) {
cfg := config.DefaultConfig()
cfg.SubnetworkID = localSubnetworkID
@ -302,7 +302,7 @@ func TestNeedMoreAddresses(t *testing.T) {
var err error
for i := 0; i < addrsToAdd; i++ {
s := fmt.Sprintf("%d.%d.173.147:8333", i/128+60, i%128+60)
s := AddressKey(fmt.Sprintf("%d.%d.173.147:8333", i/128+60, i%128+60))
addrs[i], err = amgr.DeserializeNetAddress(s)
if err != nil {
t.Errorf("Failed to turn %s into an address: %v", s, err)
@ -333,7 +333,7 @@ func TestGood(t *testing.T) {
var err error
for i := 0; i < addrsToAdd; i++ {
s := fmt.Sprintf("%d.173.147.%d:8333", i/64+60, i%64+60)
s := AddressKey(fmt.Sprintf("%d.173.147.%d:8333", i/64+60, i%64+60))
addrs[i], err = amgr.DeserializeNetAddress(s)
if err != nil {
t.Errorf("Failed to turn %s into an address: %v", s, err)
@ -382,8 +382,8 @@ func TestGoodChangeSubnetworkID(t *testing.T) {
amgr.AddAddress(addr, srcAddr, oldSubnetwork)
amgr.Good(addr, oldSubnetwork)
// make sure address was saved to addrIndex under oldSubnetwork
ka := amgr.find(addr)
// make sure address was saved to addressIndex under oldSubnetwork
ka := amgr.knownAddress(addr)
if ka == nil {
t.Fatalf("Address was not found after first time .Good called")
}
@ -392,10 +392,10 @@ func TestGoodChangeSubnetworkID(t *testing.T) {
}
// make sure address was added to correct bucket under oldSubnetwork
bucket := amgr.addrTried[*oldSubnetwork][amgr.getTriedBucket(addr)]
bucket := amgr.subnetworkTriedAddresBucketArrays[*oldSubnetwork][amgr.triedAddressBucketIndex(addr)]
wasFound := false
for e := bucket.Front(); e != nil; e = e.Next() {
if NetAddressKey(e.Value.(*KnownAddress).NetAddress()) == addrKey {
for _, ka := range bucket {
if NetAddressKey(ka.NetAddress()) == addrKey {
wasFound = true
}
}
@ -407,8 +407,8 @@ func TestGoodChangeSubnetworkID(t *testing.T) {
newSubnetwork := subnetworkid.SubnetworkIDRegistry
amgr.Good(addr, newSubnetwork)
// make sure address was updated in addrIndex under newSubnetwork
ka = amgr.find(addr)
// make sure address was updated in addressIndex under newSubnetwork
ka = amgr.knownAddress(addr)
if ka == nil {
t.Fatalf("Address was not found after second time .Good called")
}
@ -417,10 +417,10 @@ func TestGoodChangeSubnetworkID(t *testing.T) {
}
// make sure address was removed from bucket under oldSubnetwork
bucket = amgr.addrTried[*oldSubnetwork][amgr.getTriedBucket(addr)]
bucket = amgr.subnetworkTriedAddresBucketArrays[*oldSubnetwork][amgr.triedAddressBucketIndex(addr)]
wasFound = false
for e := bucket.Front(); e != nil; e = e.Next() {
if NetAddressKey(e.Value.(*KnownAddress).NetAddress()) == addrKey {
for _, ka := range bucket {
if NetAddressKey(ka.NetAddress()) == addrKey {
wasFound = true
}
}
@ -429,10 +429,10 @@ func TestGoodChangeSubnetworkID(t *testing.T) {
}
// make sure address was added to correct bucket under newSubnetwork
bucket = amgr.addrTried[*newSubnetwork][amgr.getTriedBucket(addr)]
bucket = amgr.subnetworkTriedAddresBucketArrays[*newSubnetwork][amgr.triedAddressBucketIndex(addr)]
wasFound = false
for e := bucket.Front(); e != nil; e = e.Next() {
if NetAddressKey(e.Value.(*KnownAddress).NetAddress()) == addrKey {
for _, ka := range bucket {
if NetAddressKey(ka.NetAddress()) == addrKey {
wasFound = true
}
}

View File

@ -1,5 +1,5 @@
/*
Package addrmgr implements concurrency safe Kaspa address manager.
Package addressmanager implements concurrency safe Kaspa address manager.
Address Manager Overview
@ -31,4 +31,4 @@ peers which no longer appear to be good peers as well as bias the selection
toward known good peers. The general idea is to make a best effort at only
providing usable addresses.
*/
package addrmgr
package addressmanager

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package addrmgr
package addressmanager
import (
"github.com/kaspanet/kaspad/util/mstime"
@ -19,6 +19,6 @@ func TstKnownAddressChance(ka *KnownAddress) float64 {
func TstNewKnownAddress(na *wire.NetAddress, attempts int,
lastattempt, lastsuccess mstime.Time, tried bool, refs int) *KnownAddress {
return &KnownAddress{na: na, attempts: attempts, lastattempt: lastattempt,
lastsuccess: lastsuccess, tried: tried, refs: refs}
return &KnownAddress{netAddress: na, attempts: attempts, lastAttempt: lastattempt,
lastSuccess: lastsuccess, tried: tried, referenceCount: refs}
}

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package addrmgr
package addressmanager
import (
"github.com/kaspanet/kaspad/util/mstime"
@ -16,20 +16,20 @@ import (
// KnownAddress tracks information about a known network address that is used
// to determine how viable an address is.
type KnownAddress struct {
na *wire.NetAddress
srcAddr *wire.NetAddress
attempts int
lastattempt mstime.Time
lastsuccess mstime.Time
tried bool
refs int // reference count of new buckets
subnetworkID *subnetworkid.SubnetworkID
netAddress *wire.NetAddress
sourceAddress *wire.NetAddress
attempts int
lastAttempt mstime.Time
lastSuccess mstime.Time
tried bool
referenceCount int // reference count of new buckets
subnetworkID *subnetworkid.SubnetworkID
}
// NetAddress returns the underlying wire.NetAddress associated with the
// known address.
func (ka *KnownAddress) NetAddress() *wire.NetAddress {
return ka.na
return ka.netAddress
}
// SubnetworkID returns the subnetwork ID of the known address.
@ -39,7 +39,7 @@ func (ka *KnownAddress) SubnetworkID() *subnetworkid.SubnetworkID {
// LastAttempt returns the last time the known address was attempted.
func (ka *KnownAddress) LastAttempt() mstime.Time {
return ka.lastattempt
return ka.lastAttempt
}
// chance returns the selection probability for a known address. The priority
@ -47,7 +47,7 @@ func (ka *KnownAddress) LastAttempt() mstime.Time {
// attempted and how often attempts to connect to it have failed.
func (ka *KnownAddress) chance() float64 {
now := mstime.Now()
lastAttempt := now.Sub(ka.lastattempt)
lastAttempt := now.Sub(ka.lastAttempt)
if lastAttempt < 0 {
lastAttempt = 0
@ -77,27 +77,27 @@ func (ka *KnownAddress) chance() float64 {
// All addresses that meet these criteria are assumed to be worthless and not
// worth keeping hold of.
func (ka *KnownAddress) isBad() bool {
if ka.lastattempt.After(mstime.Now().Add(-1 * time.Minute)) {
if ka.lastAttempt.After(mstime.Now().Add(-1 * time.Minute)) {
return false
}
// From the future?
if ka.na.Timestamp.After(mstime.Now().Add(10 * time.Minute)) {
if ka.netAddress.Timestamp.After(mstime.Now().Add(10 * time.Minute)) {
return true
}
// Over a month old?
if ka.na.Timestamp.Before(mstime.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
if ka.netAddress.Timestamp.Before(mstime.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
return true
}
// Never succeeded?
if ka.lastsuccess.IsZero() && ka.attempts >= numRetries {
if ka.lastSuccess.IsZero() && ka.attempts >= numRetries {
return true
}
// Hasn't succeeded in too long?
if !ka.lastsuccess.After(mstime.Now().Add(-1*minBadDays*time.Hour*24)) &&
if !ka.lastSuccess.After(mstime.Now().Add(-1*minBadDays*time.Hour*24)) &&
ka.attempts >= maxFailures {
return true
}

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package addrmgr_test
package addressmanager_test
import (
"github.com/kaspanet/kaspad/util/mstime"
@ -10,39 +10,39 @@ import (
"testing"
"time"
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/wire"
)
func TestChance(t *testing.T) {
now := mstime.Now()
var tests = []struct {
addr *addrmgr.KnownAddress
addr *addressmanager.KnownAddress
expected float64
}{
{
//Test normal case
addrmgr.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
addressmanager.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
0, mstime.Now().Add(-30*time.Minute), mstime.Now(), false, 0),
1.0,
}, {
//Test case in which lastseen < 0
addrmgr.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(20 * time.Second)},
addressmanager.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(20 * time.Second)},
0, mstime.Now().Add(-30*time.Minute), mstime.Now(), false, 0),
1.0,
}, {
//Test case in which lastattempt < 0
addrmgr.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
//Test case in which lastAttempt < 0
addressmanager.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
0, mstime.Now().Add(30*time.Minute), mstime.Now(), false, 0),
1.0 * .01,
}, {
//Test case in which lastattempt < ten minutes
addrmgr.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
//Test case in which lastAttempt < ten minutes
addressmanager.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
0, mstime.Now().Add(-5*time.Minute), mstime.Now(), false, 0),
1.0 * .01,
}, {
//Test case with several failed attempts.
addrmgr.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
addressmanager.TstNewKnownAddress(&wire.NetAddress{Timestamp: now.Add(-35 * time.Second)},
2, mstime.Now().Add(-30*time.Minute), mstime.Now(), false, 0),
1 / 1.5 / 1.5,
},
@ -50,7 +50,7 @@ func TestChance(t *testing.T) {
err := .0001
for i, test := range tests {
chance := addrmgr.TstKnownAddressChance(test.addr)
chance := addressmanager.TstKnownAddressChance(test.addr)
if math.Abs(test.expected-chance) >= err {
t.Errorf("case %d: got %f, expected %f", i, chance, test.expected)
}
@ -72,44 +72,44 @@ func TestIsBad(t *testing.T) {
currentNa := &wire.NetAddress{Timestamp: secondsOld}
//Test addresses that have been tried in the last minute.
if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(futureNa, 3, secondsOld, zeroTime, false, 0)) {
if addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(futureNa, 3, secondsOld, zeroTime, false, 0)) {
t.Errorf("test case 1: addresses that have been tried in the last minute are not bad.")
}
if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(monthOldNa, 3, secondsOld, zeroTime, false, 0)) {
if addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(monthOldNa, 3, secondsOld, zeroTime, false, 0)) {
t.Errorf("test case 2: addresses that have been tried in the last minute are not bad.")
}
if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 3, secondsOld, zeroTime, false, 0)) {
if addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(currentNa, 3, secondsOld, zeroTime, false, 0)) {
t.Errorf("test case 3: addresses that have been tried in the last minute are not bad.")
}
if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 3, secondsOld, monthOld, true, 0)) {
if addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(currentNa, 3, secondsOld, monthOld, true, 0)) {
t.Errorf("test case 4: addresses that have been tried in the last minute are not bad.")
}
if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(currentNa, 2, secondsOld, secondsOld, true, 0)) {
if addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(currentNa, 2, secondsOld, secondsOld, true, 0)) {
t.Errorf("test case 5: addresses that have been tried in the last minute are not bad.")
}
//Test address that claims to be from the future.
if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(futureNa, 0, minutesOld, hoursOld, true, 0)) {
if !addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(futureNa, 0, minutesOld, hoursOld, true, 0)) {
t.Errorf("test case 6: addresses that claim to be from the future are bad.")
}
//Test address that has not been seen in over a month.
if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(monthOldNa, 0, minutesOld, hoursOld, true, 0)) {
if !addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(monthOldNa, 0, minutesOld, hoursOld, true, 0)) {
t.Errorf("test case 7: addresses more than a month old are bad.")
}
//It has failed at least three times and never succeeded.
if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 3, minutesOld, zeroTime, true, 0)) {
if !addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(minutesOldNa, 3, minutesOld, zeroTime, true, 0)) {
t.Errorf("test case 8: addresses that have never succeeded are bad.")
}
//It has failed ten times in the last week
if !addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 10, minutesOld, monthOld, true, 0)) {
if !addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(minutesOldNa, 10, minutesOld, monthOld, true, 0)) {
t.Errorf("test case 9: addresses that have not succeeded in too long are bad.")
}
//Test an address that should work.
if addrmgr.TstKnownAddressIsBad(addrmgr.TstNewKnownAddress(minutesOldNa, 2, minutesOld, hoursOld, true, 0)) {
if addressmanager.TstKnownAddressIsBad(addressmanager.TstNewKnownAddress(minutesOldNa, 2, minutesOld, hoursOld, true, 0)) {
t.Errorf("test case 10: This should be a valid address.")
}
}

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package addrmgr
package addressmanager
import (
"github.com/kaspanet/kaspad/logger"

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package addrmgr
package addressmanager
import (
"net"
@ -200,8 +200,8 @@ func IsValid(na *wire.NetAddress) bool {
// IsRoutable returns whether or not the passed address is routable over
// the public internet. This is true as long as the address is valid and is not
// in any reserved ranges.
func (a *AddrManager) IsRoutable(na *wire.NetAddress) bool {
if a.cfg.NetParams().AcceptUnroutable {
func (am *AddressManager) IsRoutable(na *wire.NetAddress) bool {
if am.cfg.NetParams().AcceptUnroutable {
return !IsLocal(na)
}
@ -215,11 +215,11 @@ func (a *AddrManager) IsRoutable(na *wire.NetAddress) bool {
// of. This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
// "local" for a local address, and the string "unroutable" for an unroutable
// address.
func (a *AddrManager) GroupKey(na *wire.NetAddress) string {
func (am *AddressManager) GroupKey(na *wire.NetAddress) string {
if IsLocal(na) {
return "local"
}
if !a.IsRoutable(na) {
if !am.IsRoutable(na) {
return "unroutable"
}
if IsIPv4(na) {

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package addrmgr
package addressmanager
import (
"net"

File diff suppressed because it is too large Load Diff

View File

@ -224,7 +224,7 @@ func (node *blockNode) isGenesis() bool {
}
func (node *blockNode) finalityScore(dag *BlockDAG) uint64 {
return node.blueScore / uint64(dag.Params.FinalityInterval)
return node.blueScore / uint64(dag.FinalityInterval())
}
// String returns a string that contains the block hash.

View File

@ -51,14 +51,14 @@ func TestBlueBlockWindow(t *testing.T) {
expectedWindowWithGenesisPadding: []string{"B", "A", "A", "A", "A", "A", "A", "A", "A", "A"},
},
{
parents: []string{"C", "D"},
parents: []string{"D", "C"},
id: "E",
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
},
{
parents: []string{"C", "D"},
parents: []string{"D", "C"},
id: "F",
expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"},
expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"},
},
{
parents: []string{"A"},
@ -73,37 +73,37 @@ func TestBlueBlockWindow(t *testing.T) {
{
parents: []string{"H", "F"},
id: "I",
expectedWindowWithGenesisPadding: []string{"F", "C", "D", "B", "A", "A", "A", "A", "A", "A"},
expectedWindowWithGenesisPadding: []string{"F", "D", "C", "B", "A", "A", "A", "A", "A", "A"},
},
{
parents: []string{"I"},
id: "J",
expectedWindowWithGenesisPadding: []string{"I", "F", "C", "D", "B", "A", "A", "A", "A", "A"},
expectedWindowWithGenesisPadding: []string{"I", "F", "D", "C", "B", "A", "A", "A", "A", "A"},
},
{
parents: []string{"J"},
id: "K",
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "C", "D", "B", "A", "A", "A", "A"},
expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "C", "B", "A", "A", "A", "A"},
},
{
parents: []string{"K"},
id: "L",
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "C", "D", "B", "A", "A", "A"},
expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "C", "B", "A", "A", "A"},
},
{
parents: []string{"L"},
id: "M",
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "C", "D", "B", "A", "A"},
expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "C", "B", "A", "A"},
},
{
parents: []string{"M"},
id: "N",
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "C", "D", "B", "A"},
expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "C", "B", "A"},
},
{
parents: []string{"N"},
id: "O",
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "C", "D", "B"},
expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "D", "C", "B"},
},
}

View File

@ -12,7 +12,6 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"github.com/kaspanet/kaspad/util/mstime"
@ -97,11 +96,9 @@ func (dag *BlockDAG) TestSetCoinbaseMaturity(maturity uint64) {
// use of it.
func newTestDAG(params *dagconfig.Params) *BlockDAG {
index := newBlockIndex(params)
targetTimePerBlock := int64(params.TargetTimePerBlock / time.Second)
dag := &BlockDAG{
Params: params,
timeSource: NewTimeSource(),
targetTimePerBlock: targetTimePerBlock,
difficultyAdjustmentWindowSize: params.DifficultyAdjustmentWindowSize,
TimestampDeviationTolerance: params.TimestampDeviationTolerance,
powMaxBits: util.BigToCompact(params.PowMax),

View File

@ -32,7 +32,9 @@ const (
// queued.
maxOrphanBlocks = 100
isDAGCurrentMaxDiff = 12 * time.Hour
// isDAGCurrentMaxDiff is the number of blocks from the network tips (estimated by timestamps) for the current
// to be considered not synced
isDAGCurrentMaxDiff = 40_000
)
// orphanBlock represents a block that we don't yet have the parent for. It
@ -74,7 +76,6 @@ type BlockDAG struct {
// parameters. They are also set when the instance is created and
// can't be changed afterwards, so there is no need to protect them with
// a separate mutex.
targetTimePerBlock int64 // The target delay between blocks (in seconds)
difficultyAdjustmentWindowSize uint64
TimestampDeviationTolerance uint64
@ -177,7 +178,6 @@ func New(config *Config) (*BlockDAG, error) {
}
params := config.DAGParams
targetTimePerBlock := int64(params.TargetTimePerBlock / time.Second)
index := newBlockIndex(params)
dag := &BlockDAG{
@ -186,7 +186,6 @@ func New(config *Config) (*BlockDAG, error) {
timeSource: config.TimeSource,
sigCache: config.SigCache,
indexManager: config.IndexManager,
targetTimePerBlock: targetTimePerBlock,
difficultyAdjustmentWindowSize: params.DifficultyAdjustmentWindowSize,
TimestampDeviationTolerance: params.TimestampDeviationTolerance,
powMaxBits: util.BigToCompact(params.PowMax),
@ -929,6 +928,11 @@ func (dag *BlockDAG) isInSelectedParentChainOf(node *blockNode, other *blockNode
return dag.reachabilityTree.isReachabilityTreeAncestorOf(node, other)
}
// FinalityInterval is the interval that determines the finality window of the DAG.
func (dag *BlockDAG) FinalityInterval() uint64 {
return uint64(dag.Params.FinalityDuration / dag.Params.TargetTimePerBlock)
}
// checkFinalityViolation checks the new block does not violate the finality rules
// specifically - the new block selectedParent chain should contain the old finality point.
func (dag *BlockDAG) checkFinalityViolation(newNode *blockNode) error {
@ -991,7 +995,7 @@ func (dag *BlockDAG) finalizeNodesBelowFinalityPoint(deleteDiffData bool) {
}
var nodesToDelete []*blockNode
if deleteDiffData {
nodesToDelete = make([]*blockNode, 0, dag.Params.FinalityInterval)
nodesToDelete = make([]*blockNode, 0, dag.FinalityInterval())
}
for len(queue) > 0 {
var current *blockNode
@ -1448,7 +1452,7 @@ func (dag *BlockDAG) isSynced() bool {
dagTimestamp = selectedTip.timestamp
}
dagTime := mstime.UnixMilliseconds(dagTimestamp)
return dag.Now().Sub(dagTime) <= isDAGCurrentMaxDiff
return dag.Now().Sub(dagTime) <= isDAGCurrentMaxDiff*dag.Params.TargetTimePerBlock
}
// Now returns the adjusted time according to

View File

@ -957,7 +957,7 @@ func testFinalizeNodesBelowFinalityPoint(t *testing.T, deleteDiffData bool) {
flushUTXODiffStore()
return node
}
finalityInterval := dag.Params.FinalityInterval
finalityInterval := dag.FinalityInterval()
nodes := make([]*blockNode, 0, finalityInterval)
currentNode := dag.genesis
nodes = append(nodes, currentNode)
@ -1133,7 +1133,7 @@ func TestIsDAGCurrentMaxDiff(t *testing.T) {
&dagconfig.SimnetParams,
}
for _, params := range netParams {
if params.TargetTimePerBlock*time.Duration(params.FinalityInterval) < isDAGCurrentMaxDiff {
if params.FinalityDuration < isDAGCurrentMaxDiff*params.TargetTimePerBlock {
t.Errorf("in %s, a DAG can be considered current even if it's below the finality point", params.Name)
}
}

View File

@ -41,7 +41,7 @@ import (
func TestFinality(t *testing.T) {
params := dagconfig.SimnetParams
params.K = 1
params.FinalityInterval = 100
params.FinalityDuration = 100 * params.TargetTimePerBlock
dag, teardownFunc, err := blockdag.DAGSetup("TestFinality", true, blockdag.Config{
DAGParams: &params,
})
@ -75,7 +75,7 @@ func TestFinality(t *testing.T) {
currentNode := genesis
// First we build a chain of params.FinalityInterval blocks for future use
for i := uint64(0); i < params.FinalityInterval; i++ {
for i := uint64(0); i < dag.FinalityInterval(); i++ {
currentNode, err = buildNodeToDag([]*daghash.Hash{currentNode.Hash()})
if err != nil {
t.Fatalf("TestFinality: buildNodeToDag unexpectedly returned an error: %v", err)
@ -87,7 +87,7 @@ func TestFinality(t *testing.T) {
// Now we build a new chain of 2 * params.FinalityInterval blocks, pointed to genesis, and
// we expect the block with height 1 * params.FinalityInterval to be the last finality point
currentNode = genesis
for i := uint64(0); i < params.FinalityInterval; i++ {
for i := uint64(0); i < dag.FinalityInterval(); i++ {
currentNode, err = buildNodeToDag([]*daghash.Hash{currentNode.Hash()})
if err != nil {
t.Fatalf("TestFinality: buildNodeToDag unexpectedly returned an error: %v", err)
@ -96,7 +96,7 @@ func TestFinality(t *testing.T) {
expectedFinalityPoint := currentNode
for i := uint64(0); i < params.FinalityInterval; i++ {
for i := uint64(0); i < dag.FinalityInterval(); i++ {
currentNode, err = buildNodeToDag([]*daghash.Hash{currentNode.Hash()})
if err != nil {
t.Fatalf("TestFinality: buildNodeToDag unexpectedly returned an error: %v", err)
@ -176,9 +176,19 @@ func TestFinalityInterval(t *testing.T) {
&dagconfig.SimnetParams,
}
for _, params := range netParams {
if params.FinalityInterval > wire.MaxInvPerMsg {
t.Errorf("FinalityInterval in %s should be lower or equal to wire.MaxInvPerMsg", params.Name)
}
func() {
dag, teardownFunc, err := blockdag.DAGSetup("TestFinalityInterval", true, blockdag.Config{
DAGParams: params,
})
if err != nil {
t.Fatalf("Failed to setup dag instance for %s: %v", params.Name, err)
}
defer teardownFunc()
if dag.FinalityInterval() > wire.MaxInvPerMsg {
t.Errorf("FinalityInterval in %s should be lower or equal to wire.MaxInvPerMsg", params.Name)
}
}()
}
}

View File

@ -34,7 +34,7 @@ func TestGHOSTDAG(t *testing.T) {
}{
{
k: 3,
expectedReds: []string{"F", "G", "H", "I", "O", "Q"},
expectedReds: []string{"F", "G", "H", "I", "N", "P"},
dagData: []*testBlockData{
{
parents: []string{"A"},
@ -167,7 +167,7 @@ func TestGHOSTDAG(t *testing.T) {
id: "T",
expectedScore: 13,
expectedSelectedParent: "S",
expectedBlues: []string{"S", "P", "N"},
expectedBlues: []string{"S", "O", "Q"},
},
},
},

View File

@ -106,7 +106,7 @@ func (dag *BlockDAG) NextCoinbaseFromAddress(payToAddress util.Address, extraDat
// the median timestamp of the last several blocks per the DAG consensus
// rules.
func (dag *BlockDAG) NextBlockMinimumTime() mstime.Time {
return dag.CalcPastMedianTime().Add(time.Second)
return dag.CalcPastMedianTime().Add(time.Millisecond)
}
// NextBlockTime returns a valid block time for the

View File

@ -162,8 +162,7 @@ func (dag *BlockDAG) processBlockNoLock(block *util.Block, flags BehaviorFlags)
// The block must not already exist in the DAG.
if dag.IsInDAG(blockHash) && !wasBlockStored {
str := fmt.Sprintf("already have block %s", blockHash)
return false, false, ruleError(ErrDuplicateBlock, str)
return false, false, errors.Errorf("already have block %s", blockHash)
}
// The block must not already exist as an orphan.

View File

@ -101,7 +101,7 @@ func TestProcessDelayedBlocks(t *testing.T) {
t.Fatalf("error in PrepareBlockForTest: %s", err)
}
blockDelay := time.Duration(dag1.Params.TimestampDeviationTolerance*uint64(dag1.targetTimePerBlock)+5) * time.Second
blockDelay := time.Duration(dag1.Params.TimestampDeviationTolerance)*dag1.Params.TargetTimePerBlock + 5*time.Second
delayedBlock.Header.Timestamp = initialTime.Add(blockDelay)
isOrphan, isDelayed, err := dag1.ProcessBlock(util.NewBlock(delayedBlock), BFNoPoWCheck)
@ -203,9 +203,9 @@ func TestProcessDelayedBlocks(t *testing.T) {
}
// We advance the clock to the point where delayedBlock timestamp is valid.
deviationTolerance := int64(dag2.TimestampDeviationTolerance) * dag2.targetTimePerBlock
deviationTolerance := time.Duration(dag2.TimestampDeviationTolerance) * dag2.Params.TargetTimePerBlock
timeUntilDelayedBlockIsValid := delayedBlock.Header.Timestamp.
Add(-time.Duration(deviationTolerance)*time.Second).
Add(-deviationTolerance).
Sub(dag2.Now()) +
time.Second
dag2.timeSource = newFakeTimeSource(initialTime.Add(timeUntilDelayedBlockIsValid))

View File

@ -417,8 +417,7 @@ func (dag *BlockDAG) checkBlockHeaderSanity(block *util.Block, flags BehaviorFla
// the duration of time that should be waited before the block becomes valid.
// This check needs to be last as it does not return an error but rather marks the
// header as delayed (and valid).
maxTimestamp := dag.Now().Add(time.Second *
time.Duration(int64(dag.TimestampDeviationTolerance)*dag.targetTimePerBlock))
maxTimestamp := dag.Now().Add(time.Duration(dag.TimestampDeviationTolerance) * dag.Params.TargetTimePerBlock)
if header.Timestamp.After(maxTimestamp) {
return header.Timestamp.Sub(maxTimestamp), nil
}

View File

@ -479,7 +479,7 @@ func TestCheckBlockSanity(t *testing.T) {
blockInTheFuture := Block100000
expectedDelay := 10 * time.Second
deviationTolerance := time.Duration(dag.TimestampDeviationTolerance*uint64(dag.targetTimePerBlock)) * time.Second
deviationTolerance := time.Duration(dag.TimestampDeviationTolerance) * dag.Params.TargetTimePerBlock
blockInTheFuture.Header.Timestamp = dag.Now().Add(deviationTolerance + expectedDelay)
delay, err = dag.checkBlockSanity(util.NewBlock(&blockInTheFuture), BFNoPoWCheck)
if err != nil {

View File

@ -1,11 +1,11 @@
package connmanager
import (
"github.com/kaspanet/kaspad/addressmanager"
"sync"
"sync/atomic"
"time"
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/netadapter"
"github.com/kaspanet/kaspad/config"
@ -24,7 +24,7 @@ type connectionRequest struct {
type ConnectionManager struct {
cfg *config.Config
netAdapter *netadapter.NetAdapter
addressManager *addrmgr.AddrManager
addressManager *addressmanager.AddressManager
activeRequested map[string]*connectionRequest
pendingRequested map[string]*connectionRequest
@ -44,7 +44,7 @@ type ConnectionManager struct {
}
// New instantiates a new instance of a ConnectionManager
func New(cfg *config.Config, netAdapter *netadapter.NetAdapter, addressManager *addrmgr.AddrManager) (*ConnectionManager, error) {
func New(cfg *config.Config, netAdapter *netadapter.NetAdapter, addressManager *addressmanager.AddressManager) (*ConnectionManager, error) {
c := &ConnectionManager{
cfg: cfg,
netAdapter: netAdapter,

View File

@ -28,10 +28,10 @@ var genesisCoinbaseTx = wire.NewSubnetworkMsgTx(1, []*wire.TxIn{}, genesisTxOuts
// genesisHash is the hash of the first block in the block DAG for the main
// network (genesis block).
var genesisHash = daghash.Hash{
0xed, 0xac, 0xec, 0x9a, 0x9a, 0xc7, 0x24, 0x59,
0x3a, 0x02, 0x64, 0xcd, 0x6b, 0x09, 0x63, 0x48,
0x5a, 0xb5, 0x6f, 0xad, 0x92, 0x26, 0x5b, 0x99,
0xb8, 0xe0, 0x25, 0x86, 0xba, 0x38, 0x87, 0x52,
0xfa, 0x00, 0xbd, 0xcb, 0x46, 0x74, 0xc5, 0xdb,
0xf7, 0x63, 0xcb, 0x78, 0x7a, 0x94, 0xc5, 0xbf,
0xd4, 0x81, 0xd3, 0x52, 0x2d, 0x79, 0xac, 0x57,
0x73, 0xe6, 0x14, 0x7e, 0x15, 0xef, 0x85, 0x27,
}
// genesisMerkleRoot is the hash of the first transaction in the genesis block
@ -52,9 +52,9 @@ var genesisBlock = wire.MsgBlock{
HashMerkleRoot: &genesisMerkleRoot,
AcceptedIDMerkleRoot: &daghash.Hash{},
UTXOCommitment: &daghash.ZeroHash,
Timestamp: mstime.UnixMilliseconds(0x17315ed0f99),
Timestamp: mstime.UnixMilliseconds(0x1730a81bdb4),
Bits: 0x207fffff,
Nonce: 0x0,
Nonce: 0x1,
},
Transactions: []*wire.MsgTx{genesisCoinbaseTx},
}
@ -126,10 +126,10 @@ var regtestGenesisCoinbaseTx = wire.NewSubnetworkMsgTx(1, []*wire.TxIn{}, regtes
// devGenesisHash is the hash of the first block in the block DAG for the development
// network (genesis block).
var regtestGenesisHash = daghash.Hash{
0x66, 0xed, 0x54, 0xe6, 0x33, 0xac, 0x61, 0x0d,
0xf3, 0x9d, 0x1f, 0xbb, 0x56, 0xec, 0x79, 0x18,
0x4a, 0x1f, 0xb3, 0xa4, 0xf7, 0xf6, 0x7d, 0x5e,
0xd8, 0xac, 0x9f, 0x35, 0x5d, 0x36, 0x00, 0x00,
0xda, 0x23, 0x61, 0x5e, 0xf6, 0x2a, 0x95, 0x27,
0x7f, 0x5a, 0x40, 0xd5, 0x91, 0x97, 0x1c, 0xef,
0xd5, 0x86, 0xac, 0xac, 0x82, 0xb3, 0xc9, 0x43,
0xd3, 0x49, 0x5f, 0x7e, 0x93, 0x0b, 0x35, 0x2d,
}
// regtestGenesisMerkleRoot is the hash of the first transaction in the genesis block
@ -150,9 +150,9 @@ var regtestGenesisBlock = wire.MsgBlock{
HashMerkleRoot: &regtestGenesisMerkleRoot,
AcceptedIDMerkleRoot: &daghash.Hash{},
UTXOCommitment: &daghash.ZeroHash,
Timestamp: mstime.UnixMilliseconds(0x1733106fd15),
Bits: 0x1e7fffff,
Nonce: 0x8184,
Timestamp: mstime.UnixMilliseconds(0x1730a958ac4),
Bits: 0x207fffff,
Nonce: 0x0,
},
Transactions: []*wire.MsgTx{regtestGenesisCoinbaseTx},
}

View File

@ -154,8 +154,8 @@ var genesisBlockBytes = []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x0f, 0xed, 0x15, 0x73, 0x01, 0x00, 0x00, 0xff, 0xff, 0x7f,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0xbd, 0x81, 0x0a, 0x73, 0x01, 0x00, 0x00, 0xff, 0xff, 0x7f,
0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc4, 0x41, 0xe6, 0x78, 0x1d, 0xf7, 0xb3, 0x39, 0x66, 0x4d, 0x1a, 0x03,
@ -174,8 +174,8 @@ var regtestGenesisBlockBytes = []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0xfd, 0x06, 0x31, 0x73, 0x01, 0x00, 0x00, 0xff, 0xff, 0x7f,
0x1e, 0x84, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xc4, 0x8a, 0x95, 0x0a, 0x73, 0x01, 0x00, 0x00, 0xff, 0xff, 0x7f,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xd4, 0xc4, 0x87, 0x77, 0xf2, 0xe7, 0x5d, 0xf7, 0xff, 0x2d, 0xbb, 0xb6,

View File

@ -53,7 +53,6 @@ const (
timestampDeviationTolerance = 132
finalityDuration = 24 * time.Hour
targetTimePerBlock = 1 * time.Second
finalityInterval = uint64(finalityDuration / targetTimePerBlock)
)
// ConsensusDeployment defines details related to a specific consensus rule
@ -136,8 +135,8 @@ type Params struct {
// block.
TargetTimePerBlock time.Duration
// FinalityInterval is the interval that determines the finality window of the DAG.
FinalityInterval uint64
// FinalityDuration is the duration of the finality window.
FinalityDuration time.Duration
// TimestampDeviationTolerance is the maximum offset a block timestamp
// is allowed to be in the future before it gets delayed
@ -203,7 +202,7 @@ var MainnetParams = Params{
BlockCoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimePerBlock: targetTimePerBlock,
FinalityInterval: finalityInterval,
FinalityDuration: finalityDuration,
DifficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
TimestampDeviationTolerance: timestampDeviationTolerance,
@ -256,7 +255,7 @@ var RegressionNetParams = Params{
BlockCoinbaseMaturity: 100,
SubsidyReductionInterval: 150,
TargetTimePerBlock: targetTimePerBlock,
FinalityInterval: finalityInterval,
FinalityDuration: finalityDuration,
DifficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
TimestampDeviationTolerance: timestampDeviationTolerance,
@ -307,7 +306,7 @@ var TestnetParams = Params{
BlockCoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimePerBlock: targetTimePerBlock,
FinalityInterval: finalityInterval,
FinalityDuration: finalityDuration,
DifficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
TimestampDeviationTolerance: timestampDeviationTolerance,
@ -363,8 +362,8 @@ var SimnetParams = Params{
PowMax: simnetPowMax,
BlockCoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimePerBlock: targetTimePerBlock,
FinalityInterval: finalityInterval,
TargetTimePerBlock: time.Millisecond,
FinalityDuration: time.Minute,
DifficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
TimestampDeviationTolerance: timestampDeviationTolerance,
@ -413,7 +412,7 @@ var DevnetParams = Params{
BlockCoinbaseMaturity: 100,
SubsidyReductionInterval: 210000,
TargetTimePerBlock: targetTimePerBlock,
FinalityInterval: finalityInterval,
FinalityDuration: finalityDuration,
DifficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
TimestampDeviationTolerance: timestampDeviationTolerance,

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"github.com/kaspanet/kaspad/addressmanager"
"sync/atomic"
"github.com/kaspanet/kaspad/dbaccess"
@ -11,8 +12,6 @@ import (
"github.com/kaspanet/kaspad/connmanager"
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/netadapter"
"github.com/kaspanet/kaspad/util/panics"
@ -33,7 +32,7 @@ import (
type kaspad struct {
cfg *config.Config
rpcServer *rpc.Server
addressManager *addrmgr.AddrManager
addressManager *addressmanager.AddressManager
protocolManager *protocol.Manager
connectionManager *connmanager.ConnectionManager
@ -123,7 +122,7 @@ func newKaspad(cfg *config.Config, databaseContext *dbaccess.DatabaseContext, in
if err != nil {
return nil, err
}
addressManager := addrmgr.New(cfg, databaseContext)
addressManager := addressmanager.New(cfg, databaseContext)
connectionManager, err := connmanager.New(cfg, netAdapter, addressManager)
if err != nil {
@ -203,7 +202,7 @@ func setupMempool(cfg *config.Config, dag *blockdag.BlockDAG, sigCache *txscript
func setupRPC(cfg *config.Config, dag *blockdag.BlockDAG, txMempool *mempool.TxPool, sigCache *txscript.SigCache,
acceptanceIndex *indexers.AcceptanceIndex, connectionManager *connmanager.ConnectionManager,
addressManager *addrmgr.AddrManager, protocolManager *protocol.Manager) (*rpc.Server, error) {
addressManager *addressmanager.AddressManager, protocolManager *protocol.Manager) (*rpc.Server, error) {
if !cfg.DisableRPC {
policy := mining.Policy{

View File

@ -1,8 +1,10 @@
package flowcontext
import "github.com/kaspanet/kaspad/addrmgr"
import (
"github.com/kaspanet/kaspad/addressmanager"
)
// AddressManager returns the address manager associated to the flow context.
func (f *FlowContext) AddressManager() *addrmgr.AddrManager {
func (f *FlowContext) AddressManager() *addressmanager.AddressManager {
return f.addressManager
}

View File

@ -1,7 +1,7 @@
package flowcontext
import (
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/blockdag"
"github.com/kaspanet/kaspad/config"
"github.com/kaspanet/kaspad/connmanager"
@ -24,7 +24,7 @@ type FlowContext struct {
netAdapter *netadapter.NetAdapter
txPool *mempool.TxPool
dag *blockdag.BlockDAG
addressManager *addrmgr.AddrManager
addressManager *addressmanager.AddressManager
connectionManager *connmanager.ConnectionManager
transactionsToRebroadcastLock sync.Mutex
@ -43,7 +43,7 @@ type FlowContext struct {
}
// New returns a new instance of FlowContext.
func New(cfg *config.Config, dag *blockdag.BlockDAG, addressManager *addrmgr.AddrManager,
func New(cfg *config.Config, dag *blockdag.BlockDAG, addressManager *addressmanager.AddressManager,
txPool *mempool.TxPool, netAdapter *netadapter.NetAdapter,
connectionManager *connmanager.ConnectionManager) *FlowContext {

View File

@ -1,7 +1,7 @@
package addressexchange
import (
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/config"
"github.com/kaspanet/kaspad/netadapter/router"
"github.com/kaspanet/kaspad/protocol/common"
@ -13,7 +13,7 @@ import (
// ReceiveAddressesContext is the interface for the context needed for the ReceiveAddresses flow.
type ReceiveAddressesContext interface {
Config() *config.Config
AddressManager() *addrmgr.AddrManager
AddressManager() *addressmanager.AddressManager
}
// ReceiveAddresses asks a peer for more addresses if needed.
@ -37,8 +37,8 @@ func ReceiveAddresses(context ReceiveAddressesContext, incomingRoute *router.Rou
}
msgAddresses := message.(*wire.MsgAddresses)
if len(msgAddresses.AddrList) > addrmgr.GetAddressesMax {
return protocolerrors.Errorf(true, "address count excceeded %d", addrmgr.GetAddressesMax)
if len(msgAddresses.AddrList) > addressmanager.GetAddressesMax {
return protocolerrors.Errorf(true, "address count excceeded %d", addressmanager.GetAddressesMax)
}
if msgAddresses.IncludeAllSubnetworks {

View File

@ -1,7 +1,7 @@
package addressexchange
import (
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/netadapter/router"
"github.com/kaspanet/kaspad/wire"
"math/rand"
@ -9,7 +9,7 @@ import (
// SendAddressesContext is the interface for the context needed for the SendAddresses flow.
type SendAddressesContext interface {
AddressManager() *addrmgr.AddrManager
AddressManager() *addressmanager.AddressManager
}
// SendAddresses sends addresses to a peer that requests it.

View File

@ -1,12 +1,12 @@
package handshake
import (
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/protocol/common"
"github.com/kaspanet/kaspad/protocol/protocolerrors"
"sync"
"sync/atomic"
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/blockdag"
"github.com/kaspanet/kaspad/config"
"github.com/kaspanet/kaspad/netadapter"
@ -23,7 +23,7 @@ type HandleHandshakeContext interface {
Config() *config.Config
NetAdapter() *netadapter.NetAdapter
DAG() *blockdag.BlockDAG
AddressManager() *addrmgr.AddrManager
AddressManager() *addressmanager.AddressManager
StartIBDIfRequired()
AddToPeers(peer *peerpkg.Peer) error
}

View File

@ -1,7 +1,7 @@
package protocol
import (
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/blockdag"
"github.com/kaspanet/kaspad/config"
"github.com/kaspanet/kaspad/connmanager"
@ -21,7 +21,7 @@ type Manager struct {
// NewManager creates a new instance of the p2p protocol manager
func NewManager(cfg *config.Config, dag *blockdag.BlockDAG,
addressManager *addrmgr.AddrManager, txPool *mempool.TxPool,
addressManager *addressmanager.AddressManager, txPool *mempool.TxPool,
connectionManager *connmanager.ConnectionManager) (*Manager, error) {
netAdapter, err := netadapter.NewNetAdapter(cfg)

View File

@ -1,6 +1,9 @@
package rpc
import "github.com/kaspanet/kaspad/rpc/model"
import (
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/rpc/model"
)
// handleGetPeerAddresses handles getPeerAddresses commands.
func handleGetPeerAddresses(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
@ -21,8 +24,8 @@ func handleGetPeerAddresses(s *Server, cmd interface{}, closeChan <-chan struct{
for i, addr := range peersState.Addresses {
rpcPeersState.Addresses[i] = &model.GetPeerAddressesKnownAddressResult{
Addr: addr.Addr,
Src: addr.Src,
Addr: string(addr.Address),
Src: string(addr.SourceAddress),
SubnetworkID: addr.SubnetworkID,
Attempts: addr.Attempts,
TimeStamp: addr.TimeStamp,
@ -31,27 +34,35 @@ func handleGetPeerAddresses(s *Server, cmd interface{}, closeChan <-chan struct{
}
}
for subnetworkID, bucket := range peersState.NewBuckets {
for subnetworkID, bucket := range peersState.SubnetworkNewAddressBucketArrays {
rpcPeersState.NewBuckets[subnetworkID] = &model.GetPeerAddressesNewBucketResult{}
for i, addr := range bucket {
rpcPeersState.NewBuckets[subnetworkID][i] = addr
rpcPeersState.NewBuckets[subnetworkID][i] = convertAddressKeySliceToString(addr)
}
}
for i, addr := range peersState.NewBucketFullNodes {
rpcPeersState.NewBucketFullNodes[i] = addr
for i, addr := range peersState.FullNodeNewAddressBucketArray {
rpcPeersState.NewBucketFullNodes[i] = convertAddressKeySliceToString(addr)
}
for subnetworkID, bucket := range peersState.TriedBuckets {
for subnetworkID, bucket := range peersState.SubnetworkTriedAddressBucketArrays {
rpcPeersState.TriedBuckets[subnetworkID] = &model.GetPeerAddressesTriedBucketResult{}
for i, addr := range bucket {
rpcPeersState.TriedBuckets[subnetworkID][i] = addr
rpcPeersState.TriedBuckets[subnetworkID][i] = convertAddressKeySliceToString(addr)
}
}
for i, addr := range peersState.TriedBucketFullNodes {
rpcPeersState.TriedBucketFullNodes[i] = addr
for i, addr := range peersState.FullNodeTriedAddressBucketArray {
rpcPeersState.TriedBucketFullNodes[i] = convertAddressKeySliceToString(addr)
}
return rpcPeersState, nil
}
func convertAddressKeySliceToString(addressKeys []addressmanager.AddressKey) []string {
strings := make([]string, len(addressKeys))
for j, addr := range addressKeys {
strings[j] = string(addr)
}
return strings
}

View File

@ -6,7 +6,7 @@ package model
import (
"encoding/json"
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/addressmanager"
)
// GetBlockHeaderVerboseResult models the data from the getblockheader command when
@ -240,10 +240,10 @@ type GetPeerAddressesKnownAddressResult struct {
}
// GetPeerAddressesNewBucketResult models a GetPeerAddressesResult new bucket.
type GetPeerAddressesNewBucketResult [addrmgr.NewBucketCount][]string
type GetPeerAddressesNewBucketResult [addressmanager.NewBucketCount][]string
// GetPeerAddressesTriedBucketResult models a GetPeerAddressesResult tried bucket.
type GetPeerAddressesTriedBucketResult [addrmgr.TriedBucketCount][]string
type GetPeerAddressesTriedBucketResult [addressmanager.TriedBucketCount][]string
// GetRawMempoolVerboseResult models the data returned from the getrawmempool
// command when the verbose flag is set. When the verbose flag is not set,

View File

@ -12,7 +12,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/kaspanet/kaspad/addrmgr"
"github.com/kaspanet/kaspad/addressmanager"
"github.com/kaspanet/kaspad/connmanager"
"github.com/kaspanet/kaspad/protocol"
"github.com/kaspanet/kaspad/util/mstime"
@ -168,7 +168,7 @@ type Server struct {
acceptanceIndex *indexers.AcceptanceIndex
blockTemplateGenerator *mining.BlkTmplGenerator
connectionManager *connmanager.ConnectionManager
addressManager *addrmgr.AddrManager
addressManager *addressmanager.AddressManager
protocolManager *protocol.Manager
}
@ -694,7 +694,7 @@ func NewRPCServer(
acceptanceIndex *indexers.AcceptanceIndex,
blockTemplateGenerator *mining.BlkTmplGenerator,
connectionManager *connmanager.ConnectionManager,
addressManager *addrmgr.AddrManager,
addressManager *addressmanager.AddressManager,
protocolManager *protocol.Manager,
) (*Server, error) {
// Setup listeners for the configured RPC listen addresses and