github.com/koko1123/flow-go-1@v0.29.6/engine/execution/computation/committer/committer.go (about)

     1  package committer
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/hashicorp/go-multierror"
     8  
     9  	execState "github.com/koko1123/flow-go-1/engine/execution/state"
    10  	"github.com/koko1123/flow-go-1/fvm/state"
    11  	"github.com/koko1123/flow-go-1/ledger"
    12  	"github.com/koko1123/flow-go-1/model/flow"
    13  	"github.com/koko1123/flow-go-1/module"
    14  )
    15  
    16  type LedgerViewCommitter struct {
    17  	ldg    ledger.Ledger
    18  	tracer module.Tracer
    19  }
    20  
    21  func NewLedgerViewCommitter(ldg ledger.Ledger, tracer module.Tracer) *LedgerViewCommitter {
    22  	return &LedgerViewCommitter{ldg: ldg, tracer: tracer}
    23  }
    24  
    25  func (s *LedgerViewCommitter) CommitView(view state.View, baseState flow.StateCommitment) (newCommit flow.StateCommitment, proof []byte, trieUpdate *ledger.TrieUpdate, err error) {
    26  	var err1, err2 error
    27  	var wg sync.WaitGroup
    28  	wg.Add(1)
    29  	go func() {
    30  		proof, err2 = s.collectProofs(view, baseState)
    31  		wg.Done()
    32  	}()
    33  
    34  	newCommit, trieUpdate, err1 = s.commitView(view, baseState)
    35  	wg.Wait()
    36  
    37  	if err1 != nil {
    38  		err = multierror.Append(err, err1)
    39  	}
    40  	if err2 != nil {
    41  		err = multierror.Append(err, err2)
    42  	}
    43  	return
    44  }
    45  
    46  func (s *LedgerViewCommitter) commitView(view state.View, baseState flow.StateCommitment) (newCommit flow.StateCommitment, update *ledger.TrieUpdate, err error) {
    47  	return execState.CommitDelta(s.ldg, view, baseState)
    48  }
    49  
    50  func (s *LedgerViewCommitter) collectProofs(view state.View, baseState flow.StateCommitment) (proof []byte, err error) {
    51  	// get all deduplicated register IDs
    52  	allIds := view.AllRegisters()
    53  	keys := make([]ledger.Key, 0, len(allIds))
    54  	for _, id := range allIds {
    55  		keys = append(keys, execState.RegisterIDToKey(id))
    56  	}
    57  
    58  	query, err := ledger.NewQuery(ledger.State(baseState), keys)
    59  	if err != nil {
    60  		return nil, fmt.Errorf("cannot create ledger query: %w", err)
    61  	}
    62  
    63  	return s.ldg.Prove(query)
    64  }