From 2b25daf46e96cf0457da0bb5e7f60e7c4a3b4476 Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Wed, 28 Oct 2020 14:34:17 +0800 Subject: [PATCH] [wip] update test mocks todo - do something meaningful to test write loop --- statediff/testhelpers/mocks/service.go | 44 ++++++++++++++++++-------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/statediff/testhelpers/mocks/service.go b/statediff/testhelpers/mocks/service.go index 965c4fcf2..27c36f2f4 100644 --- a/statediff/testhelpers/mocks/service.go +++ b/statediff/testhelpers/mocks/service.go @@ -130,19 +130,6 @@ func (sds *MockStateDiffService) StateDiffAt(blockNumber uint64, params statedif return sds.processStateDiff(currentBlock, parentBlock.Root(), params) } -// WriteStateDiffAt mock method -func (sds *MockStateDiffService) WriteStateDiffAt(blockNumber uint64, params statediff.Params) error { - currentBlock := sds.BlockChain.GetBlockByNumber(blockNumber) - log.Info(fmt.Sprintf("sending state diff at %d", blockNumber)) - parentRoot := common.Hash{} - if blockNumber != 0 { - parentBlock := sds.BlockChain.GetBlockByHash(currentBlock.ParentHash()) - parentRoot = parentBlock.Root() - } - _, err := sds.processStateDiff(currentBlock, parentRoot, params) - return err -} - // processStateDiff method builds the state diff payload from the current block, parent state root, and provided params func (sds *MockStateDiffService) processStateDiff(currentBlock *types.Block, parentRoot common.Hash, params statediff.Params) (*statediff.Payload, error) { stateDiff, err := sds.Builder.BuildStateDiffObject(statediff.Args{ @@ -186,6 +173,37 @@ func (sds *MockStateDiffService) newPayload(stateObject []byte, block *types.Blo return payload, nil } +// WriteStateDiffAt mock method +func (sds *MockStateDiffService) WriteStateDiffAt(blockNumber uint64, params statediff.Params) error { + // TODO: something useful here + return nil +} + +// Loop mock method +func (sds *MockStateDiffService) WriteLoop(chan core.ChainEvent) { + //loop through chain events until no more + for { + select { + case block := <-sds.BlockChan: + currentBlock := block + parentBlock := <-sds.ParentBlockChan + parentHash := parentBlock.Hash() + if parentBlock == nil { + log.Error("Parent block is nil, skipping this block", + "parent block hash", parentHash.String(), + "current block number", currentBlock.Number()) + continue + } + // TODO: + // sds.writeStateDiff(currentBlock, parentBlock.Root(), statediff.Params{}) + case <-sds.QuitChan: + log.Debug("Quitting the statediff block channel") + sds.close() + return + } + } +} + // StateTrieAt mock method func (sds *MockStateDiffService) StateTrieAt(blockNumber uint64, params statediff.Params) (*statediff.Payload, error) { currentBlock := sds.BlockChain.GetBlockByNumber(blockNumber)