@@ -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
366382func (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
594623func (sds * Service ) writeStateDiff (block * types.Block , parentRoot common.Hash , params Params ) error {
595624 // log.Info("Writing state diff", "block height", block.Number().Uint64())
0 commit comments