2020package publisher
2121
2222import (
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-
5635func 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