Add *StateDB.TxHash for usage with Warp (#204)

## Why this should be merged

Coreth and Subnet-EVM require access to the current tx hash in the Warp
precompile (for selecting the correct predicate results).

This can not be exposed as a wrapper through the `OverrideEVMResetArgs`
because `SetTxContext` is called _prior_ to resetting the EVM instance.
Therefore, the `SetTxContext` function on any DB wrapper would never be
called.

## How this works

Exposes the existing `txHash` through a `TxHash()` function.

## How this was tested

Added a trivial unit test.
This commit is contained in:
Stephen Buttolph 2025-07-21 22:20:20 -04:00 committed by GitHub
parent 08e2b6da8a
commit 99f0d0b1de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View file

@ -24,6 +24,11 @@ import (
"github.com/ava-labs/libevm/libevm/stateconf"
)
// TxHash returns the current transaction hash set by [StateDB.SetTxContext].
func (s *StateDB) TxHash() common.Hash {
return s.thash
}
// SnapshotTree mirrors the functionality of a [snapshot.Tree], allowing for
// drop-in replacements. This is intended as a temporary feature as a workaround
// until a standard Tree can be used.

View file

@ -36,6 +36,18 @@ import (
"github.com/ava-labs/libevm/triedb/hashdb"
)
func TestTxHash(t *testing.T) {
db := NewDatabase(rawdb.NewMemoryDatabase())
state, err := New(types.EmptyRootHash, db, nil)
require.NoError(t, err)
assert.Zero(t, state.TxHash(), "Tx hash should initially be uninitialized")
hash := common.Hash{1}
state.SetTxContext(hash, 3)
assert.Equal(t, hash, state.TxHash(), "Tx hash should have been updated")
}
func TestStateDBCommitPropagatesOptions(t *testing.T) {
memdb := rawdb.NewMemoryDatabase()
trieRec := &triedbRecorder{Database: hashdb.New(memdb, nil, &trie.MerkleResolver{})}