From 99f0d0b1de020c94e23e38af797e6e4dba42f9a8 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Mon, 21 Jul 2025 22:20:20 -0400 Subject: [PATCH] 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. --- core/state/statedb.libevm.go | 5 +++++ core/state/statedb.libevm_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/core/state/statedb.libevm.go b/core/state/statedb.libevm.go index 7db3d676e9..df758f01d6 100644 --- a/core/state/statedb.libevm.go +++ b/core/state/statedb.libevm.go @@ -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. diff --git a/core/state/statedb.libevm_test.go b/core/state/statedb.libevm_test.go index 255f880e07..6b8285ee4f 100644 --- a/core/state/statedb.libevm_test.go +++ b/core/state/statedb.libevm_test.go @@ -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{})}