|
| 1 | +package bob |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/qubic/go-node-connector/types" |
| 11 | +) |
| 12 | + |
| 13 | +const quTransferLogType = 0 |
| 14 | + |
| 15 | +// firstQUTransfer holds the fields from the first QU_TRANSFER event for a transaction. |
| 16 | +type firstQUTransfer struct { |
| 17 | + Source string // uppercase identity |
| 18 | + Destination string // uppercase identity |
| 19 | + Amount int64 |
| 20 | + Tick uint32 |
| 21 | +} |
| 22 | + |
| 23 | +// computeMoneyFlew fetches logs for the given tick and computes the moneyFlew |
| 24 | +// status for each transaction by comparing the transaction's fields against its |
| 25 | +// first QU_TRANSFER log event. |
| 26 | +// |
| 27 | +// Algorithm (from bob maintainer): |
| 28 | +// |
| 29 | +// moneyFlew = (tx.amount == first_event.amount) |
| 30 | +// && (tx.src == first_event.src) |
| 31 | +// && (tx.dst == first_event.dst) |
| 32 | +// && (tx.tick == first_event.tick) |
| 33 | +// |
| 34 | +// Only transactions with amount > 0 are candidates; others default to false. |
| 35 | +func computeMoneyFlew(ctx context.Context, client *Client, tickResp *bobTickResponse, txs types.Transactions, tickNumber uint32) (types.TransactionStatus, error) { |
| 36 | + epoch := tickResp.TickData.Epoch |
| 37 | + logIdStart := tickResp.TickData.LogIdStart |
| 38 | + logIdEnd := tickResp.TickData.LogIdEnd |
| 39 | + |
| 40 | + // Build transaction lookup: txHash -> Transaction |
| 41 | + txByHash := make(map[string]*types.Transaction, len(txs)) |
| 42 | + for i := range txs { |
| 43 | + txID, err := txs[i].ID() |
| 44 | + if err != nil { |
| 45 | + return types.TransactionStatus{}, fmt.Errorf("computing tx ID for index %d: %w", i, err) |
| 46 | + } |
| 47 | + txByHash[txID] = &txs[i] |
| 48 | + } |
| 49 | + |
| 50 | + // Collect non-zero transaction digests and build the digest list |
| 51 | + var digestList [][32]byte |
| 52 | + for i, digestStr := range tickResp.TickData.TransactionDigests { |
| 53 | + if digestStr == "" { |
| 54 | + continue |
| 55 | + } |
| 56 | + digest, err := qubicHashToBytes32(digestStr) |
| 57 | + if err != nil { |
| 58 | + return types.TransactionStatus{}, fmt.Errorf("converting digest[%d]: %w", i, err) |
| 59 | + } |
| 60 | + digestList = append(digestList, digest) |
| 61 | + } |
| 62 | + |
| 63 | + // Build the MoneyFlew bit array |
| 64 | + var moneyFlew [128]byte |
| 65 | + |
| 66 | + // If there are no logs, return with all-zero moneyFlew |
| 67 | + if logIdStart < 0 || logIdEnd < logIdStart { |
| 68 | + return types.TransactionStatus{ |
| 69 | + CurrentTickOfNode: tickNumber, |
| 70 | + Tick: tickNumber, |
| 71 | + TxCount: uint32(len(digestList)), |
| 72 | + MoneyFlew: moneyFlew, |
| 73 | + TransactionDigests: digestList, |
| 74 | + }, nil |
| 75 | + } |
| 76 | + |
| 77 | + // Fetch all logs for this tick |
| 78 | + firstTransfers, err := fetchFirstQUTransfers(ctx, client, epoch, logIdStart, logIdEnd) |
| 79 | + if err != nil { |
| 80 | + return types.TransactionStatus{}, fmt.Errorf("fetching logs for tick %d: %w", tickNumber, err) |
| 81 | + } |
| 82 | + |
| 83 | + // For each transaction digest, determine moneyFlew |
| 84 | + for i, digest := range digestList { |
| 85 | + // Convert digest to txHash (60-char lowercase) |
| 86 | + var id types.Identity |
| 87 | + id, err := id.FromPubKey(digest, true) |
| 88 | + if err != nil { |
| 89 | + log.Printf("[WARN] failed to convert digest[%d] to identity: %v", i, err) |
| 90 | + continue |
| 91 | + } |
| 92 | + txHash := id.String() |
| 93 | + |
| 94 | + // Look up the actual transaction |
| 95 | + tx, hasTx := txByHash[txHash] |
| 96 | + if !hasTx || tx.Amount <= 0 { |
| 97 | + continue // moneyFlew stays false |
| 98 | + } |
| 99 | + |
| 100 | + // Look up the first QU_TRANSFER for this tx |
| 101 | + transfer, hasLog := firstTransfers[txHash] |
| 102 | + if !hasLog { |
| 103 | + continue // no QU_TRANSFER event -> moneyFlew = false |
| 104 | + } |
| 105 | + |
| 106 | + // Convert tx public keys to uppercase identities for comparison |
| 107 | + var srcID types.Identity |
| 108 | + srcID, err = srcID.FromPubKey(tx.SourcePublicKey, false) |
| 109 | + if err != nil { |
| 110 | + log.Printf("[WARN] failed to convert source pubkey for tx %s: %v", txHash, err) |
| 111 | + continue |
| 112 | + } |
| 113 | + var dstID types.Identity |
| 114 | + dstID, err = dstID.FromPubKey(tx.DestinationPublicKey, false) |
| 115 | + if err != nil { |
| 116 | + log.Printf("[WARN] failed to convert dest pubkey for tx %s: %v", txHash, err) |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + // Apply the moneyFlew algorithm |
| 121 | + if tx.Amount == transfer.Amount && |
| 122 | + string(srcID) == transfer.Source && |
| 123 | + string(dstID) == transfer.Destination && |
| 124 | + tx.Tick == transfer.Tick { |
| 125 | + setMoneyFlewBit(&moneyFlew, i) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return types.TransactionStatus{ |
| 130 | + CurrentTickOfNode: tickNumber, |
| 131 | + Tick: tickNumber, |
| 132 | + TxCount: uint32(len(digestList)), |
| 133 | + MoneyFlew: moneyFlew, |
| 134 | + TransactionDigests: digestList, |
| 135 | + }, nil |
| 136 | +} |
| 137 | + |
| 138 | +// fetchFirstQUTransfers fetches all logs for the tick and returns a map of |
| 139 | +// txHash -> first QU_TRANSFER event for that transaction. |
| 140 | +func fetchFirstQUTransfers(ctx context.Context, client *Client, epoch uint16, logIdStart, logIdEnd int64) (map[string]firstQUTransfer, error) { |
| 141 | + path := fmt.Sprintf("/log/%d/%d/%d", epoch, logIdStart, logIdEnd) |
| 142 | + body, err := client.RESTGet(ctx, path) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("GET %s: %w", path, err) |
| 145 | + } |
| 146 | + |
| 147 | + var logs []bobLogEvent |
| 148 | + if err := json.Unmarshal(body, &logs); err != nil { |
| 149 | + return nil, fmt.Errorf("unmarshalling logs: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + result := make(map[string]firstQUTransfer) |
| 153 | + for _, logEvent := range logs { |
| 154 | + if !logEvent.OK { |
| 155 | + continue |
| 156 | + } |
| 157 | + if logEvent.Type != quTransferLogType { |
| 158 | + continue |
| 159 | + } |
| 160 | + txHash := strings.ToLower(logEvent.TxHash) |
| 161 | + if txHash == "" { |
| 162 | + continue |
| 163 | + } |
| 164 | + // Only keep the first QU_TRANSFER per transaction |
| 165 | + if _, exists := result[txHash]; exists { |
| 166 | + continue |
| 167 | + } |
| 168 | + result[txHash] = firstQUTransfer{ |
| 169 | + Source: logEvent.Body.From, |
| 170 | + Destination: logEvent.Body.To, |
| 171 | + Amount: logEvent.Body.Amount, |
| 172 | + Tick: logEvent.Tick, |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return result, nil |
| 177 | +} |
| 178 | + |
| 179 | +// setMoneyFlewBit sets bit at position index in the MoneyFlew byte array. |
| 180 | +func setMoneyFlewBit(moneyFlew *[128]byte, index int) { |
| 181 | + bytePos := index / 8 |
| 182 | + bitPos := index % 8 |
| 183 | + moneyFlew[bytePos] |= 1 << bitPos |
| 184 | +} |
0 commit comments