Skip to content

Commit ecf779c

Browse files
authored
Merge pull request #37 from ethereum-optimism/tip/pcw109550/add-statehash-witness-to-state
rvgo: Embed witness and stateHash to state
2 parents 332fe52 + b9cb34b commit ecf779c

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

rvgo/cmd/load_elf.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func LoadELF(ctx *cli.Context) error {
3232
if err != nil {
3333
return fmt.Errorf("failed to compute program metadata: %w", err)
3434
}
35+
// Must set witness and stateHash after initial state is prepared
36+
if err := state.SetWitnessAndStateHash(); err != nil {
37+
return fmt.Errorf("failed to set witness and stateHash: %w", err)
38+
}
3539
if err := jsonutil.WriteJSON[*Metadata](ctx.Path(cannon.LoadELFMetaFlag.Name), meta, OutFilePerm); err != nil {
3640
return fmt.Errorf("failed to output metadata: %w", err)
3741
}

rvgo/cmd/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ func Run(ctx *cli.Context) error {
223223
}
224224
}
225225

226+
if err := state.SetWitnessAndStateHash(); err != nil {
227+
return fmt.Errorf("failed to set witness and stateHash: %w", err)
228+
}
229+
226230
if err := jsonutil.WriteJSON(ctx.Path(cannon.RunOutputFlag.Name), state, OutFilePerm); err != nil {
227231
return fmt.Errorf("failed to write state output: %w", err)
228232
}

rvgo/fast/state.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ type VMState struct {
5050
// Warning: the hint MAY NOT BE COMPLETE. I.e. this is buffered,
5151
// and should only be read when len(LastHint) > 4 && uint32(LastHint[:4]) >= len(LastHint[4:])
5252
LastHint hexutil.Bytes `json:"lastHint,omitempty"`
53+
54+
// VMState must hold these values because if not, we must ask FPVM again to
55+
// compute these values.
56+
Witness []byte `json:"witness,omitempty"`
57+
StateHash [32]byte `json:"stateHash,omitempty"`
5358
}
5459

5560
func NewVMState() *VMState {
@@ -59,6 +64,17 @@ func NewVMState() *VMState {
5964
}
6065
}
6166

67+
func (state *VMState) SetWitnessAndStateHash() error {
68+
witness := state.EncodeWitness()
69+
state.Witness = witness
70+
stateHash, err := witness.StateHash()
71+
if err != nil {
72+
return fmt.Errorf("failed to compute stateHash: %w", err)
73+
}
74+
state.StateHash = stateHash
75+
return nil
76+
}
77+
6278
func (state *VMState) GetStep() uint64 { return state.Step }
6379

6480
func (state *VMState) EncodeWitness() StateWitness {

0 commit comments

Comments
 (0)