Skip to content

Commit

Permalink
Merge pull request #15 from blocknative/bugfix/revert-reason-cache-key
Browse files Browse the repository at this point in the history
Cache revert reasons by transaction and block hash
  • Loading branch information
alexcampbelling authored Aug 13, 2021
2 parents 6775417 + 7590e1f commit ea2e280
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions core/revert_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ var (
revertCache *lru.Cache
)

func CacheRevertReason(h common.Hash, reason []byte) {
func CacheRevertReason(h, blockHash common.Hash, reason []byte) {
if revertCache == nil { revertCache, _ = lru.New(10000) }
if reason != nil {
key := [64]byte{}
copy(key[:32], blockHash[:])
copy(key[32:], h[:])
if reasonString, err := abi.UnpackRevert(reason); err == nil {
revertCache.Add(h, reasonString)
revertCache.Add(key, reasonString)
}
}
}

func GetRevertReason(h common.Hash) (string, bool) {
func GetRevertReason(h, blockHash common.Hash) (string, bool) {
if revertCache == nil { revertCache, _ = lru.New(10000) }
if v, ok := revertCache.Get(h); ok {
key := [64]byte{}
copy(key[:32], blockHash[:])
copy(key[32:], h[:])
if v, ok := revertCache.Get(key); ok {
return v.(string), true
}
return "", false
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon
}

if revert := result.Revert(); revert != nil {
CacheRevertReason(tx.Hash(), revert)
CacheRevertReason(tx.Hash(), blockHash, revert)
}

// Update the state with pending changes.
Expand Down
2 changes: 1 addition & 1 deletion eth/filters/peers_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (api *PublicFilterAPI) NewFullBlocksWithPeers(ctx context.Context) (*rpc.Su
if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
}
if reason, ok := core.GetRevertReason(receipt.TxHash); ok {
if reason, ok := core.GetRevertReason(receipt.TxHash, hash); ok {
fields["revertReason"] = reason
}
marshalReceipts[receipt.TxHash] = fields
Expand Down

0 comments on commit ea2e280

Please sign in to comment.