︙ | | |
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
+
|
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::node::cli {}
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 {}
|
︙ | | |
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
|
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
|
+
+
-
-
+
+
+
|
dict set database_config "configDirectory" $info(-configDirectory)
}
# set dbHandle [::nano::ledger::${database_backend}::init $database_config]
# ::nano::node::setLedgerHandle $dbHandle
}
# Only replace this function if it doesn't already exist
if {[info command ::nano::node::user_log] eq ""} {
proc ::nano::node::user_log {line} {
puts stderr $line
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 " "
|
︙ | | |
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
|
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
|
+
+
+
+
+
+
+
+
+
-
+
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
|
foreach peer [::nano::network::_dns::toIPList $peer] {
lappend completePeers [dict create address $peer port $defaultPeerPort]
}
}
}
set now [clock seconds]
# 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)
}
}
# Come up with a list of peers that we have seen recently
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]
set peerKey [dict create address $address port $peerPort]
if {($now - $lastSeen) > (5 * 60)} {
#puts "Have not seen $peerKey in a while, dropping (now=$now, lastSeen=$lastSeen)"
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 [::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 ""} {
|
︙ | | |
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
|
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
|
+
-
-
+
+
-
-
+
+
+
+
|
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)]} {
if {![info exists ::nano::node::_node_id_nonces($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 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
set peerSock [::nano::node::socket realtime $address $port]
#puts "Querying $peerSock with node_id_handshake (1)"
::nano::network::client $peerSock "node_id_handshake" query -query $node_id_nonce
}
}
}
return ""
}
proc ::nano::protocol::parse::node_id_handshake {extensions messageData} {
|
︙ | | |
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
|
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
|
+
|
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
#puts "Got node_id_handshake response from $peer"
set ::nano::node::peers($peer) [dict create lastSeen [clock seconds]]
}
return $retval
}
proc ::nano::network::server::confirm_ack {messageDict} {
|
︙ | | |
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
|
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
|
+
+
|
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]
set blockHash [::nano::block::dict::toHash $blockDict -hex]
# XXX:TEMPORARY
dict unset blockDict _blockData
dict unset blockDict _workData
set retval(blockType) $blockType
set retval(block) $blockDict
set retval(hash) $blockHash
return [array get retval]
}
proc ::nano::network::server::publish {messageDict} {
#puts "block: [binary encode hex $blockData]"
#9e1272edade3c247c738a4bd303eb0cfc3da298444bb9d13b8ffbced34ff036f4e1ff833324efc81c237776242928ef76a2cdfaa53f4c4530ee39bfff1977e26e382dd09ec8cafc2427cf817e9afe1f372ce81085ab4feb1f3de1f25ee818e5d000000008fc492fd20e57d048e000000204e7a62f25df739eaa224d403cb107b3f9caa0280113b0328fad3b402c465169006f988549a8b1e20e0a09b4b4dcae5397f6fcc4d507675f58c2b29ae02341b0a4fe562201a61bf27481aa4567c287136b4fd26b4840c93c42c7d1f5c518503d68ec561af4b8cf8
|
︙ | | |
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
|
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
|
+
+
+
+
+
+
|
return [array get result]
}
proc ::nano::network::server {message {networkType "bootstrap"} {peerSock ""}} {
set messageData [::nano::protocol::parse $message]
dict with messageData {}
if {![info exists messageTypeID]} {
::nano::node::log "*** Incoming: INVALID [binary encode hex $message] ($messageData)"
return ""
}
::nano::node::log "*** Incoming: $messageType ($messageTypeID on $networkType; from $peerSock) [binary encode hex $message] ($messageData)"
if {![info exists messageDict]} {
return ""
}
|
︙ | | |
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
|
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
|
+
+
+
+
|
set peerPort [dict get $peerInfo "port"]
set peerSock [::nano::node::socket realtime $peerAddress $peerPort]
set node_id_nonce [::nano::internal::randomBytes 32]
::nano::network::client $peerSock keepalive $peers -local true
#puts "Querying $peerSock with node_id_handshake (2)"
::nano::network::client $peerSock node_id_handshake query -query $node_id_nonce
}
::nano::node::_sleep [expr {1 * 60 * 1000}]
}
}
proc ::nano::node::start args {
package require defer
package require udp
set ::nano::node::startTime [clock seconds]
set ::nano::node::statsStartTime [clock seconds]
array set config {
-bootstrap true
-realtime true
-wait true
}
array set config $args
|
︙ | | |
3196
3197
3198
3199
3200
3201
3202
|
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
set balance [join [list $leading $trailing] "."]
set balance [string trimright $balance "."]
set result [list $balance $baseUnit]
return $result
}
proc ::nano::node::cli args {
switch -exact -- [lindex $args 0] {
"-interactive" {
set ::nano::node::cli::_using_repl true
::nano::node::cli -import
set use_tclreadline false
catch {
package require tclreadline
set use_tclreadline true
}
if {$use_tclreadline} {
proc ::tclreadline::prompt1 {} {
return "\[[dict get $::nano::node::configuration network]\] nano-node [package present nano]> "
}
::tclreadline::Loop
} else {
fconfigure stdout -blocking false
puts -nonewline "> "
flush stdout
fileevent stdin readable [list apply {{} {
uplevel #0 {
gets stdin __line__
if {$__line__ eq "" && [eof stdin]} {
exit 0
}
if {[catch $__line__ __result__]} {
puts "\[ERROR\] $::errorInfo"
} else {
if {$__result__ ne ""} {
puts "$__result__"
}
}
}
puts -nonewline "> "
flush stdout
}}]
vwait forever
}
}
"-import" {
uplevel #0 {
namespace import ::nano::node::cli::*
}
}
default {
error "Not implemented"
}
}
}
proc ::nano::node::cli::_interval {interval} {
set response [list]
foreach {divisor unit} {60 seconds 60 minutes 24 hours 36527 days 1 century} {
set amount [expr {$interval % $divisor}]
if {$amount > 0} {
if {$amount == 1} {
set unit [string range $unit 0 end-1]
}
lappend response $unit $amount
}
set interval [expr {$interval / $divisor}]
}
return "[lreverse $response]"
}
proc ::nano::node::cli::uptime {} {
set now [clock seconds]
set start $::nano::node::startTime
set statsStart $::nano::node::statsStartTime
set uptime [expr {$now - $start}]
set uptimeStats [expr {$now - $statsStart}]
set format {%-19s: %s}
lappend response [format $format Uptime [_interval $uptime]]
lappend response [format $format "Stats last cleared" "[_interval $uptimeStats] ago"]
return [join $response "\n"]
}
proc ::nano::node::cli::stats args {
if {[lindex $args 0] eq "-clear"} {
set ::nano::node::statsStartTime [clock seconds]
unset -nocomplain ::nano::node::stats
unset -nocomplain ::nano::node::_stats_seen_hashes
unset -nocomplain ::nano::node::_stats_seen_hashes_by_rep
return
}
set globalOnly false
if {[lindex $args 0] eq "-global"} {
set globalOnly true
}
set maxKeyLen 0
foreach {key val} [array get ::nano::node::stats] {
if {$globalOnly} {
if {[lindex $key 1] eq "rep"} {
continue
}
}
set keyLen [string length $key]
if {$keyLen > $maxKeyLen} {
set maxKeyLen $keyLen
}
set localStats($key) $val
}
foreach {key val} [lsort -stride 2 -dictionary [array get localStats]] {
puts [format "%-${maxKeyLen}s = $val" $key $val]
}
return
}
proc ::nano::node::cli::version {} {
return [package present nano]
}
proc ::nano::node::cli::help args {
set response [list]
if {[llength $args] == 0} {
lappend response "Commands:"
foreach command [lsort -dictionary [info command ::nano::node::cli::*]] {
set command [namespace tail $command]
set description ""
lappend response [format " %-12s - %s" $command $description]
}
}
return [join $response "\n"]
}
proc ::nano::node::cli::peers args {
if {[lindex $args 0] eq "-count"} {
return [llength [::nano::node::getPeers]]
}
if {[llength $args] == 1} {
set glob [lindex $args 0]
if {[string index $glob 0] eq "!"} {
set globInvert true
set glob [string range $glob 1 end]
} else {
set globInvert false
}
}
lappend response "Peers:"
set now [clock seconds]
foreach peer [::nano::node::getPeers] {
if {[info exists glob]} {
if {![string match $glob $peer] == !$globInvert} {
continue
}
}
if {[info exists ::nano::node::peers($peer)]} {
set peerInfo $::nano::node::peers($peer)
set lastSeen [dict get $peerInfo lastSeen]
set delta [expr {$now - $lastSeen}]
set age "last seen $delta seconds ago"
} else {
set age "statically configured peer"
}
lappend response " $peer: $age"
}
return [join $response "\n"]
}
namespace eval ::nano::node::cli {
namespace export -clear *
}
if {[info exists ::nano::node::cli::_using_repl]} {
::nano::node::cli -import
}
|