From 74bcf49aa481441e6a62726051d82ec0a12a9022 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Wed, 2 Jan 2019 14:28:59 -0600 Subject: [PATCH] Consolidate acct diff structs for updated & updated/deleted accts --- statediff/builder/builder.go | 12 ++++++------ statediff/builder/builder_test.go | 16 ++++++++-------- statediff/builder/struct.go | 20 ++++++-------------- statediff/publisher/csv.go | 4 ++-- statediff/testhelpers/test_data.go | 6 +++--- 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/statediff/builder/builder.go b/statediff/builder/builder.go index 162cabcd7..131c3626f 100644 --- a/statediff/builder/builder.go +++ b/statediff/builder/builder.go @@ -145,8 +145,8 @@ func (sdb *builder) collectDiffNodes(a, b trie.NodeIterator) (map[common.Address return diffAccounts, nil } -func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account, created bool) (map[common.Address]AccountDiffEventual, error) { - accountDiffs := make(map[common.Address]AccountDiffEventual) +func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account, created bool) (map[common.Address]AccountDiff, error) { + accountDiffs := make(map[common.Address]AccountDiff) for addr, val := range accounts { sr := val.Root storageDiffs, err := sdb.buildStorageDiffsEventual(sr, created) @@ -160,7 +160,7 @@ func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account nonce := DiffUint64{Value: &val.Nonce} balance := DiffBigInt{Value: val.Balance} contractRoot := DiffString{Value: &hexRoot} - accountDiffs[addr] = AccountDiffEventual{ + accountDiffs[addr] = AccountDiff{ Nonce: nonce, Balance: balance, CodeHash: codeHash, @@ -172,8 +172,8 @@ func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account return accountDiffs, nil } -func (sdb *builder) buildDiffIncremental(creations map[common.Address]*state.Account, deletions map[common.Address]*state.Account, updatedKeys []string) (map[common.Address]AccountDiffIncremental, error) { - updatedAccounts := make(map[common.Address]AccountDiffIncremental) +func (sdb *builder) buildDiffIncremental(creations map[common.Address]*state.Account, deletions map[common.Address]*state.Account, updatedKeys []string) (map[common.Address]AccountDiff, error) { + updatedAccounts := make(map[common.Address]AccountDiff) for _, val := range updatedKeys { createdAcc := creations[common.HexToAddress(val)] deletedAcc := deletions[common.HexToAddress(val)] @@ -190,7 +190,7 @@ func (sdb *builder) buildDiffIncremental(creations map[common.Address]*state.Acc nHexRoot := createdAcc.Root.Hex() contractRoot := DiffString{Value: &nHexRoot} - updatedAccounts[common.HexToAddress(val)] = AccountDiffIncremental{ + updatedAccounts[common.HexToAddress(val)] = AccountDiff{ Nonce: nonce, Balance: balance, CodeHash: codeHash, diff --git a/statediff/builder/builder_test.go b/statediff/builder/builder_test.go index e7bffb7c3..925cba512 100644 --- a/statediff/builder/builder_test.go +++ b/statediff/builder/builder_test.go @@ -31,8 +31,8 @@ var ( contractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056") contractAddr common.Address - emptyAccountDiffEventualMap = make(map[common.Address]b.AccountDiffEventual) - emptyAccountDiffIncrementalMap = make(map[common.Address]b.AccountDiffIncremental) + emptyAccountDiffEventualMap = make(map[common.Address]b.AccountDiff) + emptyAccountDiffIncrementalMap = make(map[common.Address]b.AccountDiff) block0Hash, block1Hash, block2Hash, block3Hash common.Hash block0, block1, block2, block3 *types.Block @@ -109,7 +109,7 @@ func TestBuilder(t *testing.T) { &b.StateDiff{ BlockNumber: block1.Number().Int64(), BlockHash: block1.Hash(), - CreatedAccounts: map[common.Address]b.AccountDiffEventual{ + CreatedAccounts: map[common.Address]b.AccountDiff{ account1Addr: { Nonce: b.DiffUint64{Value: &nonce0}, Balance: b.DiffBigInt{Value: big.NewInt(balanceChange10000)}, @@ -126,7 +126,7 @@ func TestBuilder(t *testing.T) { }, }, DeletedAccounts: emptyAccountDiffEventualMap, - UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ + UpdatedAccounts: map[common.Address]b.AccountDiff{ testBankAddress: { Nonce: b.DiffUint64{Value: &nonce1}, Balance: b.DiffBigInt{Value: big.NewInt(testBankFunds.Int64() - balanceChange10000)}, @@ -150,7 +150,7 @@ func TestBuilder(t *testing.T) { &b.StateDiff{ BlockNumber: block2.Number().Int64(), BlockHash: block2.Hash(), - CreatedAccounts: map[common.Address]b.AccountDiffEventual{ + CreatedAccounts: map[common.Address]b.AccountDiff{ account2Addr: { Nonce: b.DiffUint64{Value: &nonce0}, Balance: b.DiffBigInt{Value: big.NewInt(balanceChange1000)}, @@ -167,7 +167,7 @@ func TestBuilder(t *testing.T) { }, }, DeletedAccounts: emptyAccountDiffEventualMap, - UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ + UpdatedAccounts: map[common.Address]b.AccountDiff{ testBankAddress: { Nonce: b.DiffUint64{Value: &nonce2}, Balance: b.DiffBigInt{Value: big.NewInt(block1BankBalance - balanceChange1000)}, @@ -205,9 +205,9 @@ func TestBuilder(t *testing.T) { &b.StateDiff{ BlockNumber: block3.Number().Int64(), BlockHash: block3.Hash(), - CreatedAccounts: map[common.Address]b.AccountDiffEventual{}, + CreatedAccounts: map[common.Address]b.AccountDiff{}, DeletedAccounts: emptyAccountDiffEventualMap, - UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ + UpdatedAccounts: map[common.Address]b.AccountDiff{ account2Addr: { Nonce: b.DiffUint64{Value: &nonce0}, Balance: b.DiffBigInt{Value: big.NewInt(block2Account2Balance + miningReward)}, diff --git a/statediff/builder/struct.go b/statediff/builder/struct.go index 75c20edb4..a7fc79815 100644 --- a/statediff/builder/struct.go +++ b/statediff/builder/struct.go @@ -27,11 +27,11 @@ import ( ) type StateDiff struct { - BlockNumber int64 `json:"blockNumber" gencodec:"required"` - BlockHash common.Hash `json:"blockHash" gencodec:"required"` - CreatedAccounts map[common.Address]AccountDiffEventual `json:"createdAccounts" gencodec:"required"` - DeletedAccounts map[common.Address]AccountDiffEventual `json:"deletedAccounts" gencodec:"required"` - UpdatedAccounts map[common.Address]AccountDiffIncremental `json:"updatedAccounts" gencodec:"required"` + BlockNumber int64 `json:"blockNumber" gencodec:"required"` + BlockHash common.Hash `json:"blockHash" gencodec:"required"` + CreatedAccounts map[common.Address]AccountDiff `json:"createdAccounts" gencodec:"required"` + DeletedAccounts map[common.Address]AccountDiff `json:"deletedAccounts" gencodec:"required"` + UpdatedAccounts map[common.Address]AccountDiff `json:"updatedAccounts" gencodec:"required"` encoded []byte err error @@ -55,15 +55,7 @@ func (sd *StateDiff) Encode() ([]byte, error) { return sd.encoded, sd.err } -type AccountDiffEventual struct { - Nonce DiffUint64 `json:"nonce" gencodec:"required"` - Balance DiffBigInt `json:"balance" gencodec:"required"` - CodeHash string `json:"codeHash" gencodec:"required"` - ContractRoot DiffString `json:"contractRoot" gencodec:"required"` - Storage map[string]DiffString `json:"storage" gencodec:"required"` -} - -type AccountDiffIncremental struct { +type AccountDiff struct { Nonce DiffUint64 `json:"nonce" gencodec:"required"` Balance DiffBigInt `json:"balance" gencodec:"required"` CodeHash string `json:"codeHash" gencodec:"required"` diff --git a/statediff/publisher/csv.go b/statediff/publisher/csv.go index 163252d64..01c7477cf 100644 --- a/statediff/publisher/csv.go +++ b/statediff/publisher/csv.go @@ -97,7 +97,7 @@ func accumulateCreatedAccountRows(sd builder.StateDiff) [][]string { return createdAccountRows } -func formatAccountDiffEventual(accountDiff builder.AccountDiffEventual, sd builder.StateDiff, accountAction string) []string { +func formatAccountDiffEventual(accountDiff builder.AccountDiff, sd builder.StateDiff, accountAction string) []string { newContractRoot := accountDiff.ContractRoot.Value var storageDiffPaths []string for k := range accountDiff.Storage { @@ -116,7 +116,7 @@ func formatAccountDiffEventual(accountDiff builder.AccountDiffEventual, sd build return formattedAccountData } -func formatAccountDiffIncremental(accountDiff builder.AccountDiffIncremental, sd builder.StateDiff, accountAction string) []string { +func formatAccountDiffIncremental(accountDiff builder.AccountDiff, sd builder.StateDiff, accountAction string) []string { newContractRoot := accountDiff.ContractRoot.Value var storageDiffPaths []string for k := range accountDiff.Storage { diff --git a/statediff/testhelpers/test_data.go b/statediff/testhelpers/test_data.go index b9e831860..66376dade 100644 --- a/statediff/testhelpers/test_data.go +++ b/statediff/testhelpers/test_data.go @@ -21,7 +21,7 @@ var ( newStorage = "0x03" storage = map[string]builder.DiffString{StoragePath: {Value: &newStorage}} address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") - CreatedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: { + CreatedAccountDiffs = map[common.Address]builder.AccountDiff{address: { Nonce: builder.DiffUint64{Value: &NewNonceValue}, Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, ContractRoot: builder.DiffString{Value: &ContractRoot}, @@ -29,7 +29,7 @@ var ( Storage: storage, }} - UpdatedAccountDiffs = map[common.Address]builder.AccountDiffIncremental{address: { + UpdatedAccountDiffs = map[common.Address]builder.AccountDiff{address: { Nonce: builder.DiffUint64{Value: &NewNonceValue}, Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, CodeHash: CodeHash, @@ -37,7 +37,7 @@ var ( Storage: storage, }} - DeletedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: { + DeletedAccountDiffs = map[common.Address]builder.AccountDiff{address: { Nonce: builder.DiffUint64{Value: &NewNonceValue}, Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, ContractRoot: builder.DiffString{Value: &ContractRoot},