-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
181 lines (157 loc) · 6.17 KB
/
main.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"flag"
"fmt"
"log"
"math/big"
"os"
"time"
"github.com/ethereum-optimism/optimism/op-batcher/batcher"
"github.com/ethereum-optimism/optimism/op-batcher/compressor"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rlp"
)
const ONEBLOB = eth.MaxBlobDataSize
var channelConfig = batcher.ChannelConfig{
SeqWindowSize: 3600, // from base deploy script json
ChannelTimeout: 300, // from base deploy script json
MaxChannelDuration: 600, // 2 hrs
SubSafetyMargin: 4,
MaxFrameSize: ONEBLOB - 1, // default 1 blob
CompressorConfig: compressor.Config{
ApproxComprRatio: 0.4,
Kind: "shadow",
},
BatchType: derive.SpanBatchType, // use SpanBatchType after Delta fork
}
func u64Ptr(v uint64) *uint64 {
return &v
}
var rollupConfig = rollup.Config{
Genesis: rollup.Genesis{L2: eth.BlockID{Number: 0}},
L2ChainID: big.NewInt(8453),
EcotoneTime: u64Ptr(1710374401),
}
// Note: have to override the channel definition to make it work
func buildChannelBuilder(numberOfBlobs int, compressionAlgo string, brotliQuality int, brotliWindow int, chain string) *batcher.ChannelBuilder {
channelConfig := channelConfig
channelConfig.MaxFrameSize = uint64((eth.MaxBlobDataSize - 1) * numberOfBlobs)
channelConfig.MultiFrameTxs = true
channelConfig.TargetNumFrames = 6
channelConfig.CompressorConfig.CompressionAlgo = compressionAlgo
channelConfig.CompressorConfig.TargetOutputSize = uint64(ONEBLOB * numberOfBlobs)
channelConfig.CompressorConfig.BrotliQuality = brotliQuality
channelConfig.CompressorConfig.BrotliWindow = brotliWindow
if chain == "OP" {
rollupConfig.L2ChainID = big.NewInt(10)
}
cb, err := batcher.NewChannelBuilder(channelConfig, rollupConfig, 10)
if err != nil {
log.Fatal(err)
}
return cb
}
func calculateTxBytes(block *types.Block) int {
totalTxSize := 0
for _, tx := range block.Transactions() {
// ignore deposit type
if tx.Type() == types.DepositTxType {
continue
}
txData, err := rlp.EncodeToBytes(tx)
if err != nil {
panic(err)
}
totalTxSize += len(txData)
}
return totalTxSize
}
func main() {
var numberOfBlobs int
var startBlock int
var minimumTxBytes int
var compressionAlgo string
var brotliQuality int
var brotliWindow int
var chain string
flag.IntVar(&numberOfBlobs, "blobs", 6, "Number of blobs to compress")
flag.IntVar(&startBlock, "starting-block", 11443817, "Starting block number")
flag.IntVar(&minimumTxBytes, "minimum-tx-bytes", 450000000, "Minimum number of tx bytes to compress")
flag.StringVar(&compressionAlgo, "compression-algo", "zlib", "Compression algorithm to use")
flag.IntVar(&brotliQuality, "brotli-quality", 6, "Brotli quality")
flag.IntVar(&brotliWindow, "brotli-window", 22, "Brotli window size")
flag.StringVar(&chain, "chain", "base", "chain to use")
flag.Parse()
fmt.Println("Starting block: ", startBlock)
fmt.Println("Number of blobs: ", numberOfBlobs)
fmt.Println("Minimum tx bytes: ", minimumTxBytes)
fmt.Println("Compression algo: ", compressionAlgo)
fmt.Println("Brotli quality: ", brotliQuality)
fmt.Println("Brotli window: ", brotliWindow)
fmt.Println("Chain: ", chain)
// Open the file for writing
file, err := os.OpenFile("results.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
// Initialize the channel builder
cb := buildChannelBuilder(numberOfBlobs, compressionAlgo, brotliQuality, brotliWindow, chain)
// Connect to the local geth node
var clientLocation string
if chain == "base" {
clientLocation = "/data/geth.ipc"
} else {
clientLocation = "https://optimism-mainnet....."
}
client, err := ethclient.Dial(clientLocation)
if err != nil {
// Cannot connect to local node for some reason
log.Fatal(err)
}
totalProcessedTxSize := 0
totalBlocks := 0
var i int;
for i = startBlock; totalProcessedTxSize < minimumTxBytes; i++ {
// If we encounter an error (channel full), output the frames and print the total size of the frames
// fmt.Println(cb.OutputBytes(), cb.InputBytes(), cb.ReadyBytes(), cb.PendingFrames())
block, err := client.BlockByNumber(context.Background(), big.NewInt(int64(i)))
if err != nil {
log.Fatal(err)
}
_, err = cb.AddBlock(block)
if err != nil {
fmt.Println("Channel full, outputting frames")
fmt.Println("Processed tx size ", totalProcessedTxSize)
fmt.Println("Number of block processed ", i-startBlock)
cb.OutputFrames()
cb.Reset()
i--
continue
}
// Calculate the total size of all non-deposit transactions
totalProcessedTxSize += calculateTxBytes(block)
}
// close the channel so that the last batches are compressed and ready to be outputted
cb.Close()
cb.OutputFrames()
cb.Reset()
// Get all the outputted frame size
totalFrameSize := cb.OutputBytes()
totalBlocks = i - startBlock
fmt.Println("total frames size: ", totalFrameSize)
fmt.Println("total tx size: ", totalProcessedTxSize)
fmt.Println("compression ratio: ", float64(totalFrameSize)/float64(totalProcessedTxSize))
if compressionAlgo == "brotli" {
resultString := fmt.Sprintf("[%s] Starting block: %d\nNumber of blobs: %d\nMinimum tx bytes: %d\nTotal frames size: %d\nTotal tx size: %d\nCompression ratio: %f\nCompression Algo: %s\nTotal Blocks: %d\nBrotli Quality: %d\nBrotli Window: %d\n\n", time.Now().Format(time.RFC3339), startBlock, numberOfBlobs, minimumTxBytes, totalFrameSize, totalProcessedTxSize, float64(totalFrameSize)/float64(totalProcessedTxSize), compressionAlgo, totalBlocks, brotliQuality, brotliWindow)
file.WriteString(resultString)
} else {
resultString := fmt.Sprintf("[%s] Starting block: %d\nNumber of blobs: %d\nMinimum tx bytes: %d\nTotal frames size: %d\nTotal tx size: %d\nCompression ratio: %f\nCompression Algo: %s\nTotal Blocks: %d\n\n", time.Now().Format(time.RFC3339), startBlock, numberOfBlobs, minimumTxBytes, totalFrameSize, totalProcessedTxSize, float64(totalFrameSize)/float64(totalProcessedTxSize), compressionAlgo, totalBlocks)
file.WriteString(resultString)
}
defer client.Close()
}