Refactor accumulating account row in csv publisher

This commit is contained in:
Elizabeth Engelman 2019-01-10 15:46:48 -06:00
parent dceddbfbaa
commit 0843a227eb

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
@ -47,13 +46,13 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) (string, error)
var data [][]string var data [][]string
data = append(data, Headers) data = append(data, Headers)
for _, row := range accumulateCreatedAccountRows(sd) { for _, row := range accumulateAccountRows(sd, createdAccountAction) {
data = append(data, row) data = append(data, row)
} }
for _, row := range accumulateUpdatedAccountRows(sd) { for _, row := range accumulateAccountRows(sd, updatedAccountAction) {
data = append(data, row) data = append(data, row)
} }
for _, row := range accumulateDeletedAccountRows(sd) { for _, row := range accumulateAccountRows(sd, deletedAccountAction) {
data = append(data, row) data = append(data, row)
} }
@ -67,81 +66,45 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) (string, error)
return filePath, nil return filePath, nil
} }
func accumulateUpdatedAccountRows(sd builder.StateDiff) [][]string { func accumulateAccountRows(sd builder.StateDiff, accountAction string) [][]string {
var updatedAccountRows [][]string var accountRows [][]string
for accountAddr, accountDiff := range sd.UpdatedAccounts { for accountAddr, accountDiff := range sd.UpdatedAccounts {
formattedAccountData := formatAccountDiffIncremental(accountAddr, accountDiff, sd, updatedAccountAction) formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, accountAction)
updatedAccountRows = append(updatedAccountRows, formattedAccountData) for _, accountData := range formattedAccountData {
accountRows = append(accountRows, accountData)
}
} }
return updatedAccountRows return accountRows
} }
func accumulateDeletedAccountRows(sd builder.StateDiff) [][]string { func formatAccountData(accountAddr common.Address, accountDiff builder.AccountDiff, sd builder.StateDiff, accountAction string) [][]string {
var deletedAccountRows [][]string blockNumberString := strconv.FormatInt(sd.BlockNumber, 10)
for accountAddr, accountDiff := range sd.DeletedAccounts { blockHash := sd.BlockHash.String()
formattedAccountData := formatAccountDiffEventual(accountAddr, accountDiff, sd, deletedAccountAction) codeHash := accountDiff.CodeHash
nonce := strconv.FormatUint(*accountDiff.Nonce.Value, 10)
deletedAccountRows = append(deletedAccountRows, formattedAccountData) balance := accountDiff.Balance.Value.String()
}
return deletedAccountRows
}
func accumulateCreatedAccountRows(sd builder.StateDiff) [][]string {
var createdAccountRows [][]string
for accountAddr, accountDiff := range sd.CreatedAccounts {
formattedAccountData := formatAccountDiffEventual(accountAddr, accountDiff, sd, createdAccountAction)
createdAccountRows = append(createdAccountRows, formattedAccountData)
}
return createdAccountRows
}
func formatAccountDiffEventual(accountAddr common.Address, accountDiff builder.AccountDiff, sd builder.StateDiff, accountAction string) []string {
newContractRoot := accountDiff.ContractRoot.Value newContractRoot := accountDiff.ContractRoot.Value
var storageDiffPaths []string address := accountAddr.String()
var storageValue builder.DiffString var result [][]string
for k, v := range accountDiff.Storage { for storagePath, storageValue := range accountDiff.Storage {
storageValue = v formattedAccountData := []string{
storageDiffPaths = append(storageDiffPaths, k) blockNumberString,
blockHash,
accountAction,
codeHash,
nonce,
balance,
*newContractRoot,
storagePath,
address,
*storageValue.Value,
}
result = append(result, formattedAccountData)
} }
formattedAccountData := []string{
strconv.FormatInt(sd.BlockNumber, 10), return result
sd.BlockHash.String(),
accountAction,
accountDiff.CodeHash,
strconv.FormatUint(*accountDiff.Nonce.Value, 10),
accountDiff.Balance.Value.String(),
*newContractRoot,
strings.Join(storageDiffPaths, ","),
accountAddr.String(),
*storageValue.Value,
}
return formattedAccountData
} }
func formatAccountDiffIncremental(accountAddr common.Address, accountDiff builder.AccountDiff, sd builder.StateDiff, accountAction string) []string {
newContractRoot := accountDiff.ContractRoot.Value
var storageDiffPaths []string
var storageValue builder.DiffString
for k, v := range accountDiff.Storage {
storageDiffPaths = append(storageDiffPaths, k)
storageValue = v
}
formattedAccountData := []string{
strconv.FormatInt(sd.BlockNumber, 10),
sd.BlockHash.String(),
accountAction,
accountDiff.CodeHash,
strconv.FormatUint(*accountDiff.Nonce.Value, 10),
accountDiff.Balance.Value.String(),
*newContractRoot,
strings.Join(storageDiffPaths, ","),
accountAddr.String(),
*storageValue.Value,
}
return formattedAccountData
}