-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwriter.go
More file actions
98 lines (82 loc) · 2.04 KB
/
writer.go
File metadata and controls
98 lines (82 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sender
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"github.com/sei-protocol/sei-load/types"
)
// implements `Send`
type TxsWriter struct {
gasPerBlock uint64
nextHeight uint64
txsDir string
blocksGenerated uint64
numBlocks uint64
bufferGas uint64
txBuffer []*types.LoadTx
}
func NewTxsWriter(gasPerBlock uint64, txsDir string, startHeight uint64, numBlocks uint64) *TxsWriter {
// what height to start at?
return &TxsWriter{
gasPerBlock: gasPerBlock,
nextHeight: startHeight,
txsDir: txsDir,
blocksGenerated: 0,
numBlocks: numBlocks,
bufferGas: 0,
txBuffer: make([]*types.LoadTx, 0),
}
}
// Send writes the transaction to the writer
func (w *TxsWriter) Send(ctx context.Context, tx *types.LoadTx) error {
// if bwe would exceed gasPerBlock, flush
if w.bufferGas+tx.EthTx.Gas() > w.gasPerBlock {
if err := w.Flush(); err != nil {
return err
}
}
// add to buffer
w.txBuffer = append(w.txBuffer, tx)
w.bufferGas += tx.EthTx.Gas()
return nil
}
type TxWriteData struct {
TxPayloads [][]byte `json:"tx_payloads"`
}
func (w *TxsWriter) Flush() error {
defer func() {
// clear buffer and reset bufferGas and increment nextHeight
w.txBuffer = make([]*types.LoadTx, 0)
w.bufferGas = 0
w.nextHeight++
w.blocksGenerated++
}()
// write to dir `~/load_txs`
// make dir if it doesn't exist
err := os.MkdirAll(w.txsDir, 0755)
if err != nil {
return err
}
txsFile := filepath.Join(w.txsDir, fmt.Sprintf("%d_txs.json", w.nextHeight))
txData := TxWriteData{
TxPayloads: make([][]byte, 0),
}
for _, tx := range w.txBuffer {
txData.TxPayloads = append(txData.TxPayloads, tx.Payload)
}
txDataJSON, err := json.Marshal(txData)
if err != nil {
return err
}
if err := os.WriteFile(txsFile, txDataJSON, 0644); err != nil {
return err
}
log.Printf("Flushed %d transactions to %s", len(w.txBuffer), txsFile)
if w.blocksGenerated >= w.numBlocks {
return fmt.Errorf("reached max number of blocks: %d", w.numBlocks)
}
return nil
}