Skip to content

Commit

Permalink
Use data from witness proof.
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador committed Dec 3, 2024
1 parent 74b6021 commit b275c3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
21 changes: 17 additions & 4 deletions ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
29 changes: 15 additions & 14 deletions ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit b275c3b

Please sign in to comment.