private static int InternalTransactionHandler(IntPtr node, IntPtr chain, IntPtr context, ErrorCode error, IntPtr transaction) { var handlerHandle = (GCHandle)context; var closed = false; var keepSubscription = false; try { if (NodeNative.kth_node_stopped(node) != 0 || error == ErrorCode.ServiceStopped) { handlerHandle.Free(); closed = true; return(0); } var newTransaction = transaction != IntPtr.Zero? new Transaction(transaction) : null; var handler = (handlerHandle.Target as TransactionHandler); keepSubscription = handler(error, newTransaction); if (!keepSubscription) { handlerHandle.Free(); closed = true; } return(Helper.BoolToC(keepSubscription)); } finally { if (!keepSubscription && !closed) { handlerHandle.Free(); } } }
/// <summary> /// Create node object. Does not init database or start execution yet. /// </summary> /// <param name="settings"> Settings object. </param> /// <param name="showNodeLog"> Print Native node stdout and stderr. </param> public Node(Settings settings, bool showNodeLog = false) : this() { var native = settings.ToNative(); nativeInstance_ = NodeNative.kth_node_construct(ref native, showNodeLog ? 1 : 0); }
/// <summary> /// Be notified (called back) when the local copy of the blockchain is updated at the transaction level. /// </summary> /// <param name="handler"> Callback which will be called when a transaction is added. </param> public void SubscribeTransactionNotifications(TransactionHandler handler) { var handlerHandle = GCHandle.Alloc(handler); var handlerPtr = (IntPtr)handlerHandle; NodeNative.kth_chain_subscribe_transaction(nativeInstance_, Chain.NativeInstance, handlerPtr, internalTxHandler_); }
/// <summary> /// Be notified (called back) when the local copy of the blockchain is reorganized. /// </summary> /// <param name="handler"> Callback which will be called when blocks are added or removed. /// The callback returns 3 parameters: /// - Height (UInt64): The chain height at which reorganization takes place /// - Incoming (Blocklist): Incoming blocks (added to the blockchain). /// - Outgoing (Blocklist): Outgoing blocks (removed from the blockchain). /// </param> public void SubscribeBlockNotifications(BlockHandler handler) { var handlerHandle = GCHandle.Alloc(handler); var handlerPtr = (IntPtr)handlerHandle; NodeNative.kth_chain_subscribe_blockchain(nativeInstance_, Chain.NativeInstance, handlerPtr, internalBlockHandler_); }
public void SubscribeDsProofNotifications(DsProofHandler handler) { var handlerHandle = GCHandle.Alloc(handler); var handlerPtr = (IntPtr)handlerHandle; NodeNative.kth_chain_subscribe_ds_proof(nativeInstance_, Chain.NativeInstance, handlerPtr, internalDsProofHandler_); }
protected virtual void Dispose(bool disposing) { if (disposing) { //Release managed resources and call Dispose for member variables } Stop(); NodeNative.kth_node_destruct(nativeInstance_); }
/// <summary> /// Stops the node; that includes all activies, such as synchronization and networking. /// </summary> public void Stop() { // NodeNative.kth_node_stop(nativeInstance_); NodeNative.kth_node_signal_stop(nativeInstance_); while (running_ && !stopped_) { System.Threading.Thread.Sleep(100); } }
private void Launch(Action <ErrorCode> handler) { var handlerHandle = GCHandle.Alloc(handler); var handlerPtr = (IntPtr)handlerHandle; Task.Run(() => { NodeNative.kth_node_init_run_and_wait_for_signal(nativeInstance_, handlerPtr, internalRunNodeHandler_); stopped_ = true; }); }
private void InternalRunNodeHandler(IntPtr node, IntPtr handlerPtr, int error) { var handlerHandle = (GCHandle)handlerPtr; var handler = (handlerHandle.Target as Action <ErrorCode>); try { if (error == 0) { chain_ = new Chain(NodeNative.kth_node_get_chain(nativeInstance_)); running_ = true; stopped_ = false; } handler((ErrorCode)error); } finally { handlerHandle.Free(); } }
private static int InternalBlockHandler(IntPtr node, IntPtr chain, IntPtr context, ErrorCode error, UInt64 forkHeight, IntPtr incoming, IntPtr outgoing) { var handlerHandle = (GCHandle)context; var closed = false; var keepSubscription = false; try { if (NodeNative.kth_node_stopped(node) != 0 || error == ErrorCode.ServiceStopped) { handlerHandle.Free(); closed = true; return(0); } var incomingBlocks = incoming != IntPtr.Zero? new BlockList(incoming) : null; var outgoingBlocks = outgoing != IntPtr.Zero? new BlockList(outgoing) : null; var handler = (handlerHandle.Target as BlockHandler); keepSubscription = handler(error, forkHeight, incomingBlocks, outgoingBlocks); incomingBlocks?.Dispose(); outgoingBlocks?.Dispose(); if (!keepSubscription) { handlerHandle.Free(); closed = true; } return(Helper.BoolToC(keepSubscription)); } finally { if (!keepSubscription && !closed) { handlerHandle.Free(); } } }
/// <summary> /// Closes the node; that includes all activies, such as synchronization and networking. /// </summary> public void Close() { NodeNative.kth_node_close(nativeInstance_); }