|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/cloudstruct/go-ouroboros-network" |
| 9 | + "github.com/cloudstruct/go-ouroboros-network/cmd/common" |
| 10 | + |
| 11 | + "github.com/fxamacker/cbor/v2" |
| 12 | + "golang.org/x/crypto/blake2b" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + // Parse commandline |
| 17 | + f := common.NewGlobalFlags() |
| 18 | + f.Parse() |
| 19 | + // Create connection |
| 20 | + conn := common.CreateClientConnection(f) |
| 21 | + errorChan := make(chan error) |
| 22 | + go func() { |
| 23 | + for { |
| 24 | + err := <-errorChan |
| 25 | + fmt.Printf("ERROR(async): %s\n", err) |
| 26 | + os.Exit(1) |
| 27 | + } |
| 28 | + }() |
| 29 | + o, err := ouroboros.New( |
| 30 | + ouroboros.WithConnection(conn), |
| 31 | + ouroboros.WithNetworkMagic(uint32(f.NetworkMagic)), |
| 32 | + ouroboros.WithErrorChan(errorChan), |
| 33 | + ouroboros.WithNodeToNode(f.NtnProto), |
| 34 | + ouroboros.WithKeepAlive(true), |
| 35 | + ) |
| 36 | + if err != nil { |
| 37 | + fmt.Printf("ERROR: %s\n", err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + o.LocalTxMonitor().Client.Start() |
| 41 | + |
| 42 | + capacity, size, numberOfTxs, err := o.LocalTxMonitor().Client.GetSizes() |
| 43 | + if err != nil { |
| 44 | + fmt.Printf("ERROR(GetSizes): %s\n", err) |
| 45 | + os.Exit(1) |
| 46 | + } |
| 47 | + fmt.Printf("Mempool size/capacity (bytes): %d / %d, TXs: %d\n", size, capacity, numberOfTxs) |
| 48 | + |
| 49 | + fmt.Printf("Transactions:\n\n") |
| 50 | + for { |
| 51 | + tx, err := o.LocalTxMonitor().Client.NextTx() |
| 52 | + if err != nil { |
| 53 | + fmt.Printf("ERROR(NextTx): %s\n", err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + if tx == nil { |
| 57 | + break |
| 58 | + } |
| 59 | + // Unwrap raw transaction bytes into a CBOR array |
| 60 | + var txUnwrap []cbor.RawMessage |
| 61 | + if err := cbor.Unmarshal(tx, &txUnwrap); err != nil { |
| 62 | + fmt.Printf("ERROR(unwrap): %s\n", err) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + // index 0 is the transaction body |
| 66 | + // Store index 0 (transaction body) as byte array |
| 67 | + txBody := txUnwrap[0] |
| 68 | + // Convert the body into a blake2b256 hash string |
| 69 | + txIdHash := blake2b.Sum256(txBody) |
| 70 | + // Encode hash string as byte array to hex string |
| 71 | + txIdHex := hex.EncodeToString(txIdHash[:]) |
| 72 | + fmt.Printf("%s\n", txIdHex) |
| 73 | + } |
| 74 | +} |
0 commit comments