Skip to content

Commit 299d004

Browse files
committed
stateDiffFor endpoints for fetching or writing statediff object by blockhash; bump statediff version
1 parent fe49597 commit 299d004

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

params/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
VersionMajor = 1 // Major version component of the current release
2525
VersionMinor = 9 // Minor version component of the current release
2626
VersionPatch = 25 // Patch version component of the current release
27-
VersionMeta = "statediff-0.0.14" // Version metadata to append to the version string
27+
VersionMeta = "statediff-0.0.15" // Version metadata to append to the version string
2828
)
2929

3030
// Version holds the textual version string.

statediff/api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package statediff
1919
import (
2020
"context"
2121

22+
"github.com/ethereum/go-ethereum/common"
23+
2224
"github.com/ethereum/go-ethereum/log"
2325
"github.com/ethereum/go-ethereum/rpc"
2426
. "github.com/ethereum/go-ethereum/statediff/types"
@@ -95,6 +97,11 @@ func (api *PublicStateDiffAPI) StateDiffAt(ctx context.Context, blockNumber uint
9597
return api.sds.StateDiffAt(blockNumber, params)
9698
}
9799

100+
// StateDiffFor returns a state diff payload for the specific blockhash
101+
func (api *PublicStateDiffAPI) StateDiffFor(ctx context.Context, blockHash common.Hash, params Params) (*Payload, error) {
102+
return api.sds.StateDiffFor(blockHash, params)
103+
}
104+
98105
// StateTrieAt returns a state trie payload at the specific blockheight
99106
func (api *PublicStateDiffAPI) StateTrieAt(ctx context.Context, blockNumber uint64, params Params) (*Payload, error) {
100107
return api.sds.StateTrieAt(blockNumber, params)
@@ -137,3 +144,8 @@ func (api *PublicStateDiffAPI) StreamCodeAndCodeHash(ctx context.Context, blockN
137144
func (api *PublicStateDiffAPI) WriteStateDiffAt(ctx context.Context, blockNumber uint64, params Params) error {
138145
return api.sds.WriteStateDiffAt(blockNumber, params)
139146
}
147+
148+
// WriteStateDiffFor writes a state diff object directly to DB for the specific block hash
149+
func (api *PublicStateDiffAPI) WriteStateDiffFor(ctx context.Context, blockHash common.Hash, params Params) error {
150+
return api.sds.WriteStateDiffFor(blockHash, params)
151+
}

statediff/service.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,16 @@ type IService interface {
8181
Unsubscribe(id rpc.ID) error
8282
// Method to get state diff object at specific block
8383
StateDiffAt(blockNumber uint64, params Params) (*Payload, error)
84+
// Method to get state diff object at specific block
85+
StateDiffFor(blockHash common.Hash, params Params) (*Payload, error)
8486
// Method to get state trie object at specific block
8587
StateTrieAt(blockNumber uint64, params Params) (*Payload, error)
8688
// Method to stream out all code and codehash pairs
8789
StreamCodeAndCodeHash(blockNumber uint64, outChan chan<- CodeAndCodeHash, quitChan chan<- bool)
8890
// Method to write state diff object directly to DB
8991
WriteStateDiffAt(blockNumber uint64, params Params) error
92+
// Method to write state diff object directly to DB
93+
WriteStateDiffFor(blockHash common.Hash, params Params) error
9094
// Event loop for progressively processing and writing diffs directly to DB
9195
WriteLoop(chainEventCh chan core.ChainEvent)
9296
}
@@ -362,6 +366,18 @@ func (sds *Service) StateDiffAt(blockNumber uint64, params Params) (*Payload, er
362366
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
363367
}
364368

369+
// StateDiffFor returns a state diff object payload for the specific blockhash
370+
// This operation cannot be performed back past the point of db pruning; it requires an archival node for historical data
371+
func (sds *Service) StateDiffFor(blockHash common.Hash, params Params) (*Payload, error) {
372+
currentBlock := sds.BlockChain.GetBlockByHash(blockHash)
373+
log.Info("sending state diff", "block hash", blockHash)
374+
if currentBlock.NumberU64() == 0 {
375+
return sds.processStateDiff(currentBlock, common.Hash{}, params)
376+
}
377+
parentBlock := sds.BlockChain.GetBlockByHash(currentBlock.ParentHash())
378+
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
379+
}
380+
365381
// processStateDiff method builds the state diff payload from the current block, parent state root, and provided params
366382
func (sds *Service) processStateDiff(currentBlock *types.Block, parentRoot common.Hash, params Params) (*Payload, error) {
367383
stateDiff, err := sds.Builder.BuildStateDiffObject(Args{
@@ -590,6 +606,19 @@ func (sds *Service) WriteStateDiffAt(blockNumber uint64, params Params) error {
590606
return sds.writeStateDiff(currentBlock, parentRoot, params)
591607
}
592608

609+
// WriteStateDiffFor writes a state diff for the specific blockhash directly to the database
610+
// This operation cannot be performed back past the point of db pruning; it requires an archival node
611+
// for historical data
612+
func (sds *Service) WriteStateDiffFor(blockHash common.Hash, params Params) error {
613+
currentBlock := sds.BlockChain.GetBlockByHash(blockHash)
614+
parentRoot := common.Hash{}
615+
if currentBlock.NumberU64() != 0 {
616+
parentBlock := sds.BlockChain.GetBlockByHash(currentBlock.ParentHash())
617+
parentRoot = parentBlock.Root()
618+
}
619+
return sds.writeStateDiff(currentBlock, parentRoot, params)
620+
}
621+
593622
// Writes a state diff from the current block, parent state root, and provided params
594623
func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, params Params) error {
595624
// log.Info("Writing state diff", "block height", block.Number().Uint64())

statediff/testhelpers/mocks/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ func (sds *MockStateDiffService) StateDiffAt(blockNumber uint64, params statedif
130130
return sds.processStateDiff(currentBlock, parentBlock.Root(), params)
131131
}
132132

133+
// StateDiffFor mock method
134+
func (sds *MockStateDiffService) StateDiffFor(blockHash common.Hash, params statediff.Params) (*statediff.Payload, error) {
135+
// TODO: something useful here
136+
return nil, nil
137+
}
138+
133139
// processStateDiff method builds the state diff payload from the current block, parent state root, and provided params
134140
func (sds *MockStateDiffService) processStateDiff(currentBlock *types.Block, parentRoot common.Hash, params statediff.Params) (*statediff.Payload, error) {
135141
stateDiff, err := sds.Builder.BuildStateDiffObject(statediff.Args{
@@ -179,6 +185,12 @@ func (sds *MockStateDiffService) WriteStateDiffAt(blockNumber uint64, params sta
179185
return nil
180186
}
181187

188+
// WriteStateDiffFor mock method
189+
func (sds *MockStateDiffService) WriteStateDiffFor(blockHash common.Hash, params statediff.Params) error {
190+
// TODO: something useful here
191+
return nil
192+
}
193+
182194
// Loop mock method
183195
func (sds *MockStateDiffService) WriteLoop(chan core.ChainEvent) {
184196
//loop through chain events until no more

tests/testdata

Submodule testdata updated 1323 files

0 commit comments

Comments
 (0)