forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_stats.go
155 lines (130 loc) · 4.46 KB
/
output_stats.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
package simulation
import (
"bytes"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/obscuronet/go-obscuro/go/common/log"
"github.com/obscuronet/go-obscuro/integration/common/testlog"
"github.com/obscuronet/go-obscuro/go/ethadapter"
"github.com/obscuronet/go-obscuro/go/common"
)
// OutputStats decouples the processing of data and the collection of statistics
// there's a bit more to do around this, this serves as a first iteration
type OutputStats struct {
simulation *Simulation
l2RollupCountInHeaders int // Number of rollups counted while node rollup header traversing
l2RollupCountInL1Blocks int // Number of rollups counted while traversing the node block header and searching the txs
l2RollupTxCountInL1Blocks int // Number of rollup Txs counted while traversing the node block header
l1Height int // Last known l1 block height
l2Height int // Last known l2 block height
canonicalERC20DepositCount int // Number of erc20 deposits on the canonical chain
}
// NewOutputStats processes the simulation and retrieves the output statistics
func NewOutputStats(simulation *Simulation) *OutputStats {
outputStats := &OutputStats{
simulation: simulation,
}
outputStats.countBlockChain()
outputStats.populateHeights()
return outputStats
}
func (o *OutputStats) populateHeights() {
l1Height, err := o.simulation.RPCHandles.EthClients[0].BlockNumber()
if err != nil {
panic(fmt.Errorf("simulation failed because could not read L1 height. Cause: %w", err))
}
o.l1Height = int(l1Height)
obscuroClient := o.simulation.RPCHandles.ObscuroClients[0]
hRollup, err := getHeadBatchHeader(obscuroClient)
if err != nil {
panic(err)
}
o.l2Height = int(hRollup.Number.Uint64())
}
func (o *OutputStats) countBlockChain() {
l1Node := o.simulation.RPCHandles.EthClients[0]
obscuroClient := o.simulation.RPCHandles.ObscuroClients[0]
// iterate the Node Headers and get the rollups
header, err := getHeadBatchHeader(obscuroClient)
if err != nil {
panic(err)
}
for {
if header != nil && !bytes.Equal(header.Hash().Bytes(), (common.L1BlockHash{}).Bytes()) {
break
}
o.l2RollupCountInHeaders++
header, err = obscuroClient.BatchHeaderByHash(header.ParentHash)
if err != nil {
testlog.Logger().Crit("could not retrieve rollup by hash.", log.ErrKey, err)
}
}
// iterate the L1 Blocks and get the rollups
for block, _ := l1Node.FetchHeadBlock(); block != nil && !bytes.Equal(block.Hash().Bytes(), (common.L1BlockHash{}).Bytes()); block, _ = l1Node.BlockByHash(block.ParentHash()) {
o.incrementStats(block, l1Node)
}
}
func (o *OutputStats) incrementStats(block *types.Block, _ ethadapter.EthClient) {
for _, tx := range block.Transactions() {
t := o.simulation.Params.MgmtContractLib.DecodeTx(tx)
if t == nil {
t = o.simulation.Params.ERC20ContractLib.DecodeTx(tx)
}
if t == nil {
continue
}
switch l1Tx := t.(type) {
case *ethadapter.L1RollupTx:
_, err := common.DecodeRollup(l1Tx.Rollup)
if err != nil {
testlog.Logger().Crit("could not decode rollup.", log.ErrKey, err)
}
//if l1Node.IsBlockAncestor(block, r.Header.L1Proof) {
// o.l2RollupCountInL1Blocks++
// for _, batch := range r.BatchPayloads {
// o.l2RollupTxCountInL1Blocks += len(batch.TxHashes)
// }
//}
case *ethadapter.L1DepositTx:
o.canonicalERC20DepositCount++
}
}
}
func (o *OutputStats) String() string {
return fmt.Sprintf("\n"+
"nrMiners: %d\n"+
"l1Height: %d\n"+
"l2Height: %d\n"+
"totalL1Blocks: %d\n"+
"totalL2Blocks: %v\n"+
"l2RollupCountInHeaders: %d\n"+
"l2RollupCountInL1Blocks: %d\n"+
"l2RollupTxCountInL1Blocks: %d\n"+
"maxRollupsPerBlock: %d \n"+
"nrEmptyBlocks: %d\n"+
"noL1Reorgs: %+v\n"+
"noL2Recalcs: %+v\n"+
"totalDepositedAmount: %d\n"+
"totalWithdrawnAmount: %d\n"+
"rollupWithMoreRecentProof: %d\n"+
"nrTransferTransactions: %d\n"+
"nrBlockParsedERC20Deposits: %d\n",
o.simulation.Stats.NrMiners,
o.l1Height,
o.l2Height,
o.simulation.Stats.TotalL1Blocks,
o.simulation.Stats.NoL2Blocks,
o.l2RollupCountInHeaders,
o.l2RollupCountInL1Blocks,
o.l2RollupTxCountInL1Blocks,
o.simulation.Stats.MaxRollupsPerBlock,
o.simulation.Stats.NrEmptyBlocks,
o.simulation.Stats.NoL1Reorgs,
o.simulation.Stats.NoL2Recalcs,
o.simulation.Stats.TotalDepositedAmount,
o.simulation.Stats.TotalWithdrawalRequestedAmount,
o.simulation.Stats.RollupWithMoreRecentProofCount,
o.simulation.Stats.NrTransferTransactions,
o.canonicalERC20DepositCount,
)
}