Skip to content

Commit 9421bd3

Browse files
Merge pull request #6201 from onflow/janez/change-key-index-to-uint32
[FVM] Changed key index type to uint32
2 parents 08e28b9 + ae2c75a commit 9421bd3

File tree

84 files changed

+683
-363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+683
-363
lines changed

access/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ type API interface {
4444
GetAccountBalanceAtLatestBlock(ctx context.Context, address flow.Address) (uint64, error)
4545
GetAccountBalanceAtBlockHeight(ctx context.Context, address flow.Address, height uint64) (uint64, error)
4646

47-
GetAccountKeyAtLatestBlock(ctx context.Context, address flow.Address, keyIndex uint64) (*flow.AccountPublicKey, error)
48-
GetAccountKeyAtBlockHeight(ctx context.Context, address flow.Address, keyIndex uint64, height uint64) (*flow.AccountPublicKey, error)
47+
GetAccountKeyAtLatestBlock(ctx context.Context, address flow.Address, keyIndex uint32) (*flow.AccountPublicKey, error)
48+
GetAccountKeyAtBlockHeight(ctx context.Context, address flow.Address, keyIndex uint32, height uint64) (*flow.AccountPublicKey, error)
4949
GetAccountKeysAtLatestBlock(ctx context.Context, address flow.Address) ([]flow.AccountPublicKey, error)
5050
GetAccountKeysAtBlockHeight(ctx context.Context, address flow.Address, height uint64) ([]flow.AccountPublicKey, error)
5151

access/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (e InvalidAddressError) Error() string {
6464
// DuplicatedSignatureError indicates that two signatures havs been provided for a key (combination of account and key index)
6565
type DuplicatedSignatureError struct {
6666
Address flow.Address
67-
KeyIndex uint64
67+
KeyIndex uint32
6868
}
6969

7070
func (e DuplicatedSignatureError) Error() string {

access/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ func (h *Handler) GetAccountKeyAtLatestBlock(
661661
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %v", err)
662662
}
663663

664-
keyByIndex, err := h.api.GetAccountKeyAtLatestBlock(ctx, address, uint64(req.GetIndex()))
664+
keyByIndex, err := h.api.GetAccountKeyAtLatestBlock(ctx, address, req.GetIndex())
665665
if err != nil {
666666
return nil, err
667667
}
@@ -739,7 +739,7 @@ func (h *Handler) GetAccountKeyAtBlockHeight(
739739
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %v", err)
740740
}
741741

742-
keyByIndex, err := h.api.GetAccountKeyAtBlockHeight(ctx, address, uint64(req.GetIndex()), req.GetBlockHeight())
742+
keyByIndex, err := h.api.GetAccountKeyAtBlockHeight(ctx, address, req.GetIndex(), req.GetBlockHeight())
743743
if err != nil {
744744
return nil, err
745745
}

access/legacy/convert/convert.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func MessageToTransaction(m *entitiesproto.Transaction, chain flow.Chain) (flow.
3030
if err != nil {
3131
return *t, err
3232
}
33-
t.SetProposalKey(proposalAddress, uint64(proposalKey.GetKeyId()), proposalKey.GetSequenceNumber())
33+
t.SetProposalKey(proposalAddress, proposalKey.GetKeyId(), proposalKey.GetSequenceNumber())
3434
}
3535

3636
payer := m.GetPayer()
@@ -55,15 +55,15 @@ func MessageToTransaction(m *entitiesproto.Transaction, chain flow.Chain) (flow.
5555
if err != nil {
5656
return *t, err
5757
}
58-
t.AddPayloadSignature(addr, uint64(sig.GetKeyId()), sig.GetSignature())
58+
t.AddPayloadSignature(addr, sig.GetKeyId(), sig.GetSignature())
5959
}
6060

6161
for _, sig := range m.GetEnvelopeSignatures() {
6262
addr, err := convert.Address(sig.GetAddress(), chain)
6363
if err != nil {
6464
return *t, err
6565
}
66-
t.AddEnvelopeSignature(addr, uint64(sig.GetKeyId()), sig.GetSignature())
66+
t.AddEnvelopeSignature(addr, sig.GetKeyId(), sig.GetSignature())
6767
}
6868

6969
t.SetScript(m.GetScript())

access/mock/api.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

access/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (v *TransactionValidator) checkAddresses(tx *flow.TransactionBody) error {
303303
func (v *TransactionValidator) checkSignatureDuplications(tx *flow.TransactionBody) error {
304304
type uniqueKey struct {
305305
address flow.Address
306-
index uint64
306+
index uint32
307307
}
308308
observedSigs := make(map[uniqueKey]bool)
309309
for _, sig := range append(tx.PayloadSignatures, tx.EnvelopeSignatures...) {

cmd/bootstrap/cmd/check_machine_account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func checkMachineAccountRun(_ *cobra.Command, _ []string) {
6363
log.Debug().
6464
Str("machine_account_address", machineAccountInfo.Address).
6565
Str("machine_account_pub_key", fmt.Sprintf("%x", encodedRuntimeAccountPubKey(machineAccountPrivKey))).
66-
Uint("key_index", machineAccountInfo.KeyIndex).
66+
Uint32("key_index", machineAccountInfo.KeyIndex).
6767
Str("signing_algo", machineAccountInfo.SigningAlgorithm.String()).
6868
Str("hash_algo", machineAccountInfo.HashAlgorithm.String()).
6969
Msg("read machine account info from disk")

cmd/util/cmd/exec-data-json-export/transaction_exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type transactionInContext struct {
2828
GasLimit uint64 `json:"gas_limit"`
2929
PayerAddressHex string `json:"payer_address_hex"`
3030
ProposalKeyAddressHex string `json:"proposal_key_address_hex"`
31-
ProposalKeyID uint64 `json:"proposal_key_id"`
31+
ProposalKeyID uint32 `json:"proposal_key_id"`
3232
ProposalSequenceNumber uint64 `json:"proposal_sequence_number"`
3333
AuthorizersAddressHex []string `json:"authorizers_address_hex"`
3434
EnvelopeSize int `json:"envelope_size"`

cmd/util/cmd/execution-state-extract/execution_state_extract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func newMigrations(
480480
log,
481481
opts.NWorker,
482482
[]migrators.AccountBasedMigration{
483-
&migrators.AccountUsageMigration{},
483+
migrators.NewAccountUsageMigration(rwf),
484484
},
485485
),
486486
},

cmd/util/ledger/migrations/fix_broken_data_migration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func TestFixSlabsWithBrokenReferences(t *testing.T) {
119119
accountStatusRegisterID := mustDecodeHex("612e73")
120120
updatedAccountStatusRegister := ledger.NewPayload(
121121
ledger.NewKey([]ledger.KeyPart{ownerKey, {Type: 2, Value: accountStatusRegisterID}}),
122-
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0xcc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
122+
[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0xc8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
123123
)
124124

125125
expectedNewPayloads := make([]*ledger.Payload, len(oldPayloads))

cmd/util/ledger/migrations/storage_used_migration.go

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/onflow/flow-go/cmd/util/ledger/reporters"
8+
79
"github.com/onflow/cadence/runtime/common"
810
"github.com/rs/zerolog"
911
"github.com/rs/zerolog/log"
@@ -14,13 +16,21 @@ import (
1416
)
1517

1618
// AccountUsageMigration iterates through each payload, and calculate the storage usage
17-
// and update the accounts status with the updated storage usage
19+
// and update the accounts status with the updated storage usage. It also upgrades the
20+
// account status registers to the latest version.
1821
type AccountUsageMigration struct {
1922
log zerolog.Logger
23+
rw reporters.ReportWriter
2024
}
2125

2226
var _ AccountBasedMigration = &AccountUsageMigration{}
2327

28+
func NewAccountUsageMigration(rw reporters.ReportWriterFactory) *AccountUsageMigration {
29+
return &AccountUsageMigration{
30+
rw: rw.ReportWriter("account-usage-migration"),
31+
}
32+
}
33+
2434
func (m *AccountUsageMigration) InitMigration(
2535
log zerolog.Logger,
2636
_ *registers.ByAccount,
@@ -30,8 +40,6 @@ func (m *AccountUsageMigration) InitMigration(
3040
return nil
3141
}
3242

33-
const oldAccountStatusSize = 25
34-
3543
func (m *AccountUsageMigration) Close() error {
3644
return nil
3745
}
@@ -44,7 +52,7 @@ func (m *AccountUsageMigration) MigrateAccount(
4452

4553
var status *environment.AccountStatus
4654
var statusValue []byte
47-
actualSize := uint64(0)
55+
actualUsed := uint64(0)
4856

4957
// Find the account status register,
5058
// and calculate the storage usage
@@ -60,7 +68,7 @@ func (m *AccountUsageMigration) MigrateAccount(
6068
}
6169
}
6270

63-
actualSize += uint64(environment.RegisterSize(
71+
actualUsed += uint64(environment.RegisterSize(
6472
flow.RegisterID{
6573
Owner: owner,
6674
Key: key,
@@ -82,58 +90,57 @@ func (m *AccountUsageMigration) MigrateAccount(
8290
log.Error().
8391
Str("account", address.HexWithPrefix()).
8492
Msgf("could not find account status register")
85-
return nil
93+
return fmt.Errorf("could not find account status register")
8694
}
8795

88-
isOldVersionOfStatusRegister := len(statusValue) == oldAccountStatusSize
96+
// reading the status will upgrade the status to the latest version, so it might
97+
// have a different size than the one in the register
98+
newStatusValue := status.ToBytes()
99+
statusSizeDiff := len(newStatusValue) - len(statusValue)
100+
101+
// the status size diff should be added to the actual size
102+
if statusSizeDiff < 0 {
103+
if uint64(-statusSizeDiff) > actualUsed {
104+
log.Error().
105+
Str("account", address.HexWithPrefix()).
106+
Msgf("account storage used would be negative")
107+
return fmt.Errorf("account storage used would be negative")
108+
}
89109

90-
same := m.compareUsage(isOldVersionOfStatusRegister, status, actualSize, address)
91-
if same {
92-
// there is no problem with the usage, return
93-
return nil
110+
actualUsed = actualUsed - uint64(-statusSizeDiff)
111+
} else if statusSizeDiff > 0 {
112+
actualUsed = actualUsed + uint64(statusSizeDiff)
94113
}
95114

96-
if isOldVersionOfStatusRegister {
97-
// size will grow by 8 bytes because of the on-the-fly
98-
// migration of account status in AccountStatusFromBytes
99-
actualSize += 8
100-
}
115+
currentUsed := status.StorageUsed()
101116

102-
// update storage used
103-
status.SetStorageUsed(actualSize)
117+
// update storage used if the actual size is different from the size in the status register
118+
// or if the status size is different.
119+
if actualUsed != currentUsed || statusSizeDiff != 0 {
120+
// update storage used
121+
status.SetStorageUsed(actualUsed)
104122

105-
err = accountRegisters.Set(
106-
string(address[:]),
107-
flow.AccountStatusKey,
108-
status.ToBytes(),
109-
)
110-
if err != nil {
111-
return fmt.Errorf("could not update account status register: %w", err)
123+
err = accountRegisters.Set(
124+
string(address[:]),
125+
flow.AccountStatusKey,
126+
status.ToBytes(),
127+
)
128+
if err != nil {
129+
return fmt.Errorf("could not update account status register: %w", err)
130+
}
131+
132+
m.rw.Write(accountUsageMigrationReportData{
133+
AccountAddress: address,
134+
OldStorageUsed: currentUsed,
135+
NewStorageUsed: actualUsed,
136+
})
112137
}
113138

114139
return nil
115140
}
116141

117-
func (m *AccountUsageMigration) compareUsage(
118-
isOldVersionOfStatusRegister bool,
119-
status *environment.AccountStatus,
120-
actualSize uint64,
121-
address common.Address,
122-
) bool {
123-
oldSize := status.StorageUsed()
124-
if isOldVersionOfStatusRegister {
125-
// size will be reported as 8 bytes larger than the actual size due to on-the-fly
126-
// migration of account status in AccountStatusFromBytes
127-
oldSize -= 8
128-
}
129-
130-
if oldSize != actualSize {
131-
m.log.Warn().
132-
Uint64("old_size", oldSize).
133-
Uint64("new_size", actualSize).
134-
Str("account", address.HexWithPrefix()).
135-
Msg("account storage used usage mismatch")
136-
return false
137-
}
138-
return true
142+
type accountUsageMigrationReportData struct {
143+
AccountAddress common.Address
144+
OldStorageUsed uint64
145+
NewStorageUsed uint64
139146
}

0 commit comments

Comments
 (0)