code.vegaprotocol.io/vega@v0.79.0/datanode/networkhistory/testdata/update_test_evts_file.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package main 17 18 import ( 19 "compress/gzip" 20 "context" 21 "fmt" 22 "io" 23 "os" 24 "os/signal" 25 26 broker2 "code.vegaprotocol.io/vega/core/broker" 27 "code.vegaprotocol.io/vega/core/events" 28 "code.vegaprotocol.io/vega/datanode/broker" 29 "code.vegaprotocol.io/vega/datanode/entities" 30 "code.vegaprotocol.io/vega/logging" 31 ) 32 33 const ( 34 toHeight = 5000 35 EventFile = "smoketest_to_block_5000.evts" 36 ) 37 38 // reads in a source event file and write the events to target events file until the given block height is reached 39 func main() { 40 if len(os.Args) != 1 { 41 fmt.Printf("expected <source events file>") 42 } 43 44 sourceDir := os.Args[1] 45 46 fmt.Printf("creating target event file to height %d") 47 48 ctx := context.Background() 49 ctx, cancel := context.WithCancel(ctx) 50 c := make(chan os.Signal, 1) 51 signal.Notify(c, os.Interrupt) 52 defer func() { 53 signal.Stop(c) 54 cancel() 55 }() 56 57 config := broker.NewDefaultConfig() 58 59 fileEventSource, err := broker.NewBufferFilesEventSource(sourceDir, 0, config.FileEventSourceConfig.SendChannelBufferSize, 60 "testnet-001") 61 if err != nil { 62 panic(err) 63 } 64 65 eventsCh, errCh := fileEventSource.Receive(ctx) 66 67 fileClient, err := broker2.NewFileClient(logging.NewTestLogger(), &broker2.FileConfig{ 68 Enabled: true, 69 File: EventFile, 70 }) 71 if err != nil { 72 panic(err) 73 } 74 75 for { 76 select { 77 case <-ctx.Done(): 78 return 79 case err := <-errCh: 80 panic(err) 81 case event := <-eventsCh: 82 if event.Type() == events.TimeUpdate { 83 timeUpdate := event.(entities.TimeUpdateEvent) 84 fmt.Printf("Block: %d\n", timeUpdate.BlockNr()) 85 if timeUpdate.BlockNr() > toHeight+1 { 86 fileClient.Close() 87 compressEventFile(EventFile, EventFile+".gz") 88 err = os.RemoveAll(EventFile) 89 if err != nil { 90 panic(err) 91 } 92 return 93 } 94 95 } 96 97 fileClient.Send(event) 98 } 99 } 100 } 101 102 func compressEventFile(source string, target string) { 103 sourceFile, err := os.Open(source) 104 if err != nil { 105 panic(err) 106 } 107 defer func() { 108 sourceFile.Close() 109 }() 110 111 fileToWrite, err := os.Create(target) 112 if err != nil { 113 panic(err) 114 } 115 116 zw := gzip.NewWriter(fileToWrite) 117 if err != nil { 118 panic(err) 119 } 120 defer func() { 121 zw.Close() 122 }() 123 124 if _, err := io.Copy(zw, sourceFile); err != nil { 125 panic(err) 126 } 127 }