Write state diff to CSV #2
@ -2,7 +2,6 @@ package extractor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/ethereum/go-ethereum/statediff/testhelpers"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
b "github.com/ethereum/go-ethereum/statediff/builder"
|
||||
@ -10,10 +9,11 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"bytes"
|
||||
"reflect"
|
||||
"github.com/ethereum/go-ethereum/statediff/testhelpers/mocks"
|
||||
)
|
||||
|
||||
var publisher testhelpers.MockPublisher
|
||||
var builder testhelpers.MockBuilder
|
||||
var publisher mocks.Publisher
|
||||
var builder mocks.Builder
|
||||
var currentBlockNumber *big.Int
|
||||
var parentBlock, currentBlock *types.Block
|
||||
var expectedStateDiff b.StateDiff
|
||||
@ -21,8 +21,8 @@ var extractor e.Extractor
|
||||
var err error
|
||||
|
||||
func TestExtractor(t *testing.T) {
|
||||
publisher = testhelpers.MockPublisher{}
|
||||
builder = testhelpers.MockBuilder{}
|
||||
publisher = mocks.Publisher{}
|
||||
builder = mocks.Builder{}
|
||||
extractor, err = e.NewExtractor(&builder, &publisher)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -63,14 +63,14 @@ func testBuildStateDiffStruct(t *testing.T) {
|
||||
}
|
||||
|
||||
func testBuildStateDiffErrorHandling(t *testing.T) {
|
||||
builder.SetBuilderError(testhelpers.MockError)
|
||||
builder.SetBuilderError(mocks.Error)
|
||||
|
||||
_, err = extractor.ExtractStateDiff(*parentBlock, *currentBlock)
|
||||
if err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !equals(err, testhelpers.MockError) { t.Error() }
|
||||
if !equals(err, mocks.Error) { t.Error() }
|
||||
builder.SetBuilderError(nil)
|
||||
}
|
||||
|
||||
@ -86,13 +86,13 @@ func testPublishingStateDiff(t *testing.T) {
|
||||
}
|
||||
|
||||
func testPublisherErrorHandling(t *testing.T) {
|
||||
publisher.SetPublisherError(testhelpers.MockError)
|
||||
publisher.SetPublisherError(mocks.Error)
|
||||
|
||||
_, err = extractor.ExtractStateDiff(*parentBlock, *currentBlock)
|
||||
if err == nil {
|
||||
t.Error("Expected an error, but it didn't occur.")
|
||||
}
|
||||
if !equals(err, testhelpers.MockError) { t.Error() }
|
||||
if !equals(err, mocks.Error) { t.Error() }
|
||||
|
||||
publisher.SetPublisherError(nil)
|
||||
}
|
||||
|
@ -76,7 +76,8 @@ func TestPublisher(t *testing.T) {
|
||||
|
||||
type Test func(t *testing.T)
|
||||
|
||||
var tests = []Test{testColumnHeaders,
|
||||
var tests = []Test{
|
||||
testColumnHeaders,
|
||||
testAccountDiffs,
|
||||
testWhenNoDiff,
|
||||
testDefaultPublisher,
|
||||
|
@ -7,54 +7,10 @@ import (
|
||||
service2 "github.com/ethereum/go-ethereum/statediff/service"
|
||||
"reflect"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"github.com/ethereum/go-ethereum/statediff/testhelpers/mocks"
|
||||
)
|
||||
|
||||
type MockExtractor struct {
|
||||
ParentBlocks []types.Block
|
||||
CurrentBlocks []types.Block
|
||||
extractError error
|
||||
}
|
||||
|
||||
func (me *MockExtractor) ExtractStateDiff(parent, current types.Block) (string, error) {
|
||||
me.ParentBlocks = append(me.ParentBlocks, parent)
|
||||
me.CurrentBlocks = append(me.CurrentBlocks, current)
|
||||
|
||||
return "", me.extractError
|
||||
}
|
||||
|
||||
func (me *MockExtractor) SetExtractError(err error) {
|
||||
me.extractError = err
|
||||
}
|
||||
|
||||
type MockChain struct {
|
||||
ParentHashesLookedUp []common.Hash
|
||||
parentBlocksToReturn []*types.Block
|
||||
callCount int
|
||||
}
|
||||
|
||||
func (mc *MockChain) SetParentBlockToReturn(blocks []*types.Block) {
|
||||
mc.parentBlocksToReturn = blocks
|
||||
}
|
||||
|
||||
func (mc *MockChain) GetBlockByHash(hash common.Hash) *types.Block {
|
||||
mc.ParentHashesLookedUp = append(mc.ParentHashesLookedUp, hash)
|
||||
|
||||
var parentBlock types.Block
|
||||
if len(mc.parentBlocksToReturn) > 0 {
|
||||
parentBlock = *mc.parentBlocksToReturn[mc.callCount]
|
||||
}
|
||||
|
||||
mc.callCount++
|
||||
return &parentBlock
|
||||
}
|
||||
|
||||
func (MockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func TestServiceLoop(t *testing.T) {
|
||||
testServiceLoop(t)
|
||||
}
|
||||
@ -85,10 +41,10 @@ func testServiceLoop(t *testing.T) {
|
||||
eventsChannel <- event1
|
||||
eventsChannel <- event2
|
||||
|
||||
extractor := MockExtractor{}
|
||||
extractor := mocks.Extractor{}
|
||||
close(eventsChannel)
|
||||
|
||||
blockChain := MockChain{}
|
||||
blockChain := mocks.BlockChain{}
|
||||
service := service2.StateDiffService{
|
||||
Builder: nil,
|
||||
Extractor: &extractor,
|
||||
|
@ -1,16 +0,0 @@
|
||||
package statediff_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestStatediff(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Statediff Suite")
|
||||
}
|
||||
|
||||
//convert this over to use built in golang library
|
||||
//only save the new value, and have a pointer to the old value - not sure how this pointer will work for the CSV version
|
@ -1,49 +0,0 @@
|
||||
package testhelpers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/statediff/builder"
|
||||
)
|
||||
|
||||
var MockError = errors.New("mock error")
|
||||
|
||||
type MockBuilder struct {
|
||||
OldStateRoot common.Hash
|
||||
NewStateRoot common.Hash
|
||||
BlockNumber int64
|
||||
BlockHash common.Hash
|
||||
stateDiff *builder.StateDiff
|
||||
builderError error
|
||||
}
|
||||
|
||||
func (builder *MockBuilder) BuildStateDiff(oldStateRoot, newStateRoot common.Hash, blockNumber int64, blockHash common.Hash) (*builder.StateDiff, error) {
|
||||
builder.OldStateRoot = oldStateRoot
|
||||
builder.NewStateRoot = newStateRoot
|
||||
builder.BlockNumber = blockNumber
|
||||
builder.BlockHash = blockHash
|
||||
|
||||
return builder.stateDiff, builder.builderError
|
||||
}
|
||||
|
||||
func (builder *MockBuilder) SetStateDiffToBuild(stateDiff *builder.StateDiff) {
|
||||
builder.stateDiff = stateDiff
|
||||
}
|
||||
|
||||
func (builder *MockBuilder) SetBuilderError(err error) {
|
||||
builder.builderError = err
|
||||
}
|
||||
|
||||
type MockPublisher struct {
|
||||
StateDiff *builder.StateDiff
|
||||
publisherError error
|
||||
}
|
||||
|
||||
func (publisher *MockPublisher) PublishStateDiff(sd *builder.StateDiff) (string, error) {
|
||||
publisher.StateDiff = sd
|
||||
return "", publisher.publisherError
|
||||
}
|
||||
|
||||
func (publisher *MockPublisher) SetPublisherError(err error) {
|
||||
publisher.publisherError = err
|
||||
}
|
35
statediff/testhelpers/mocks/blockchain.go
Normal file
35
statediff/testhelpers/mocks/blockchain.go
Normal file
@ -0,0 +1,35 @@
|
||||
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/event"
|
||||
)
|
||||
|
||||
type BlockChain struct {
|
||||
ParentHashesLookedUp []common.Hash
|
||||
parentBlocksToReturn []*types.Block
|
||||
callCount int
|
||||
}
|
||||
|
||||
func (mc *BlockChain) SetParentBlockToReturn(blocks []*types.Block) {
|
||||
mc.parentBlocksToReturn = blocks
|
||||
}
|
||||
|
||||
func (mc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block {
|
||||
mc.ParentHashesLookedUp = append(mc.ParentHashesLookedUp, hash)
|
||||
|
||||
var parentBlock types.Block
|
||||
if len(mc.parentBlocksToReturn) > 0 {
|
||||
parentBlock = *mc.parentBlocksToReturn[mc.callCount]
|
||||
}
|
||||
|
||||
mc.callCount++
|
||||
return &parentBlock
|
||||
}
|
||||
|
||||
func (BlockChain) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
|
||||
panic("implement me")
|
||||
}
|
||||
|
33
statediff/testhelpers/mocks/builder.go
Normal file
33
statediff/testhelpers/mocks/builder.go
Normal file
@ -0,0 +1,33 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/statediff/builder"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
type Builder struct {
|
||||
OldStateRoot common.Hash
|
||||
NewStateRoot common.Hash
|
||||
BlockNumber int64
|
||||
BlockHash common.Hash
|
||||
stateDiff *builder.StateDiff
|
||||
builderError error
|
||||
}
|
||||
|
||||
func (builder *Builder) BuildStateDiff(oldStateRoot, newStateRoot common.Hash, blockNumber int64, blockHash common.Hash) (*builder.StateDiff, error) {
|
||||
builder.OldStateRoot = oldStateRoot
|
||||
builder.NewStateRoot = newStateRoot
|
||||
builder.BlockNumber = blockNumber
|
||||
builder.BlockHash = blockHash
|
||||
|
||||
return builder.stateDiff, builder.builderError
|
||||
}
|
||||
|
||||
func (builder *Builder) SetStateDiffToBuild(stateDiff *builder.StateDiff) {
|
||||
builder.stateDiff = stateDiff
|
||||
}
|
||||
|
||||
func (builder *Builder) SetBuilderError(err error) {
|
||||
builder.builderError = err
|
||||
}
|
||||
|
5
statediff/testhelpers/mocks/error.go
Normal file
5
statediff/testhelpers/mocks/error.go
Normal file
@ -0,0 +1,5 @@
|
||||
package mocks
|
||||
|
||||
import "errors"
|
||||
|
||||
var Error = errors.New("mock error")
|
21
statediff/testhelpers/mocks/extractor.go
Normal file
21
statediff/testhelpers/mocks/extractor.go
Normal file
@ -0,0 +1,21 @@
|
||||
package mocks
|
||||
|
||||
import "github.com/ethereum/go-ethereum/core/types"
|
||||
|
||||
type Extractor struct {
|
||||
ParentBlocks []types.Block
|
||||
CurrentBlocks []types.Block
|
||||
extractError error
|
||||
}
|
||||
|
||||
func (me *Extractor) ExtractStateDiff(parent, current types.Block) (string, error) {
|
||||
me.ParentBlocks = append(me.ParentBlocks, parent)
|
||||
me.CurrentBlocks = append(me.CurrentBlocks, current)
|
||||
|
||||
return "", me.extractError
|
||||
}
|
||||
|
||||
func (me *Extractor) SetExtractError(err error) {
|
||||
me.extractError = err
|
||||
}
|
||||
|
17
statediff/testhelpers/mocks/publisher.go
Normal file
17
statediff/testhelpers/mocks/publisher.go
Normal file
@ -0,0 +1,17 @@
|
||||
package mocks
|
||||
|
||||
import "github.com/ethereum/go-ethereum/statediff/builder"
|
||||
|
||||
type Publisher struct {
|
||||
StateDiff *builder.StateDiff
|
||||
publisherError error
|
||||
}
|
||||
|
||||
func (publisher *Publisher) PublishStateDiff(sd *builder.StateDiff) (string, error) {
|
||||
publisher.StateDiff = sd
|
||||
return "", publisher.publisherError
|
||||
}
|
||||
|
||||
func (publisher *Publisher) SetPublisherError(err error) {
|
||||
publisher.publisherError = err
|
||||
}
|
Loading…
Reference in New Issue
Block a user