Skip to content

Commit

Permalink
Merge pull request #54 from vulcanize/v1.9.25-statediff-0.0.15
Browse files Browse the repository at this point in the history
V1.9.25 statediff 0.0.15
  • Loading branch information
i-norden committed Feb 24, 2021
2 parents fe49597 + 299d004 commit 05aadb9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
VersionMajor = 1 // Major version component of the current release
VersionMinor = 9 // Minor version component of the current release
VersionPatch = 25 // Patch version component of the current release
VersionMeta = "statediff-0.0.14" // Version metadata to append to the version string
VersionMeta = "statediff-0.0.15" // Version metadata to append to the version string
)

// Version holds the textual version string.
Expand Down
12 changes: 12 additions & 0 deletions statediff/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package statediff
import (
"context"

"github.com/ethereum/go-ethereum/common"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
. "github.com/ethereum/go-ethereum/statediff/types"
Expand Down Expand Up @@ -95,6 +97,11 @@ func (api *PublicStateDiffAPI) StateDiffAt(ctx context.Context, blockNumber uint
return api.sds.StateDiffAt(blockNumber, params)
}

// StateDiffFor returns a state diff payload for the specific blockhash
func (api *PublicStateDiffAPI) StateDiffFor(ctx context.Context, blockHash common.Hash, params Params) (*Payload, error) {
return api.sds.StateDiffFor(blockHash, params)
}

// StateTrieAt returns a state trie payload at the specific blockheight
func (api *PublicStateDiffAPI) StateTrieAt(ctx context.Context, blockNumber uint64, params Params) (*Payload, error) {
return api.sds.StateTrieAt(blockNumber, params)
Expand Down Expand Up @@ -137,3 +144,8 @@ func (api *PublicStateDiffAPI) StreamCodeAndCodeHash(ctx context.Context, blockN
func (api *PublicStateDiffAPI) WriteStateDiffAt(ctx context.Context, blockNumber uint64, params Params) error {
return api.sds.WriteStateDiffAt(blockNumber, params)
}

// WriteStateDiffFor writes a state diff object directly to DB for the specific block hash
func (api *PublicStateDiffAPI) WriteStateDiffFor(ctx context.Context, blockHash common.Hash, params Params) error {
return api.sds.WriteStateDiffFor(blockHash, params)
}
29 changes: 29 additions & 0 deletions statediff/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ type IService interface {
Unsubscribe(id rpc.ID) error
// Method to get state diff object at specific block
StateDiffAt(blockNumber uint64, params Params) (*Payload, error)
// Method to get state diff object at specific block
StateDiffFor(blockHash common.Hash, params Params) (*Payload, error)
// Method to get state trie object at specific block
StateTrieAt(blockNumber uint64, params Params) (*Payload, error)
// Method to stream out all code and codehash pairs
StreamCodeAndCodeHash(blockNumber uint64, outChan chan<- CodeAndCodeHash, quitChan chan<- bool)
// Method to write state diff object directly to DB
WriteStateDiffAt(blockNumber uint64, params Params) error
// Method to write state diff object directly to DB
WriteStateDiffFor(blockHash common.Hash, params Params) error
// Event loop for progressively processing and writing diffs directly to DB
WriteLoop(chainEventCh chan core.ChainEvent)
}
Expand Down Expand Up @@ -362,6 +366,18 @@ func (sds *Service) StateDiffAt(blockNumber uint64, params Params) (*Payload, er
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
}

// StateDiffFor returns a state diff object payload for the specific blockhash
// This operation cannot be performed back past the point of db pruning; it requires an archival node for historical data
func (sds *Service) StateDiffFor(blockHash common.Hash, params Params) (*Payload, error) {
currentBlock := sds.BlockChain.GetBlockByHash(blockHash)
log.Info("sending state diff", "block hash", blockHash)
if currentBlock.NumberU64() == 0 {
return sds.processStateDiff(currentBlock, common.Hash{}, params)
}
parentBlock := sds.BlockChain.GetBlockByHash(currentBlock.ParentHash())
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
}

// processStateDiff method builds the state diff payload from the current block, parent state root, and provided params
func (sds *Service) processStateDiff(currentBlock *types.Block, parentRoot common.Hash, params Params) (*Payload, error) {
stateDiff, err := sds.Builder.BuildStateDiffObject(Args{
Expand Down Expand Up @@ -590,6 +606,19 @@ func (sds *Service) WriteStateDiffAt(blockNumber uint64, params Params) error {
return sds.writeStateDiff(currentBlock, parentRoot, params)
}

// WriteStateDiffFor writes a state diff for the specific blockhash directly to the database
// This operation cannot be performed back past the point of db pruning; it requires an archival node
// for historical data
func (sds *Service) WriteStateDiffFor(blockHash common.Hash, params Params) error {
currentBlock := sds.BlockChain.GetBlockByHash(blockHash)
parentRoot := common.Hash{}
if currentBlock.NumberU64() != 0 {
parentBlock := sds.BlockChain.GetBlockByHash(currentBlock.ParentHash())
parentRoot = parentBlock.Root()
}
return sds.writeStateDiff(currentBlock, parentRoot, params)
}

// Writes a state diff from the current block, parent state root, and provided params
func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, params Params) error {
// log.Info("Writing state diff", "block height", block.Number().Uint64())
Expand Down
12 changes: 12 additions & 0 deletions statediff/testhelpers/mocks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func (sds *MockStateDiffService) StateDiffAt(blockNumber uint64, params statedif
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
}

// StateDiffFor mock method
func (sds *MockStateDiffService) StateDiffFor(blockHash common.Hash, params statediff.Params) (*statediff.Payload, error) {
// TODO: something useful here
return nil, nil
}

// 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{
Expand Down Expand Up @@ -179,6 +185,12 @@ func (sds *MockStateDiffService) WriteStateDiffAt(blockNumber uint64, params sta
return nil
}

// WriteStateDiffFor mock method
func (sds *MockStateDiffService) WriteStateDiffFor(blockHash common.Hash, 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
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata
Submodule testdata updated 1323 files

0 comments on commit 05aadb9

Please sign in to comment.