Skip to content

Commit

Permalink
Adding event limit per transaction (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramtinms authored Dec 11, 2020
1 parent 12c92e5 commit 115ccee
Show file tree
Hide file tree
Showing 22 changed files with 351 additions and 176 deletions.
10 changes: 2 additions & 8 deletions engine/execution/computation/computer/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (e *blockComputer) executeTransaction(

txView := collectionView.NewChild()

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, txIndex)

err := e.vm.Run(ctx, tx, txView)

Expand All @@ -255,12 +255,6 @@ func (e *blockComputer) executeTransaction(
return nil, flow.TransactionResult{}, 0, fmt.Errorf("failed to execute transaction: %w", err)
}

txEvents, err := tx.ConvertEvents(txIndex)

if err != nil {
return nil, flow.TransactionResult{}, 0, fmt.Errorf("failed to create flow events: %w", err)
}

txResult := flow.TransactionResult{
TransactionID: tx.ID,
}
Expand All @@ -282,5 +276,5 @@ func (e *blockComputer) executeTransaction(
collectionView.MergeView(txView)
}

return txEvents, txResult, tx.GasUsed, nil
return tx.Events, txResult, tx.GasUsed, nil
}
17 changes: 6 additions & 11 deletions engine/execution/computation/computer/computer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/rand"
"testing"

"github.com/onflow/cadence"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -94,15 +93,13 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
// create a block with 2 collections with 2 transactions each
block := generateBlock(collectionCount, transactionsPerCollection)

// create dummy events
events := generateEvents(eventsPerTransaction)

vm.On("Run", mock.Anything, mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
tx := args[1].(*fvm.TransactionProcedure)

tx.Err = &fvm.MissingPayerError{}
tx.Events = events
// create dummy events
tx.Events = generateEvents(eventsPerTransaction, tx.TxIndex)
}).
Return(nil).
Times(totalTransactionCount)
Expand All @@ -125,7 +122,7 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
for expectedTxIndex := 0; expectedTxIndex < totalTransactionCount; expectedTxIndex++ {
for expectedEventIndex := 0; expectedEventIndex < eventsPerTransaction; expectedEventIndex++ {
e := result.Events[k]
assert.EqualValues(t, expectedEventIndex, e.EventIndex)
assert.EqualValues(t, expectedEventIndex, int(e.EventIndex))
assert.EqualValues(t, expectedTxIndex, e.TransactionIndex)
k++
}
Expand Down Expand Up @@ -194,13 +191,11 @@ func generateCollection(transactionCount int) *entity.CompleteCollection {
}
}

func generateEvents(eventCount int) []cadence.Event {
events := make([]cadence.Event, eventCount)
func generateEvents(eventCount int, txIndex uint32) []flow.Event {
events := make([]flow.Event, eventCount)
for i := 0; i < eventCount; i++ {
// creating some dummy event
event := cadence.Event{EventType: &cadence.EventType{
Identifier: "whatever",
}}
event := flow.Event{Type: "whatever", EventIndex: uint32(i), TransactionIndex: txIndex}
events[i] = event
}
return events
Expand Down
12 changes: 8 additions & 4 deletions engine/execution/testutil/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func CreateAccountsWithSimpleAddresses(

serviceAddress := chain.ServiceAddress()

for _, privateKey := range privateKeys {
for i, privateKey := range privateKeys {
accountKey := privateKey.PublicKey(fvm.AccountKeyWeightThreshold)
encAccountKey, _ := flow.EncodeRuntimeAccountPublicKey(accountKey)
cadAccountKey := BytesToCadenceArray(encAccountKey)
Expand All @@ -179,7 +179,7 @@ func CreateAccountsWithSimpleAddresses(
AddArgument(encCadAccountKey).
AddAuthorizer(serviceAddress)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, uint32(i))
err := vm.Run(ctx, tx, ledger)
if err != nil {
return nil, err
Expand All @@ -192,8 +192,12 @@ func CreateAccountsWithSimpleAddresses(
var addr flow.Address

for _, event := range tx.Events {
if event.EventType.ID() == string(flow.EventAccountCreated) {
addr = event.Fields[0].ToGoValue().([8]byte)
if event.Type == flow.EventAccountCreated {
data, err := jsoncdc.Decode(event.Payload)
if err != nil {
return nil, errors.New("error decoding events")
}
addr = flow.Address(data.(cadence.Event).Fields[0].(cadence.Address))
break
}

Expand Down
6 changes: 4 additions & 2 deletions fvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ tx := flow.NewTransactionBody().
ctx := fvm.NewContext()
ledger := state.NewMapLedger()

txProc := fvm.Transaction(tx)
txIndex := uint32(0)
txProc := fvm.Transaction(tx, txIndex)

err := vm.Run(ctx, txProc, ledger)
if err != nil {
Expand All @@ -48,8 +49,9 @@ A transaction procedure can be created from a `flow.TransactionBody`:

```go
var tx flow.TransactionBody
var txIndex uint32

i := fvm.Transaction(tx)
i := fvm.Transaction(tx, txIndex)
```

A transaction procedure has the following steps:
Expand Down
1 change: 1 addition & 0 deletions fvm/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func initFlowTokenTransaction(accountAddress, serviceAddress flow.Address) *Tran
flow.NewTransactionBody().
SetScript([]byte(fmt.Sprintf(initFlowTokenTransactionTemplate, serviceAddress))).
AddAuthorizer(accountAddress),
0,
)
}

Expand Down
57 changes: 32 additions & 25 deletions fvm/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ func createAccount(t *testing.T, vm *fvm.VirtualMachine, chain flow.Chain, ctx f
SetScript([]byte(createAccountTransaction)).
AddAuthorizer(chain.ServiceAddress())

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
require.NoError(t, tx.Err)

require.Equal(t, string(flow.EventAccountCreated), tx.Events[0].EventType.TypeID)
require.Equal(t, flow.EventAccountCreated, tx.Events[0].Type)

address := flow.Address(tx.Events[0].Fields[0].(cadence.Address))
data, err := jsoncdc.Decode(tx.Events[0].Payload)
require.NoError(t, err)
address := flow.Address(data.(cadence.Event).Fields[0].(cadence.Address))

return address
}
Expand All @@ -55,7 +57,7 @@ func addAccountKey(
AddArgument(cadencePublicKey).
AddAuthorizer(address)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -83,7 +85,7 @@ func addAccountCreator(
SetScript(script).
AddAuthorizer(chain.ServiceAddress())

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand All @@ -110,7 +112,7 @@ func removeAccountCreator(
SetScript(script).
AddAuthorizer(chain.ServiceAddress())

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -238,17 +240,20 @@ func TestCreateAccount(t *testing.T) {
SetScript([]byte(createAccountTransaction)).
AddAuthorizer(payer)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)

assert.NoError(t, tx.Err)

require.Len(t, tx.Events, 1)
assert.Equal(t, string(flow.EventAccountCreated), tx.Events[0].EventType.TypeID)

address := flow.Address(tx.Events[0].Fields[0].(cadence.Address))
require.Equal(t, flow.EventAccountCreated, tx.Events[0].Type)

data, err := jsoncdc.Decode(tx.Events[0].Payload)
require.NoError(t, err)
address := flow.Address(data.(cadence.Event).Fields[0].(cadence.Address))

account, err := vm.GetAccount(ctx, address, ledger)
require.NoError(t, err)
Expand All @@ -266,7 +271,7 @@ func TestCreateAccount(t *testing.T) {
SetScript([]byte(createMultipleAccountsTransaction)).
AddAuthorizer(payer)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand All @@ -276,9 +281,11 @@ func TestCreateAccount(t *testing.T) {
require.Len(t, tx.Events, count)

for i := 0; i < count; i++ {
require.Equal(t, string(flow.EventAccountCreated), tx.Events[i].EventType.TypeID)
require.Equal(t, flow.EventAccountCreated, tx.Events[i].Type)

address := flow.Address(tx.Events[i].Fields[0].(cadence.Address))
data, err := jsoncdc.Decode(tx.Events[i].Payload)
require.NoError(t, err)
address := flow.Address(data.(cadence.Event).Fields[0].(cadence.Address))

account, err := vm.GetAccount(ctx, address, ledger)
require.NoError(t, err)
Expand All @@ -303,7 +310,7 @@ func TestCreateAccount_WithRestrictedAccountCreation(t *testing.T) {
SetScript([]byte(createAccountTransaction)).
AddAuthorizer(payer)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand All @@ -318,7 +325,7 @@ func TestCreateAccount_WithRestrictedAccountCreation(t *testing.T) {
SetScript([]byte(createAccountTransaction)).
AddAuthorizer(chain.ServiceAddress())

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand All @@ -337,7 +344,7 @@ func TestCreateAccount_WithRestrictedAccountCreation(t *testing.T) {
SetPayer(payer).
AddAuthorizer(payer)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand All @@ -355,7 +362,7 @@ func TestCreateAccount_WithRestrictedAccountCreation(t *testing.T) {
SetScript([]byte(createAccountTransaction)).
AddAuthorizer(payer)

validTx := fvm.Transaction(txBody)
validTx := fvm.Transaction(txBody, 0)

err := vm.Run(ctx, validTx, ledger)
require.NoError(t, err)
Expand All @@ -364,7 +371,7 @@ func TestCreateAccount_WithRestrictedAccountCreation(t *testing.T) {

removeAccountCreator(t, vm, chain, ctx, ledger, payer)

invalidTx := fvm.Transaction(txBody)
invalidTx := fvm.Transaction(txBody, 0)

err = vm.Run(ctx, invalidTx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -411,7 +418,7 @@ func TestAddAccountKey(t *testing.T) {
AddArgument(cadencePublicKey).
AddAuthorizer(address)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err = vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -449,7 +456,7 @@ func TestAddAccountKey(t *testing.T) {
AddArgument(publicKey2Arg).
AddAuthorizer(address)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err = vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -490,7 +497,7 @@ func TestAddAccountKey(t *testing.T) {
AddArgument(invalidPublicKeyArg).
AddAuthorizer(address)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err = vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -521,7 +528,7 @@ func TestAddAccountKey(t *testing.T) {
AddArgument(publicKey2Arg).
AddAuthorizer(address)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err = vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -570,7 +577,7 @@ func TestRemoveAccountKey(t *testing.T) {
require.NoError(t, err)
assert.Len(t, before.Keys, keyCount)

for _, keyIndex := range []int{-1, keyCount, keyCount + 1} {
for i, keyIndex := range []int{-1, keyCount, keyCount + 1} {
keyIndexArg, err := jsoncdc.Encode(cadence.NewInt(keyIndex))
require.NoError(t, err)

Expand All @@ -579,7 +586,7 @@ func TestRemoveAccountKey(t *testing.T) {
AddArgument(keyIndexArg).
AddAuthorizer(address)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, uint32(i))

err = vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -620,7 +627,7 @@ func TestRemoveAccountKey(t *testing.T) {
AddArgument(keyIndexArg).
AddAuthorizer(address)

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err = vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down Expand Up @@ -664,7 +671,7 @@ func TestRemoveAccountKey(t *testing.T) {
txBody.AddArgument(keyIndexArg)
}

tx := fvm.Transaction(txBody)
tx := fvm.Transaction(txBody, 0)

err = vm.Run(ctx, tx, ledger)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 115ccee

Please sign in to comment.