From 2a8a9147055529135b7fed30eaae23e5c1e2e2bc Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman Date: Fri, 21 Dec 2018 11:51:42 -0600 Subject: [PATCH] Updates per go fmt --- statediff/builder/builder.go | 20 ++-- statediff/builder/builder_test.go | 128 +++++++++++----------- statediff/builder/struct.go | 12 +- statediff/extractor/extractor_test.go | 40 ++++--- statediff/publisher/csv.go | 1 - statediff/publisher/publisher_test.go | 110 +++++++++++++------ statediff/service/service.go | 6 +- statediff/service/service_test.go | 23 ++-- statediff/testhelpers/mocks/blockchain.go | 5 +- statediff/testhelpers/mocks/builder.go | 3 +- statediff/testhelpers/mocks/extractor.go | 5 +- statediff/testhelpers/test_data.go | 58 +++++----- 12 files changed, 236 insertions(+), 175 deletions(-) diff --git a/statediff/builder/builder.go b/statediff/builder/builder.go index 7a4f7bafb..f40eff45c 100644 --- a/statediff/builder/builder.go +++ b/statediff/builder/builder.go @@ -21,12 +21,12 @@ package builder import ( "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/common/hexutil" ) type Builder interface { @@ -160,9 +160,9 @@ func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account hexRoot := val.Root.Hex() if created { - nonce := DiffUint64{ Value: &val.Nonce } - balance := DiffBigInt{ Value: val.Balance } - contractRoot := DiffString{ Value: &hexRoot } + nonce := DiffUint64{Value: &val.Nonce} + balance := DiffBigInt{Value: val.Balance} + contractRoot := DiffString{Value: &hexRoot} accountDiffs[addr] = AccountDiffEventual{ Nonce: nonce, Balance: balance, @@ -172,9 +172,9 @@ func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account Storage: storageDiffs, } } else { - nonce := DiffUint64{ Value: &val.Nonce } - balance := DiffBigInt{ Value: val.Balance } - contractRoot := DiffString{ Value: &hexRoot } + nonce := DiffUint64{Value: &val.Nonce} + balance := DiffBigInt{Value: val.Balance} + contractRoot := DiffString{Value: &hexRoot} accountDiffs[addr] = AccountDiffEventual{ Nonce: nonce, Balance: balance, @@ -199,12 +199,12 @@ func (sdb *builder) buildDiffIncremental(creations map[common.Address]*state.Acc log.Error("Failed building storage diffs", "Address", val, "error", err) return nil, err } else { - nonce := DiffUint64{ Value: &createdAcc.Nonce } - balance := DiffBigInt{ Value: createdAcc.Balance } + nonce := DiffUint64{Value: &createdAcc.Nonce} + balance := DiffBigInt{Value: createdAcc.Balance} codeHash := hexutil.Encode(createdAcc.CodeHash) nHexRoot := createdAcc.Root.Hex() - contractRoot := DiffString{ Value: &nHexRoot } + contractRoot := DiffString{Value: &nHexRoot} updatedAccounts[common.HexToAddress(val)] = AccountDiffIncremental{ Nonce: nonce, diff --git a/statediff/builder/builder_test.go b/statediff/builder/builder_test.go index 2ba6ca9b8..671eaf245 100644 --- a/statediff/builder/builder_test.go +++ b/statediff/builder/builder_test.go @@ -1,18 +1,18 @@ package builder_test import ( - "testing" - "math/big" - "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/common" - b "github.com/ethereum/go-ethereum/statediff/builder" - "github.com/ethereum/go-ethereum/core/types" "bytes" - "reflect" - "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/ethash" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/params" + b "github.com/ethereum/go-ethereum/statediff/builder" + "math/big" + "reflect" + "testing" ) var ( @@ -57,30 +57,30 @@ func TestBuilder(t *testing.T) { type arguments struct { oldStateRoot common.Hash newStateRoot common.Hash - blockNumber int64 - blockHash common.Hash + blockNumber int64 + blockHash common.Hash } var ( - balanceChange10000 = int64(10000) - balanceChange1000 = int64(1000) + balanceChange10000 = int64(10000) + balanceChange1000 = int64(1000) block1BankBalance = int64(99990000) block1Account1Balance = int64(10000) block2Account2Balance = int64(1000) - nonce0 = uint64(0) - nonce1 = uint64(1) - nonce2 = uint64(2) - nonce3 = uint64(3) - originalContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" - newContractRoot = "0x9e676b23802aff85d29b4f0243939bc6ecfdca2a41532310091781854d6ffeb2" - newStorageValue = "0x03" + nonce0 = uint64(0) + nonce1 = uint64(1) + nonce2 = uint64(2) + nonce3 = uint64(3) + originalContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + newContractRoot = "0x9e676b23802aff85d29b4f0243939bc6ecfdca2a41532310091781854d6ffeb2" + newStorageValue = "0x03" ) - var tests = []struct{ - name string + var tests = []struct { + name string startingArguments arguments - expected *b.StateDiff - } { + expected *b.StateDiff + }{ { "testEmptyDiff", arguments{ @@ -107,33 +107,33 @@ func TestBuilder(t *testing.T) { blockHash: block1Hash, }, &b.StateDiff{ - BlockNumber: block1.Number().Int64(), - BlockHash: block1.Hash(), + BlockNumber: block1.Number().Int64(), + BlockHash: block1.Hash(), CreatedAccounts: map[common.Address]b.AccountDiffEventual{ account1Addr: { - Nonce: b.DiffUint64{ Value: &nonce0 }, - Balance: b.DiffBigInt{ Value: big.NewInt(balanceChange10000) }, + Nonce: b.DiffUint64{Value: &nonce0}, + Balance: b.DiffBigInt{Value: big.NewInt(balanceChange10000)}, Code: nil, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, burnAddress: { - Nonce: b.DiffUint64{ Value: &nonce0 }, - Balance: b.DiffBigInt{ Value: big.NewInt(miningReward)}, + Nonce: b.DiffUint64{Value: &nonce0}, + Balance: b.DiffBigInt{Value: big.NewInt(miningReward)}, Code: nil, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, }, DeletedAccounts: emptyAccountDiffEventualMap, UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ testBankAddress: { - Nonce: b.DiffUint64{ Value: &nonce1 }, - Balance: b.DiffBigInt{ Value: big.NewInt(testBankFunds.Int64() - balanceChange10000) }, + Nonce: b.DiffUint64{Value: &nonce1}, + Balance: b.DiffBigInt{Value: big.NewInt(testBankFunds.Int64() - balanceChange10000)}, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, }, @@ -150,47 +150,47 @@ func TestBuilder(t *testing.T) { blockHash: block2Hash, }, &b.StateDiff{ - BlockNumber: block2.Number().Int64(), - BlockHash: block2.Hash(), + BlockNumber: block2.Number().Int64(), + BlockHash: block2.Hash(), CreatedAccounts: map[common.Address]b.AccountDiffEventual{ account2Addr: { - Nonce: b.DiffUint64{ Value: &nonce0 }, - Balance: b.DiffBigInt{ Value: big.NewInt(balanceChange1000) }, + Nonce: b.DiffUint64{Value: &nonce0}, + Balance: b.DiffBigInt{Value: big.NewInt(balanceChange1000)}, Code: nil, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot}, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, contractAddr: { - Nonce: b.DiffUint64{ Value: &nonce1 }, - Balance: b.DiffBigInt{ Value: big.NewInt(0) }, + Nonce: b.DiffUint64{Value: &nonce1}, + Balance: b.DiffBigInt{Value: big.NewInt(0)}, Code: []byte{96, 96, 96, 64, 82, 96, 0, 53, 124, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 4, 128, 99, 96, 205, 38, 133, 20, 96, 65, 87, 128, 99, 193, 100, 49, 185, 20, 96, 107, 87, 96, 63, 86, 91, 0, 91, 96, 85, 96, 4, 128, 128, 53, 144, 96, 32, 1, 144, 145, 144, 80, 80, 96, 169, 86, 91, 96, 64, 81, 128, 130, 129, 82, 96, 32, 1, 145, 80, 80, 96, 64, 81, 128, 145, 3, 144, 243, 91, 96, 136, 96, 4, 128, 128, 53, 144, 96, 32, 1, 144, 145, 144, 128, 53, 144, 96, 32, 1, 144, 145, 144, 80, 80, 96, 138, 86, 91, 0, 91, 128, 96, 0, 96, 0, 80, 131, 96, 100, 129, 16, 21, 96, 2, 87, 144, 144, 1, 96, 0, 91, 80, 129, 144, 85, 80, 91, 80, 80, 86, 91, 96, 0, 96, 0, 96, 0, 80, 130, 96, 100, 129, 16, 21, 96, 2, 87, 144, 144, 1, 96, 0, 91, 80, 84, 144, 80, 96, 199, 86, 91, 145, 144, 80, 86}, CodeHash: "0x1c671ee4ae8abbacab7da59d6f8785cce8295eb086551ce7ac266a2e93666c0f", - ContractRoot: b.DiffString{ Value: &originalContractRoot}, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, }, DeletedAccounts: emptyAccountDiffEventualMap, UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ testBankAddress: { - Nonce: b.DiffUint64{ Value: &nonce2 }, - Balance: b.DiffBigInt{ Value: big.NewInt(block1BankBalance - balanceChange1000) }, + Nonce: b.DiffUint64{Value: &nonce2}, + Balance: b.DiffBigInt{Value: big.NewInt(block1BankBalance - balanceChange1000)}, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, account1Addr: { - Nonce: b.DiffUint64{ Value: &nonce2 }, - Balance: b.DiffBigInt{ Value: big.NewInt(block1Account1Balance - balanceChange1000 + balanceChange1000) }, + Nonce: b.DiffUint64{Value: &nonce2}, + Balance: b.DiffBigInt{Value: big.NewInt(block1Account1Balance - balanceChange1000 + balanceChange1000)}, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, burnAddress: { - Nonce: b.DiffUint64{ Value: &nonce0 }, - Balance: b.DiffBigInt{ Value: big.NewInt(miningReward + miningReward) }, + Nonce: b.DiffUint64{Value: &nonce0}, + Balance: b.DiffBigInt{Value: big.NewInt(miningReward + miningReward)}, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, }, @@ -213,27 +213,27 @@ func TestBuilder(t *testing.T) { DeletedAccounts: emptyAccountDiffEventualMap, UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ account2Addr: { - Nonce: b.DiffUint64{ Value: &nonce0 }, - Balance: b.DiffBigInt{ Value: big.NewInt(block2Account2Balance + miningReward) }, + Nonce: b.DiffUint64{Value: &nonce0}, + Balance: b.DiffBigInt{Value: big.NewInt(block2Account2Balance + miningReward)}, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, contractAddr: { - Nonce: b.DiffUint64{ Value: &nonce1 }, - Balance: b.DiffBigInt{ Value: big.NewInt(0) }, + Nonce: b.DiffUint64{Value: &nonce1}, + Balance: b.DiffBigInt{Value: big.NewInt(0)}, CodeHash: "0x1c671ee4ae8abbacab7da59d6f8785cce8295eb086551ce7ac266a2e93666c0f", - ContractRoot: b.DiffString{ Value: &newContractRoot }, - Storage: map[string]b.DiffString{ + ContractRoot: b.DiffString{Value: &newContractRoot}, + Storage: map[string]b.DiffString{ "0x405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace": { - Value: &newStorageValue }, + Value: &newStorageValue}, }, }, testBankAddress: { - Nonce: b.DiffUint64{ Value: &nonce3 }, - Balance: b.DiffBigInt{ Value: big.NewInt(99989000) }, + Nonce: b.DiffUint64{Value: &nonce3}, + Balance: b.DiffBigInt{Value: big.NewInt(99989000)}, CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", - ContractRoot: b.DiffString{ Value: &originalContractRoot }, + ContractRoot: b.DiffString{Value: &originalContractRoot}, Storage: map[string]b.DiffString{}, }, }, diff --git a/statediff/builder/struct.go b/statediff/builder/struct.go index a867465b5..9a4ce5786 100644 --- a/statediff/builder/struct.go +++ b/statediff/builder/struct.go @@ -72,6 +72,12 @@ type AccountDiffIncremental struct { Storage map[string]DiffString `json:"storage" gencodec:"required"` } -type DiffString struct{ Value *string `json:"value" gencodec:"optional"` } -type DiffUint64 struct{ Value *uint64 `json:"value" gencodec:"optional"` } -type DiffBigInt struct{ Value *big.Int `json:"value" gencodec:"optional"` } +type DiffString struct { + Value *string `json:"value" gencodec:"optional"` +} +type DiffUint64 struct { + Value *uint64 `json:"value" gencodec:"optional"` +} +type DiffBigInt struct { + Value *big.Int `json:"value" gencodec:"optional"` +} diff --git a/statediff/extractor/extractor_test.go b/statediff/extractor/extractor_test.go index 57429fefc..2270dcd45 100644 --- a/statediff/extractor/extractor_test.go +++ b/statediff/extractor/extractor_test.go @@ -1,15 +1,15 @@ package extractor_test import ( - "testing" - "math/big" - "math/rand" + "bytes" + "github.com/ethereum/go-ethereum/core/types" b "github.com/ethereum/go-ethereum/statediff/builder" e "github.com/ethereum/go-ethereum/statediff/extractor" - "github.com/ethereum/go-ethereum/core/types" - "bytes" - "reflect" "github.com/ethereum/go-ethereum/statediff/testhelpers/mocks" + "math/big" + "math/rand" + "reflect" + "testing" ) var publisher mocks.Publisher @@ -56,10 +56,18 @@ func testBuildStateDiffStruct(t *testing.T) { t.Error(err) } - if !equals(builder.OldStateRoot, parentBlock.Root()) { t.Error()} - if !equals(builder.NewStateRoot, currentBlock.Root()) { t.Error()} - if !equals(builder.BlockNumber,currentBlockNumber.Int64()) { t.Error()} - if !equals(builder.BlockHash, currentBlock.Hash()) { t.Error()} + if !equals(builder.OldStateRoot, parentBlock.Root()) { + t.Error() + } + if !equals(builder.NewStateRoot, currentBlock.Root()) { + t.Error() + } + if !equals(builder.BlockNumber, currentBlockNumber.Int64()) { + t.Error() + } + if !equals(builder.BlockHash, currentBlock.Hash()) { + t.Error() + } } func testBuildStateDiffErrorHandling(t *testing.T) { @@ -70,7 +78,9 @@ func testBuildStateDiffErrorHandling(t *testing.T) { t.Error(err) } - if !equals(err, mocks.Error) { t.Error() } + if !equals(err, mocks.Error) { + t.Error() + } builder.SetBuilderError(nil) } @@ -82,7 +92,9 @@ func testPublishingStateDiff(t *testing.T) { t.Error(err) } - if !equals(publisher.StateDiff, &expectedStateDiff) { t.Error() } + if !equals(publisher.StateDiff, &expectedStateDiff) { + t.Error() + } } func testPublisherErrorHandling(t *testing.T) { @@ -92,7 +104,9 @@ func testPublisherErrorHandling(t *testing.T) { if err == nil { t.Error("Expected an error, but it didn't occur.") } - if !equals(err, mocks.Error) { t.Error() } + if !equals(err, mocks.Error) { + t.Error() + } publisher.SetPublisherError(nil) } diff --git a/statediff/publisher/csv.go b/statediff/publisher/csv.go index d4bcfe5d6..0125608f1 100644 --- a/statediff/publisher/csv.go +++ b/statediff/publisher/csv.go @@ -51,7 +51,6 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) error { for _, row := range accumulateUpdatedAccountRows(sd) { data = append(data, row) } - for _, row := range accumulateDeletedAccountRows(sd) { data = append(data, row) } diff --git a/statediff/publisher/publisher_test.go b/statediff/publisher/publisher_test.go index 9b8484131..5a3b8c857 100644 --- a/statediff/publisher/publisher_test.go +++ b/statediff/publisher/publisher_test.go @@ -1,19 +1,19 @@ package publisher_test import ( - "testing" - "os" - "strconv" - "github.com/ethereum/go-ethereum/statediff/testhelpers" - p "github.com/ethereum/go-ethereum/statediff/publisher" - "io/ioutil" - "github.com/ethereum/go-ethereum/statediff" - "encoding/csv" - "path/filepath" "bytes" - "reflect" + "encoding/csv" + "github.com/ethereum/go-ethereum/statediff" "github.com/ethereum/go-ethereum/statediff/builder" + p "github.com/ethereum/go-ethereum/statediff/publisher" + "github.com/ethereum/go-ethereum/statediff/testhelpers" "github.com/pkg/errors" + "io/ioutil" + "os" + "path/filepath" + "reflect" + "strconv" + "testing" ) var ( @@ -98,7 +98,7 @@ func removeFilesFromDir(dir string, t *testing.T) { for _, file := range files { err = os.RemoveAll(file) - if err !=nil { + if err != nil { t.Error() } } @@ -119,9 +119,13 @@ func testColumnHeaders(t *testing.T) { if err != nil { t.Error(err) } - if len(lines) <= 1 { t.Error() } + if len(lines) <= 1 { + t.Error() + } - if !equals(lines[0], p.Headers) { t.Error() } + if !equals(lines[0], p.Headers) { + t.Error() + } } func testAccountDiffs(t *testing.T) { @@ -140,10 +144,18 @@ func testAccountDiffs(t *testing.T) { if err != nil { t.Error(err) } - if len(lines) <= 3 { t.Error() } - if !equals(lines[1], expectedCreatedAccountRow) { t.Error() } - if !equals(lines[2], expectedUpdatedAccountRow) { t.Error()} - if !equals(lines[3], expectedDeletedAccountRow) { t.Error()} + if len(lines) <= 3 { + t.Error() + } + if !equals(lines[1], expectedCreatedAccountRow) { + t.Error() + } + if !equals(lines[2], expectedUpdatedAccountRow) { + t.Error() + } + if !equals(lines[3], expectedDeletedAccountRow) { + t.Error() + } } func testWhenNoDiff(t *testing.T) { @@ -163,52 +175,84 @@ func testWhenNoDiff(t *testing.T) { if err != nil { t.Error(err) } - if !equals(len(lines), 1) { t.Error() } + if !equals(len(lines), 1) { + t.Error() + } } func testDefaultPublisher(t *testing.T) { //it defaults to publishing state diffs to a CSV file when no mode is configured config := statediff.Config{Path: dir} publisher, err = p.NewPublisher(config) - if err != nil { t.Error(err) } + if err != nil { + t.Error(err) + } _, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff) - if err != nil { t.Error(err) } + if err != nil { + t.Error(err) + } file, err := getTestDiffFile(dir) - if err != nil { t.Error(err) } + if err != nil { + t.Error(err) + } lines, err := csv.NewReader(file).ReadAll() - if err != nil { t.Error(err) } - if !equals(len(lines), 4) { t.Error()} - if !equals(lines[0],p.Headers) { t.Error()} + if err != nil { + t.Error(err) + } + if !equals(len(lines), 4) { + t.Error() + } + if !equals(lines[0], p.Headers) { + t.Error() + } } func testDefaultDirectory(t *testing.T) { //it defaults to publishing CSV files in the current directory when no path is configured config := statediff.Config{} publisher, err = p.NewPublisher(config) - if err != nil { t.Error(err) } + if err != nil { + t.Error(err) + } err := os.Chdir(dir) - if err != nil { t.Error(err) } + if err != nil { + t.Error(err) + } _, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff) - if err != nil { t.Error(err) } + if err != nil { + t.Error(err) + } file, err := getTestDiffFile(dir) - if err != nil { t.Error(err) } + if err != nil { + t.Error(err) + } lines, err := csv.NewReader(file).ReadAll() - if err != nil { t.Error(err) } - if !equals(len(lines), 4) { t.Error() } - if !equals(lines[0], p.Headers) { t.Error() } + if err != nil { + t.Error(err) + } + if !equals(len(lines), 4) { + t.Error() + } + if !equals(lines[0], p.Headers) { + t.Error() + } } func getTestDiffFile(dir string) (*os.File, error) { files, err := ioutil.ReadDir(dir) - if err != nil { return nil, err } - if len(files) == 0 { return nil, errors.New("There are 0 files.") } + if err != nil { + return nil, err + } + if len(files) == 0 { + return nil, errors.New("There are 0 files.") + } fileName := files[0].Name() filePath := filepath.Join(dir, fileName) diff --git a/statediff/service/service.go b/statediff/service/service.go index 3ceebc15b..581e79f39 100644 --- a/statediff/service/service.go +++ b/statediff/service/service.go @@ -10,14 +10,14 @@ import ( e "github.com/ethereum/go-ethereum/statediff/extractor" p "github.com/ethereum/go-ethereum/statediff/publisher" - "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" ) type BlockChain interface { - SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription - GetBlockByHash(hash common.Hash) *types.Block + SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription + GetBlockByHash(hash common.Hash) *types.Block } type StateDiffService struct { diff --git a/statediff/service/service_test.go b/statediff/service/service_test.go index 039f4bbec..97a4a0c97 100644 --- a/statediff/service/service_test.go +++ b/statediff/service/service_test.go @@ -1,16 +1,17 @@ package service_test import ( - "testing" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/core" - service2 "github.com/ethereum/go-ethereum/statediff/service" - "reflect" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + service2 "github.com/ethereum/go-ethereum/statediff/service" + "github.com/ethereum/go-ethereum/statediff/testhelpers/mocks" "math/big" "math/rand" - "github.com/ethereum/go-ethereum/statediff/testhelpers/mocks" + "reflect" + "testing" ) + func TestServiceLoop(t *testing.T) { testServiceLoop(t) } @@ -27,14 +28,14 @@ var ( parentHash1 = parentBlock1.Hash() parentHash2 = parentBlock2.Hash() - header1 = types.Header{ ParentHash: parentHash1 } - header2 = types.Header{ ParentHash: parentHash2 } + header1 = types.Header{ParentHash: parentHash1} + header2 = types.Header{ParentHash: parentHash2} block1 = types.NewBlock(&header1, nil, nil, nil) block2 = types.NewBlock(&header2, nil, nil, nil) - event1 = core.ChainEvent{ Block: block1 } - event2 = core.ChainEvent{ Block: block2 } + event1 = core.ChainEvent{Block: block1} + event2 = core.ChainEvent{Block: block2} ) func testServiceLoop(t *testing.T) { @@ -45,7 +46,7 @@ func testServiceLoop(t *testing.T) { close(eventsChannel) blockChain := mocks.BlockChain{} - service := service2.StateDiffService{ + service := service2.StateDiffService{ Builder: nil, Extractor: &extractor, BlockChain: &blockChain, diff --git a/statediff/testhelpers/mocks/blockchain.go b/statediff/testhelpers/mocks/blockchain.go index 2b98ae97e..baa7b3cec 100644 --- a/statediff/testhelpers/mocks/blockchain.go +++ b/statediff/testhelpers/mocks/blockchain.go @@ -2,15 +2,15 @@ package mocks import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/event" ) type BlockChain struct { ParentHashesLookedUp []common.Hash parentBlocksToReturn []*types.Block - callCount int + callCount int } func (mc *BlockChain) SetParentBlockToReturn(blocks []*types.Block) { @@ -32,4 +32,3 @@ func (mc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { func (BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { panic("implement me") } - diff --git a/statediff/testhelpers/mocks/builder.go b/statediff/testhelpers/mocks/builder.go index 13f6450ee..ae9ff5ced 100644 --- a/statediff/testhelpers/mocks/builder.go +++ b/statediff/testhelpers/mocks/builder.go @@ -1,8 +1,8 @@ package mocks import ( - "github.com/ethereum/go-ethereum/statediff/builder" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/statediff/builder" ) type Builder struct { @@ -30,4 +30,3 @@ func (builder *Builder) SetStateDiffToBuild(stateDiff *builder.StateDiff) { func (builder *Builder) SetBuilderError(err error) { builder.builderError = err } - diff --git a/statediff/testhelpers/mocks/extractor.go b/statediff/testhelpers/mocks/extractor.go index ac0f82975..067497646 100644 --- a/statediff/testhelpers/mocks/extractor.go +++ b/statediff/testhelpers/mocks/extractor.go @@ -3,8 +3,8 @@ package mocks import "github.com/ethereum/go-ethereum/core/types" type Extractor struct { - ParentBlocks []types.Block - CurrentBlocks []types.Block + ParentBlocks []types.Block + CurrentBlocks []types.Block extractError error } @@ -18,4 +18,3 @@ func (me *Extractor) ExtractStateDiff(parent, current types.Block) (string, erro func (me *Extractor) SetExtractError(err error) { me.extractError = err } - diff --git a/statediff/testhelpers/test_data.go b/statediff/testhelpers/test_data.go index f13f21533..4439df599 100644 --- a/statediff/testhelpers/test_data.go +++ b/statediff/testhelpers/test_data.go @@ -8,43 +8,43 @@ import ( ) var ( - BlockNumber = rand.Int63() - BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73" - CodeHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - OldNonceValue = rand.Uint64() - NewNonceValue = OldNonceValue + 1 - OldBalanceValue = rand.Int63() - NewBalanceValue = OldBalanceValue - 1 - ContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" - StoragePath = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - oldStorage = "0x0" - newStorage = "0x03" - storage = map[string]builder.DiffString{StoragePath: { Value: &newStorage }} + BlockNumber = rand.Int63() + BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73" + CodeHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + OldNonceValue = rand.Uint64() + NewNonceValue = OldNonceValue + 1 + OldBalanceValue = rand.Int63() + NewBalanceValue = OldBalanceValue - 1 + ContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + StoragePath = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + oldStorage = "0x0" + newStorage = "0x03" + storage = map[string]builder.DiffString{StoragePath: {Value: &newStorage}} address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") CreatedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: { - Nonce: builder.DiffUint64{ Value: &NewNonceValue }, - Balance: builder.DiffBigInt{ Value: big.NewInt(NewBalanceValue) }, - ContractRoot: builder.DiffString{ Value: &ContractRoot }, - Code: []byte("created account code"), - CodeHash: CodeHash, - Storage: storage, + Nonce: builder.DiffUint64{Value: &NewNonceValue}, + Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, + ContractRoot: builder.DiffString{Value: &ContractRoot}, + Code: []byte("created account code"), + CodeHash: CodeHash, + Storage: storage, }} UpdatedAccountDiffs = map[common.Address]builder.AccountDiffIncremental{address: { - Nonce: builder.DiffUint64{ Value: &NewNonceValue }, - Balance: builder.DiffBigInt{ Value: big.NewInt(NewBalanceValue) }, - CodeHash: CodeHash, - ContractRoot: builder.DiffString{ Value: &ContractRoot }, - Storage: storage, + Nonce: builder.DiffUint64{Value: &NewNonceValue}, + Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, + CodeHash: CodeHash, + ContractRoot: builder.DiffString{Value: &ContractRoot}, + Storage: storage, }} DeletedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: { - Nonce: builder.DiffUint64{ Value: &NewNonceValue }, - Balance: builder.DiffBigInt{ Value: big.NewInt(NewBalanceValue) }, - ContractRoot: builder.DiffString{ Value: &ContractRoot }, - Code: []byte("deleted account code"), - CodeHash: CodeHash, - Storage: storage, + Nonce: builder.DiffUint64{Value: &NewNonceValue}, + Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)}, + ContractRoot: builder.DiffString{Value: &ContractRoot}, + Code: []byte("deleted account code"), + CodeHash: CodeHash, + Storage: storage, }} TestStateDiff = builder.StateDiff{