Commit ec73572d authored by clabby's avatar clabby

Use a reader instead of offsets when parsing the `SystemConfig` update event in `op-node`

parent d8e328ae
...@@ -69,50 +69,115 @@ func ProcessSystemConfigUpdateLogEvent(destSysCfg *eth.SystemConfig, ev *types.L ...@@ -69,50 +69,115 @@ func ProcessSystemConfigUpdateLogEvent(destSysCfg *eth.SystemConfig, ev *types.L
} }
// indexed 1 // indexed 1
updateType := ev.Topics[2] updateType := ev.Topics[2]
// unindexed data
// Create a reader of the unindexed data
reader := bytes.NewReader(ev.Data)
// Allocate a placeholder word to read into
word := make([]byte, 32)
// Attempt to read unindexed data
switch updateType { switch updateType {
case SystemConfigUpdateBatcher: case SystemConfigUpdateBatcher:
if len(ev.Data) != 32*3 { if len(ev.Data) != 32*3 {
return fmt.Errorf("expected 32*3 bytes in batcher hash update, but got %d bytes", len(ev.Data)) return fmt.Errorf("expected 32*3 bytes in batcher hash update, but got %d bytes", len(ev.Data))
} }
if x := common.BytesToHash(ev.Data[:32]); x != (common.Hash{31: 32}) {
return fmt.Errorf("expected offset to point to length location, but got %s", x) // Attempt to read the pointer, it should always equal 32.
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read pointer in batcher address update")
}
if common.BytesToHash(word) != (common.Hash{31: 32}) {
return fmt.Errorf("expected offset to point to length location, but got %s", word)
}
// Attempt to read the length, it should also always equal 32.
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read length in batcher address update")
}
if common.BytesToHash(word) != (common.Hash{31: 32}) {
return fmt.Errorf("expected length to be 32 bytes, but got %s", word)
} }
if x := common.BytesToHash(ev.Data[32:64]); x != (common.Hash{31: 32}) {
return fmt.Errorf("expected length of 1 bytes32, but got %s", x) // Attempt to read the batcher hash
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read batcher hash in batcher address update")
} }
if !bytes.Equal(ev.Data[64:64+12], make([]byte, 12)) { // Indexing `word` directly is always safe here, it is guaranteed to be 32 bytes in length.
return fmt.Errorf("expected version 0 batcher hash with zero padding, but got %x", ev.Data) // Check that the batcher address is correctly zero-padded.
if !bytes.Equal(word[:12], make([]byte, 12)) {
return fmt.Errorf("expected version 0 batcher hash with zero padding, but got %x", word)
} }
destSysCfg.BatcherAddr.SetBytes(ev.Data[64+12:]) destSysCfg.BatcherAddr.SetBytes(word[12:])
return nil return nil
case SystemConfigUpdateGasConfig: // left padded uint8 case SystemConfigUpdateGasConfig:
if len(ev.Data) != 32*4 { if len(ev.Data) != 32*4 {
return fmt.Errorf("expected 32*4 bytes in GPO params update data, but got %d", len(ev.Data)) return fmt.Errorf("expected 32*4 bytes in GPO params update data, but got %d", len(ev.Data))
} }
if x := common.BytesToHash(ev.Data[:32]); x != (common.Hash{31: 32}) {
return fmt.Errorf("expected offset to point to length location, but got %s", x) // Attempt to read the pointer, it should always equal 32.
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read pointer in GPO params update")
}
if common.BytesToHash(word) != (common.Hash{31: 32}) {
return fmt.Errorf("expected offset to point to length location, but got %s", word)
}
// Attempt to read the length, it should always equal 64.
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read length in GPO params update")
}
if common.BytesToHash(word) != (common.Hash{31: 64}) {
return fmt.Errorf("expected length to be 64 bytes, but got %s", word)
}
// Attempt to read the overhead
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read overhead in GPO params update")
} }
if x := common.BytesToHash(ev.Data[32:64]); x != (common.Hash{31: 64}) {
return fmt.Errorf("expected length of 2 bytes32, but got %s", x) // Allocate a second word to read the scalar into
secondWord := make([]byte, 32)
// Attempt to read the scalar
if _, err := reader.Read(secondWord); err != nil {
return fmt.Errorf("failed to read scalar in GPO params update")
} }
copy(destSysCfg.Overhead[:], ev.Data[64:96])
copy(destSysCfg.Scalar[:], ev.Data[96:128]) // Set the system config's overhead and scalar values to the values read from the log
copy(destSysCfg.Overhead[:], word)
copy(destSysCfg.Scalar[:], secondWord)
return nil return nil
case SystemConfigUpdateGasLimit: case SystemConfigUpdateGasLimit:
if len(ev.Data) != 32*3 { if len(ev.Data) != 32*3 {
return fmt.Errorf("expected 32*3 bytes in gas limit update, but got %d bytes", len(ev.Data)) return fmt.Errorf("expected 32*3 bytes in gas limit update, but got %d bytes", len(ev.Data))
} }
if x := common.BytesToHash(ev.Data[:32]); x != (common.Hash{31: 32}) {
return fmt.Errorf("expected offset to point to length location, but got %s", x) // Attempt to read the pointer, it should always equal 32.
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read pointer in gas limit update")
}
if common.BytesToHash(word) != (common.Hash{31: 32}) {
return fmt.Errorf("expected offset to point to length location, but got %s", word)
}
// Attempt to read the length, it should also always equal 32.
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read length in gas limit update")
}
if common.BytesToHash(word) != (common.Hash{31: 32}) {
return fmt.Errorf("expected length to be 32 bytes, but got %s", word)
} }
if x := common.BytesToHash(ev.Data[32:64]); x != (common.Hash{31: 32}) {
return fmt.Errorf("expected length of 1 bytes32, but got %s", x) // Attempt to read the gas limit
if _, err := reader.Read(word); err != nil {
return fmt.Errorf("failed to read gas limit in gas limit update")
} }
if !bytes.Equal(ev.Data[64:64+24], make([]byte, 24)) { // Indexing `word` directly is always safe here, it is guaranteed to be 32 bytes in length.
return fmt.Errorf("expected zero padding for gaslimit, but got %x", ev.Data) // Check that the gas limit is correctly zero-padded.
if !bytes.Equal(word[:24], make([]byte, 24)) {
return fmt.Errorf("expected zero padding for gaslimit, but got %x", word)
} }
destSysCfg.GasLimit = binary.BigEndian.Uint64(ev.Data[64+24:]) destSysCfg.GasLimit = binary.BigEndian.Uint64(word[24:])
return nil return nil
case SystemConfigUpdateUnsafeBlockSigner: case SystemConfigUpdateUnsafeBlockSigner:
// Ignored in derivation. This configurable applies to runtime configuration outside of the derivation. // Ignored in derivation. This configurable applies to runtime configuration outside of the derivation.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment