Skip to content

Commit

Permalink
Avoid compressing signatures without storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusx1211 committed Feb 9, 2024
1 parent 799895a commit ef9d721
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions compressor/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func (cm *Compressor) IsSaneCompression(
return fmt.Errorf("exec data does not match input")
}

if !bytes.Equal(decompressed, ed2) {
return fmt.Errorf("exec data does not match input")
if !bytes.Equal(decompressed, ed2) && !bytes.Equal(decompressed, ed1) {
return fmt.Errorf("decompressed exec data does not match input")
}

return nil
Expand Down
16 changes: 10 additions & 6 deletions compressor/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,16 @@ func (c *Encoder) WriteBytesOptimized(dest *CBuffer, bytes []byte, saveWord bool
// we generate a snapshot of the dest buffer and try to encode it as a signature, if it fails
// or if the result is bigger than the original bytes, we restore the snapshot and continue.
// Notice: pass `false` to `mayUseBytes` or else this will be an infinite loop
snapshot := dest.Snapshot()
t, err := c.WriteSignature(dest, bytes, false)
if err == nil && dest.Len() < len(bytes)+3+len(snapshot.Commited) {
return t, nil
// DO NOT use this method if storage is set to false
// it is never worth it if we need to use calldata
if dest.Refs.useContractStorage {
snapshot := dest.Snapshot()
t, err := c.WriteSignature(dest, bytes, false)
if err == nil && dest.Len() < len(bytes)+3+len(snapshot.Commited) {
return t, nil
}
dest.Restore(snapshot)
}
dest.Restore(snapshot)

// If the bytes are a multiple of 32 + 4 bytes (max 6 * 32 + 4) then it
// can be encoded as an ABI call with 0 to 6 parameters
Expand Down Expand Up @@ -637,7 +641,7 @@ func (c *Encoder) WriteSignature(dest *CBuffer, signature []byte, mayUseBytes bo
}()

// First byte determines the signature type
if mayUseBytes && (len(signature) == 0) {
if mayUseBytes && (len(signature) == 0 || !dest.Refs.useContractStorage) {
// Guestmodule signatures are empty
return c.WriteBytesOptimized(dest, signature, false)
}
Expand Down

0 comments on commit ef9d721

Please sign in to comment.