mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 10:50:44 +00:00
trie/bintrie: preserve low balance bytes
This commit is contained in:
parent
111e7b8b48
commit
1ce418ac74
2 changed files with 23 additions and 1 deletions
|
|
@ -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
|
// TODO: reduce the size of the allocation in devmode, then panic instead
|
||||||
// of truncating.
|
// of truncating.
|
||||||
if len(balanceBytes) > 16 {
|
if len(balanceBytes) > 16 {
|
||||||
balanceBytes = balanceBytes[16:]
|
balanceBytes = balanceBytes[len(balanceBytes)-16:]
|
||||||
}
|
}
|
||||||
copy(basicData[HashSize-len(balanceBytes):], balanceBytes[:])
|
copy(basicData[HashSize-len(balanceBytes):], balanceBytes[:])
|
||||||
values[BasicDataLeafKey] = basicData[:]
|
values[BasicDataLeafKey] = basicData[:]
|
||||||
|
|
|
||||||
|
|
@ -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
|
// TestDeleteAccountRoundTrip verifies the basic delete path: create an
|
||||||
// account, read it back, delete it, confirm subsequent reads return nil.
|
// account, read it back, delete it, confirm subsequent reads return nil.
|
||||||
// Regression test for the no-op DeleteAccount bug where the deletion was
|
// Regression test for the no-op DeleteAccount bug where the deletion was
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue