Skip to content

Commit

Permalink
commit storage temporarily before creating account, so host can obser…
Browse files Browse the repository at this point in the history
…ve existing writes
  • Loading branch information
turbolent authored and SupunS committed Nov 8, 2023
1 parent 8ff9776 commit 208154f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 12 additions & 1 deletion runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3231,19 +3231,30 @@ func TestRuntimeTransaction_CreateAccount(t *testing.T) {
script := []byte(`
transaction {
prepare(signer: AuthAccount) {
// Important: Perform a write which will be pending until the end of the transaction,
// but should be (temporarily) committed when the AuthAccount constructor is called
signer.save(42, to: /storage/answer)
AuthAccount(payer: signer)
}
}
`)

var events []cadence.Event

var performedWrite bool

onWrite := func(owner, key, value []byte) {
performedWrite = true
}

runtimeInterface := &testRuntimeInterface{
storage: newTestLedger(nil, nil),
storage: newTestLedger(nil, onWrite),
getSigningAccounts: func() ([]Address, error) {
return []Address{{42}}, nil
},
createAccount: func(payer Address) (address Address, err error) {
// Check that pending writes were committed before
assert.True(t, performedWrite)
return Address{42}, nil
},
emitEvent: func(event cadence.Event) error {
Expand Down
14 changes: 12 additions & 2 deletions runtime/stdlib/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type AccountIDGenerator interface {
GenerateAccountID(address common.Address) (uint64, error)
}

type StorageCommitter interface {
CommitStorageTemporarily(inter *interpreter.Interpreter) error
}

type AuthAccountHandler interface {
AccountIDGenerator
BalanceProvider
Expand All @@ -77,6 +81,7 @@ type AuthAccountHandler interface {
}

type AccountCreator interface {
StorageCommitter
EventEmitter
AuthAccountHandler
// CreateAccount creates a new account.
Expand Down Expand Up @@ -120,6 +125,11 @@ func NewAuthAccountConstructor(creator AccountCreator) StandardLibraryValue {

payerAddress := payerAddressValue.ToAddress()

err := creator.CommitStorageTemporarily(inter)
if err != nil {
panic(err)

Check warning on line 130 in runtime/stdlib/account.go

View check run for this annotation

Codecov / codecov/patch

runtime/stdlib/account.go#L130

Added line #L130 was not covered by tests
}

addressValue := interpreter.NewAddressValueFromConstructor(
inter,
func() (address common.Address) {
Expand Down Expand Up @@ -383,7 +393,7 @@ func newAccountAvailableBalanceGetFunction(
}

type StorageUsedProvider interface {
CommitStorageTemporarily(inter *interpreter.Interpreter) error
StorageCommitter
// GetStorageUsed gets storage used in bytes by the address at the moment of the function call.
GetStorageUsed(address common.Address) (uint64, error)
}
Expand Down Expand Up @@ -422,7 +432,7 @@ func newStorageUsedGetFunction(
}

type StorageCapacityProvider interface {
CommitStorageTemporarily(inter *interpreter.Interpreter) error
StorageCommitter
// GetStorageCapacity gets storage capacity in bytes on the address.
GetStorageCapacity(address common.Address) (uint64, error)
}
Expand Down

0 comments on commit 208154f

Please sign in to comment.