Index: build/test/test.tcl ================================================================== --- build/test/test.tcl +++ build/test/test.tcl @@ -220,32 +220,32 @@ proc test_work {} { # Verification ## Positive set blockhash "0CF7F1E71B6C692BD8CBCF440CB1E4DF386761E6E66609563BD62A649DF6D0BE" set work "01A87EEC1B6C692B" - set verify [::nano::internal::validateWork [binary decode hex $blockhash] [binary decode hex $work]] + set verify [::nano::work::validate $blockhash $work] if {!$verify} { puts "\[1.FAIL\] Got: $verify" puts "\[1.FAIL\] Exp: true" return false } ## Negative set work "11A87EEC1B6C692B" - set verify [::nano::internal::validateWork [binary decode hex $blockhash] [binary decode hex $work]] + set verify [::nano::work::validate $blockhash $work] if {$verify} { puts "\[2.FAIL\] Got: $verify" puts "\[2.FAIL\] Exp: false" return false } # Generation set blockhash "1C840FED01000000D8CBCF440CB1E4DF386761E6E66609563BD62A649DF6D0BE" - set work [binary encode hex [::nano::internal::generateWork [binary decode hex $blockhash]]] - set verify [::nano::internal::validateWork [binary decode hex $blockhash] [binary decode hex $work]] + set work [::nano::work::fromBlockhash $blockhash] + set verify [::nano::work::validate $blockhash $work] if {!$verify} { puts "\[3.FAIL\] Got: $verify" puts "\[3.FAIL\] Exp: true" return false Index: examples/generate-work/generate-work ================================================================== --- examples/generate-work/generate-work +++ examples/generate-work/generate-work @@ -8,13 +8,11 @@ exit 1 } foreach arg $argv { if {[catch { - set work [::nano::internal::generateWork [binary decode hex $arg]] - set work [binary encode hex $work] - set work [string toupper $work] + set work [::nano::work::fromBlockhash $arg] } err]} { puts stderr "Error generating work for \"$arg\": $err" } else { puts "WORK($arg) = $work" } Index: nano.tcl ================================================================== --- nano.tcl +++ nano.tcl @@ -6,10 +6,11 @@ namespace eval ::nano {} namespace eval ::nano::address {} namespace eval ::nano::key {} namespace eval ::nano::block {} namespace eval ::nano::block::create {} +namespace eval ::nano::work {} namespace eval ::nano::account {} set ::nano::block::hashLength 32 set ::nano::block::signatureLength 64 set ::nano::key::publicKeyLength 32 @@ -371,11 +372,11 @@ } } set blockJSONFields { type account source destination previous representative balance - link link_as_account _blockHash _workHash signature _comment + link link_as_account _blockHash _workHash work signature _comment } set blockJSONEntries [lmap field $blockJSONFields { if {![info exists block($field)]} { continue @@ -628,10 +629,46 @@ dict set blockDict signKey $block(signKey) } tailcall ::nano::block::jsonFromDict $blockDict } + +proc ::nano::work::fromBlockhash {blockhash} { + if {[string length $blockhash] != 32} { + set blockhash [binary decode hex $blockhash] + } + + set work [binary encode hex [::nano::internal::generateWork $blockhash]] + set work [string tolower $work] + return $work +} + +proc ::nano::work::fromBlock {blockJSON} { + set blockDict [::json::json2dict $blockJSON] + set workhash [dict get $blockDict _workHash] + tailcall ::nano::work::fromBlockhash $workhash +} + +proc ::nano::work::updateBlock {blockJSON} { + set blockDict [::json::json2dict $blockJSON] + set workhash [dict get $blockDict _workHash] + set work [::nano::work::fromBlockhash $workhash] + dict set blockDict work $work + tailcall ::nano::block::_dictToJSON $blockDict +} + +proc ::nano::work::validate {blockhash work} { + if {[string length $blockhash] != 32} { + set blockhash [binary decode hex $blockhash] + } + + if {[string length $work] != 8} { + set work [binary decode hex $work] + } + + tailcall ::nano::internal::validateWork $blockhash $work +} # -- Tracked accounts -- proc ::nano::account::setFrontier {account frontierHash balance representative} { set accountPubKey [::nano::address::toPublicKey $account -hex] set ::nano::account::frontiers($accountPubKey) [dict create \