Updates per go fmt

This commit is contained in:
Elizabeth Engelman 2018-12-21 11:51:42 -06:00
parent 0a6dbb8a27
commit 2a8a914705
12 changed files with 236 additions and 175 deletions

View File

@ -21,12 +21,12 @@ package builder
import ( import (
"github.com/ethereum/go-ethereum/common" "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/core/state"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/common/hexutil"
) )
type Builder interface { type Builder interface {
@ -160,9 +160,9 @@ func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account
hexRoot := val.Root.Hex() hexRoot := val.Root.Hex()
if created { if created {
nonce := DiffUint64{ Value: &val.Nonce } nonce := DiffUint64{Value: &val.Nonce}
balance := DiffBigInt{ Value: val.Balance } balance := DiffBigInt{Value: val.Balance}
contractRoot := DiffString{ Value: &hexRoot } contractRoot := DiffString{Value: &hexRoot}
accountDiffs[addr] = AccountDiffEventual{ accountDiffs[addr] = AccountDiffEventual{
Nonce: nonce, Nonce: nonce,
Balance: balance, Balance: balance,
@ -172,9 +172,9 @@ func (sdb *builder) buildDiffEventual(accounts map[common.Address]*state.Account
Storage: storageDiffs, Storage: storageDiffs,
} }
} else { } else {
nonce := DiffUint64{ Value: &val.Nonce } nonce := DiffUint64{Value: &val.Nonce}
balance := DiffBigInt{ Value: val.Balance } balance := DiffBigInt{Value: val.Balance}
contractRoot := DiffString{ Value: &hexRoot } contractRoot := DiffString{Value: &hexRoot}
accountDiffs[addr] = AccountDiffEventual{ accountDiffs[addr] = AccountDiffEventual{
Nonce: nonce, Nonce: nonce,
Balance: balance, 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) log.Error("Failed building storage diffs", "Address", val, "error", err)
return nil, err return nil, err
} else { } else {
nonce := DiffUint64{ Value: &createdAcc.Nonce } nonce := DiffUint64{Value: &createdAcc.Nonce}
balance := DiffBigInt{ Value: createdAcc.Balance } balance := DiffBigInt{Value: createdAcc.Balance}
codeHash := hexutil.Encode(createdAcc.CodeHash) codeHash := hexutil.Encode(createdAcc.CodeHash)
nHexRoot := createdAcc.Root.Hex() nHexRoot := createdAcc.Root.Hex()
contractRoot := DiffString{ Value: &nHexRoot } contractRoot := DiffString{Value: &nHexRoot}
updatedAccounts[common.HexToAddress(val)] = AccountDiffIncremental{ updatedAccounts[common.HexToAddress(val)] = AccountDiffIncremental{
Nonce: nonce, Nonce: nonce,

View File

@ -1,18 +1,18 @@
package builder_test package builder_test
import ( 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" "bytes"
"reflect" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/consensus/ethash" "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 ( var (
@ -57,30 +57,30 @@ func TestBuilder(t *testing.T) {
type arguments struct { type arguments struct {
oldStateRoot common.Hash oldStateRoot common.Hash
newStateRoot common.Hash newStateRoot common.Hash
blockNumber int64 blockNumber int64
blockHash common.Hash blockHash common.Hash
} }
var ( var (
balanceChange10000 = int64(10000) balanceChange10000 = int64(10000)
balanceChange1000 = int64(1000) balanceChange1000 = int64(1000)
block1BankBalance = int64(99990000) block1BankBalance = int64(99990000)
block1Account1Balance = int64(10000) block1Account1Balance = int64(10000)
block2Account2Balance = int64(1000) block2Account2Balance = int64(1000)
nonce0 = uint64(0) nonce0 = uint64(0)
nonce1 = uint64(1) nonce1 = uint64(1)
nonce2 = uint64(2) nonce2 = uint64(2)
nonce3 = uint64(3) nonce3 = uint64(3)
originalContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" originalContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
newContractRoot = "0x9e676b23802aff85d29b4f0243939bc6ecfdca2a41532310091781854d6ffeb2" newContractRoot = "0x9e676b23802aff85d29b4f0243939bc6ecfdca2a41532310091781854d6ffeb2"
newStorageValue = "0x03" newStorageValue = "0x03"
) )
var tests = []struct{ var tests = []struct {
name string name string
startingArguments arguments startingArguments arguments
expected *b.StateDiff expected *b.StateDiff
} { }{
{ {
"testEmptyDiff", "testEmptyDiff",
arguments{ arguments{
@ -107,33 +107,33 @@ func TestBuilder(t *testing.T) {
blockHash: block1Hash, blockHash: block1Hash,
}, },
&b.StateDiff{ &b.StateDiff{
BlockNumber: block1.Number().Int64(), BlockNumber: block1.Number().Int64(),
BlockHash: block1.Hash(), BlockHash: block1.Hash(),
CreatedAccounts: map[common.Address]b.AccountDiffEventual{ CreatedAccounts: map[common.Address]b.AccountDiffEventual{
account1Addr: { account1Addr: {
Nonce: b.DiffUint64{ Value: &nonce0 }, Nonce: b.DiffUint64{Value: &nonce0},
Balance: b.DiffBigInt{ Value: big.NewInt(balanceChange10000) }, Balance: b.DiffBigInt{Value: big.NewInt(balanceChange10000)},
Code: nil, Code: nil,
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
burnAddress: { burnAddress: {
Nonce: b.DiffUint64{ Value: &nonce0 }, Nonce: b.DiffUint64{Value: &nonce0},
Balance: b.DiffBigInt{ Value: big.NewInt(miningReward)}, Balance: b.DiffBigInt{Value: big.NewInt(miningReward)},
Code: nil, Code: nil,
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
}, },
DeletedAccounts: emptyAccountDiffEventualMap, DeletedAccounts: emptyAccountDiffEventualMap,
UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{
testBankAddress: { testBankAddress: {
Nonce: b.DiffUint64{ Value: &nonce1 }, Nonce: b.DiffUint64{Value: &nonce1},
Balance: b.DiffBigInt{ Value: big.NewInt(testBankFunds.Int64() - balanceChange10000) }, Balance: b.DiffBigInt{Value: big.NewInt(testBankFunds.Int64() - balanceChange10000)},
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
}, },
@ -150,47 +150,47 @@ func TestBuilder(t *testing.T) {
blockHash: block2Hash, blockHash: block2Hash,
}, },
&b.StateDiff{ &b.StateDiff{
BlockNumber: block2.Number().Int64(), BlockNumber: block2.Number().Int64(),
BlockHash: block2.Hash(), BlockHash: block2.Hash(),
CreatedAccounts: map[common.Address]b.AccountDiffEventual{ CreatedAccounts: map[common.Address]b.AccountDiffEventual{
account2Addr: { account2Addr: {
Nonce: b.DiffUint64{ Value: &nonce0 }, Nonce: b.DiffUint64{Value: &nonce0},
Balance: b.DiffBigInt{ Value: big.NewInt(balanceChange1000) }, Balance: b.DiffBigInt{Value: big.NewInt(balanceChange1000)},
Code: nil, Code: nil,
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot}, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
contractAddr: { contractAddr: {
Nonce: b.DiffUint64{ Value: &nonce1 }, Nonce: b.DiffUint64{Value: &nonce1},
Balance: b.DiffBigInt{ Value: big.NewInt(0) }, 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}, 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", CodeHash: "0x1c671ee4ae8abbacab7da59d6f8785cce8295eb086551ce7ac266a2e93666c0f",
ContractRoot: b.DiffString{ Value: &originalContractRoot}, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
}, },
DeletedAccounts: emptyAccountDiffEventualMap, DeletedAccounts: emptyAccountDiffEventualMap,
UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{
testBankAddress: { testBankAddress: {
Nonce: b.DiffUint64{ Value: &nonce2 }, Nonce: b.DiffUint64{Value: &nonce2},
Balance: b.DiffBigInt{ Value: big.NewInt(block1BankBalance - balanceChange1000) }, Balance: b.DiffBigInt{Value: big.NewInt(block1BankBalance - balanceChange1000)},
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
account1Addr: { account1Addr: {
Nonce: b.DiffUint64{ Value: &nonce2 }, Nonce: b.DiffUint64{Value: &nonce2},
Balance: b.DiffBigInt{ Value: big.NewInt(block1Account1Balance - balanceChange1000 + balanceChange1000) }, Balance: b.DiffBigInt{Value: big.NewInt(block1Account1Balance - balanceChange1000 + balanceChange1000)},
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
burnAddress: { burnAddress: {
Nonce: b.DiffUint64{ Value: &nonce0 }, Nonce: b.DiffUint64{Value: &nonce0},
Balance: b.DiffBigInt{ Value: big.NewInt(miningReward + miningReward) }, Balance: b.DiffBigInt{Value: big.NewInt(miningReward + miningReward)},
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
}, },
@ -213,27 +213,27 @@ func TestBuilder(t *testing.T) {
DeletedAccounts: emptyAccountDiffEventualMap, DeletedAccounts: emptyAccountDiffEventualMap,
UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{ UpdatedAccounts: map[common.Address]b.AccountDiffIncremental{
account2Addr: { account2Addr: {
Nonce: b.DiffUint64{ Value: &nonce0 }, Nonce: b.DiffUint64{Value: &nonce0},
Balance: b.DiffBigInt{ Value: big.NewInt(block2Account2Balance + miningReward) }, Balance: b.DiffBigInt{Value: big.NewInt(block2Account2Balance + miningReward)},
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
contractAddr: { contractAddr: {
Nonce: b.DiffUint64{ Value: &nonce1 }, Nonce: b.DiffUint64{Value: &nonce1},
Balance: b.DiffBigInt{ Value: big.NewInt(0) }, Balance: b.DiffBigInt{Value: big.NewInt(0)},
CodeHash: "0x1c671ee4ae8abbacab7da59d6f8785cce8295eb086551ce7ac266a2e93666c0f", CodeHash: "0x1c671ee4ae8abbacab7da59d6f8785cce8295eb086551ce7ac266a2e93666c0f",
ContractRoot: b.DiffString{ Value: &newContractRoot }, ContractRoot: b.DiffString{Value: &newContractRoot},
Storage: map[string]b.DiffString{ Storage: map[string]b.DiffString{
"0x405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace": { "0x405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace": {
Value: &newStorageValue }, Value: &newStorageValue},
}, },
}, },
testBankAddress: { testBankAddress: {
Nonce: b.DiffUint64{ Value: &nonce3 }, Nonce: b.DiffUint64{Value: &nonce3},
Balance: b.DiffBigInt{ Value: big.NewInt(99989000) }, Balance: b.DiffBigInt{Value: big.NewInt(99989000)},
CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", CodeHash: "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
ContractRoot: b.DiffString{ Value: &originalContractRoot }, ContractRoot: b.DiffString{Value: &originalContractRoot},
Storage: map[string]b.DiffString{}, Storage: map[string]b.DiffString{},
}, },
}, },

View File

@ -72,6 +72,12 @@ type AccountDiffIncremental struct {
Storage map[string]DiffString `json:"storage" gencodec:"required"` Storage map[string]DiffString `json:"storage" gencodec:"required"`
} }
type DiffString struct{ Value *string `json:"value" gencodec:"optional"` } type DiffString struct {
type DiffUint64 struct{ Value *uint64 `json:"value" gencodec:"optional"` } Value *string `json:"value" gencodec:"optional"`
type DiffBigInt struct{ Value *big.Int `json:"value" gencodec:"optional"` } }
type DiffUint64 struct {
Value *uint64 `json:"value" gencodec:"optional"`
}
type DiffBigInt struct {
Value *big.Int `json:"value" gencodec:"optional"`
}

View File

@ -1,15 +1,15 @@
package extractor_test package extractor_test
import ( import (
"testing" "bytes"
"math/big" "github.com/ethereum/go-ethereum/core/types"
"math/rand"
b "github.com/ethereum/go-ethereum/statediff/builder" b "github.com/ethereum/go-ethereum/statediff/builder"
e "github.com/ethereum/go-ethereum/statediff/extractor" 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" "github.com/ethereum/go-ethereum/statediff/testhelpers/mocks"
"math/big"
"math/rand"
"reflect"
"testing"
) )
var publisher mocks.Publisher var publisher mocks.Publisher
@ -56,10 +56,18 @@ func testBuildStateDiffStruct(t *testing.T) {
t.Error(err) t.Error(err)
} }
if !equals(builder.OldStateRoot, parentBlock.Root()) { t.Error()} if !equals(builder.OldStateRoot, parentBlock.Root()) {
if !equals(builder.NewStateRoot, currentBlock.Root()) { t.Error()} t.Error()
if !equals(builder.BlockNumber,currentBlockNumber.Int64()) { t.Error()} }
if !equals(builder.BlockHash, currentBlock.Hash()) { 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) { func testBuildStateDiffErrorHandling(t *testing.T) {
@ -70,7 +78,9 @@ func testBuildStateDiffErrorHandling(t *testing.T) {
t.Error(err) t.Error(err)
} }
if !equals(err, mocks.Error) { t.Error() } if !equals(err, mocks.Error) {
t.Error()
}
builder.SetBuilderError(nil) builder.SetBuilderError(nil)
} }
@ -82,7 +92,9 @@ func testPublishingStateDiff(t *testing.T) {
t.Error(err) t.Error(err)
} }
if !equals(publisher.StateDiff, &expectedStateDiff) { t.Error() } if !equals(publisher.StateDiff, &expectedStateDiff) {
t.Error()
}
} }
func testPublisherErrorHandling(t *testing.T) { func testPublisherErrorHandling(t *testing.T) {
@ -92,7 +104,9 @@ func testPublisherErrorHandling(t *testing.T) {
if err == nil { if err == nil {
t.Error("Expected an error, but it didn't occur.") 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) publisher.SetPublisherError(nil)
} }

View File

@ -51,7 +51,6 @@ func (p *publisher) publishStateDiffToCSV(sd builder.StateDiff) error {
for _, row := range accumulateUpdatedAccountRows(sd) { for _, row := range accumulateUpdatedAccountRows(sd) {
data = append(data, row) data = append(data, row)
} }
for _, row := range accumulateDeletedAccountRows(sd) { for _, row := range accumulateDeletedAccountRows(sd) {
data = append(data, row) data = append(data, row)
} }

View File

@ -1,19 +1,19 @@
package publisher_test package publisher_test
import ( 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" "bytes"
"reflect" "encoding/csv"
"github.com/ethereum/go-ethereum/statediff"
"github.com/ethereum/go-ethereum/statediff/builder" "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" "github.com/pkg/errors"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strconv"
"testing"
) )
var ( var (
@ -98,7 +98,7 @@ func removeFilesFromDir(dir string, t *testing.T) {
for _, file := range files { for _, file := range files {
err = os.RemoveAll(file) err = os.RemoveAll(file)
if err !=nil { if err != nil {
t.Error() t.Error()
} }
} }
@ -119,9 +119,13 @@ func testColumnHeaders(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) 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) { func testAccountDiffs(t *testing.T) {
@ -140,10 +144,18 @@ func testAccountDiffs(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if len(lines) <= 3 { t.Error() } if len(lines) <= 3 {
if !equals(lines[1], expectedCreatedAccountRow) { t.Error() } t.Error()
if !equals(lines[2], expectedUpdatedAccountRow) { t.Error()} }
if !equals(lines[3], expectedDeletedAccountRow) { 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) { func testWhenNoDiff(t *testing.T) {
@ -163,52 +175,84 @@ func testWhenNoDiff(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if !equals(len(lines), 1) { t.Error() } if !equals(len(lines), 1) {
t.Error()
}
} }
func testDefaultPublisher(t *testing.T) { func testDefaultPublisher(t *testing.T) {
//it defaults to publishing state diffs to a CSV file when no mode is configured //it defaults to publishing state diffs to a CSV file when no mode is configured
config := statediff.Config{Path: dir} config := statediff.Config{Path: dir}
publisher, err = p.NewPublisher(config) publisher, err = p.NewPublisher(config)
if err != nil { t.Error(err) } if err != nil {
t.Error(err)
}
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff) _, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
if err != nil { t.Error(err) } if err != nil {
t.Error(err)
}
file, err := getTestDiffFile(dir) file, err := getTestDiffFile(dir)
if err != nil { t.Error(err) } if err != nil {
t.Error(err)
}
lines, err := csv.NewReader(file).ReadAll() lines, err := csv.NewReader(file).ReadAll()
if err != nil { t.Error(err) } if err != nil {
if !equals(len(lines), 4) { t.Error()} t.Error(err)
if !equals(lines[0],p.Headers) { t.Error()} }
if !equals(len(lines), 4) {
t.Error()
}
if !equals(lines[0], p.Headers) {
t.Error()
}
} }
func testDefaultDirectory(t *testing.T) { func testDefaultDirectory(t *testing.T) {
//it defaults to publishing CSV files in the current directory when no path is configured //it defaults to publishing CSV files in the current directory when no path is configured
config := statediff.Config{} config := statediff.Config{}
publisher, err = p.NewPublisher(config) publisher, err = p.NewPublisher(config)
if err != nil { t.Error(err) } if err != nil {
t.Error(err)
}
err := os.Chdir(dir) err := os.Chdir(dir)
if err != nil { t.Error(err) } if err != nil {
t.Error(err)
}
_, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff) _, err = publisher.PublishStateDiff(&testhelpers.TestStateDiff)
if err != nil { t.Error(err) } if err != nil {
t.Error(err)
}
file, err := getTestDiffFile(dir) file, err := getTestDiffFile(dir)
if err != nil { t.Error(err) } if err != nil {
t.Error(err)
}
lines, err := csv.NewReader(file).ReadAll() lines, err := csv.NewReader(file).ReadAll()
if err != nil { t.Error(err) } if err != nil {
if !equals(len(lines), 4) { t.Error() } t.Error(err)
if !equals(lines[0], p.Headers) { t.Error() } }
if !equals(len(lines), 4) {
t.Error()
}
if !equals(lines[0], p.Headers) {
t.Error()
}
} }
func getTestDiffFile(dir string) (*os.File, error) { func getTestDiffFile(dir string) (*os.File, error) {
files, err := ioutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
if err != nil { return nil, err } if err != nil {
if len(files) == 0 { return nil, errors.New("There are 0 files.") } return nil, err
}
if len(files) == 0 {
return nil, errors.New("There are 0 files.")
}
fileName := files[0].Name() fileName := files[0].Name()
filePath := filepath.Join(dir, fileName) filePath := filepath.Join(dir, fileName)

View File

@ -10,14 +10,14 @@ import (
e "github.com/ethereum/go-ethereum/statediff/extractor" e "github.com/ethereum/go-ethereum/statediff/extractor"
p "github.com/ethereum/go-ethereum/statediff/publisher" 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/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
) )
type BlockChain interface { type BlockChain interface {
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
GetBlockByHash(hash common.Hash) *types.Block GetBlockByHash(hash common.Hash) *types.Block
} }
type StateDiffService struct { type StateDiffService struct {

View File

@ -1,16 +1,17 @@
package service_test package service_test
import ( 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/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/big"
"math/rand" "math/rand"
"github.com/ethereum/go-ethereum/statediff/testhelpers/mocks" "reflect"
"testing"
) )
func TestServiceLoop(t *testing.T) { func TestServiceLoop(t *testing.T) {
testServiceLoop(t) testServiceLoop(t)
} }
@ -27,14 +28,14 @@ var (
parentHash1 = parentBlock1.Hash() parentHash1 = parentBlock1.Hash()
parentHash2 = parentBlock2.Hash() parentHash2 = parentBlock2.Hash()
header1 = types.Header{ ParentHash: parentHash1 } header1 = types.Header{ParentHash: parentHash1}
header2 = types.Header{ ParentHash: parentHash2 } header2 = types.Header{ParentHash: parentHash2}
block1 = types.NewBlock(&header1, nil, nil, nil) block1 = types.NewBlock(&header1, nil, nil, nil)
block2 = types.NewBlock(&header2, nil, nil, nil) block2 = types.NewBlock(&header2, nil, nil, nil)
event1 = core.ChainEvent{ Block: block1 } event1 = core.ChainEvent{Block: block1}
event2 = core.ChainEvent{ Block: block2 } event2 = core.ChainEvent{Block: block2}
) )
func testServiceLoop(t *testing.T) { func testServiceLoop(t *testing.T) {
@ -45,7 +46,7 @@ func testServiceLoop(t *testing.T) {
close(eventsChannel) close(eventsChannel)
blockChain := mocks.BlockChain{} blockChain := mocks.BlockChain{}
service := service2.StateDiffService{ service := service2.StateDiffService{
Builder: nil, Builder: nil,
Extractor: &extractor, Extractor: &extractor,
BlockChain: &blockChain, BlockChain: &blockChain,

View File

@ -2,15 +2,15 @@ package mocks
import ( import (
"github.com/ethereum/go-ethereum/common" "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"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
) )
type BlockChain struct { type BlockChain struct {
ParentHashesLookedUp []common.Hash ParentHashesLookedUp []common.Hash
parentBlocksToReturn []*types.Block parentBlocksToReturn []*types.Block
callCount int callCount int
} }
func (mc *BlockChain) SetParentBlockToReturn(blocks []*types.Block) { 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 { func (BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
panic("implement me") panic("implement me")
} }

View File

@ -1,8 +1,8 @@
package mocks package mocks
import ( import (
"github.com/ethereum/go-ethereum/statediff/builder"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/statediff/builder"
) )
type Builder struct { type Builder struct {
@ -30,4 +30,3 @@ func (builder *Builder) SetStateDiffToBuild(stateDiff *builder.StateDiff) {
func (builder *Builder) SetBuilderError(err error) { func (builder *Builder) SetBuilderError(err error) {
builder.builderError = err builder.builderError = err
} }

View File

@ -3,8 +3,8 @@ package mocks
import "github.com/ethereum/go-ethereum/core/types" import "github.com/ethereum/go-ethereum/core/types"
type Extractor struct { type Extractor struct {
ParentBlocks []types.Block ParentBlocks []types.Block
CurrentBlocks []types.Block CurrentBlocks []types.Block
extractError error extractError error
} }
@ -18,4 +18,3 @@ func (me *Extractor) ExtractStateDiff(parent, current types.Block) (string, erro
func (me *Extractor) SetExtractError(err error) { func (me *Extractor) SetExtractError(err error) {
me.extractError = err me.extractError = err
} }

View File

@ -8,43 +8,43 @@ import (
) )
var ( var (
BlockNumber = rand.Int63() BlockNumber = rand.Int63()
BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73" BlockHash = "0xfa40fbe2d98d98b3363a778d52f2bcd29d6790b9b3f3cab2b167fd12d3550f73"
CodeHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" CodeHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
OldNonceValue = rand.Uint64() OldNonceValue = rand.Uint64()
NewNonceValue = OldNonceValue + 1 NewNonceValue = OldNonceValue + 1
OldBalanceValue = rand.Int63() OldBalanceValue = rand.Int63()
NewBalanceValue = OldBalanceValue - 1 NewBalanceValue = OldBalanceValue - 1
ContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" ContractRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
StoragePath = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" StoragePath = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
oldStorage = "0x0" oldStorage = "0x0"
newStorage = "0x03" newStorage = "0x03"
storage = map[string]builder.DiffString{StoragePath: { Value: &newStorage }} storage = map[string]builder.DiffString{StoragePath: {Value: &newStorage}}
address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592")
CreatedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: { CreatedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: {
Nonce: builder.DiffUint64{ Value: &NewNonceValue }, Nonce: builder.DiffUint64{Value: &NewNonceValue},
Balance: builder.DiffBigInt{ Value: big.NewInt(NewBalanceValue) }, Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)},
ContractRoot: builder.DiffString{ Value: &ContractRoot }, ContractRoot: builder.DiffString{Value: &ContractRoot},
Code: []byte("created account code"), Code: []byte("created account code"),
CodeHash: CodeHash, CodeHash: CodeHash,
Storage: storage, Storage: storage,
}} }}
UpdatedAccountDiffs = map[common.Address]builder.AccountDiffIncremental{address: { UpdatedAccountDiffs = map[common.Address]builder.AccountDiffIncremental{address: {
Nonce: builder.DiffUint64{ Value: &NewNonceValue }, Nonce: builder.DiffUint64{Value: &NewNonceValue},
Balance: builder.DiffBigInt{ Value: big.NewInt(NewBalanceValue) }, Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)},
CodeHash: CodeHash, CodeHash: CodeHash,
ContractRoot: builder.DiffString{ Value: &ContractRoot }, ContractRoot: builder.DiffString{Value: &ContractRoot},
Storage: storage, Storage: storage,
}} }}
DeletedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: { DeletedAccountDiffs = map[common.Address]builder.AccountDiffEventual{address: {
Nonce: builder.DiffUint64{ Value: &NewNonceValue }, Nonce: builder.DiffUint64{Value: &NewNonceValue},
Balance: builder.DiffBigInt{ Value: big.NewInt(NewBalanceValue) }, Balance: builder.DiffBigInt{Value: big.NewInt(NewBalanceValue)},
ContractRoot: builder.DiffString{ Value: &ContractRoot }, ContractRoot: builder.DiffString{Value: &ContractRoot},
Code: []byte("deleted account code"), Code: []byte("deleted account code"),
CodeHash: CodeHash, CodeHash: CodeHash,
Storage: storage, Storage: storage,
}} }}
TestStateDiff = builder.StateDiff{ TestStateDiff = builder.StateDiff{