From 1ce418ac746b0b47a6397f9baf6e4cb5f64cc1fc Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Thu, 9 Jul 2026 23:51:22 +0200 Subject: [PATCH] trie/bintrie: preserve low balance bytes --- trie/bintrie/trie.go | 2 +- trie/bintrie/trie_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/trie/bintrie/trie.go b/trie/bintrie/trie.go index e8b3021284..93dd016d2c 100644 --- a/trie/bintrie/trie.go +++ b/trie/bintrie/trie.go @@ -259,7 +259,7 @@ func (t *BinaryTrie) UpdateAccount(addr common.Address, acc *types.StateAccount, // TODO: reduce the size of the allocation in devmode, then panic instead // of truncating. if len(balanceBytes) > 16 { - balanceBytes = balanceBytes[16:] + balanceBytes = balanceBytes[len(balanceBytes)-16:] } copy(basicData[HashSize-len(balanceBytes):], balanceBytes[:]) values[BasicDataLeafKey] = basicData[:] diff --git a/trie/bintrie/trie_test.go b/trie/bintrie/trie_test.go index 8b7d9e46d6..8a38541fae 100644 --- a/trie/bintrie/trie_test.go +++ b/trie/bintrie/trie_test.go @@ -271,6 +271,28 @@ func makeAccount(nonce uint64, balance uint64, codeHash common.Hash) *types.Stat } } +func TestUpdateAccountTruncatesBalanceToLow128Bits(t *testing.T) { + tr := newEmptyTestTrie(t) + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + balanceBytes := common.Hex2Bytes("0102030405060708090a0b0c0d0e0f1011") + account := &types.StateAccount{ + Balance: new(uint256.Int).SetBytes(balanceBytes), + CodeHash: types.EmptyCodeHash[:], + } + + if err := tr.UpdateAccount(addr, account, 0); err != nil { + t.Fatalf("UpdateAccount: %v", err) + } + got, err := tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount: %v", err) + } + want := new(uint256.Int).SetBytes(balanceBytes[1:]) + if got.Balance.Cmp(want) != 0 { + t.Fatalf("Balance: got %s, want %s", got.Balance, want) + } +} + // TestDeleteAccountRoundTrip verifies the basic delete path: create an // account, read it back, delete it, confirm subsequent reads return nil. // Regression test for the no-op DeleteAccount bug where the deletion was