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