Commit 59d4fbb2 authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

op-chain-ops: delete dead code (#10298)

Now that genesis generation is in solidity, delete
dead code. This code serialized solidity types suitable
for storage. Very similar to abi encoding but not always
the same, big difference was dynamic types, this library
only supported dynamic types <= 31 bytes to not handle the
chunking. This impacted the sorts of types that could
be in the genesis storage.
parent 12a72abf
This diff is collapsed.
package state
import (
"errors"
"fmt"
"math/big"
"github.com/ethereum-optimism/optimism/op-bindings/solc"
"github.com/ethereum/go-ethereum/common"
)
var (
errInvalidType = errors.New("invalid type")
errUnimplemented = errors.New("type unimplemented")
)
// StorageValues represents the values to be set in storage.
// The key is the name of the storage variable and the value
// is the value to set in storage.
type StorageValues map[string]any
// StorageConfig represents the storage configuration for the L2 predeploy
// contracts.
type StorageConfig map[string]StorageValues
// EncodedStorage represents the storage key and value serialized
// to be placed in Ethereum state.
type EncodedStorage struct {
Key common.Hash
Value common.Hash
}
// EncodeStorage will encode a storage layout
func EncodeStorage(entry solc.StorageLayoutEntry, value any, storageType solc.StorageLayoutType) ([]*EncodedStorage, error) {
if storageType.NumberOfBytes > 32 {
return nil, fmt.Errorf("%s is larger than 32 bytes", storageType.Encoding)
}
encoded, err := EncodeStorageKeyValue(value, entry, storageType)
if err != nil {
return nil, err
}
return encoded, nil
}
// ComputeStorageSlots will compute the storage slots for a given contract.
func ComputeStorageSlots(layout *solc.StorageLayout, values StorageValues) ([]*EncodedStorage, error) {
encodedStorage := make([]*EncodedStorage, 0)
for label, value := range values {
var target solc.StorageLayoutEntry
for _, entry := range layout.Storage {
if label == entry.Label {
target = entry
break
}
}
if target.Label == "" {
return nil, fmt.Errorf("storage layout entry for %s not found", label)
}
storageType := layout.Types[target.Type]
if storageType.Label == "" {
return nil, fmt.Errorf("storage type for %s not found", label)
}
storage, err := EncodeStorage(target, value, storageType)
if err != nil {
return nil, fmt.Errorf("cannot encode storage for %s: %w", target.Label, err)
}
encodedStorage = append(encodedStorage, storage...)
}
results := MergeStorage(encodedStorage)
return results, nil
}
// MergeStorage will combine any overlapping storage slots for
// when values are tightly packed. Do this by checking to see if any
// of the produced storage slots have a matching key, if so use a
// binary or to add the storage values together
func MergeStorage(storage []*EncodedStorage) []*EncodedStorage {
encodedKV := make(map[common.Hash]common.Hash)
var encodedKeys []common.Hash // for deterministic result order
for _, storage := range storage {
if prev, ok := encodedKV[storage.Key]; ok {
combined := new(big.Int).Or(prev.Big(), storage.Value.Big())
encodedKV[storage.Key] = common.BigToHash(combined)
} else {
encodedKV[storage.Key] = storage.Value
encodedKeys = append(encodedKeys, storage.Key)
}
}
results := make([]*EncodedStorage, 0)
for _, key := range encodedKeys {
val := encodedKV[key]
results = append(results, &EncodedStorage{key, val})
}
return results
}
This diff is collapsed.
This diff is collapsed.
{
"storage": [
{
"astId": 3,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "_address",
"offset": 0,
"slot": "0",
"type": "t_address"
},
{
"astId": 7,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "addresses",
"offset": 0,
"slot": "1",
"type": "t_mapping(t_uint256,t_address)"
},
{
"astId": 9,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "_bool",
"offset": 0,
"slot": "2",
"type": "t_bool"
},
{
"astId": 11,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "_uint256",
"offset": 0,
"slot": "3",
"type": "t_uint256"
},
{
"astId": 13,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "offset0",
"offset": 0,
"slot": "4",
"type": "t_uint8"
},
{
"astId": 15,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "offset1",
"offset": 1,
"slot": "4",
"type": "t_uint8"
},
{
"astId": 17,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "offset2",
"offset": 2,
"slot": "4",
"type": "t_uint16"
},
{
"astId": 19,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "offset3",
"offset": 4,
"slot": "4",
"type": "t_uint32"
},
{
"astId": 21,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "offset4",
"offset": 8,
"slot": "4",
"type": "t_uint64"
},
{
"astId": 23,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "offset5",
"offset": 16,
"slot": "4",
"type": "t_uint128"
},
{
"astId": 25,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "_bytes32",
"offset": 0,
"slot": "5",
"type": "t_bytes32"
},
{
"astId": 27,
"contract": "contracts/HelloWorld.sol:HelloWorld",
"label": "_string",
"offset": 0,
"slot": "6",
"type": "t_string_storage"
}
],
"types": {
"t_address": {
"encoding": "inplace",
"label": "address",
"numberOfBytes": "20"
},
"t_bool": {
"encoding": "inplace",
"label": "bool",
"numberOfBytes": "1"
},
"t_bytes32": {
"encoding": "inplace",
"label": "bytes32",
"numberOfBytes": "32"
},
"t_mapping(t_uint256,t_address)": {
"encoding": "mapping",
"key": "t_uint256",
"label": "mapping(uint256 => address)",
"numberOfBytes": "32",
"value": "t_address"
},
"t_string_storage": {
"encoding": "bytes",
"label": "string",
"numberOfBytes": "32"
},
"t_uint128": {
"encoding": "inplace",
"label": "uint128",
"numberOfBytes": "16"
},
"t_uint16": {
"encoding": "inplace",
"label": "uint16",
"numberOfBytes": "2"
},
"t_uint256": {
"encoding": "inplace",
"label": "uint256",
"numberOfBytes": "32"
},
"t_uint32": {
"encoding": "inplace",
"label": "uint32",
"numberOfBytes": "4"
},
"t_uint64": {
"encoding": "inplace",
"label": "uint64",
"numberOfBytes": "8"
},
"t_uint8": {
"encoding": "inplace",
"label": "uint8",
"numberOfBytes": "1"
}
}
}
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