2019-01-28 21:31:01 +00:00
|
|
|
package publisher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2019-01-31 14:18:11 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff/builder"
|
2019-01-28 21:31:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Headers = []string{
|
|
|
|
"blockNumber", "blockHash", "accountAction", "codeHash",
|
|
|
|
"nonceValue", "balanceValue", "contractRoot", "storageDiffPaths",
|
2019-02-07 21:33:42 +00:00
|
|
|
"accountLeafKey", "storageKey", "storageValue",
|
2019-01-28 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timeStampFormat = "20060102150405.00000"
|
|
|
|
deletedAccountAction = "deleted"
|
|
|
|
createdAccountAction = "created"
|
|
|
|
updatedAccountAction = "updated"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createCSVFilePath(path, blockNumber string) string {
|
|
|
|
now := time.Now()
|
|
|
|
timeStamp := now.Format(timeStampFormat)
|
|
|
|
suffix := timeStamp + "-" + blockNumber
|
|
|
|
filePath := filepath.Join(path, suffix)
|
|
|
|
filePath = filePath + ".csv"
|
|
|
|
return filePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) (string, error) {
|
|
|
|
filePath := createCSVFilePath(p.Config.Path, strconv.FormatInt(sd.BlockNumber, 10))
|
|
|
|
|
|
|
|
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
writer := csv.NewWriter(file)
|
|
|
|
defer writer.Flush()
|
|
|
|
|
|
|
|
var data [][]string
|
|
|
|
data = append(data, Headers)
|
2019-01-31 16:05:33 +00:00
|
|
|
data = append(data, accumulateAccountRows(sd)...)
|
2019-01-28 21:31:01 +00:00
|
|
|
for _, value := range data {
|
|
|
|
err := writer.Write(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func accumulateAccountRows(sd builder.StateDiff) [][]string {
|
|
|
|
var accountRows [][]string
|
|
|
|
for accountAddr, accountDiff := range sd.CreatedAccounts {
|
|
|
|
formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, createdAccountAction)
|
|
|
|
|
2019-01-31 16:05:33 +00:00
|
|
|
accountRows = append(accountRows, formattedAccountData...)
|
2019-01-28 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for accountAddr, accountDiff := range sd.UpdatedAccounts {
|
|
|
|
formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, updatedAccountAction)
|
|
|
|
|
2019-01-31 16:05:33 +00:00
|
|
|
accountRows = append(accountRows, formattedAccountData...)
|
2019-01-28 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for accountAddr, accountDiff := range sd.DeletedAccounts {
|
|
|
|
formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, deletedAccountAction)
|
|
|
|
|
2019-01-31 16:05:33 +00:00
|
|
|
accountRows = append(accountRows, formattedAccountData...)
|
2019-01-28 21:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return accountRows
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:33:42 +00:00
|
|
|
func formatAccountData(accountAddr common.Hash, accountDiff builder.AccountDiff, sd builder.StateDiff, accountAction string) [][]string {
|
2019-01-28 21:31:01 +00:00
|
|
|
blockNumberString := strconv.FormatInt(sd.BlockNumber, 10)
|
|
|
|
blockHash := sd.BlockHash.String()
|
|
|
|
codeHash := accountDiff.CodeHash
|
|
|
|
nonce := strconv.FormatUint(*accountDiff.Nonce.Value, 10)
|
|
|
|
balance := accountDiff.Balance.Value.String()
|
|
|
|
newContractRoot := accountDiff.ContractRoot.Value
|
|
|
|
address := accountAddr.String()
|
|
|
|
var result [][]string
|
|
|
|
|
|
|
|
if len(accountDiff.Storage) > 0 {
|
|
|
|
for storagePath, storage := range accountDiff.Storage {
|
|
|
|
formattedAccountData := []string{
|
|
|
|
blockNumberString,
|
|
|
|
blockHash,
|
|
|
|
accountAction,
|
|
|
|
codeHash,
|
|
|
|
nonce,
|
|
|
|
balance,
|
|
|
|
*newContractRoot,
|
|
|
|
storagePath,
|
|
|
|
address,
|
|
|
|
*storage.Key,
|
|
|
|
*storage.Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, formattedAccountData)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
formattedAccountData := []string{
|
|
|
|
blockNumberString,
|
|
|
|
blockHash,
|
|
|
|
accountAction,
|
|
|
|
codeHash,
|
|
|
|
nonce,
|
|
|
|
balance,
|
|
|
|
*newContractRoot,
|
|
|
|
"",
|
|
|
|
address,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
}
|
|
|
|
result = append(result, formattedAccountData)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|