Skip to content

Commit

Permalink
Merge pull request #69 from onflow/m4ksio/4800-dont-transfer-spocks
Browse files Browse the repository at this point in the history
Don't transfer spocks #4800
  • Loading branch information
Kay-Zee authored Nov 3, 2020
2 parents b1edd0c + a9698a9 commit fed56cf
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 45 deletions.
3 changes: 0 additions & 3 deletions cmd/util/cmd/exec-data-json-export/delta_snapshot_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jsonexporter

import (
"bufio"
"encoding/hex"
"encoding/json"
"fmt"
"os"
Expand All @@ -19,7 +18,6 @@ import (
type dSnapshot struct {
DeltaJSONStr string `json:"delta_json_str"`
Reads []string `json:"reads"`
SpockSecret string `json:"spock_secret_data"`
}

// ExportDeltaSnapshots exports all the delta snapshots
Expand Down Expand Up @@ -79,7 +77,6 @@ func ExportDeltaSnapshots(blockID flow.Identifier, dbPath string, outputPath str
data := dSnapshot{
DeltaJSONStr: string(m),
Reads: reads,
SpockSecret: hex.EncodeToString(snap[0].SpockSecret),
}

jsonData, err := json.Marshal(data)
Expand Down
2 changes: 1 addition & 1 deletion engine/execution/computation/computer/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (e *blockComputer) executeBlock(

var gasUsed uint64

interactions := make([]*delta.Snapshot, len(collections)+1)
interactions := make([]*delta.SpockSnapshot, len(collections)+1)

events := make([]flow.Event, 0)
blockTxResults := make([]flow.TransactionResult, 0)
Expand Down
55 changes: 32 additions & 23 deletions engine/execution/ingestion/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,15 +975,38 @@ func (e *Engine) handleComputationResult(
// There is one result per transaction
e.metrics.ExecutionTotalExecutedTransactions(len(result.TransactionResult))

receipt, err := e.saveExecutionResults(
snapshots := make([]*delta.Snapshot, len(result.StateSnapshots))
for i, stateSnapshot := range result.StateSnapshots {
snapshots[i] = &stateSnapshot.Snapshot
}

executionResult, err := e.saveExecutionResults(
ctx,
result.ExecutableBlock,
result.StateSnapshots,
snapshots,
result.Events,
result.TransactionResult,
startState,
)
if err != nil {
return nil, nil, fmt.Errorf("could not save execution results: %w", err)
}

receipt, err := e.generateExecutionReceipt(ctx, executionResult, result.StateSnapshots)
if err != nil {
return nil, nil, fmt.Errorf("could not generate execution receipt: %w", err)
}

err = func() error {
span, _ := e.tracer.StartSpanFromContext(ctx, trace.EXESaveExecutionReceipt)
defer span.Finish()

err = e.execState.PersistExecutionReceipt(ctx, receipt)
if err != nil && !errors.Is(err, storage.ErrAlreadyExists) {
return fmt.Errorf("could not persist execution receipt: %w", err)
}
return nil
}()
if err != nil {
return nil, nil, err
}
Expand All @@ -1009,7 +1032,7 @@ func (e *Engine) saveExecutionResults(
events []flow.Event,
txResults []flow.TransactionResult,
startState flow.StateCommitment,
) (*flow.ExecutionReceipt, error) {
) (*flow.ExecutionResult, error) {

span, childCtx := e.tracer.StartSpanFromContext(ctx, trace.EXESaveExecutionResults)
defer span.Finish()
Expand Down Expand Up @@ -1081,9 +1104,9 @@ func (e *Engine) saveExecutionResults(
return nil, fmt.Errorf("could not generate execution result: %w", err)
}

receipt, err := e.generateExecutionReceipt(childCtx, executionResult, stateInteractions)
err = e.execState.PersistExecutionResult(childCtx, executionResult)
if err != nil {
return nil, fmt.Errorf("could not generate execution receipt: %w", err)
return nil, fmt.Errorf("could not persist execution result: %w", err)
}

// not update the highest executed until the result and receipts are saved.
Expand Down Expand Up @@ -1123,27 +1146,13 @@ func (e *Engine) saveExecutionResults(
return nil, err
}

err = func() error {
span, _ := e.tracer.StartSpanFromContext(childCtx, trace.EXESaveExecutionReceipt)
defer span.Finish()

err = e.execState.PersistExecutionReceipt(ctx, receipt)
if err != nil && !errors.Is(err, storage.ErrAlreadyExists) {
return fmt.Errorf("could not persist execution receipt: %w", err)
}
return nil
}()
if err != nil {
return nil, err
}

e.log.Debug().
Hex("block_id", logging.Entity(executableBlock)).
Hex("start_state", originalState).
Hex("final_state", endState).
Msg("saved computation results")

return receipt, nil
return executionResult, nil
}

// logExecutableBlock logs all data about an executable block
Expand Down Expand Up @@ -1228,7 +1237,7 @@ func (e *Engine) generateExecutionResultForBlock(
func (e *Engine) generateExecutionReceipt(
ctx context.Context,
result *flow.ExecutionResult,
stateInteractions []*delta.Snapshot,
stateInteractions []*delta.SpockSnapshot,
) (*flow.ExecutionReceipt, error) {

spocks := make([]crypto.Signature, len(stateInteractions))
Expand Down Expand Up @@ -1649,7 +1658,7 @@ func (e *Engine) applyStateDelta(delta *messages.ExecutionStateDelta) {

// TODO - validate state delta, reject invalid messages

executionReceipt, err := e.saveExecutionResults(
executionResult, err := e.saveExecutionResults(
e.unit.Ctx(),
&delta.ExecutableBlock,
delta.StateInteractions,
Expand All @@ -1662,7 +1671,7 @@ func (e *Engine) applyStateDelta(delta *messages.ExecutionStateDelta) {
log.Fatal().Err(err).Msg("fatal error while processing sync message")
}

finalState, ok := executionReceipt.ExecutionResult.FinalStateCommitment()
finalState, ok := executionResult.FinalStateCommitment()
if !ok {
// set to start state next line will fail anyways
finalState = delta.StartState
Expand Down
12 changes: 11 additions & 1 deletion engine/execution/ingestion/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ func (ctx *testingContext) assertSuccessfulBlockComputation(executableBlock *ent
On("UpdateHighestExecutedBlockIfHigher", mock.Anything, executableBlock.Block.Header).
Return(nil)

ctx.executionState.
On(
"PersistExecutionResult",
mock.Anything,
mock.MatchedBy(func(executionResult *flow.ExecutionResult) bool {
return executionResult.BlockID == executableBlock.Block.ID() && executionResult.PreviousResultID == previousExecutionResultID
}),
).
Return(nil)

ctx.executionState.
On(
"PersistExecutionReceipt",
Expand Down Expand Up @@ -512,7 +522,7 @@ func TestExecuteScriptAtBlockID(t *testing.T) {
func Test_SPOCKGeneration(t *testing.T) {
runWithEngine(t, func(ctx testingContext) {

snapshots := []*delta.Snapshot{
snapshots := []*delta.SpockSnapshot{
{
SpockSecret: []byte{1, 2, 3},
},
Expand Down
2 changes: 1 addition & 1 deletion engine/execution/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ComputationOrder struct {

type ComputationResult struct {
ExecutableBlock *entity.ExecutableBlock
StateSnapshots []*delta.Snapshot
StateSnapshots []*delta.SpockSnapshot
Events []flow.Event
TransactionResult []flow.TransactionResult
GasUsed uint64
Expand Down
20 changes: 13 additions & 7 deletions engine/execution/state/delta/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ type View struct {
readFunc GetRegisterFunc
}

// Snapshot is set of interactions with the register
type Snapshot struct {
Delta Delta
Reads []flow.RegisterID
Delta Delta
Reads []flow.RegisterID
}

// Snapshot is state of interactions with the register
type SpockSnapshot struct {
Snapshot
SpockSecret []byte
}

Expand All @@ -43,7 +47,7 @@ func NewView(readFunc GetRegisterFunc) *View {
}

// Snapshot returns copy of current state of interactions with a View
func (v *View) Interactions() *Snapshot {
func (v *View) Interactions() *SpockSnapshot {

var delta = Delta{
Data: make(map[string]flow.RegisterEntry, len(v.delta.Data)),
Expand All @@ -63,9 +67,11 @@ func (v *View) Interactions() *Snapshot {
var spockSecret = make([]byte, len(spockSecHashSum))
copy(spockSecret, spockSecHashSum)

return &Snapshot{
Delta: delta,
Reads: reads,
return &SpockSnapshot{
Snapshot: Snapshot{
Delta: delta,
Reads: reads,
},
SpockSecret: spockSecret,
}
}
Expand Down
14 changes: 14 additions & 0 deletions engine/execution/state/mock/execution_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions engine/execution/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type ExecutionState interface {
// PersistChunkDataPack stores a chunk data pack by chunk ID.
PersistChunkDataPack(context.Context, *flow.ChunkDataPack) error

PersistExecutionResult(ctx context.Context, result *flow.ExecutionResult) error

PersistExecutionReceipt(context.Context, *flow.ExecutionReceipt) error

PersistStateInteractions(context.Context, flow.Identifier, []*delta.Snapshot) error
Expand All @@ -111,6 +113,20 @@ type state struct {
db *badger.DB
}

func (s *state) PersistExecutionResult(ctx context.Context, executionResult *flow.ExecutionResult) error {

err := s.results.Store(executionResult)
if err != nil {
return fmt.Errorf("could not store result: %w", err)
}

err = s.results.Index(executionResult.BlockID, executionResult.ID())
if err != nil {
return fmt.Errorf("could not index execution result: %w", err)
}
return nil
}

func RegisterIDToKey(reg flow.RegisterID) ledger.Key {
return ledger.NewKey([]ledger.KeyPart{
ledger.NewKeyPart(KeyPartOwner, []byte(reg.Owner)),
Expand Down Expand Up @@ -351,10 +367,6 @@ func (s *state) PersistExecutionReceipt(ctx context.Context, receipt *flow.Execu
if err != nil {
return fmt.Errorf("could not index execution receipt: %w", err)
}
err = s.results.Index(receipt.ExecutionResult.BlockID, receipt.ExecutionResult.ID())
if err != nil {
return fmt.Errorf("could not index execution result: %w", err)
}
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions engine/execution/state/unittest/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"github.com/onflow/flow-go/utils/unittest"
)

func StateInteractionsFixture() *delta.Snapshot {
func StateInteractionsFixture() *delta.SpockSnapshot {
return delta.NewView(nil).Interactions()
}

func ComputationResultFixture(collectionsSignerIDs [][]flow.Identifier) *execution.ComputationResult {
stateViews := make([]*delta.Snapshot, len(collectionsSignerIDs))
stateViews := make([]*delta.SpockSnapshot, len(collectionsSignerIDs))
for i := 0; i < len(collectionsSignerIDs); i++ {
stateViews[i] = StateInteractionsFixture()
}
Expand All @@ -25,7 +25,7 @@ func ComputationResultFixture(collectionsSignerIDs [][]flow.Identifier) *executi

func ComputationResultForBlockFixture(completeBlock *entity.ExecutableBlock) *execution.ComputationResult {
n := len(completeBlock.CompleteCollections)
stateViews := make([]*delta.Snapshot, n)
stateViews := make([]*delta.SpockSnapshot, n)
for i := 0; i < n; i++ {
stateViews[i] = StateInteractionsFixture()
}
Expand Down
2 changes: 1 addition & 1 deletion storage/badger/operation/interactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestStateInteractionsInsertCheckRetrieve(t *testing.T) {
_, err = d1.Get(string([]byte{3}), "", "")
require.NoError(t, err)

interactions := []*delta.Snapshot{d1.Interactions(), d2.Interactions()}
interactions := []*delta.Snapshot{&d1.Interactions().Snapshot, &d2.Interactions().Snapshot}

blockID := unittest.IdentifierFixture()

Expand Down
2 changes: 1 addition & 1 deletion utils/unittest/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func BlockWithParentFixture(parent *flow.Header) flow.Block {
}

func StateInteractionsFixture() *delta.Snapshot {
return delta.NewView(nil).Interactions()
return &delta.NewView(nil).Interactions().Snapshot
}

func BlockWithParentAndProposerFixture(parent *flow.Header, proposer flow.Identifier) flow.Block {
Expand Down

0 comments on commit fed56cf

Please sign in to comment.