From d0ea3a92eb092edb58632ace4b4de9199528949a Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Wed, 23 Jan 2019 13:59:20 -0600 Subject: [PATCH] Fix publisher to include rows for accounts that don't have store updates --- statediff/publisher/csv.go | 57 +++++++++++++++++++-------- statediff/publisher/publisher_test.go | 25 ++++++++++-- statediff/testhelpers/test_data.go | 50 ++++++++++++++--------- 3 files changed, 93 insertions(+), 39 deletions(-) diff --git a/statediff/publisher/csv.go b/statediff/publisher/csv.go index 4054b7b15..82eb3e466 100644 --- a/statediff/publisher/csv.go +++ b/statediff/publisher/csv.go @@ -46,16 +46,9 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) (string, error) var data [][]string data = append(data, Headers) - for _, row := range accumulateAccountRows(sd, createdAccountAction) { + for _, row := range accumulateAccountRows(sd) { data = append(data, row) } - for _, row := range accumulateAccountRows(sd, updatedAccountAction) { - data = append(data, row) - } - for _, row := range accumulateAccountRows(sd, deletedAccountAction) { - data = append(data, row) - } - for _, value := range data { err := writer.Write(value) if err != nil { @@ -66,10 +59,26 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) (string, error) return filePath, nil } -func accumulateAccountRows(sd builder.StateDiff, accountAction string) [][]string { +func accumulateAccountRows(sd builder.StateDiff) [][]string { var accountRows [][]string + for accountAddr, accountDiff := range sd.CreatedAccounts { + formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, createdAccountAction) + + for _, accountData := range formattedAccountData { + accountRows = append(accountRows, accountData) + } + } + for accountAddr, accountDiff := range sd.UpdatedAccounts { - formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, accountAction) + formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, updatedAccountAction) + + for _, accountData := range formattedAccountData { + accountRows = append(accountRows, accountData) + } + } + + for accountAddr, accountDiff := range sd.DeletedAccounts { + formattedAccountData := formatAccountData(accountAddr, accountDiff, sd, deletedAccountAction) for _, accountData := range formattedAccountData { accountRows = append(accountRows, accountData) @@ -89,7 +98,25 @@ func formatAccountData(accountAddr common.Address, accountDiff builder.AccountDi address := accountAddr.String() var result [][]string - for storagePath, storage := range accountDiff.Storage { + 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, @@ -98,15 +125,13 @@ func formatAccountData(accountAddr common.Address, accountDiff builder.AccountDi nonce, balance, *newContractRoot, - storagePath, + "", address, - *storage.Key, - *storage.Value, + "", + "", } - result = append(result, formattedAccountData) } return result } - diff --git a/statediff/publisher/publisher_test.go b/statediff/publisher/publisher_test.go index ad9e29318..4319b7bd7 100644 --- a/statediff/publisher/publisher_test.go +++ b/statediff/publisher/publisher_test.go @@ -39,6 +39,20 @@ var expectedCreatedAccountRow = []string{ testhelpers.StorageValue, } +var expectedCreatedAccountWithoutStorageUpdateRow = []string{ + strconv.FormatInt(testhelpers.BlockNumber, 10), + testhelpers.BlockHash, + "created", + testhelpers.CodeHash, + strconv.FormatUint(testhelpers.NewNonceValue, 10), + strconv.FormatInt(testhelpers.NewBalanceValue, 10), + testhelpers.ContractRoot, + "", + testhelpers.AnotherContractAddress, + "", + "", +} + var expectedUpdatedAccountRow = []string{ strconv.FormatInt(testhelpers.BlockNumber, 10), testhelpers.BlockHash, @@ -176,10 +190,13 @@ func testAccountDiffs(t *testing.T) { if !equals(lines[1], expectedCreatedAccountRow) { t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) } - if !equals(lines[2], expectedUpdatedAccountRow) { + if !equals(lines[2], expectedCreatedAccountWithoutStorageUpdateRow) { t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) } - if !equals(lines[3], expectedDeletedAccountRow) { + if !equals(lines[3], expectedUpdatedAccountRow) { + t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) + } + if !equals(lines[4], expectedDeletedAccountRow) { t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) } } @@ -229,7 +246,7 @@ func testDefaultPublisher(t *testing.T) { if err != nil { t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) } - if !equals(len(lines), 4) { + if !equals(len(lines), 5) { t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) } if !equals(lines[0], p.Headers) { @@ -264,7 +281,7 @@ func testDefaultDirectory(t *testing.T) { if err != nil { t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) } - if !equals(len(lines), 4) { + if !equals(len(lines), 5) { t.Errorf(testhelpers.ErrorFormatString, t.Name(), err) } if !equals(lines[0], p.Headers) { diff --git a/statediff/testhelpers/test_data.go b/statediff/testhelpers/test_data.go index cd03e1e67..e7d94b373 100644 --- a/statediff/testhelpers/test_data.go +++ b/statediff/testhelpers/test_data.go @@ -8,28 +8,40 @@ import ( ) var ( - BlockNumber = rand.Int63() - BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73" - CodeHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - NewNonceValue = rand.Uint64() - NewBalanceValue = rand.Int63() - ContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" - StoragePath = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - StorageKey = "0000000000000000000000000000000000000000000000000000000000000001" - StorageValue = "0x03" - storage = map[string]builder.DiffStorage{StoragePath: { + BlockNumber = rand.Int63() + BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73" + CodeHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + NewNonceValue = rand.Uint64() + NewBalanceValue = rand.Int63() + ContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + StoragePath = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + StorageKey = "0000000000000000000000000000000000000000000000000000000000000001" + StorageValue = "0x03" + storage = map[string]builder.DiffStorage{StoragePath: { Key: &StorageKey, Value: &StorageValue, }} - address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") - ContractAddress = address.String() - CreatedAccountDiffs = map[common.Address]builder.AccountDiff{address: { - Nonce: builder.DiffUint64{Value: &NewNonceValue}, - Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, - ContractRoot: builder.DiffString{Value: &ContractRoot}, - CodeHash: CodeHash, - Storage: storage, - }} + emptyStorage = map[string]builder.DiffStorage{} + address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") + anotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593") + ContractAddress = address.String() + AnotherContractAddress = anotherAddress.String() + CreatedAccountDiffs = map[common.Address]builder.AccountDiff{ + address: { + Nonce: builder.DiffUint64{Value: &NewNonceValue}, + Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, + ContractRoot: builder.DiffString{Value: &ContractRoot}, + CodeHash: CodeHash, + Storage: storage, + }, + anotherAddress: { + Nonce: builder.DiffUint64{Value: &NewNonceValue}, + Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, + CodeHash: CodeHash, + ContractRoot: builder.DiffString{Value: &ContractRoot}, + Storage: emptyStorage, + }, + } UpdatedAccountDiffs = map[common.Address]builder.AccountDiff{address: { Nonce: builder.DiffUint64{Value: &NewNonceValue},