|
| 1 | +package kernel |
| 2 | + |
| 3 | +/* |
| 4 | +#include "kernel/bitcoinkernel.h" |
| 5 | +*/ |
| 6 | +import "C" |
| 7 | +import ( |
| 8 | + "runtime/cgo" |
| 9 | + "unsafe" |
| 10 | +) |
| 11 | + |
| 12 | +// NotificationCallbacks contains all the Go callback function types for notifications. |
| 13 | +type NotificationCallbacks struct { |
| 14 | + OnBlockTip func(state SynchronizationState, index *BlockIndex, progress float64) |
| 15 | + OnHeaderTip func(state SynchronizationState, height int64, timestamp int64, presync bool) |
| 16 | + OnProgress func(title string, percent int, resumable bool) |
| 17 | + OnWarningSet func(warning Warning, message string) |
| 18 | + OnWarningUnset func(warning Warning) |
| 19 | + OnFlushError func(message string) |
| 20 | + OnFatalError func(message string) |
| 21 | +} |
| 22 | + |
| 23 | +//export go_notify_block_tip_bridge |
| 24 | +func go_notify_block_tip_bridge(user_data unsafe.Pointer, state C.kernel_SynchronizationState, index *C.kernel_BlockIndex, verification_progress C.double) { |
| 25 | + // Convert void* back to Handle - user_data contains Handle ID |
| 26 | + handle := cgo.Handle(user_data) |
| 27 | + // Retrieve original Go callback struct |
| 28 | + callbacks := handle.Value().(*NotificationCallbacks) |
| 29 | + |
| 30 | + if callbacks.OnBlockTip != nil { |
| 31 | + goState := SynchronizationState(state) |
| 32 | + // Note: BlockIndex from notification is const and owned by kernel library |
| 33 | + // We create a wrapper but don't set finalizer since we don't own it |
| 34 | + goIndex := &BlockIndex{ptr: (*C.kernel_BlockIndex)(unsafe.Pointer(index))} |
| 35 | + callbacks.OnBlockTip(goState, goIndex, float64(verification_progress)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +//export go_notify_header_tip_bridge |
| 40 | +func go_notify_header_tip_bridge(user_data unsafe.Pointer, state C.kernel_SynchronizationState, height C.int64_t, timestamp C.int64_t, presync C.bool) { |
| 41 | + handle := cgo.Handle(user_data) |
| 42 | + callbacks := handle.Value().(*NotificationCallbacks) |
| 43 | + |
| 44 | + if callbacks.OnHeaderTip != nil { |
| 45 | + goState := SynchronizationState(state) |
| 46 | + callbacks.OnHeaderTip(goState, int64(height), int64(timestamp), bool(presync)) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +//export go_notify_progress_bridge |
| 51 | +func go_notify_progress_bridge(user_data unsafe.Pointer, title *C.char, title_len C.size_t, progress_percent C.int, resume_possible C.bool) { |
| 52 | + handle := cgo.Handle(user_data) |
| 53 | + callbacks := handle.Value().(*NotificationCallbacks) |
| 54 | + |
| 55 | + if callbacks.OnProgress != nil { |
| 56 | + goTitle := C.GoStringN(title, C.int(title_len)) |
| 57 | + callbacks.OnProgress(goTitle, int(progress_percent), bool(resume_possible)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +//export go_notify_warning_set_bridge |
| 62 | +func go_notify_warning_set_bridge(user_data unsafe.Pointer, warning C.kernel_Warning, message *C.char, message_len C.size_t) { |
| 63 | + handle := cgo.Handle(user_data) |
| 64 | + callbacks := handle.Value().(*NotificationCallbacks) |
| 65 | + |
| 66 | + if callbacks.OnWarningSet != nil { |
| 67 | + goWarning := Warning(warning) |
| 68 | + goMessage := C.GoStringN(message, C.int(message_len)) |
| 69 | + callbacks.OnWarningSet(goWarning, goMessage) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +//export go_notify_warning_unset_bridge |
| 74 | +func go_notify_warning_unset_bridge(user_data unsafe.Pointer, warning C.kernel_Warning) { |
| 75 | + handle := cgo.Handle(user_data) |
| 76 | + callbacks := handle.Value().(*NotificationCallbacks) |
| 77 | + |
| 78 | + if callbacks.OnWarningUnset != nil { |
| 79 | + goWarning := Warning(warning) |
| 80 | + callbacks.OnWarningUnset(goWarning) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +//export go_notify_flush_error_bridge |
| 85 | +func go_notify_flush_error_bridge(user_data unsafe.Pointer, message *C.char, message_len C.size_t) { |
| 86 | + handle := cgo.Handle(user_data) |
| 87 | + callbacks := handle.Value().(*NotificationCallbacks) |
| 88 | + |
| 89 | + if callbacks.OnFlushError != nil { |
| 90 | + goMessage := C.GoStringN(message, C.int(message_len)) |
| 91 | + callbacks.OnFlushError(goMessage) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +//export go_notify_fatal_error_bridge |
| 96 | +func go_notify_fatal_error_bridge(user_data unsafe.Pointer, message *C.char, message_len C.size_t) { |
| 97 | + handle := cgo.Handle(user_data) |
| 98 | + callbacks := handle.Value().(*NotificationCallbacks) |
| 99 | + |
| 100 | + if callbacks.OnFatalError != nil { |
| 101 | + goMessage := C.GoStringN(message, C.int(message_len)) |
| 102 | + callbacks.OnFatalError(goMessage) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// SynchronizationState represents the current sync state passed to tip changed callbacks |
| 107 | +type SynchronizationState int |
| 108 | + |
| 109 | +const ( |
| 110 | + SyncStateInitReindex SynchronizationState = iota |
| 111 | + SyncStateInitDownload |
| 112 | + SyncStatePostInit |
| 113 | +) |
| 114 | + |
| 115 | +// Warning represents possible warning types issued by validation |
| 116 | +type Warning int |
| 117 | + |
| 118 | +const ( |
| 119 | + WarningUnknownNewRulesActivated Warning = iota |
| 120 | + WarningLargeWorkInvalidChain |
| 121 | +) |
0 commit comments