Skip to content

Commit 8578fc2

Browse files
Pull CSV publishing methods into their own file
1 parent 5b63446 commit 8578fc2

File tree

4 files changed

+389
-327
lines changed

4 files changed

+389
-327
lines changed

statediff/publisher/csv.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package publisher
2+
3+
import (
4+
"github.com/ethereum/go-ethereum/statediff/builder"
5+
"time"
6+
"os"
7+
"encoding/csv"
8+
"strconv"
9+
"strings"
10+
"path/filepath"
11+
)
12+
13+
var (
14+
Headers = []string{
15+
"blockNumber", "blockHash", "accountAction",
16+
"code", "codeHash",
17+
"oldNonceValue", "newNonceValue",
18+
"oldBalanceValue", "newBalanceValue",
19+
"oldContractRoot", "newContractRoot",
20+
"storageDiffPaths",
21+
}
22+
23+
timeStampFormat = "20060102150405.00000"
24+
deletedAccountAction = "deleted"
25+
createdAccountAction = "created"
26+
updatedAccountAction = "updated"
27+
)
28+
29+
func createCSVFilePath(path string) string {
30+
now := time.Now()
31+
timeStamp := now.Format(timeStampFormat)
32+
filePath := filepath.Join(path, timeStamp)
33+
filePath = filePath + ".csv"
34+
return filePath
35+
}
36+
37+
func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) error {
38+
filePath := createCSVFilePath(p.Config.Path)
39+
40+
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
41+
if err != nil {
42+
return err
43+
}
44+
defer file.Close()
45+
46+
writer := csv.NewWriter(file)
47+
defer writer.Flush()
48+
49+
var data [][]string
50+
data = append(data, Headers)
51+
for _, row := range accumulateCreatedAccountRows(sd) {
52+
data = append(data, row)
53+
}
54+
for _, row := range accumulateUpdatedAccountRows(sd) {
55+
data = append(data, row)
56+
}
57+
58+
for _, row := range accumulateDeletedAccountRows(sd) {
59+
data = append(data, row)
60+
}
61+
62+
for _, value := range data{
63+
err := writer.Write(value)
64+
if err != nil {
65+
return err
66+
}
67+
}
68+
69+
return nil
70+
}
71+
72+
func accumulateUpdatedAccountRows(sd builder.StateDiff) [][]string {
73+
var updatedAccountRows [][]string
74+
for _, accountDiff := range sd.UpdatedAccounts {
75+
formattedAccountData := formatAccountDiffIncremental(accountDiff, sd, updatedAccountAction)
76+
77+
updatedAccountRows = append(updatedAccountRows, formattedAccountData)
78+
}
79+
80+
return updatedAccountRows
81+
}
82+
83+
func accumulateDeletedAccountRows(sd builder.StateDiff) [][]string {
84+
var deletedAccountRows [][]string
85+
for _, accountDiff := range sd.DeletedAccounts {
86+
formattedAccountData := formatAccountDiffEventual(accountDiff, sd, deletedAccountAction)
87+
88+
deletedAccountRows = append(deletedAccountRows, formattedAccountData)
89+
}
90+
91+
return deletedAccountRows
92+
}
93+
94+
func accumulateCreatedAccountRows(sd builder.StateDiff) [][]string {
95+
var createdAccountRows [][]string
96+
for _, accountDiff := range sd.CreatedAccounts {
97+
formattedAccountData := formatAccountDiffEventual(accountDiff, sd, createdAccountAction)
98+
99+
createdAccountRows = append(createdAccountRows, formattedAccountData)
100+
}
101+
102+
return createdAccountRows
103+
}
104+
105+
func formatAccountDiffEventual(accountDiff builder.AccountDiffEventual, sd builder.StateDiff, accountAction string) []string {
106+
oldContractRoot := accountDiff.ContractRoot.OldValue
107+
newContractRoot := accountDiff.ContractRoot.NewValue
108+
var storageDiffPaths []string
109+
for k := range accountDiff.Storage {
110+
storageDiffPaths = append(storageDiffPaths, k)
111+
}
112+
formattedAccountData := []string{
113+
strconv.FormatInt(sd.BlockNumber, 10),
114+
sd.BlockHash.String(),
115+
accountAction,
116+
string(accountDiff.Code),
117+
accountDiff.CodeHash,
118+
strconv.FormatUint(*accountDiff.Nonce.OldValue, 10),
119+
strconv.FormatUint(*accountDiff.Nonce.NewValue, 10),
120+
accountDiff.Balance.OldValue.String(),
121+
accountDiff.Balance.NewValue.String(),
122+
*oldContractRoot,
123+
*newContractRoot,
124+
strings.Join(storageDiffPaths, ","),
125+
}
126+
return formattedAccountData
127+
}
128+
129+
func formatAccountDiffIncremental(accountDiff builder.AccountDiffIncremental, sd builder.StateDiff, accountAction string) []string {
130+
oldContractRoot := accountDiff.ContractRoot.OldValue
131+
newContractRoot := accountDiff.ContractRoot.NewValue
132+
var storageDiffPaths []string
133+
for k := range accountDiff.Storage {
134+
storageDiffPaths = append(storageDiffPaths, k)
135+
}
136+
formattedAccountData := []string{
137+
strconv.FormatInt(sd.BlockNumber, 10),
138+
sd.BlockHash.String(),
139+
accountAction,
140+
"",
141+
accountDiff.CodeHash,
142+
strconv.FormatUint(*accountDiff.Nonce.OldValue, 10),
143+
strconv.FormatUint(*accountDiff.Nonce.NewValue, 10),
144+
accountDiff.Balance.OldValue.String(),
145+
accountDiff.Balance.NewValue.String(),
146+
*oldContractRoot,
147+
*newContractRoot,
148+
strings.Join(storageDiffPaths, ","),
149+
}
150+
return formattedAccountData
151+
}
152+

statediff/publisher/publisher.go

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
package publisher
2121

2222
import (
23-
"os"
24-
"encoding/csv"
25-
"time"
26-
"strconv"
27-
"strings"
2823
"github.com/ethereum/go-ethereum/statediff/builder"
2924
"github.com/ethereum/go-ethereum/statediff"
3025
)
@@ -37,22 +32,6 @@ type publisher struct {
3732
Config statediff.Config
3833
}
3934

40-
var (
41-
Headers = []string{
42-
"blockNumber", "blockHash", "accountAction",
43-
"code", "codeHash",
44-
"oldNonceValue", "newNonceValue",
45-
"oldBalanceValue", "newBalanceValue",
46-
"oldContractRoot", "newContractRoot",
47-
"storageDiffPaths",
48-
}
49-
50-
timeStampFormat = "20060102150405.00000"
51-
deletedAccountAction = "deleted"
52-
createdAccountAction = "created"
53-
updatedAccountAction = "updated"
54-
)
55-
5635
func NewPublisher(config statediff.Config) (*publisher, error) {
5736
return &publisher{
5837
Config: config,
@@ -67,121 +46,3 @@ func (p *publisher) PublishStateDiff(sd *builder.StateDiff) (string, error) {
6746
return "", p.publishStateDiffToCSV(*sd)
6847
}
6948
}
70-
71-
func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) error {
72-
now := time.Now()
73-
timeStamp := now.Format(timeStampFormat)
74-
filePath := p.Config.Path + timeStamp + ".csv"
75-
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
76-
if err != nil {
77-
return err
78-
}
79-
defer file.Close()
80-
81-
writer := csv.NewWriter(file)
82-
defer writer.Flush()
83-
84-
var data [][]string
85-
data = append(data, Headers)
86-
for _, row := range accumulateCreatedAccountRows(sd) {
87-
data = append(data, row)
88-
}
89-
for _, row := range accumulateUpdatedAccountRows(sd) {
90-
data = append(data, row)
91-
}
92-
93-
for _, row := range accumulateDeletedAccountRows(sd) {
94-
data = append(data, row)
95-
}
96-
97-
for _, value := range data{
98-
err := writer.Write(value)
99-
if err != nil {
100-
return err
101-
}
102-
}
103-
104-
return nil
105-
}
106-
107-
func accumulateUpdatedAccountRows(sd builder.StateDiff) [][]string {
108-
var updatedAccountRows [][]string
109-
for _, accountDiff := range sd.UpdatedAccounts {
110-
formattedAccountData := formatAccountDiffIncremental(accountDiff, sd, updatedAccountAction)
111-
112-
updatedAccountRows = append(updatedAccountRows, formattedAccountData)
113-
}
114-
115-
return updatedAccountRows
116-
}
117-
118-
func accumulateDeletedAccountRows(sd builder.StateDiff) [][]string {
119-
var deletedAccountRows [][]string
120-
for _, accountDiff := range sd.DeletedAccounts {
121-
formattedAccountData := formatAccountDiffEventual(accountDiff, sd, deletedAccountAction)
122-
123-
deletedAccountRows = append(deletedAccountRows, formattedAccountData)
124-
}
125-
126-
return deletedAccountRows
127-
}
128-
129-
func accumulateCreatedAccountRows(sd builder.StateDiff) [][]string {
130-
var createdAccountRows [][]string
131-
for _, accountDiff := range sd.CreatedAccounts {
132-
formattedAccountData := formatAccountDiffEventual(accountDiff, sd, createdAccountAction)
133-
134-
createdAccountRows = append(createdAccountRows, formattedAccountData)
135-
}
136-
137-
return createdAccountRows
138-
}
139-
140-
func formatAccountDiffEventual(accountDiff builder.AccountDiffEventual, sd builder.StateDiff, accountAction string) []string {
141-
oldContractRoot := accountDiff.ContractRoot.OldValue
142-
newContractRoot := accountDiff.ContractRoot.NewValue
143-
var storageDiffPaths []string
144-
for k := range accountDiff.Storage {
145-
storageDiffPaths = append(storageDiffPaths, k)
146-
}
147-
formattedAccountData := []string{
148-
strconv.FormatInt(sd.BlockNumber, 10),
149-
sd.BlockHash.String(),
150-
accountAction,
151-
string(accountDiff.Code),
152-
accountDiff.CodeHash,
153-
strconv.FormatUint(*accountDiff.Nonce.OldValue, 10),
154-
strconv.FormatUint(*accountDiff.Nonce.NewValue, 10),
155-
accountDiff.Balance.OldValue.String(),
156-
accountDiff.Balance.NewValue.String(),
157-
*oldContractRoot,
158-
*newContractRoot,
159-
strings.Join(storageDiffPaths, ","),
160-
}
161-
return formattedAccountData
162-
}
163-
164-
func formatAccountDiffIncremental(accountDiff builder.AccountDiffIncremental, sd builder.StateDiff, accountAction string) []string {
165-
oldContractRoot := accountDiff.ContractRoot.OldValue
166-
newContractRoot := accountDiff.ContractRoot.NewValue
167-
var storageDiffPaths []string
168-
for k := range accountDiff.Storage {
169-
storageDiffPaths = append(storageDiffPaths, k)
170-
}
171-
formattedAccountData := []string{
172-
strconv.FormatInt(sd.BlockNumber, 10),
173-
sd.BlockHash.String(),
174-
accountAction,
175-
"",
176-
accountDiff.CodeHash,
177-
strconv.FormatUint(*accountDiff.Nonce.OldValue, 10),
178-
strconv.FormatUint(*accountDiff.Nonce.NewValue, 10),
179-
accountDiff.Balance.OldValue.String(),
180-
accountDiff.Balance.NewValue.String(),
181-
*oldContractRoot,
182-
*newContractRoot,
183-
strings.Join(storageDiffPaths, ","),
184-
}
185-
return formattedAccountData
186-
}
187-

0 commit comments

Comments
 (0)