forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathwitness_utils.go
220 lines (182 loc) · 6.86 KB
/
witness_utils.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package witness
import (
"bytes"
"context"
"errors"
"fmt"
"math"
"github.com/holiman/uint256"
coreState "github.com/ledgerwatch/erigon/core/state"
db2 "github.com/ledgerwatch/erigon/smt/pkg/db"
"github.com/ledgerwatch/erigon/smt/pkg/smt"
"github.com/ledgerwatch/erigon/turbo/trie"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/state"
corestate "github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/rawdb"
eritypes "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/stagedsync"
dstypes "github.com/ledgerwatch/erigon/zk/datastream/types"
zkSmt "github.com/ledgerwatch/erigon/zk/smt"
zkUtils "github.com/ledgerwatch/erigon/zk/utils"
"github.com/ledgerwatch/log/v3"
)
var (
ErrNoWitnesses = errors.New("witness count is 0")
)
func UnwindForWitness(ctx context.Context, tx kv.RwTx, startBlock, latestBlock uint64, dirs datadir.Dirs, historyV3 bool, agg *state.Aggregator) (err error) {
unwindState := &stagedsync.UnwindState{UnwindPoint: startBlock - 1}
stageState := &stagedsync.StageState{BlockNumber: latestBlock}
hashStageCfg := stagedsync.StageHashStateCfg(nil, dirs, historyV3, agg)
if err := stagedsync.UnwindHashStateStage(unwindState, stageState, tx, hashStageCfg, ctx, log.New(), true); err != nil {
return fmt.Errorf("UnwindHashStateStage: %w", err)
}
var expectedRootHash common.Hash
syncHeadHeader, err := rawdb.ReadHeaderByNumber_zkevm(tx, unwindState.UnwindPoint)
if err != nil {
return fmt.Errorf("ReadHeaderByNumber_zkevm for block %d: %v", unwindState.UnwindPoint, err)
}
if syncHeadHeader == nil {
log.Warn("header not found for block number", "block", unwindState.UnwindPoint)
} else {
expectedRootHash = syncHeadHeader.Root
}
if _, err := zkSmt.UnwindZkSMT(ctx, "api.generateWitness", stageState.BlockNumber, unwindState.UnwindPoint, tx, true, &expectedRootHash, true); err != nil {
return fmt.Errorf("UnwindZkSMT: %w", err)
}
return nil
}
type gerForWitnessDb interface {
GetBatchNoByL2Block(blockNum uint64) (uint64, error)
GetBatchGlobalExitRoots(lastBatch, currentBatch uint64) (*[]dstypes.GerUpdate, error)
GetBlockGlobalExitRoot(blockNum uint64) (common.Hash, error)
}
func PrepareGersForWitness(block *eritypes.Block, db gerForWitnessDb, tds *coreState.TrieDbState, trieStateWriter *coreState.TrieStateWriter) error {
blockNum := block.NumberU64()
//[zkevm] get batches between last block and this one
// plus this blocks ger
lastBatchInserted, err := db.GetBatchNoByL2Block(blockNum - 1)
if err != nil {
return fmt.Errorf("GetBatchNoByL2Block for block %d: %w", blockNum-1, err)
}
currentBatch, err := db.GetBatchNoByL2Block(blockNum)
if err != nil {
return fmt.Errorf("GetBatchNoByL2Block for block %d: %v", blockNum, err)
}
gersInBetween, err := db.GetBatchGlobalExitRoots(lastBatchInserted, currentBatch)
if err != nil {
return fmt.Errorf("GetBatchGlobalExitRoots for block %d: %v", blockNum, err)
}
var globalExitRoots []dstypes.GerUpdate
if gersInBetween != nil {
globalExitRoots = append(globalExitRoots, *gersInBetween...)
}
blockGer, err := db.GetBlockGlobalExitRoot(blockNum)
if err != nil {
return fmt.Errorf("GetBlockGlobalExitRoot for block %d: %v", blockNum, err)
}
emptyHash := common.Hash{}
if blockGer != emptyHash {
blockGerUpdate := dstypes.GerUpdate{
GlobalExitRoot: blockGer,
Timestamp: block.Header().Time,
}
globalExitRoots = append(globalExitRoots, blockGerUpdate)
}
for _, ger := range globalExitRoots {
// [zkevm] - add GER if there is one for this batch
if err := zkUtils.WriteGlobalExitRoot(tds, trieStateWriter, ger.GlobalExitRoot, ger.Timestamp); err != nil {
return fmt.Errorf("WriteGlobalExitRoot: %w", err)
}
}
return nil
}
type trieDbState interface {
ResolveSMTRetainList(inclusion map[common.Address][]common.Hash) (*trie.RetainList, error)
}
func BuildWitnessFromTrieDbState(ctx context.Context, tx kv.Tx, tds trieDbState, reader *corestate.PlainState, forcedContracts []common.Address, forcedInfoTreeUpdates []common.Hash, witnessFull bool) (witness *trie.Witness, err error) {
var rl trie.RetainDecider
// if full is true, we will send all the nodes to the witness
rl = &trie.AlwaysTrueRetainDecider{}
if !witnessFull {
inclusion := make(map[common.Address][]common.Hash)
for _, contract := range forcedContracts {
err = reader.ForEachStorage(contract, common.Hash{}, func(key, secKey common.Hash, value uint256.Int) bool {
inclusion[contract] = append(inclusion[contract], key)
return false
}, math.MaxInt64)
if err != nil {
return nil, err
}
}
// ensure that the ger manager is in the inclusion list if there are forced info tree updates
if len(forcedInfoTreeUpdates) > 0 {
if _, ok := inclusion[coreState.GER_MANAGER_ADDRESS]; !ok {
inclusion[coreState.GER_MANAGER_ADDRESS] = []common.Hash{}
}
}
// add any forced info tree updates to the inclusion list that aren't already there
for _, forced := range forcedInfoTreeUpdates {
skip := false
for _, hash := range inclusion[coreState.GER_MANAGER_ADDRESS] {
if hash == forced {
skip = true
break
}
}
if !skip {
inclusion[coreState.GER_MANAGER_ADDRESS] = append(inclusion[coreState.GER_MANAGER_ADDRESS], forced)
}
}
rl, err = tds.ResolveSMTRetainList(inclusion)
if err != nil {
return nil, err
}
}
eridb := db2.NewRoEriDb(tx)
smtTrie := smt.NewRoSMT(eridb)
if witness, err = smtTrie.BuildWitness(rl, ctx); err != nil {
return nil, fmt.Errorf("BuildWitness: %w", err)
}
return
}
func GetWitnessBytes(witness *trie.Witness, debug bool) ([]byte, error) {
var buf bytes.Buffer
if _, err := witness.WriteInto(&buf, debug); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func ParseWitnessFromBytes(input []byte, trace bool) (*trie.Witness, error) {
return trie.NewWitnessFromReader(bytes.NewReader(input), trace)
}
// merges witnesses into one
// corresponds to a witness built on a range of blocks
// input witnesses should be ordered by consequent blocks
// it replaces values from 2,3,4 into the first witness
func MergeWitnesses(ctx context.Context, witnesses []*trie.Witness) (*trie.Witness, error) {
if len(witnesses) == 0 {
return nil, ErrNoWitnesses
}
if len(witnesses) == 1 {
return witnesses[0], nil
}
baseSmt, err := smt.BuildSMTFromWitness(witnesses[0])
if err != nil {
return nil, fmt.Errorf("BuildSMTfromWitness: %w", err)
}
for i := 1; i < len(witnesses); i++ {
if err := smt.AddWitnessToSMT(baseSmt, witnesses[i]); err != nil {
return nil, fmt.Errorf("AddWitnessToSMT: %w", err)
}
}
// if full is true, we will send all the nodes to the witness
rl := &trie.AlwaysTrueRetainDecider{}
witness, err := baseSmt.BuildWitness(rl, ctx)
if err != nil {
return nil, fmt.Errorf("BuildWitness: %w", err)
}
return witness, nil
}