diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index 32afa9957945..76caca0fbc7b 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -616,7 +616,7 @@ func genVktFromAlloc(alloc core.GenesisAlloc) (*trie.VerkleTrie, error) { CodeHash: crypto.Keccak256Hash(acc.Code).Bytes(), Root: common.Hash{}, } - err := vkt.UpdateAccount(addr, account) + err := vkt.UpdateAccount(addr, account, len(acc.Code)) if err != nil { return nil, fmt.Errorf("error inserting account: %w", err) } diff --git a/core/overlay/conversion.go b/core/overlay/conversion.go index 06f3e84617dc..d98c081eec76 100644 --- a/core/overlay/conversion.go +++ b/core/overlay/conversion.go @@ -96,28 +96,23 @@ func (kvm *keyValueMigrator) addAccount(addr []byte, acc *types.StateAccount) { leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex)) var basicData [verkle.LeafValueSize]byte - basicData[utils.BasicDataVersionOffset] = 0 // version + basicData[utils.BasicDataVersionOffset] = 0 + binary.BigEndian.PutUint64(basicData[utils.BasicDataNonceOffset:], acc.Nonce) // get the lower 16 bytes of water and change its endianness balanceBytes := acc.Balance.Bytes() - for i := 0; i < 16 && i < len(balanceBytes); i++ { - basicData[utils.BasicDataBalanceOffset+i] = balanceBytes[len(balanceBytes)-1-i] - } - binary.LittleEndian.PutUint64(basicData[utils.BasicDataNonceOffset:], acc.Nonce) + copy(basicData[32-len(balanceBytes):], balanceBytes[:]) leafNodeData.Values[utils.BasicDataLeafKey] = basicData[:] leafNodeData.Values[utils.CodeHashLeafKey] = acc.CodeHash[:] } // addAccountCode needs to be called AFTER addAccount, as it will reuse the leaf -// that was created in there. func (kvm *keyValueMigrator) addAccountCode(addr []byte, codeSize uint64, chunks []byte) { leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex)) // Save the code size. - var cs [8]byte - binary.LittleEndian.PutUint64(cs[:], codeSize) - copy(leafNodeData.Values[utils.BasicDataLeafKey][utils.BasicDataCodeSizeOffset:utils.BasicDataNonceOffset], cs[:3]) + binary.BigEndian.PutUint32(leafNodeData.Values[utils.BasicDataLeafKey][utils.BasicDataCodeSizeOffset-1:utils.BasicDataNonceOffset], uint32(codeSize)) // The first 128 chunks are stored in the account header leaf. for i := 0; i < 128 && i < len(chunks)/32; i++ { diff --git a/trie/verkle.go b/trie/verkle.go index 9737ae4a0f64..8b8625c0cda3 100644 --- a/trie/verkle.go +++ b/trie/verkle.go @@ -129,7 +129,7 @@ func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error } if len(values[utils.BasicDataLeafKey]) > 0 { - acc.Nonce = binary.LittleEndian.Uint64(values[utils.BasicDataLeafKey][utils.BasicDataNonceOffset:]) + acc.Nonce = binary.BigEndian.Uint64(values[utils.BasicDataLeafKey][utils.BasicDataNonceOffset:]) } // if the account has been deleted, then values[10] will be 0 and not nil. If it has @@ -142,11 +142,8 @@ func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error return nil, nil } } - var balance [32]byte - copy(balance[16:], values[utils.BasicDataLeafKey][utils.BasicDataBalanceOffset:]) - for i := 0; i < 8; i++ { - balance[31-i], balance[utils.BasicDataBalanceOffset+i] = balance[utils.BasicDataBalanceOffset+i], balance[31-i] - } + var balance [16]byte + copy(balance[:], values[utils.BasicDataLeafKey][utils.BasicDataBalanceOffset:]) acc.Balance = new(big.Int).SetBytes(balance[:]) acc.CodeHash = values[utils.CodeHashLeafKey] @@ -163,16 +160,11 @@ func (t *VerkleTrie) UpdateAccount(addr common.Address, acc *types.StateAccount, stem = t.pointCache.GetTreeKeyBasicDataCached(addr[:]) ) + binary.BigEndian.PutUint32(basicData[utils.BasicDataCodeSizeOffset-1:], uint32(codeLen)) binary.BigEndian.PutUint64(basicData[utils.BasicDataNonceOffset:], acc.Nonce) // get the lower 16 bytes of water and change its endianness balanceBytes := acc.Balance.Bytes() - for i := 0; i < 16 && i < len(balanceBytes); i++ { - basicData[utils.BasicDataBalanceOffset+i] = balanceBytes[i] - } - // var cs [8]byte - // binary.BigEndian.PutUint64(cs[:], uint64(codeLen)) - // copy(basicData[utils.BasicDataCodeSizeOffset:], cs[:3]) - + copy(basicData[32-len(balanceBytes):], balanceBytes[:]) values[utils.BasicDataLeafKey] = basicData[:] values[utils.CodeHashLeafKey] = acc.CodeHash[:] @@ -455,25 +447,6 @@ func (t *VerkleTrie) UpdateContractCode(addr common.Address, codeHash common.Has } values[groupOffset] = chunks[i : i+32] - // Reuse the calculated key to also update the code size. - if i == 0 { - var basicDataKey [32]byte - copy(basicDataKey[:], key[:31]) - basicDataKey[31] = utils.BasicDataLeafKey - // XXX add subfield update api - basicdata, err := t.root.Get(basicDataKey[:], nil) - if err != nil { - return fmt.Errorf("UpdateContractCode (addr=%x) error getting basic data leaf: %w", addr[:], err) - } - if len(basicdata) == 0 { - return fmt.Errorf("UpdateContractCode (addr=%x) error getting non-zero basic data leaf: %w", addr[:], err) - } - cs := make([]byte, 32) - copy(cs[:], basicdata) - binary.BigEndian.PutUint32(cs[utils.BasicDataCodeSizeOffset-1:], uint32(len(code))) - values[utils.BasicDataLeafKey] = cs - } - if groupOffset == 255 || len(chunks)-i <= 32 { err = t.UpdateStem(key[:31], values)