Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding address field for AccountInfo #703

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion state/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type Facade interface {
BlockHash(height uint32) hash.Hash
BlockHeight(hash hash.Hash) uint32
AccountByAddress(addr crypto.Address) *account.Account
AccountByNumber(number int32) *account.Account
ValidatorByAddress(addr crypto.Address) *validator.Validator
ValidatorByNumber(number int32) *validator.Validator
ValidatorAddresses() []crypto.Address
Expand Down
8 changes: 0 additions & 8 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,6 @@ func (st *state) AccountByAddress(addr crypto.Address) *account.Account {
return acc
}

func (st *state) AccountByNumber(number int32) *account.Account {
acc, err := st.store.AccountByNumber(number)
if err != nil {
st.logger.Trace("error on retrieving account", "error", err)
}
return acc
}

func (st *state) ValidatorAddresses() []crypto.Address {
return st.store.ValidatorAddresses()
}
Expand Down
12 changes: 0 additions & 12 deletions store/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

type accountStore struct {
db *leveldb.DB
numberMap map[int32]*account.Account
addressMap map[crypto.Address]*account.Account
total int32
}
Expand Down Expand Up @@ -44,7 +43,6 @@ func newAccountStore(db *leveldb.DB) *accountStore {
return &accountStore{
db: db,
total: total,
numberMap: numberMap,
addressMap: addressMap,
}
}
Expand All @@ -63,15 +61,6 @@ func (as *accountStore) account(addr crypto.Address) (*account.Account, error) {
return nil, ErrNotFound
}

func (as *accountStore) accountByNumber(number int32) (*account.Account, error) {
acc, ok := as.numberMap[number]
kehiy marked this conversation as resolved.
Show resolved Hide resolved
if ok {
return acc.Clone(), nil
}

return nil, ErrNotFound
}

func (as *accountStore) iterateAccounts(consumer func(crypto.Address, *account.Account) (stop bool)) {
for addr, acc := range as.addressMap {
stopped := consumer(addr, acc.Clone())
Expand All @@ -92,7 +81,6 @@ func (as *accountStore) updateAccount(batch *leveldb.Batch, addr crypto.Address,
if !as.hasAccount(addr) {
as.total++
}
as.numberMap[acc.Number()] = acc
as.addressMap[addr] = acc

batch.Put(accountKey(addr), data)
Expand Down
62 changes: 3 additions & 59 deletions store/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ func TestAccountCounter(t *testing.T) {
acc1, err := td.store.Account(signer.Address())
assert.NoError(t, err)

acc2, err := td.store.AccountByNumber(num)
assert.NoError(t, err)

assert.Equal(t, acc1.Hash(), acc2.Hash())
assert.Equal(t, acc1.Hash(), acc.Hash())
assert.Equal(t, td.store.TotalAccounts(), int32(1))
assert.True(t, td.store.HasAccount(signer.Address()))
})
Expand All @@ -65,55 +62,6 @@ func TestAccountBatchSaving(t *testing.T) {
})
}

func TestAccountByNumber(t *testing.T) {
td := setup(t)

total := td.RandInt32NonZero(100)
t.Run("Add some accounts", func(t *testing.T) {
for i := int32(0); i < total; i++ {
acc, signer := td.GenerateTestAccount(i)
td.store.UpdateAccount(signer.Address(), acc)
}
assert.NoError(t, td.store.WriteBatch())
assert.Equal(t, td.store.TotalAccounts(), total)
})

t.Run("Get a random account", func(t *testing.T) {
num := td.RandInt32(total)
acc, err := td.store.AccountByNumber(num)
assert.NoError(t, err)
require.NotNil(t, acc)
assert.Equal(t, acc.Number(), num)
})

t.Run("negative number", func(t *testing.T) {
acc, err := td.store.AccountByNumber(-1)
assert.Error(t, err)
assert.Nil(t, acc)
})

t.Run("Non existing account", func(t *testing.T) {
acc, err := td.store.AccountByNumber(total + 1)
assert.Error(t, err)
assert.Nil(t, acc)
})

t.Run("Reopen the store", func(t *testing.T) {
td.store.Close()
store, _ := NewStore(td.store.config, 21)

num := td.RandInt32(total)
acc, err := store.AccountByNumber(num)
assert.NoError(t, err)
require.NotNil(t, acc)
assert.Equal(t, acc.Number(), num)

acc, err = td.store.AccountByNumber(total + 1)
assert.Error(t, err)
assert.Nil(t, acc)
})
}

func TestAccountByAddress(t *testing.T) {
td := setup(t)

Expand Down Expand Up @@ -190,11 +138,7 @@ func TestAccountDeepCopy(t *testing.T) {
acc1, signer := td.GenerateTestAccount(num)
td.store.UpdateAccount(signer.Address(), acc1)

acc2, _ := td.store.AccountByNumber(num)
acc2, _ := td.store.Account(signer.Address())
acc2.AddToBalance(1)
assert.NotEqual(t, td.store.accountStore.numberMap[num].Hash(), acc2.Hash())

acc3, _ := td.store.Account(signer.Address())
acc3.AddToBalance(1)
assert.NotEqual(t, td.store.accountStore.numberMap[num].Hash(), acc3.Hash())
assert.NotEqual(t, td.store.accountStore.addressMap[signer.Address()].Hash(), acc2.Hash())
}
1 change: 0 additions & 1 deletion store/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ type Reader interface {
PublicKey(addr crypto.Address) (*bls.PublicKey, error)
HasAccount(crypto.Address) bool
Account(addr crypto.Address) (*account.Account, error)
AccountByNumber(number int32) (*account.Account, error)
TotalAccounts() int32
HasValidator(addr crypto.Address) bool
ValidatorAddresses() []crypto.Address
Expand Down
7 changes: 0 additions & 7 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,6 @@ func (s *store) Account(addr crypto.Address) (*account.Account, error) {
return s.accountStore.account(addr)
}

func (s *store) AccountByNumber(number int32) (*account.Account, error) {
s.lk.RLock()
defer s.lk.RUnlock()

return s.accountStore.accountByNumber(number)
}

func (s *store) TotalAccounts() int32 {
s.lk.Lock()
defer s.lk.Unlock()
Expand Down
6 changes: 0 additions & 6 deletions wallet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ func (s *blockchainServer) GetValidatorAddresses(_ context.Context,
return &pactus.GetValidatorAddressesResponse{}, nil
}

func (s *blockchainServer) GetAccountByNumber(_ context.Context,
_ *pactus.GetAccountByNumberRequest,
) (*pactus.GetAccountResponse, error) {
return &pactus.GetAccountResponse{}, nil
}

func (s *blockchainServer) GetValidatorByNumber(_ context.Context,
_ *pactus.GetValidatorByNumberRequest,
) (*pactus.GetValidatorResponse, error) {
Expand Down
18 changes: 3 additions & 15 deletions www/grpc/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,12 @@ func (s *blockchainServer) GetAccount(_ context.Context,
return nil, status.Errorf(codes.NotFound, "account not found")
}
res := &pactus.GetAccountResponse{
Account: accountToProto(acc),
Account: accountToProto(addr, acc),
}

return res, nil
}

func (s *blockchainServer) GetAccountByNumber(_ context.Context,
req *pactus.GetAccountByNumberRequest,
) (*pactus.GetAccountResponse, error) {
acc := s.state.AccountByNumber(req.Number)
if acc == nil {
return nil, status.Errorf(codes.InvalidArgument, "account not found")
}

return &pactus.GetAccountResponse{
Account: accountToProto(acc),
}, nil
}

func (s *blockchainServer) GetValidatorByNumber(_ context.Context,
req *pactus.GetValidatorByNumberRequest,
) (*pactus.GetValidatorResponse, error) {
Expand Down Expand Up @@ -250,13 +237,14 @@ func validatorToProto(val *validator.Validator) *pactus.ValidatorInfo {
}
}

func accountToProto(acc *account.Account) *pactus.AccountInfo {
func accountToProto(addr crypto.Address, acc *account.Account) *pactus.AccountInfo {
data, _ := acc.Bytes()
return &pactus.AccountInfo{
Hash: acc.Hash().Bytes(),
Data: data,
Number: acc.Number(),
Balance: acc.Balance(),
Address: addr.String(),
}
}

Expand Down
34 changes: 0 additions & 34 deletions www/grpc/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,40 +181,6 @@ func TestGetAccount(t *testing.T) {
assert.Nil(t, conn.Close(), "Error closing connection")
}

func TestGetAccountByNumber(t *testing.T) {
ts := testsuite.NewTestSuite(t)

conn, client := testBlockchainClient(t)
acc, _ := tMockState.TestStore.AddTestAccount()

t.Run("Should return nil value due to invalid number ", func(t *testing.T) {
res, err := client.GetAccountByNumber(tCtx,
&pactus.GetAccountByNumberRequest{Number: -1})

assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("Should return nil for non existing account ", func(t *testing.T) {
res, err := client.GetAccountByNumber(tCtx,
&pactus.GetAccountByNumberRequest{Number: ts.RandInt32(1000)})

assert.Error(t, err)
assert.Nil(t, res)
})

t.Run("Should return account details", func(t *testing.T) {
res, err := client.GetAccountByNumber(tCtx,
&pactus.GetAccountByNumberRequest{Number: acc.Number()})

assert.Nil(t, err)
assert.NotNil(t, res)
assert.Equal(t, res.Account.Balance, acc.Balance())
assert.Equal(t, res.Account.Number, acc.Number())
})
assert.Nil(t, conn.Close(), "Error closing connection")
}

func TestGetValidator(t *testing.T) {
ts := testsuite.NewTestSuite(t)

Expand Down
65 changes: 14 additions & 51 deletions www/grpc/gen/dart/blockchain.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,53 +64,6 @@ class GetAccountRequest extends $pb.GeneratedMessage {
void clearAddress() => clearField(1);
}

class GetAccountByNumberRequest extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'GetAccountByNumberRequest', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
..a<$core.int>(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'number', $pb.PbFieldType.O3)
..hasRequiredFields = false
;

GetAccountByNumberRequest._() : super();
factory GetAccountByNumberRequest({
$core.int? number,
}) {
final _result = create();
if (number != null) {
_result.number = number;
}
return _result;
}
factory GetAccountByNumberRequest.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory GetAccountByNumberRequest.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
GetAccountByNumberRequest clone() => GetAccountByNumberRequest()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
GetAccountByNumberRequest copyWith(void Function(GetAccountByNumberRequest) updates) => super.copyWith((message) => updates(message as GetAccountByNumberRequest)) as GetAccountByNumberRequest; // ignore: deprecated_member_use
$pb.BuilderInfo get info_ => _i;
@$core.pragma('dart2js:noInline')
static GetAccountByNumberRequest create() => GetAccountByNumberRequest._();
GetAccountByNumberRequest createEmptyInstance() => create();
static $pb.PbList<GetAccountByNumberRequest> createRepeated() => $pb.PbList<GetAccountByNumberRequest>();
@$core.pragma('dart2js:noInline')
static GetAccountByNumberRequest getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<GetAccountByNumberRequest>(create);
static GetAccountByNumberRequest? _defaultInstance;

@$pb.TagNumber(1)
$core.int get number => $_getIZ(0);
@$pb.TagNumber(1)
set number($core.int v) { $_setSignedInt32(0, v); }
@$pb.TagNumber(1)
$core.bool hasNumber() => $_has(0);
@$pb.TagNumber(1)
void clearNumber() => clearField(1);
}

class GetAccountResponse extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = $pb.BuilderInfo(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'GetAccountResponse', package: const $pb.PackageName(const $core.bool.fromEnvironment('protobuf.omit_message_names') ? '' : 'pactus'), createEmptyInstance: create)
..aOM<AccountInfo>(1, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'account', subBuilder: AccountInfo.create)
Expand Down Expand Up @@ -1140,6 +1093,7 @@ class AccountInfo extends $pb.GeneratedMessage {
..a<$core.List<$core.int>>(2, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'data', $pb.PbFieldType.OY)
..a<$core.int>(3, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'number', $pb.PbFieldType.O3)
..aInt64(4, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'balance')
..aOS(5, const $core.bool.fromEnvironment('protobuf.omit_field_names') ? '' : 'address')
..hasRequiredFields = false
;

Expand All @@ -1149,6 +1103,7 @@ class AccountInfo extends $pb.GeneratedMessage {
$core.List<$core.int>? data,
$core.int? number,
$fixnum.Int64? balance,
$core.String? address,
}) {
final _result = create();
if (hash != null) {
Expand All @@ -1163,6 +1118,9 @@ class AccountInfo extends $pb.GeneratedMessage {
if (balance != null) {
_result.balance = balance;
}
if (address != null) {
_result.address = address;
}
return _result;
}
factory AccountInfo.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
Expand Down Expand Up @@ -1221,6 +1179,15 @@ class AccountInfo extends $pb.GeneratedMessage {
$core.bool hasBalance() => $_has(3);
@$pb.TagNumber(4)
void clearBalance() => clearField(4);

@$pb.TagNumber(5)
$core.String get address => $_getSZ(4);
@$pb.TagNumber(5)
set address($core.String v) { $_setString(4, v); }
@$pb.TagNumber(5)
$core.bool hasAddress() => $_has(4);
@$pb.TagNumber(5)
void clearAddress() => clearField(5);
}

class BlockHeaderInfo extends $pb.GeneratedMessage {
Expand Down Expand Up @@ -1631,10 +1598,6 @@ class BlockchainApi {
var emptyResponse = GetAccountResponse();
return _client.invoke<GetAccountResponse>(ctx, 'Blockchain', 'GetAccount', request, emptyResponse);
}
$async.Future<GetAccountResponse> getAccountByNumber($pb.ClientContext? ctx, GetAccountByNumberRequest request) {
var emptyResponse = GetAccountResponse();
return _client.invoke<GetAccountResponse>(ctx, 'Blockchain', 'GetAccountByNumber', request, emptyResponse);
}
$async.Future<GetValidatorResponse> getValidator($pb.ClientContext? ctx, GetValidatorRequest request) {
var emptyResponse = GetValidatorResponse();
return _client.invoke<GetValidatorResponse>(ctx, 'Blockchain', 'GetValidator', request, emptyResponse);
Expand Down
Loading
Loading