public void modelUpdate(action_t action) { if (!isActionOk(action)) { return; // should be assert } if (!m_last_update_percept == true) { return; // should be assert } // Update internal model symbol_list_t action_syms = new symbol_list_t(0); //(UInt64) m_actions_bits); encodeAction(action_syms, action); m_ct.update(action_syms); m_ct.updateHistory(action_syms); if (m_use_self_model) { m_self_model.update(action_syms); } m_hash = hashAfterSymbols(action_syms); m_time_cycle++; m_last_update_percept = false; }
/// <summary> /// 32 bytes transaction hash + 4 bytes signature hash type /// </summary> /// <param name="sigHashType"> Sighash type. </param> /// <returns> Hash and sighash type. </returns> public byte[] GetHashBySigHashType(UInt32 sigHashType) { var managedHash = new hash_t(); TransactionNative.chain_transaction_hash_sighash_type_out(nativeInstance_, sigHashType, ref managedHash); return(managedHash.hash); }
protected override byte[] GetNthNativeElement(UInt64 n) { var managedHash = new hash_t(); HashListNative.kth_core_hash_list_nth_out(NativeInstance, n, ref managedHash); return(managedHash.hash); }
public SearchNode(UInt64 hash, bool is_chance_node) { m_chance_node = is_chance_node; m_mean = 0.0; m_visits = 0; m_hash = hash; }
/// <summary> /// Get the Nth transaction hash from the block. /// </summary> /// <param name="n">Zerp-based index.</param> /// <returns> Transaction hash in 32 byte array format. </returns> public byte[] GetNthHash(int n) { var managedHash = new hash_t(); MerkleBlockNative.kth_chain_merkle_block_hash_nth_out(nativeInstance_, (UIntPtr)n, ref managedHash); return(managedHash.hash); }
// construct a save point public ModelUndo(Agent agent) { m_age = agent.age(); m_hash = agent.hash(); m_reward = agent.reward(); m_history_size = agent.historySize(); m_last_update_percept = agent.m_last_update_percept; }
private void GetTransaction(byte[] txHash, bool requireConfirmed, Action <ErrorCode, Transaction, UInt64, UInt64> handler) { var managedHash = new hash_t { hash = txHash }; IntPtr contextPtr = CreateContext(handler, managedHash); ChainNative.kth_chain_async_transaction(nativeInstance_, contextPtr, managedHash, Helper.BoolToC(requireConfirmed), internalGetTransactionHandler_); }
/// <summary> /// Create an output point from a hash and index pair. /// </summary> /// <param name="pointHash"></param> /// <param name="index"></param> public OutputPoint(byte[] pointHash, UInt32 index) { var managedHash = new hash_t { hash = pointHash }; NativeInstance = OutputPointNative.chain_output_point_construct_from_hash_index(managedHash, index); ownsNativeObject_ = true; }
private void GetBlockHeaderByHash(byte[] blockHash, Action <ErrorCode, Header, UInt64> handler) { var managedHash = new hash_t { hash = blockHash }; IntPtr contextPtr = CreateContext(handler, managedHash); ChainNative.kth_chain_async_block_header_by_hash(nativeInstance_, contextPtr, managedHash, internalGetBlockHeaderHandlerByHash_); }
/* update the non-context tree part of an internal agent after receiving a percept */ public void nonCTModelUpdate(symbol_list_t percept) { if (m_use_self_model) { m_self_model.updateHistory(percept); } m_hash = hashAfterSymbols(percept); m_total_reward += rewardFromPercept(percept); m_last_update_percept = true; }
private void FetchMerkleBlockByHash(byte[] blockHash, Action <ErrorCode, MerkleBlock, UInt64> handler) { var managedHash = new hash_t { hash = blockHash }; IntPtr contextPtr = CreateContext(handler, managedHash); ChainNative.chain_fetch_merkle_block_by_hash(nativeInstance_, contextPtr, managedHash, internalMerkleBlockFetchHandlerByHash_); }
private void FetchBlockHeight(byte[] blockHash, Action <ErrorCode, UInt64> handler) { var managedHash = new hash_t { hash = blockHash }; IntPtr contextPtr = CreateContext(handler, managedHash); ChainNative.chain_fetch_block_height(nativeInstance_, contextPtr, managedHash, internalFetchBlockHeightHandler_); }
private void FetchTransactionPosition(byte[] txHash, bool requireConfirmed, Action <ErrorCode, UInt64, UInt64> handler) { var managedHash = new hash_t { hash = txHash }; IntPtr contextPtr = CreateContext(handler, managedHash); ChainNative.chain_fetch_transaction_position(nativeInstance_, contextPtr, managedHash, requireConfirmed ? 1 : 0, internalFetchTransactionPositionHandler_); }
private void FetchBlockHeaderByHashTxSizes(byte[] blockHash, FetchBlockHeaderByHashTxsSizeHandler handler) { var managedHash = new hash_t { hash = blockHash }; IntPtr contextPtr = CreateContext(handler, managedHash); ChainNative.chain_fetch_block_header_by_hash_txs_size(nativeInstance_, contextPtr, managedHash, internalFetchBlockHeaderByHashTxsSizeHandler_); }
// resets the agent public void reset() { m_ct.clear(); if (m_use_self_model) { m_self_model.clear(); } m_time_cycle = 0; m_total_reward = 0.0; m_last_update_percept = false; m_hash = 5381 << 32; }
/* computes the resultant history hash after processing a set of symbols */ hash_t hashAfterSymbols(symbol_list_t new_syms) { hash_t rval = m_hash; // update the hash of the history //symbol_list_t::const_iterator it = new_syms.begin(); int it = 0; for (; it != new_syms.bits.Length; ++it) { rval = hashAfterSymbol(new_syms.bits[it], rval); } return(rval); }
/* computes the resultant history hash after processing a single symbol */ hash_t hashAfterSymbol(symbol_t sym, hash_t hash) { UInt64 c = (sym == true) ? '1' : '0'; // update with a single iteration of the SDBM hash hash_t low = (hash << 32) >> 32; low = c + (low << 6) + (low << 16) - low; // update with a single iteration of the DJB2 hash hash_t high = hash >> 32; high = ((high << 5) + high) + c; // combine return((high << 32) | low); }
private static void KeokenStateDelegatedCreateAssetHandlerInternal(IntPtr state, string asset_name, Int64 asset_amount, IntPtr owner, UInt64 block_height, hash_t txid) { using (var owner_address = new PaymentAddress(owner)) { internalState_.CreateAsset(asset_name, asset_amount, owner_address, block_height, txid.hash); } }
// revert the agent's internal model of the world // to that of a previous time cycle, false on failure public bool modelRevert(ModelUndo mu) { // return false; // TODONE: implement // assert(m_ct->historySize() > mu.historySize()); // assert(!m_use_self_model || m_self_model->historySize() > mu.historySize()); if (m_time_cycle < mu.age()) { return(false); } // agent properties must be reverted before context update, // since the predicates that depend on the context may // depend on them m_time_cycle = mu.age(); m_hash = mu.hash(); m_total_reward = mu.reward(); m_last_update_percept = mu.lastUpdatePercept(); // revert the context tree and history back to it's previous state if (mu.lastUpdatePercept()) { // if we are undoing an action m_ct.revertHistory(mu.historySize()); if (m_use_self_model) { Int64 end_size = (Int64)m_self_model.historySize(); for (Int64 i = 0; i < (Int64)end_size - (Int64)mu.historySize(); i++) { m_self_model.revert(); } } } else { // if we are undoing an observation / reward Int64 end_size = (Int64)m_ct.historySize(); Int64 percept_bits = (Int64)(m_obs_bits + m_rew_bits); Int64 lim = (Int64)end_size - (Int64)mu.historySize(); for (Int64 i = 0; i < (Int64)end_size - (Int64)mu.historySize(); i++) { //ORIGINAL :: m_ct.revert(percept_bits - i - 1); Int64 offset = percept_bits - i - 1; m_ct.revert(); for (Int64 ix = 0; ix < (Int64)m_ct.size(); ix++) { if (ix != offset) { m_ct.m_history.pop_back(); } } } if (m_use_self_model) { m_self_model.revertHistory(mu.historySize()); } } //assert(!m_use_self_model || m_self_model.historySize() == m_ct.historySize()); return(true); }
private static void FetchBlockByHeightHashTimestampInternalHandler(IntPtr chain, IntPtr context, ErrorCode error, hash_t blockHash, UInt32 timestamp, UInt64 height) { GCHandle handlerHandle = (GCHandle)context; try { var handler = (handlerHandle.Target as FetchBlockByHeightHashTimestampHandler); //Copy native memory before it goes out of scope byte[] blockHashCopy = new byte[blockHash.hash.Length]; blockHash.hash.CopyTo(blockHashCopy, 0); //Convert Unix timestamp to date DateTime blockDate = DateTimeOffset.FromUnixTimeSeconds(timestamp).UtcDateTime; handler(error, blockHashCopy, blockDate, height); } finally { handlerHandle.Free(); } }
public static extern void keoken_memory_state_create_balance_entry(IntPtr state, UInt32 asset_id, Int64 asset_amount, IntPtr source, IntPtr target, UInt64 block_height, hash_t txid);
public static extern void keoken_memory_state_create_asset(IntPtr state, [MarshalAs(UnmanagedType.LPStr)] string asset_name, Int64 asset_amount, IntPtr owner, UInt64 block_height, hash_t txid);
private static void KeokenStateDelegatedCreateBalanceEntryHandlerInternal(IntPtr state, UInt32 asset_id, Int64 asset_amount, IntPtr source, IntPtr target, UInt64 block_height, hash_t txid) { using (var source_address = new PaymentAddress(source)) using (var target_address = new PaymentAddress(target)) { internalState_.CreateBalanceEntry(asset_id, asset_amount, source_address, target_address, block_height, txid.hash); } }