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

Updating interface methods #470

Merged
merged 14 commits into from
Nov 27, 2020
4 changes: 2 additions & 2 deletions runtime/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ func PrepareInterpreter(filename string) (*interpreter.Interpreter, *sema.Checke
inter, err := interpreter.NewInterpreter(
checker,
interpreter.WithPredefinedValues(valueDeclarations.ToValues()),
interpreter.WithUUIDHandler(func() uint64 {
interpreter.WithUUIDHandler(func() (uint64, error) {
defer func() { uuid++ }()
return uuid
return uuid, nil
}),
)
must(err)
Expand Down
12 changes: 6 additions & 6 deletions runtime/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func TestRuntimeContract(t *testing.T) {

runtimeInterface := &testRuntimeInterface{
storage: storage,
getSigningAccounts: func() []Address {
return []Address{signerAddress}
getSigningAccounts: func() ([]Address, error) {
return []Address{signerAddress}, nil
},
log: func(message string) {
loggedMessages = append(loggedMessages, message)
Expand Down Expand Up @@ -471,8 +471,8 @@ func TestRuntimeImportMultipleContracts(t *testing.T) {

runtimeInterface := &testRuntimeInterface{
storage: newTestStorage(nil, nil),
getSigningAccounts: func() []Address {
return []Address{common.BytesToAddress([]byte{0x1})}
getSigningAccounts: func() ([]Address, error) {
return []Address{common.BytesToAddress([]byte{0x1})}, nil
},
updateAccountContractCode: func(address Address, name string, code []byte) error {
key := contractKey{
Expand All @@ -498,7 +498,7 @@ func TestRuntimeImportMultipleContracts(t *testing.T) {
delete(deployedContracts, key)
return nil
},
resolveLocation: func(identifiers []ast.Identifier, location ast.Location) (result []sema.ResolvedLocation) {
resolveLocation: func(identifiers []ast.Identifier, location ast.Location) (result []sema.ResolvedLocation, err error) {

// Resolve each identifier as an address location

Expand All @@ -519,7 +519,7 @@ func TestRuntimeImportMultipleContracts(t *testing.T) {
log: func(message string) {
loggedMessages = append(loggedMessages, message)
},
emitEvent: func(event cadence.Event) error{
emitEvent: func(event cadence.Event) error {
events = append(events, event)
return nil
},
Expand Down
8 changes: 4 additions & 4 deletions runtime/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func TestRuntimeCrypto_verify(t *testing.T) {
publicKey []byte,
signatureAlgorithm string,
hashAlgorithm string,
) bool {
) (bool, error) {
called = true
assert.Equal(t, []byte{3, 4}, signature)
assert.Equal(t, "user", tag)
assert.Equal(t, []byte{5, 6}, signedData)
assert.Equal(t, []byte{1, 2}, publicKey)
assert.Equal(t, "ECDSA_P256", signatureAlgorithm)
assert.Equal(t, "SHA3_256", hashAlgorithm)
return true
return true, nil
},
}

Expand Down Expand Up @@ -145,11 +145,11 @@ func TestRuntimeCrypto_hash(t *testing.T) {
hash: func(
data []byte,
hashAlgorithm string,
) []byte {
) ([]byte, error) {
called = true
assert.Equal(t, []byte{1, 2, 3, 4}, data)
assert.Equal(t, "SHA3_256", hashAlgorithm)
return []byte{5, 6, 7, 8}
return []byte{5, 6, 7, 8}, nil
},
log: func(message string) {
loggedMessages = append(loggedMessages, message)
Expand Down
28 changes: 14 additions & 14 deletions runtime/deferral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
return accountCode, nil
},
storage: newTestStorage(onRead, onWrite),
getSigningAccounts: func() []Address {
return []Address{common.BytesToAddress(addressValue.Bytes())}
getSigningAccounts: func() ([]Address, error) {
return []Address{common.BytesToAddress(addressValue.Bytes())}, nil
},
updateAccountContractCode: func(_ Address, _ string, code []byte) error {
accountCode = code
Expand Down Expand Up @@ -623,8 +623,8 @@ func TestRuntimeStorageDeferredResourceDictionaryValues_Nested(t *testing.T) {
return accountCode, nil
},
storage: newTestStorage(onRead, onWrite),
getSigningAccounts: func() []Address {
return []Address{common.BytesToAddress(addressValue.Bytes())}
getSigningAccounts: func() ([]Address, error) {
return []Address{common.BytesToAddress(addressValue.Bytes())}, nil
},
updateAccountContractCode: func(_ Address, _ string, code []byte) error {
accountCode = code
Expand Down Expand Up @@ -856,11 +856,11 @@ func TestRuntimeStorageDeferredResourceDictionaryValues_DictionaryTransfer(t *te
return accountCode, nil
},
storage: newTestStorage(onRead, onWrite),
getSigningAccounts: func() []Address {
getSigningAccounts: func() ([]Address, error) {
return []Address{
signer1,
signer2,
}
}, nil
},
resolveLocation: singleIdentifierLocationResolver(t),
getAccountContractCode: func(_ Address, _ string) (code []byte, err error) {
Expand Down Expand Up @@ -1013,8 +1013,8 @@ func TestRuntimeStorageDeferredResourceDictionaryValues_Removal(t *testing.T) {
return accountCode, nil
},
storage: newTestStorage(nil, nil),
getSigningAccounts: func() []Address {
return []Address{signer}
getSigningAccounts: func() ([]Address, error) {
return []Address{signer}, nil
},
resolveLocation: singleIdentifierLocationResolver(t),
getAccountContractCode: func(_ Address, _ string) (code []byte, err error) {
Expand Down Expand Up @@ -1095,8 +1095,8 @@ func TestRuntimeStorageDeferredResourceDictionaryValues_Destruction(t *testing.T
return accountCode, nil
},
storage: newTestStorage(nil, nil),
getSigningAccounts: func() []Address {
return []Address{signer}
getSigningAccounts: func() ([]Address, error) {
return []Address{signer}, nil
},
resolveLocation: singleIdentifierLocationResolver(t),
getAccountContractCode: func(_ Address, _ string) (code []byte, err error) {
Expand Down Expand Up @@ -1211,8 +1211,8 @@ func TestRuntimeStorageDeferredResourceDictionaryValues_Insertion(t *testing.T)
return accountCode, nil
},
storage: newTestStorage(nil, nil),
getSigningAccounts: func() []Address {
return []Address{signer}
getSigningAccounts: func() ([]Address, error) {
return []Address{signer}, nil
},
resolveLocation: singleIdentifierLocationResolver(t),
getAccountContractCode: func(_ Address, _ string) (code []byte, err error) {
Expand Down Expand Up @@ -1330,8 +1330,8 @@ func TestRuntimeStorageDeferredResourceDictionaryValues_ValueTransferAndDestroy(
return accountCode, nil
},
storage: testStorage,
getSigningAccounts: func() []Address {
return signers
getSigningAccounts: func() ([]Address, error) {
return signers, nil
},
resolveLocation: singleIdentifierLocationResolver(t),
getAccountContractCode: func(_ Address, _ string) (code []byte, err error) {
Expand Down
60 changes: 34 additions & 26 deletions runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ResolvedLocation = sema.ResolvedLocation

type Interface interface {
// ResolveLocation resolves an import location.
ResolveLocation(identifiers []Identifier, location Location) []ResolvedLocation
ResolveLocation(identifiers []Identifier, location Location) ([]ResolvedLocation, error)
// GetCode returns the code at a given location
GetCode(location Location) ([]byte, error)
// GetCachedProgram attempts to get a parsed program from a cache.
Expand All @@ -65,26 +65,28 @@ type Interface interface {
// RemoveAccountContractCode removes the code associated with an account contract.
RemoveAccountContractCode(address Address, name string) (err error)
// GetSigningAccounts returns the signing accounts.
GetSigningAccounts() []Address
GetSigningAccounts() ([]Address, error)
// Log logs a string.
Log(string)
Log(string) error
// EmitEvent is called when an event is emitted by the runtime.
EmitEvent(cadence.Event) (err error)
EmitEvent(cadence.Event) error
// ValueExists returns true if the given key exists in the storage, owned by the given account.
ValueExists(owner, key []byte) (exists bool, err error)
// GenerateUUID is called to generate a UUID.
GenerateUUID() uint64
GenerateUUID() (uint64, error)
// GetComputationLimit returns the computation limit. A value <= 0 means there is no limit
GetComputationLimit() uint64
// SetComputationUsed reports the amount of computation used.
SetComputationUsed(used uint64) error
// DecodeArgument decodes a transaction argument against the given type.
DecodeArgument(argument []byte, argumentType cadence.Type) (cadence.Value, error)
// GetCurrentBlockHeight returns the current block height.
GetCurrentBlockHeight() uint64
GetCurrentBlockHeight() (uint64, error)
// GetBlockAtHeight returns the block at the given height.
GetBlockAtHeight(height uint64) (block Block, exists bool, err error)
// UnsafeRandom returns a random uint64, where the process of random number derivation is not cryptographically
// secure.
UnsafeRandom() uint64
UnsafeRandom() (uint64, error)
// VerifySignature returns true if the given signature was produced by signing the given tag + data
// using the given public key, signature algorithm, and hash algorithm.
VerifySignature(
Expand All @@ -94,9 +96,9 @@ type Interface interface {
publicKey []byte,
signatureAlgorithm string,
hashAlgorithm string,
) bool
) (bool, error)
// Hash returns the digest of hashing the given data with using the given hash algorithm
Hash(data []byte, hashAlgorithm string) []byte
Hash(data []byte, hashAlgorithm string) ([]byte, error)
// GetStorageUsed gets storage used in bytes by the address at the moment of the function call.
GetStorageUsed(address Address) (value uint64, err error)
// GetStorageCapacity gets storage capacity in bytes on the address.
Expand Down Expand Up @@ -127,13 +129,13 @@ type EmptyRuntimeInterface struct{}

var _ Interface = &EmptyRuntimeInterface{}

func (i *EmptyRuntimeInterface) ResolveLocation(identifiers []Identifier, location Location) []ResolvedLocation {
func (i *EmptyRuntimeInterface) ResolveLocation(identifiers []Identifier, location Location) ([]ResolvedLocation, error) {
return []ResolvedLocation{
{
Location: location,
Identifiers: identifiers,
},
}
}, nil
}

func (i *EmptyRuntimeInterface) GetCode(_ Location) ([]byte, error) {
Expand Down Expand Up @@ -188,38 +190,44 @@ func (i *EmptyRuntimeInterface) RemoveAccountContractCode(_ Address, _ string) (
return nil
}

func (i *EmptyRuntimeInterface) GetSigningAccounts() []Address {
return nil
func (i *EmptyRuntimeInterface) GetSigningAccounts() ([]Address, error) {
return nil, nil
}

func (i *EmptyRuntimeInterface) Log(_ string) {}
func (i *EmptyRuntimeInterface) Log(_ string) error {
return nil
}

func (i *EmptyRuntimeInterface) EmitEvent(_ cadence.Event) error {
return nil
}

func (i *EmptyRuntimeInterface) GenerateUUID() uint64 {
return 0
func (i *EmptyRuntimeInterface) GenerateUUID() (uint64, error) {
return 0, nil
}

func (i *EmptyRuntimeInterface) GetComputationLimit() uint64 {
return 0
}

func (i *EmptyRuntimeInterface) SetComputationUsed(uint64) error {
return nil
}

func (i *EmptyRuntimeInterface) DecodeArgument(_ []byte, _ cadence.Type) (cadence.Value, error) {
return nil, nil
}

func (i *EmptyRuntimeInterface) GetCurrentBlockHeight() uint64 {
return 0
func (i *EmptyRuntimeInterface) GetCurrentBlockHeight() (uint64, error) {
return 0, nil
}

func (i *EmptyRuntimeInterface) GetBlockAtHeight(_ uint64) (block Block, exists bool, err error) {
return
}

func (i *EmptyRuntimeInterface) UnsafeRandom() uint64 {
return 0
func (i *EmptyRuntimeInterface) UnsafeRandom() (uint64, error) {
return 0, nil
}

func (i *EmptyRuntimeInterface) VerifySignature(
Expand All @@ -229,21 +237,21 @@ func (i *EmptyRuntimeInterface) VerifySignature(
_ []byte,
_ string,
_ string,
) bool {
return false
) (bool, error) {
return false, nil
}

func (i *EmptyRuntimeInterface) Hash(
_ []byte,
_ string,
) []byte {
return nil
) ([]byte, error) {
return nil, nil
}

func (i EmptyRuntimeInterface) GetStorageUsed(_ Address) (uint64,error) {
func (i EmptyRuntimeInterface) GetStorageUsed(_ Address) (uint64, error) {
return 0, nil
}

func (i EmptyRuntimeInterface) GetStorageCapacity(_ Address) (uint64,error) {
func (i EmptyRuntimeInterface) GetStorageCapacity(_ Address) (uint64, error) {
return 0, nil
}