Skip to content

Commit

Permalink
Merge pull request #3465 from onflow/janez/change-account-key-index-t…
Browse files Browse the repository at this point in the history
…o-uint32
  • Loading branch information
turbolent authored Jul 16, 2024
2 parents cc99e72 + 63abfd5 commit 3610da6
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 79 deletions.
256 changes: 211 additions & 45 deletions runtime/account_test.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions runtime/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,11 @@ func (*StandardLibraryHandler) Hash(_ []byte, _ string, _ sema.HashAlgorithm) ([
return nil, goerrors.New("crypto functionality is not available in this environment")
}

func (*StandardLibraryHandler) GetAccountKey(_ common.Address, _ int) (*stdlib.AccountKey, error) {
func (*StandardLibraryHandler) GetAccountKey(_ common.Address, _ uint32) (*stdlib.AccountKey, error) {
return nil, goerrors.New("accounts are not supported in this environment")
}

func (*StandardLibraryHandler) AccountKeysCount(_ common.Address) (uint64, error) {
func (*StandardLibraryHandler) AccountKeysCount(_ common.Address) (uint32, error) {
return 0, goerrors.New("accounts are not supported in this environment")
}

Expand Down Expand Up @@ -354,7 +354,7 @@ func (*StandardLibraryHandler) AddAccountKey(
return nil, goerrors.New("accounts are not available in this environment")
}

func (*StandardLibraryHandler) RevokeAccountKey(_ common.Address, _ int) (*stdlib.AccountKey, error) {
func (*StandardLibraryHandler) RevokeAccountKey(_ common.Address, _ uint32) (*stdlib.AccountKey, error) {
return nil, goerrors.New("accounts are not available in this environment")
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,11 @@ func (e *interpreterEnvironment) GetStorageCapacity(address common.Address) (uin
return e.runtimeInterface.GetStorageCapacity(address)
}

func (e *interpreterEnvironment) GetAccountKey(address common.Address, index int) (*stdlib.AccountKey, error) {
func (e *interpreterEnvironment) GetAccountKey(address common.Address, index uint32) (*stdlib.AccountKey, error) {
return e.runtimeInterface.GetAccountKey(address, index)
}

func (e *interpreterEnvironment) AccountKeysCount(address common.Address) (uint64, error) {
func (e *interpreterEnvironment) AccountKeysCount(address common.Address) (uint32, error) {
return e.runtimeInterface.AccountKeysCount(address)
}

Expand Down Expand Up @@ -395,7 +395,7 @@ func (e *interpreterEnvironment) AddAccountKey(
return e.runtimeInterface.AddAccountKey(address, key, algo, weight)
}

func (e *interpreterEnvironment) RevokeAccountKey(address common.Address, index int) (*stdlib.AccountKey, error) {
func (e *interpreterEnvironment) RevokeAccountKey(address common.Address, index uint32) (*stdlib.AccountKey, error) {
return e.runtimeInterface.RevokeAccountKey(address, index)
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ type Interface interface {
// AddAccountKey appends a key to an account.
AddAccountKey(address Address, publicKey *PublicKey, hashAlgo HashAlgorithm, weight int) (*AccountKey, error)
// GetAccountKey retrieves a key from an account by index.
GetAccountKey(address Address, index int) (*AccountKey, error)
AccountKeysCount(address Address) (uint64, error)
GetAccountKey(address Address, index uint32) (*AccountKey, error)
AccountKeysCount(address Address) (uint32, error)
// RevokeAccountKey removes a key from an account by index.
RevokeAccountKey(address Address, index int) (*AccountKey, error)
RevokeAccountKey(address Address, index uint32) (*AccountKey, error)
// UpdateAccountContractCode updates the code associated with an account contract.
UpdateAccountContractCode(location common.AddressLocation, code []byte) (err error)
// GetAccountContractCode returns the code associated with an account contract.
Expand Down
18 changes: 18 additions & 0 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -4177,6 +4177,24 @@ func (v IntValue) ToInt(locationRange LocationRange) int {
return int(v.BigInt.Int64())
}

func (v IntValue) ToUint32(locationRange LocationRange) uint32 {
if !v.BigInt.IsUint64() {
panic(OverflowError{
LocationRange: locationRange,
})
}

result := v.BigInt.Uint64()

if result > math.MaxUint32 {
panic(OverflowError{
LocationRange: locationRange,
})
}

return uint32(result)
}

func (v IntValue) ByteLength() int {
return common.BigIntByteLength(v.BigInt)
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7938,7 +7938,7 @@ func TestRuntimeErrorExcerpts(t *testing.T) {
OnGetAccountAvailableBalance: noopRuntimeUInt64Getter,
OnGetStorageUsed: noopRuntimeUInt64Getter,
OnGetStorageCapacity: noopRuntimeUInt64Getter,
OnAccountKeysCount: noopRuntimeUInt64Getter,
OnAccountKeysCount: noopRuntimeUInt32Getter,
Storage: NewTestLedger(nil, nil),
}

Expand Down Expand Up @@ -7990,7 +7990,7 @@ func TestRuntimeErrorExcerptsMultiline(t *testing.T) {
OnGetAccountAvailableBalance: noopRuntimeUInt64Getter,
OnGetStorageUsed: noopRuntimeUInt64Getter,
OnGetStorageCapacity: noopRuntimeUInt64Getter,
OnAccountKeysCount: noopRuntimeUInt64Getter,
OnAccountKeysCount: noopRuntimeUInt32Getter,
Storage: NewTestLedger(nil, nil),
}

Expand Down
22 changes: 11 additions & 11 deletions runtime/stdlib/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ func newAccountKeysAddFunction(

type AccountKey struct {
PublicKey *PublicKey
KeyIndex int
KeyIndex uint32
Weight int
HashAlgo sema.HashAlgorithm
IsRevoked bool
Expand All @@ -679,8 +679,8 @@ type AccountKeyProvider interface {
BLSPoPVerifier
Hasher
// GetAccountKey retrieves a key from an account by index.
GetAccountKey(address common.Address, index int) (*AccountKey, error)
AccountKeysCount(address common.Address) (uint64, error)
GetAccountKey(address common.Address, index uint32) (*AccountKey, error)
AccountKeysCount(address common.Address) (uint32, error)
}

func newAccountKeysGetFunction(
Expand All @@ -704,7 +704,7 @@ func newAccountKeysGetFunction(
panic(errors.NewUnreachableError())
}
locationRange := invocation.LocationRange
index := indexValue.ToInt(locationRange)
index := indexValue.ToUint32(locationRange)

var err error
var accountKey *AccountKey
Expand Down Expand Up @@ -788,7 +788,7 @@ func newAccountKeysForEachFunction(
)
}

var count uint64
var count uint32
var err error

errors.WrapPanic(func() {
Expand All @@ -801,9 +801,9 @@ func newAccountKeysForEachFunction(

var accountKey *AccountKey

for index := uint64(0); index < count; index++ {
for index := uint32(0); index < count; index++ {
errors.WrapPanic(func() {
accountKey, err = provider.GetAccountKey(address, int(index))
accountKey, err = provider.GetAccountKey(address, index)
})
if err != nil {
panic(interpreter.WrappedExternalError(err))
Expand Down Expand Up @@ -852,7 +852,7 @@ func newAccountKeysCountGetter(

return func() interpreter.UInt64Value {
return interpreter.NewUInt64Value(gauge, func() uint64 {
var count uint64
var count uint32
var err error

errors.WrapPanic(func() {
Expand All @@ -864,7 +864,7 @@ func newAccountKeysCountGetter(
panic(interpreter.WrappedExternalError(err))
}

return count
return uint64(count)
})
}
}
Expand All @@ -876,7 +876,7 @@ type AccountKeyRevocationHandler interface {
PublicKeySignatureVerifier
BLSPoPVerifier
// RevokeAccountKey removes a key from an account by index.
RevokeAccountKey(address common.Address, index int) (*AccountKey, error)
RevokeAccountKey(address common.Address, index uint32) (*AccountKey, error)
}

func newAccountKeysRevokeFunction(
Expand All @@ -899,7 +899,7 @@ func newAccountKeysRevokeFunction(
panic(errors.NewUnreachableError())
}
locationRange := invocation.LocationRange
index := indexValue.ToInt(locationRange)
index := indexValue.ToUint32(locationRange)

var err error
var accountKey *AccountKey
Expand Down
12 changes: 6 additions & 6 deletions runtime/tests/interpreter/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ type testAccountHandler struct {
)
blsVerifyPOP func(publicKey *stdlib.PublicKey, signature []byte) (bool, error)
hash func(data []byte, tag string, algorithm sema.HashAlgorithm) ([]byte, error)
getAccountKey func(address common.Address, index int) (*stdlib.AccountKey, error)
accountKeysCount func(address common.Address) (uint64, error)
getAccountKey func(address common.Address, index uint32) (*stdlib.AccountKey, error)
accountKeysCount func(address common.Address) (uint32, error)
emitEvent func(
inter *interpreter.Interpreter,
locationRange interpreter.LocationRange,
Expand All @@ -104,7 +104,7 @@ type testAccountHandler struct {
*stdlib.AccountKey,
error,
)
revokeAccountKey func(address common.Address, index int) (*stdlib.AccountKey, error)
revokeAccountKey func(address common.Address, index uint32) (*stdlib.AccountKey, error)
getAccountContractCode func(location common.AddressLocation) ([]byte, error)
parseAndCheckProgram func(
code []byte,
Expand Down Expand Up @@ -225,14 +225,14 @@ func (t *testAccountHandler) Hash(data []byte, tag string, algorithm sema.HashAl
return t.hash(data, tag, algorithm)
}

func (t *testAccountHandler) GetAccountKey(address common.Address, index int) (*stdlib.AccountKey, error) {
func (t *testAccountHandler) GetAccountKey(address common.Address, index uint32) (*stdlib.AccountKey, error) {
if t.getAccountKey == nil {
panic(errors.NewUnexpectedError("unexpected call to GetAccountKey"))
}
return t.getAccountKey(address, index)
}

func (t *testAccountHandler) AccountKeysCount(address common.Address) (uint64, error) {
func (t *testAccountHandler) AccountKeysCount(address common.Address) (uint32, error) {
if t.accountKeysCount == nil {
panic(errors.NewUnexpectedError("unexpected call to AccountKeysCount"))
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func (t *testAccountHandler) AddAccountKey(
)
}

func (t *testAccountHandler) RevokeAccountKey(address common.Address, index int) (*stdlib.AccountKey, error) {
func (t *testAccountHandler) RevokeAccountKey(address common.Address, index uint32) (*stdlib.AccountKey, error) {
if t.revokeAccountKey == nil {
panic(errors.NewUnexpectedError("unexpected call to RevokeAccountKey"))
}
Expand Down
12 changes: 6 additions & 6 deletions runtime/tests/runtime_utils/testinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ type TestRuntimeInterface struct {
hashAlgo runtime.HashAlgorithm,
weight int,
) (*stdlib.AccountKey, error)
OnGetAccountKey func(address runtime.Address, index int) (*stdlib.AccountKey, error)
OnRemoveAccountKey func(address runtime.Address, index int) (*stdlib.AccountKey, error)
OnAccountKeysCount func(address runtime.Address) (uint64, error)
OnGetAccountKey func(address runtime.Address, index uint32) (*stdlib.AccountKey, error)
OnRemoveAccountKey func(address runtime.Address, index uint32) (*stdlib.AccountKey, error)
OnAccountKeysCount func(address runtime.Address) (uint32, error)
OnUpdateAccountContractCode func(location common.AddressLocation, code []byte) error
OnGetAccountContractCode func(location common.AddressLocation) (code []byte, err error)
OnRemoveAccountContractCode func(location common.AddressLocation) (err error)
Expand Down Expand Up @@ -259,21 +259,21 @@ func (i *TestRuntimeInterface) AddAccountKey(
return i.OnAddAccountKey(address, publicKey, hashAlgo, weight)
}

func (i *TestRuntimeInterface) GetAccountKey(address runtime.Address, index int) (*stdlib.AccountKey, error) {
func (i *TestRuntimeInterface) GetAccountKey(address runtime.Address, index uint32) (*stdlib.AccountKey, error) {
if i.OnGetAccountKey == nil {
panic("must specify TestRuntimeInterface.OnGetAccountKey")
}
return i.OnGetAccountKey(address, index)
}

func (i *TestRuntimeInterface) AccountKeysCount(address runtime.Address) (uint64, error) {
func (i *TestRuntimeInterface) AccountKeysCount(address runtime.Address) (uint32, error) {
if i.OnAccountKeysCount == nil {
panic("must specify TestRuntimeInterface.OnAccountKeysCount")
}
return i.OnAccountKeysCount(address)
}

func (i *TestRuntimeInterface) RevokeAccountKey(address runtime.Address, index int) (*stdlib.AccountKey, error) {
func (i *TestRuntimeInterface) RevokeAccountKey(address runtime.Address, index uint32) (*stdlib.AccountKey, error) {
if i.OnRemoveAccountKey == nil {
panic("must specify TestRuntimeInterface.OnRemoveAccountKey")
}
Expand Down

0 comments on commit 3610da6

Please sign in to comment.