Overview
Comment: | Split protocol parsing/creating from other tasks and added more supported items |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
5195f616e1f5935ae6b122ecba64d549 |
User & Date: | rkeene on 2018-12-04 18:02:35 |
Other Links: | manifest | tags |
Context
2018-12-04
| ||
20:07 | Added support for selecting network and super peers check-in: 06095e42bd user: rkeene tags: trunk | |
18:02 | Split protocol parsing/creating from other tasks and added more supported items check-in: 5195f616e1 user: rkeene tags: trunk | |
2018-11-19
| ||
20:12 | Made help text more consistent check-in: 4a4d2c1e37 user: rkeene tags: trunk | |
Changes
Modified nano.tcl from [ecb41a3def] to [d46c2149d2].
︙ | ︙ | |||
16 17 18 19 20 21 22 | namespace eval ::nano::account {} namespace eval ::nano::node {} namespace eval ::nano::ledger {} namespace eval ::nano::ledger::lmdb {} namespace eval ::nano::rpc {} namespace eval ::nano::rpc::client {} namespace eval ::nano::balance {} | < > > > | 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | namespace eval ::nano::account {} namespace eval ::nano::node {} namespace eval ::nano::ledger {} namespace eval ::nano::ledger::lmdb {} namespace eval ::nano::rpc {} namespace eval ::nano::rpc::client {} namespace eval ::nano::balance {} namespace eval ::nano::node::bootstrap {} namespace eval ::nano::node::realtime {} namespace eval ::nano::network::client {} namespace eval ::nano::network::server {} namespace eval ::nano::protocol::create {} namespace eval ::nano::protocol::parse {} namespace eval ::nano::protocol::extensions {} namespace eval ::nano::network::_dns {} # Constants set ::nano::block::stateBlockPreamble [binary decode hex "0000000000000000000000000000000000000000000000000000000000000006"] set ::nano::block::zero "0000000000000000000000000000000000000000000000000000000000000000" set ::nano::balance::zero "00000000000000000000000000000000" set ::nano::address::zero $::nano::block::zero |
︙ | ︙ | |||
70 71 72 73 74 75 76 77 78 79 80 81 82 83 | unano 1000000000000000000 uxrb 1000000000000000000 Traw 1000000000000 Graw 1000000000 Mraw 1000000 Kraw 1000 raw 1 } # Address management functions proc ::nano::address::toPublicKey {address args} { set performChecksumCheck false set outputFormat "bytes" foreach arg $args { | > > > > > > | 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | unano 1000000000000000000 uxrb 1000000000000000000 Traw 1000000000000 Graw 1000000000 Mraw 1000000 Kraw 1000 raw 1 } set ::nano::protocol::extensions { node_id_handshake { query 1 response 2 } } # Address management functions proc ::nano::address::toPublicKey {address args} { set performChecksumCheck false set outputFormat "bytes" foreach arg $args { |
︙ | ︙ | |||
461 462 463 464 465 466 467 | set blockJSONEntries [join $blockJSONEntries] set blockJSON [json::write object {*}$blockJSONEntries] return $blockJSON } | | | 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | set blockJSONEntries [join $blockJSONEntries] set blockJSON [json::write object {*}$blockJSONEntries] return $blockJSON } proc ::nano::block::typeIDFromType {type args} { set outputFormat decimal foreach arg $args { switch -exact -- $arg { "-dec" - "-decimal" { set outputFormat "decimal" } "-hex" { |
︙ | ︙ | |||
488 489 490 491 492 493 494 495 496 497 498 499 500 501 | "invalid" { set typeId 0 } "not_a_block" { set typeId 1 } "send" { set typeId 2 } "receive" { set typeId 3 } "open" { set typeId 4 } "change" { set typeId 5 } "state" { set typeId 6 } } switch -- $outputFormat { "decimal" { set result $typeId } "hex" { | > | 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | "invalid" { set typeId 0 } "not_a_block" { set typeId 1 } "send" { set typeId 2 } "receive" { set typeId 3 } "open" { set typeId 4 } "change" { set typeId 5 } "state" { set typeId 6 } default { error "Invalid type: $type" } } switch -- $outputFormat { "decimal" { set result $typeId } "hex" { |
︙ | ︙ | |||
545 546 547 548 549 550 551 552 553 554 555 556 557 558 | 0 { set type "invalid" } 1 { set type "not_a_block" } 2 { set type "send" } 3 { set type "receive" } 4 { set type "open" } 5 { set type "change" } 6 { set type "state" } } return $type } proc ::nano::block::lengthFromType {type args} { set includeType false | > > > | 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | 0 { set type "invalid" } 1 { set type "not_a_block" } 2 { set type "send" } 3 { set type "receive" } 4 { set type "open" } 5 { set type "change" } 6 { set type "state" } default { error "Invalid type ID: $typeId" } } return $type } proc ::nano::block::lengthFromType {type args} { set includeType false |
︙ | ︙ | |||
1722 1723 1724 1725 1726 1727 1728 | ] catch { set basis_node [dict create] set basis_node [dict get $basis "node"] } set default_node [dict create \ | | | 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 | ] catch { set basis_node [dict create] set basis_node [dict get $basis "node"] } set default_node [dict create \ "client_id_private_key" [::nano::key::newKey] \ "bootstrap_connections" "4" \ ] # XXX:TODO: Consider network dict set default_node "peering_port" "7075" dict set default_node "preconfigured_peers" [list "rai.raiblocks.net"] |
︙ | ︙ | |||
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 | if {![dict exists $database_config "configDirectory"]} { dict set database_config "configDirectory" $info(-configDirectory) } # set dbHandle [::nano::ledger::${database_backend}::init $database_config] # ::nano::node::setLedgerHandle $dbHandle } proc ::nano::node::log {message {level "debug"}} { set linePrefix "" foreach line [split $message "\n"] { | > > > > | > > > > > > > > > > > > > > > > > > > > > > > > | | | | | 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 | if {![dict exists $database_config "configDirectory"]} { dict set database_config "configDirectory" $info(-configDirectory) } # set dbHandle [::nano::ledger::${database_backend}::init $database_config] # ::nano::node::setLedgerHandle $dbHandle } proc ::nano::node::user_log {line} { puts stderr $line } proc ::nano::node::log {message {level "debug"}} { set linePrefix "" foreach line [split $message "\n"] { ::nano::node::user_log [format {%-40s %10s [%5s] %s} [::info coroutine] [clock seconds] $level ${linePrefix}$line] set linePrefix " " } } proc ::nano::protocol::extensions::get {messageType extensions} { ::set flags [list] foreach {checkFlagName checkFlag} [dict get $::nano::protocol::extensions $messageType] { if {($extensions & $checkFlag) == $checkFlag} { lappend flags $checkFlagName } } return $flags } proc ::nano::protocol::extensions::set {messageType extensionsVar flag} { upvar 1 $extensionsVar extensions ::set value [dict get $::nano::protocol::extensions $messageType $flag] ::set extensions [expr {$extensions | $value}] } proc ::nano::protocol::extensions::unset {messageType extensionsVar flag} { upvar 1 $extensionsVar extensions ::set value [dict get $::nano::protocol::extensions $messageType $flag] ::set extensions [expr {$extensions & ((~$value) & 0xffff)}] } proc ::nano::protocol::create::bulk_pull {account {end ""}} { set accountPubKey [::nano::address::toPublicKey $account -binary] if {$end ne ""} { if {[string length $end] != $::nano::block::hashLength} { set end [binary decode hex $end] } } else { set end [binary decode hex $::nano::block::zero] } return [dict create data [binary format a32a32 \ $accountPubKey \ $end \ ]] } proc ::nano::protocol::create::_bulk_pull_account_flagsParse {direction flag} { switch -- $direction { "toInt" { switch -- $flag { "" - "default" - "pendingHashAndAmount" { set flagValue 0 } "pendingAddressOnly" { |
︙ | ︙ | |||
1906 1907 1908 1909 1910 1911 1912 | return $flagValue } } return -code error "Invalid direction: $direction" } | | | | | | 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 | return $flagValue } } return -code error "Invalid direction: $direction" } proc ::nano::protocol::create::bulk_pull_account {account {minPending 0} {flags ""}} { set accountPubKey [::nano::address::toPublicKey $account -binary] set minPendingHex [format %032llx $minPending] if {[string length $minPendingHex] > 32} { return -code error "Invalid amount: $minPending" } set flagsInt [_bulk_pull_account_flagsParse "toInt" $flags] return [dict create data [binary format a32H32c \ $accountPubKey \ $minPendingHex \ $flagsInt \ ]] } proc ::nano::protocol::parse::bulk_pull_account_response {sock account {minPending 0} {flags ""}} { set frontierBlockhash [::nano::network::_recv $sock 32] set frontierBalance [::nano::network::_recv $sock 16] set frontierBlockhash [binary encode hex $frontierBlockhash] set frontierBlockhash [string toupper $frontierBlockhash] set frontierBalance [binary encode hex $frontierBalance] |
︙ | ︙ | |||
2010 2011 2012 2013 2014 2015 2016 | ] \ pending $pendingInfo \ ] return $retval } | | | > > > > | > > > | > > > | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 | ] \ pending $pendingInfo \ ] return $retval } proc ::nano::protocol::create::frontier_req {{startAccount ""} {age ""} {count ""}} { if {$startAccount eq ""} { set accountPubKey [binary decode hex $::nano::address::zero] } else { set accountPubKey [::nano::address::toPublicKey $startAccount -binary] } if {$age eq ""} { set age [expr {2**32-1}] } if {$count eq ""} { set count [expr {2**32-1}] } return [dict create data [binary format a32ii $accountPubKey $age $count]] } proc ::nano::protocol::create::node_id_handshake {types args} { array set argInfo $args set extensions 0 set data "" if {[llength $types] < 1} { return -code error "Invalid types specified: (none)" } foreach type $types { if {$type in {"query" "response"}} { continue } return -code error "Invalid type specified: $type" } if {"query" in $types} { ::nano::protocol::extensions::set "node_id_handshake" extensions "query" # XXX:TODO: Verify this is exactly a 32-byte nonce append data $argInfo(-query) } if {"response" in $types} { ::nano::protocol::extensions::set "node_id_handshake" extensions "response" if {![info exists argInfo(-privateKey)]} { set argInfo(-privateKey) [::nano::key::newKey] } set publicKey [::nano::key::publicKeyFromPrivateKey $argInfo(-privateKey) -binary] set signature [::nano::internal::signDetached $argInfo(-query) $argInfo(-privateKey)] if {[string length $argInfo(-query)] != 32} { return -code error "Invalid input data" } if {[string length $publicKey] != 32 || [string length $signature] != 64} { return -code error "Invalid output data" } set response [binary format a32a64 $publicKey $signature] append data $response } return [dict create extensions $extensions data $data] } proc ::nano::network::_localIP {version} { if {[info exists ::nano::network::_localIP($version)]} { return $::nano::network::_localIP($version) } |
︙ | ︙ | |||
2064 2065 2066 2067 2068 2069 2070 | set localIP "${localIPPrefix}${localIP}" set ::nano::network::_localIP($version) $localIP return $::nano::network::_localIP($version) } | > > | > > > > > | > > | | | | | | | | | | | > | | > | < | | > > > | > > > > > > > > > > > | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | | > > | | | | > > > > > > | > | | | 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 | set localIP "${localIPPrefix}${localIP}" set ::nano::network::_localIP($version) $localIP return $::nano::network::_localIP($version) } proc ::nano::protocol::create::keepalive {ipPortDictList args} { set retval "" # Allow creating an empty keepalive array set argInfo { -local true } array set argInfo $args if {$argInfo(-local)} { set localPort [dict get $::nano::node::configuration "node" "peering_port"] set ipPortDictListHead [list] foreach ipVersion {v4 v6} { unset -nocomplain localIP catch { set localIP [::nano::network::_localIP $ipVersion] } if {![info exists localIP]} { continue } lappend ipPortDictListHead [dict create address $localIP port $localPort] } set ipPortDictList [concat $ipPortDictListHead $ipPortDictList] } if {[llength $ipPortDictList] == 0} { return [dict create data ""] } while {[string length $retval] < 144} { foreach ipPortDict $ipPortDictList { set ip [dict get $ipPortDict address] set port [dict get $ipPortDict port] # Encode IP address as a byte array if {[::ip::version $ip] == 4} { # Convert to IPv6 if needed set ip "::ffff:$ip" } set ip [binary decode hex [string map [list ":" ""] [::ip::normalize $ip]]] # Encode port as a 16-bit integer in network byte order (big endian) set port [binary format s $port] append retval "${ip}${port}" } } return [dict create data [string range $retval 0 143]] } proc ::nano::protocol::create::confirm_req {blockDict} { set blockData [::nano::block::dict::toBlock $blockDict] set blockType [dict get $blockDict type] set blockTypeID [::nano::block::typeIDFromType $blockType -decimal] set retval(blockType) $blockTypeID set retval(data) $blockData return [array get retval] } proc ::nano::protocol::parse::confirm_req {extensions blockData} { set blockTypeID [expr {($extensions >> 8) & 0x0f}] set blockType [::nano::block::typeFromTypeID $blockTypeID] set blockDict [::nano::block::dict::fromBlock $blockData -type=$blockType] # XXX:TEMPORARY dict unset blockDict _blockData dict unset blockDict _workData set retval(blockType) $blockType set retval(block) $blockDict return [array get retval] } proc ::nano::protocol::parse::confirm_ack {extensions blockData} { set blockTypeID [expr {($extensions >> 8) & 0x0f}] set blockType [::nano::block::typeFromTypeID $blockTypeID] # Header binary scan $blockData H64H128wa* \ retval(voteAccount) \ retval(voteSignature) \ retval(voteSequence) \ newBlockData set blockData $newBlockData # Determine whether or not this is a vote-by-hash block set voteType "full" if {$blockType eq "not_a_block"} { set voteType "hash-only" } set retval(voteType) $voteType # Generate the data to hash and verify set signedData "" if {$voteType eq "hash-only"} { ### Vote-by-hash blocks have a prefix containing the bytes "vote " append signedData "vote " } # Process the vote data if {$voteType eq "hash-only"} { set retval(hashes) [list] while {[string length $blockData] >= 32} { binary scan $blockData H64a* hash blockData lappend retval(hashes) $hash append signedData [binary decode hex $hash] } } else { set blockDict [::nano::block::dict::fromBlock $blockData -type=$blockType] set blockHash [::nano::block::dict::toHash $blockDict -hex] append signedData [binary decode hex $blockHash] # XXX:TEMPORARY dict unset blockDict _blockData dict unset blockDict _workData set retval(blockType) $blockType set retval(hashes) [list $blockHash] set retval(block) $blockDict } # Add the sequence number to the signed data append signedData [binary format w $retval(voteSequence)] # Verify signature of the hash set valid false catch { set valid [::nano::internal::verifyDetached \ [::nano::internal::hashData $signedData $::nano::block::hashLength] \ [binary decode hex $retval(voteSignature)] \ [binary decode hex $retval(voteAccount)] \ ] } # Convert the account to an account form set retval(voteAccount) [::nano::address::fromPublicKey $retval(voteAccount)] # If the signature is invalid, clean things up a bit if {!$valid} { set retval(valid) false # If the vote isn't valid, don't create entries regarding this vote foreach renameKey [array names retval] { if {$renameKey eq "valid"} { continue } set retval(invalid_${renameKey}) $retval($renameKey) unset retval $renameKey } } else { set retval(valid) true } return [array get retval] } proc ::nano::protocol::create {messageType args} { set versionUsing 14 set versionMin 13 set versionMax $versionUsing set messageInfo(extensions) 0 set messageInfo(blockType) 0 set messageType [string tolower $messageType] set messageTypeID [lsearch -exact $::nano::network::messageTypes $messageType] if {$messageTypeID == -1} { return -code error "Invalid message type: $messageType" } array set messageInfo [::nano::protocol::create::${messageType} {*}$args] set messageInfo(extensions) [expr {$messageInfo(extensions) | (($messageInfo(blockType) << 8) & 0x0f00)}] set message [binary format a2ccccs \ RC \ $versionMax \ $versionUsing \ $versionMin \ $messageTypeID \ $messageInfo(extensions) \ ] append message $messageInfo(data) return $message } proc ::nano::network::client {sock messageType args} { set message [::nano::protocol::create $messageType {*}$args] ::nano::node::log "Sending message [binary encode hex $message] ([::nano::protocol::parse $message]) to socket $sock" catch { if {[dict get $sock "type"] eq "realtime"} { set sockInfo $sock set sock [dict get $sock "socket"] } } if {[info exists sockInfo]} { fconfigure $sock -remote [dict get $sockInfo "remote"] } chan configure $sock -translation binary -encoding binary puts -nonewline $sock $message flush $sock set responseCommand ::nano::protocol::parse::${messageType}_response set response "" if {[info command $responseCommand] ne ""} { set response [$responseCommand $sock {*}$args] } return $response } |
︙ | ︙ | |||
2260 2261 2262 2263 2264 2265 2266 | } proc ::nano::node::bootstrap {} { set ::nano::node::bootstrap::frontiers_to_pull [list] set ::nano::node::bootstrap::frontier_req_running false while true { | | | 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 | } proc ::nano::node::bootstrap {} { set ::nano::node::bootstrap::frontiers_to_pull [list] set ::nano::node::bootstrap::frontier_req_running false while true { set peerInfoList [::nano::node::getPeers] ::nano::node::log "Have [llength $peerInfoList] peers" foreach peerInfo $peerInfoList { set peer [dict get $peerInfo "address"] set peerPort [dict get $peerInfo "port"] if {[llength [info command ::nano::node::bootstrap::peer_*]] >= [dict get $::nano::node::configuration node bootstrap_connections]} { |
︙ | ︙ | |||
2286 2287 2288 2289 2290 2291 2292 | ::nano::node::_sleep 30000 } } proc ::nano::network::_connect {host port} { if {[info coroutine] eq ""} { | | | | 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 | ::nano::node::_sleep 30000 } } proc ::nano::network::_connect {host port} { if {[info coroutine] eq ""} { set sock [::socket $host $port] } else { set sock [::socket -async $host $port] chan event $sock writable [info coroutine] chan event $sock readable [info coroutine] if {![chan configure $sock -connecting]} { if {[chan configure $sock -error] ne ""} { close $sock |
︙ | ︙ | |||
2370 2371 2372 2373 2374 2375 2376 | ::nano::node::log "Sleeping for $ms ms" } after $ms [info coroutine] yield } | | | > > > > | > > > > | > > | > > | < | | | | | | < < < < | | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > | > > | > | | | | | | > > | > > > > | > > | > | > > > > | | | | > > > > > > > > > > > > > > > > > > > > > > > > < < < | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | < | | < < < < | | < < > > > > > > > > > > | < | < | | > > > > > > > > | > > > | | > > | > | 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 | ::nano::node::log "Sleeping for $ms ms" } after $ms [info coroutine] yield } proc ::nano::node::getPeers {} { if {[info exists ::nano::node::configuration]} { set peers [dict get $::nano::node::configuration node preconfigured_peers] set defaultPeerPort [dict get $::nano::node::configuration node peering_port] } else { return [list] } set completePeers [list] foreach peer $peers { catch { foreach peer [::nano::network::_dns::toIPList $peer] { lappend completePeers [dict create address $peer port $defaultPeerPort] } } } set now [clock seconds] foreach {peerKeyInfo peerInfo} [array get ::nano::node::peers] { set lastSeen [dict get $peerInfo "lastSeen"] set address [dict get $peerKeyInfo "address"] set peerPort [dict get $peerKeyInfo "port"] set peerKey [dict create address $peer port $peerPort] if {($now - $lastSeen) > (5 * 60)} { unset -nocomplain ::nano::node::peers($peerKeyInfo) unset -nocomplain ::nano::node::_node_id_nonces($peerKey) continue } lappend completePeers $peerKey } # Cleanup nonces while we are here foreach {peerKey peerInfo} [array get ::nano::node::_node_id_nonces] { set lastSeen [dict get $peerInfo "lastSeen"] if {($now - $lastSeen) > (5 * 60)} { unset ::nano::node::_node_id_nonces($peerKey) } } set completePeers [::nano::node::_randomSortList -unique $completePeers] set retval [list] foreach peer $completePeers { lappend retval $peer } return $retval } proc ::nano::protocol::parse::keepalive {extensions messageData} { set peers [list] while {$messageData ne ""} { # Parse an address and port pair set foundElements [binary scan $messageData H32s address port] if {$foundElements != 2} { break } # Remove the parsed portion set messageData [string range $messageData 18 end] # Convert the hex-notation to an IPv6 address set address [string trim [regsub -all {....} $address {&:}] ":"] set address [::ip::contract $address] if {[string match "::ffff:*" $address] && [llength [split $address :]] == 5} { # Convert IPv4 addresses to dotted quad notation set address [::ip::normalize $address] set address [split $address :] set address [join [lrange $address end-1 end] ""] set address [::ip::intToString 0x$address] } set port [expr {$port & 0xffff}] lappend peers [dict create "address" $address "port" $port] } if {$messageData ne ""} { return -code error "Invalid keepalive packet [binary encode hex $messageData]: Had extra bytes" } return [dict create "peers" $peers] } proc ::nano::network::server::keepalive {messageDict} { set now [clock seconds] set peers [dict get $messageDict "peers"] foreach peer $peers { set address [dict get $peer "address"] set port [dict get $peer "port"] # Canonicalize the peer name to a key set peer [dict create address $address port $port] # If this peer is not already known, contact them requesting a handshake if {![info exists ::nano::node::peers($peer)]} { set node_id_nonce [::nano::internal::randomBytes 32] set ::nano::node::_node_id_nonces($peer) [dict create query $node_id_nonce lastSeen $now] set peerSock [::nano::node::socket realtime $address $port] ::nano::network::client $peerSock "node_id_handshake" query -query $node_id_nonce } } return "" } proc ::nano::protocol::parse::node_id_handshake {extensions messageData} { array set result [list] set flags [nano::protocol::extensions::get "node_id_handshake" $extensions] set result(flags) $flags if {"query" in $flags} { binary scan $messageData H64a* result(query) messageData } if {"response" in $flags} { binary scan $messageData H64H128 result(key) result(signature) } return [array get result] } proc ::nano::network::server::node_id_handshake {messageDict} { set retval "" if {"query" in [dict get $messageDict flags]} { set query [dict get $messageDict query] set retval [dict create "invoke_client" [list node_id_handshake response -query [binary decode hex $query]]] } if {"response" in [dict get $messageDict flags]} { set peerInfo [dict get $messageDict socket remote] set peerAddress [lindex $peerInfo 0] set peerPort [lindex $peerInfo 1] set peer [dict create address $peerAddress port $peerPort] # XXX:TODO: Verify the nonce if {![info exists ::nano::node::_node_id_nonces($peer)]} { return "" } set sentNonce $::nano::node::_node_id_nonces($peer) unset ::nano::node::_node_id_nonces($peer) # Add the peer to our list of peers set ::nano::node::peers($peer) [dict create lastSeen [clock seconds]] } return $retval } proc ::nano::network::server::confirm_ack {messageDict} { # keep statistics dict with messageDict {} incr ::nano::node::stats([list confirm_ack valid $valid]) if {!$valid} { return "" } incr ::nano::node::stats([list confirm_ack rep $voteAccount valid $valid]) incr ::nano::node::stats([list confirm_ack voteType $voteType]) incr ::nano::node::stats([list confirm_ack rep $voteAccount voteType $voteType]) if {![info exists ::nano::node::stats([list confirm_ack rep $voteAccount minVoteSequence])]} { set ::nano::node::stats([list confirm_ack rep $voteAccount minVoteSequence]) $voteSequence } else { if {$::nano::node::stats([list confirm_ack rep $voteAccount minVoteSequence]) > $voteSequence} { set ::nano::node::stats([list confirm_ack rep $voteAccount minVoteSequence]) $voteSequence } } if {![info exists ::nano::node::stats([list confirm_ack rep $voteAccount maxVoteSequence])]} { set ::nano::node::stats([list confirm_ack rep $voteAccount maxVoteSequence]) $voteSequence } else { if {$::nano::node::stats([list confirm_ack rep $voteAccount maxVoteSequence]) < $voteSequence} { set ::nano::node::stats([list confirm_ack rep $voteAccount maxVoteSequence]) $voteSequence } } set votedOn [llength $hashes] incr ::nano::node::stats([list confirm_ack votedOnCount]) $votedOn incr ::nano::node::stats([list confirm_ack rep $voteAccount votedOnCount]) $votedOn foreach hash $hashes { set ::nano::node::_stats_seen_hashes($hashes) 1 set ::nano::node::_stats_seen_hashes_by_rep([list $voteAccount $hashes]) 1 } set ::nano::node::stats([list confirm_ack votedOnUniqueCount]) [llength [array names ::nano::node::_stats_seen_hashes]] set ::nano::node::stats([list confirm_ack rep $voteAccount votedOnUniqueCount]) [llength [array names ::nano::node::_stats_seen_hashes_by_rep [list $voteAccount *]]] return "" } proc ::nano::protocol::parse::publish {extensions messageData} { set blockTypeID [expr {($extensions >> 8) & 0x0f}] set blockType [::nano::block::typeFromTypeID $blockTypeID] set blockDict [::nano::block::dict::fromBlock $messageData -type=$blockType] # XXX:TEMPORARY dict unset blockDict _blockData dict unset blockDict _workData set retval(blockType) $blockType set retval(block) $blockDict return [array get retval] } proc ::nano::network::server::publish {messageDict} { #puts "block: [binary encode hex $blockData]" #9e1272edade3c247c738a4bd303eb0cfc3da298444bb9d13b8ffbced34ff036f4e1ff833324efc81c237776242928ef76a2cdfaa53f4c4530ee39bfff1977e26e382dd09ec8cafc2427cf817e9afe1f372ce81085ab4feb1f3de1f25ee818e5d000000008fc492fd20e57d048e000000204e7a62f25df739eaa224d403cb107b3f9caa0280113b0328fad3b402c465169006f988549a8b1e20e0a09b4b4dcae5397f6fcc4d507675f58c2b29ae02341b0a4fe562201a61bf27481aa4567c287136b4fd26b4840c93c42c7d1f5c518503d68ec561af4b8cf8 #9e1272edade3c247c738a4bd303eb0cfc3da298444bb9d13b8ffbced34ff036fa5e3647d3d296ec72baea013ba7fa1bf5c3357c33c90196f078ba091295e6e03e382dd09ec8cafc2427cf817e9afe1f372ce81085ab4feb1f3de1f25ee818e5d000000008fb2604ebd1fe098b8000000204e7a62f25df739eaa224d403cb107b3f9caa0280113b0328fad3b402c465165287cd9c61752dc9d011f666534dbdc10461e927503f9599791d73b1cca7fdc032d76db3f91e5b5c3d6206fa48b01bd08da4a89f2e880242e9917cfc3db80d0b9bfe8e6d1dd183d5 return "" } # Namespace ::nano::protocol::parse deals with the network level protocol (outside the node) # Namespace ::nano::network::server deals with the node's actual interaction with the network proc ::nano::protocol::parse {message} { set messageParsed [binary scan $message a2ccccsa* \ result(packetMagic) \ result(versionMax) \ result(versionUsing) \ result(versionMin) \ result(messageTypeID) \ result(extensions) \ newBlockData ] if {![info exists newBlockData]} { return "" } set blockData $newBlockData if {![info exists result(packetMagic)] || ![info exists result(messageTypeID)] || ![info exists result(extensions)]} { return "" } switch -- $result(packetMagic) { "RC" { set result(network) "main" } default { return "" } } set result(messageType) [lindex $::nano::network::messageTypes $result(messageTypeID)] if {[catch { set result(messageDict) [::nano::protocol::parse::$result(messageType) $result(extensions) $blockData] } err]} { if {![string match "invalid command name *" $err]} { ::nano::node::log "Error parsing $result(messageType): $err" } } return [array get result] } proc ::nano::network::server {message {networkType "bootstrap"} {peerSock ""}} { set messageData [::nano::protocol::parse $message] dict with messageData {} ::nano::node::log "*** Incoming: $messageType ($messageTypeID on $networkType; from $peerSock) [binary encode hex $message] ($messageData)" if {![info exists messageDict]} { return "" } dict set messageDict socket $peerSock set retval [dict create] if {[catch { set retval [::nano::network::server::${messageType} $messageDict] } err]} { if {![string match "invalid command name *" $err]} { ::nano::node::log "Error handling ${messageType}: $err" } } return $retval } proc ::nano::node::socket {type address port} { if {$type eq "realtime"} { # Determine our listening port set peeringPort [dict get $::nano::node::configuration node peering_port] # Start a UDP listening socket foreach protocolVersion {v4 v6} { if {[info exists ::nano::node::realtime::socket($protocolVersion)]} { continue } set flags [list] if {$protocolVersion eq "v6"} { lappend flags ipv6 } set protocolSocket [udp_open $peeringPort {*}$flags reuse] set ::nano::node::realtime::socket($protocolVersion) $protocolSocket fconfigure $protocolSocket -blocking false -encoding binary -translation binary chan event $protocolSocket readable [list ::nano::node::realtime::incoming $protocolSocket] } # Used to just start listening if {$address eq "" || $port eq ""} { return } set protocolVersion "v[::ip::version $address]" set socket $::nano::node::realtime::socket($protocolVersion) set peerSock [list type "realtime" remote [list $address $port] socket $socket] } elseif {$type eq "bootstrap"} { set peerSock [::nano::network::_connect $address $port] } else { return -code error "Unknown network type: $type" } return $peerSock } proc ::nano::node::realtime::incoming {socket} { set data [read $socket 8192] if {$data eq ""} { return } set remote [chan configure $socket -peer] set address [lindex $remote 0] set port [lindex $remote 1] set peerSock [::nano::node::socket realtime $address $port] set response [::nano::network::server $data "realtime" $peerSock] if {$response eq ""} { return } set response [dict get $response "invoke_client"] ::nano::network::client $peerSock {*}$response return } proc ::nano::node::realtime {} { package require udp set clientIDPrivateKey [dict get $::nano::node::configuration node client_id_private_key] # Start listening ::nano::node::socket realtime "" "" # Periodically send keepalives to all known peers while true { set allPeers [::nano::node::getPeers] set peers [lrange $allPeers 0 15] set peerNotifyCount [expr {int(sqrt([llength $allPeers]))}] if {$peerNotifyCount < 16} { set peerNotifyCount 16 } if {$peerNotifyCount > 128} { set peerNotifyCount 128 } foreach peerInfo [lrange $allPeers 0 $peerNotifyCount] { set peerAddress [dict get $peerInfo "address"] set peerPort [dict get $peerInfo "port"] set peerSock [::nano::node::socket realtime $peerAddress $peerPort] ::nano::network::client $peerSock "keepalive" $peers -local true } ::nano::node::_sleep [expr {1 * 60 * 1000}] } } proc ::nano::node::start args { package require defer package require udp array set config { -bootstrap true -realtime true -wait true } array set config $args if {$config(-bootstrap)} { coroutine ::nano::node::bootstrap::run ::nano::node::bootstrap } if {$config(-realtime)} { coroutine ::nano::node::realtime::run ::nano::node::realtime } if {$config(-wait)} { vwait ::nano::node::_FOREVER_ } } # RPC Client ## Side-effect: Sets ::nano::rpc::client::config proc ::nano::rpc::client::init args { if {![info exists ::nano::rpc::client::config]} { set ::nano::rpc::client::config [dict create \ |
︙ | ︙ |