From b275c3b46f86ec1f53ea2fc4e179d8a3ba91d7ac Mon Sep 17 00:00:00 2001 From: Petr Hanzl Date: Tue, 3 Dec 2024 14:06:10 +0100 Subject: [PATCH] Use data from witness proof. --- ethapi/api.go | 21 +++++++++++++++++---- ethapi/api_test.go | 29 +++++++++++++++-------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/ethapi/api.go b/ethapi/api.go index 847a5cd62..73c0fc4f0 100644 --- a/ethapi/api.go +++ b/ethapi/api.go @@ -728,17 +728,30 @@ func (s *PublicBlockChainAPI) GetAccount(ctx context.Context, address common.Add if err != nil { return nil, err } + defer state.Release() proof, err := state.GetProof(address, nil) if err != nil { return nil, err } + codeHash, _, err := proof.GetCodeHash(cc.Hash(header.Root), cc.Address(address)) + if err != nil { + return nil, err + } _, storageRoot, _ := proof.GetAccountElements(cc.Hash(header.Root), cc.Address(address)) - defer state.Release() + balance, _, err := proof.GetBalance(cc.Hash(header.Root), cc.Address(address)) + if err != nil { + return nil, err + } + nonce, _, err := proof.GetNonce(cc.Hash(header.Root), cc.Address(address)) + if err != nil { + return nil, err + } + u256Balance := balance.Uint256() return &GetAccountResult{ - CodeHash: state.GetCodeHash(address), + CodeHash: common.Hash(codeHash), StorageRoot: common.Hash(storageRoot), - Balance: (*hexutil.U256)(state.GetBalance(address)), - Nonce: hexutil.Uint64(state.GetNonce(address)), + Balance: (*hexutil.U256)(&u256Balance), + Nonce: hexutil.Uint64(nonce.ToUint64()), }, state.Error() } diff --git a/ethapi/api_test.go b/ethapi/api_test.go index 20665f1b2..c1c814656 100644 --- a/ethapi/api_test.go +++ b/ethapi/api_test.go @@ -3,10 +3,10 @@ package ethapi import ( "context" cc "github.com/Fantom-foundation/Carmen/go/common" + "github.com/Fantom-foundation/Carmen/go/common/amount" "github.com/Fantom-foundation/Carmen/go/common/witness" "github.com/Fantom-foundation/go-opera/inter/state" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/holiman/uint256" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" "math/big" @@ -62,11 +62,11 @@ func TestAPI_GetAccount(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - addr := common.Address{1} - codeHash := common.Hash{2} + addr := cc.Address{1} + codeHash := cc.Hash{2} storageRoot := cc.Hash{3} - balance := uint256.NewInt(4) - nonce := uint64(5) + balance := amount.New(4) + nonce := cc.ToNonce(5) headerRoot := common.Hash{123} mockBackend := NewMockBackend(ctrl) @@ -77,23 +77,24 @@ func TestAPI_GetAccount(t *testing.T) { blkNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) mockBackend.EXPECT().StateAndHeaderByNumberOrHash(gomock.Any(), blkNr).Return(mockState, mockHeader, nil) - mockState.EXPECT().GetProof(addr, nil).Return(mockProof, nil) - mockProof.EXPECT().GetAccountElements(cc.Hash(headerRoot), cc.Address(addr)).Return(nil, storageRoot, true) - mockState.EXPECT().GetCodeHash(addr).Return(codeHash) - mockState.EXPECT().GetBalance(addr).Return(balance) - mockState.EXPECT().GetNonce(addr).Return(nonce) + mockState.EXPECT().GetProof(common.Address(addr), nil).Return(mockProof, nil) + mockProof.EXPECT().GetCodeHash(cc.Hash(headerRoot), addr).Return(codeHash, true, nil) + mockProof.EXPECT().GetAccountElements(cc.Hash(headerRoot), addr).Return(nil, storageRoot, true) + mockProof.EXPECT().GetBalance(cc.Hash(headerRoot), addr).Return(balance, true, nil) + mockProof.EXPECT().GetNonce(cc.Hash(headerRoot), addr).Return(nonce, true, nil) mockState.EXPECT().Error().Return(nil) mockState.EXPECT().Release() api := NewPublicBlockChainAPI(mockBackend) - account, err := api.GetAccount(context.Background(), addr, blkNr) + account, err := api.GetAccount(context.Background(), common.Address(addr), blkNr) require.NoError(t, err, "failed to get account") - require.Equal(t, codeHash, account.CodeHash) + u256Balance := balance.Uint256() + require.Equal(t, common.Hash(codeHash), account.CodeHash) require.Equal(t, common.Hash(storageRoot), account.StorageRoot) - require.Equal(t, (*hexutil.U256)(balance), account.Balance) - require.Equal(t, hexutil.Uint64(nonce), account.Nonce) + require.Equal(t, (*hexutil.U256)(&u256Balance), account.Balance) + require.Equal(t, hexutil.Uint64(nonce.ToUint64()), account.Nonce) } func testGetBlockReceipts(t *testing.T, blockParam rpc.BlockNumberOrHash) ([]map[string]interface{}, error) {