org.klomp.snark.dht

## Class NodeInfo

- java.lang.Object
- - net.i2p.data.SimpleDataStructure
  - - org.klomp.snark.dht.NodeInfo

- All Implemented Interfaces:
    DataStructure

      class NodeInfo
      extends SimpleDataStructure

  Comprehensive node information container for DHT operations.

  This class encapsulates all essential information about a DHT node including its Node ID (NID), destination hash, port, and optionally the full I2P destination. It serves as the primary data structure stored in DHTNodes for routing table management and node discovery operations.

  Data structure components:
  - NID: 20-byte node identifier used for Kademlia distance calculations
  - Hash: 32-byte destination hash for I2P routing
  - Port: 2-byte network port for DHT communication
  - Destination: Optional full I2P destination (may be added later)

  The compact representation (54 bytes) is immutable and contains NID, hash, and port. This compact format is used for network transmission and efficient storage. The full destination may be added later when available, enabling direct communication with the node.

  Design considerations:
  - DHT exchanges often use only hashes initially, with destinations added later
  - Compact format enables efficient network transmission and storage
  - Immutable core data ensures consistency in routing operations
  - Optional destination allows progressive information discovery

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `LENGTH`

    - ### Fields inherited from class net.i2p.data.SimpleDataStructure

      `_data`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NodeInfo(byte[] compactInfo, int offset)` No Destination yet available
    `NodeInfo(Destination dest, int port)` With a fake NID used for pings
    `NodeInfo(NID nID, Destination dest, int port)` Use this if we have the full destination
    `NodeInfo(NID nID, Hash hash, int port)` No Destination yet available
    `NodeInfo(String s)` Create from persistent storage string.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `Hash` | `calculateHash()` Calculate the SHA256 value of this object (useful for a few scenarios)
    `boolean` | `equals(Object o)` Warning - this returns true for two different classes with the same size and same data, e.g.
    `static NID` | `generateNID(Hash h, int p, RandomSource random)` Generate a secure NID that matches the Hash and port.
    `Destination` | `getDestination()`
    `Hash` | `getHash()`
    `NID` | `getNID()`
    `int` | `getPort()`
    `int` | `hashCode()` We assume the data has enough randomness in it, so use the first 4 bytes for speed.
    `long` | `lastSeen()`
    `int` | `length()` The legal length of the byte array in this data structure
    `void` | `setDestination(Destination dest)` This can come in later but the hash must match.
    `String` | `toPersistentString()` To persistent storage string.
    `String` | `toString()`

    - ### Methods inherited from class net.i2p.data.SimpleDataStructure

      `fromBase64, fromByteArray, getData, read, readBytes, setData, toBase64, toByteArray, writeBytes`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### LENGTH

          public static final int LENGTH

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### NodeInfo

          public NodeInfo(byte[] compactInfo,
                          int offset)

      No Destination yet available
      Parameters:
        `compactInfo` - 20 byte node ID, 32 byte destHash, 2 byte port
        `offset` - starting at this offset in compactInfo
      Throws:
        `IllegalArgumentException`
        `ArrayIndexOutOfBoundsException`

    - #### NodeInfo

          public NodeInfo(Destination dest,
                          int port)

      With a fake NID used for pings

    - #### NodeInfo

          public NodeInfo(NID nID,
                          Destination dest,
                          int port)

      Use this if we have the full destination
      Throws:
        `IllegalArgumentException`

    - #### NodeInfo

          public NodeInfo(NID nID,
                          Hash hash,
                          int port)

      No Destination yet available
      Throws:
        `IllegalArgumentException`

    - #### NodeInfo

          public NodeInfo(String s)
                   throws DataFormatException

      Create from persistent storage string. Format: NID:Hash:Destination:port First 3 in base 64; Destination may be empty string
      Throws:
        `IllegalArgumentException`
        `DataFormatException`

  - ### Method Detail

    - #### calculateHash

          public Hash calculateHash()

      Description copied from interface: `DataStructure`
      Calculate the SHA256 value of this object (useful for a few scenarios)
      Specified by:
        `calculateHash` in interface `DataStructure`
      Overrides:
        `calculateHash` in class `SimpleDataStructure`
      Returns:
        the SHA256 hash of the byte array, or null if the data is null

    - #### equals

          public boolean equals(Object o)

      Description copied from class: `SimpleDataStructure`
      Warning - this returns true for two different classes with the same size and same data, e.g. SessionKey and SessionTag, but you wouldn't put them in the same Set, would you?
      Overrides:
        `equals` in class `SimpleDataStructure`

    - #### generateNID

          public static NID generateNID(Hash h,
                                        int p,
                                        RandomSource random)

      Generate a secure NID that matches the Hash and port. Rules: First 4 bytes must match Hash. Next 2 bytes must match Hash ^ port. Remaining bytes may be random.
      Throws:
        `IllegalArgumentException`

    - #### getDestination

          public Destination getDestination()

      Returns:
        may be null if we don't have it

    - #### getHash

          public Hash getHash()

    - #### getNID

          public NID getNID()

    - #### getPort

          public int getPort()

    - #### hashCode

          public int hashCode()

      Description copied from class: `SimpleDataStructure`
      We assume the data has enough randomness in it, so use the first 4 bytes for speed. If this is not the case, override in the extending class.
      Overrides:
        `hashCode` in class `SimpleDataStructure`

    - #### lastSeen

          public long lastSeen()

    - #### length

          public int length()

      Description copied from class: `SimpleDataStructure`
      The legal length of the byte array in this data structure
      Specified by:
        `length` in class `SimpleDataStructure`

    - #### setDestination

          public void setDestination(Destination dest)
                              throws IllegalArgumentException

      This can come in later but the hash must match.
      Throws:
        `IllegalArgumentException` - if hash of dest doesn't match previous hash

    - #### toPersistentString

          public String toPersistentString()

      To persistent storage string. Format: NID:Hash:Destination:port First 3 in base 64; Destination may be empty string

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `SimpleDataStructure`