Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish storage format changes #1042

Merged
merged 9 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions runtime/cmd/decode-state-values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func worker(jobs <-chan entry, wg *sync.WaitGroup, decoded *uint64) {
owner := common.BytesToAddress(rawOwner)

decodeFunction := interpreter.DecodeValue
if version <= 3 {
decodeFunction = interpreter.DecodeValueV3
if version <= 4 {
decodeFunction = interpreter.DecodeValueV4
}

var value interpreter.Value
Expand Down
94 changes: 58 additions & 36 deletions runtime/interpreter/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"io"
"math"
"math/big"
"math/bits"
"strconv"
"strings"
Expand Down Expand Up @@ -328,7 +327,7 @@ func (d *DecoderV5) decodeArray(path []string, deferDecoding bool) (*ArrayValue,
return nil, err
}

// Decode type at array index encodedArrayValueStaticTypeFieldKey
// Decode type at array index encodedArrayValueStaticTypeFieldKeyV5
arrayStaticType, err := d.decodeStaticType()
if err != nil {
return nil, err
Expand Down Expand Up @@ -546,7 +545,7 @@ func (d *DecoderV5) decodeAddressLocation() (common.Location, error) {

// Address

// Decode address at array index encodedAddressLocationAddressFieldKey
// Decode address at array index encodedAddressLocationAddressFieldKeyV5
encodedAddress, err := d.decoder.DecodeBytes()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand All @@ -562,7 +561,7 @@ func (d *DecoderV5) decodeAddressLocation() (common.Location, error) {

// Name

// Decode name at array index encodedAddressLocationNameFieldKey
// Decode name at array index encodedAddressLocationNameFieldKeyV5
name, err := d.decoder.DecodeString()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down Expand Up @@ -605,8 +604,6 @@ func (d *DecoderV5) decodeComposite(path []string) (*CompositeValue, error) {
return NewDeferredCompositeValue(valuePath, content, d.owner, d.decodeCallback, d.version), nil
}

var bigOne = big.NewInt(1)

func (d *DecoderV5) decodeInt() (IntValue, error) {
bigInt, err := d.decoder.DecodeBigInt()
if err != nil {
Expand Down Expand Up @@ -1004,7 +1001,7 @@ func (d *DecoderV5) decodePath() (PathValue, error) {
)
}

// Decode domain at array index encodedPathValueDomainFieldKey
// Decode domain at array index encodedPathValueDomainFieldKeyV5
domain, err := d.decoder.DecodeUint64()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand All @@ -1013,7 +1010,7 @@ func (d *DecoderV5) decodePath() (PathValue, error) {
return PathValue{}, err
}

// Decode identifier at array index encodedPathValueIdentifierFieldKey
// Decode identifier at array index encodedPathValueIdentifierFieldKeyV5
identifier, err := d.decoder.DecodeString()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down Expand Up @@ -1052,7 +1049,7 @@ func (d *DecoderV5) decodeCapability() (CapabilityValue, error) {

// address

// Decode address at array index encodedCapabilityValueAddressFieldKey
// Decode address at array index encodedCapabilityValueAddressFieldKeyV5
var num uint64
num, err = d.decoder.DecodeTagNumber()
if err != nil {
Expand All @@ -1068,7 +1065,7 @@ func (d *DecoderV5) decodeCapability() (CapabilityValue, error) {

// path

// Decode path at array index encodedCapabilityValuePathFieldKey
// Decode path at array index encodedCapabilityValuePathFieldKeyV5
num, err = d.decoder.DecodeTagNumber()
if err != nil {
return CapabilityValue{}, fmt.Errorf("invalid capability path: %w", err)
Expand All @@ -1081,7 +1078,7 @@ func (d *DecoderV5) decodeCapability() (CapabilityValue, error) {
return CapabilityValue{}, fmt.Errorf("invalid capability path: %w", err)
}

// Decode borrow type at array index encodedCapabilityValueBorrowTypeFieldKey
// Decode borrow type at array index encodedCapabilityValueBorrowTypeFieldKeyV5

// borrow type (optional, for backwards compatibility)
// Capabilities used to be untyped, i.e. they didn't have a borrow type.
Expand Down Expand Up @@ -1132,7 +1129,7 @@ func (d *DecoderV5) decodeLink() (LinkValue, error) {
)
}

// Decode path at array index encodedLinkValueTargetPathFieldKey
// Decode path at array index encodedLinkValueTargetPathFieldKeyV5
num, err := d.decoder.DecodeTagNumber()
if err != nil {
return LinkValue{}, fmt.Errorf("invalid link target path encoding: %w", err)
Expand All @@ -1145,7 +1142,7 @@ func (d *DecoderV5) decodeLink() (LinkValue, error) {
return LinkValue{}, fmt.Errorf("invalid link target path encoding: %w", err)
}

// Decode type at array index encodedLinkValueTypeFieldKey
// Decode type at array index encodedLinkValueTypeFieldKeyV5
staticType, err := d.decodeStaticType()
if err != nil {
return LinkValue{}, fmt.Errorf("invalid link type encoding: %w", err)
Expand Down Expand Up @@ -1246,19 +1243,19 @@ func (d *DecoderV5) decodeCompositeStaticType() (StaticType, error) {
)
}

// Decode location at array index encodedCompositeStaticTypeLocationFieldKey
// Decode location at array index encodedCompositeStaticTypeLocationFieldKeyV5
location, err := d.decodeLocation()
if err != nil {
return nil, fmt.Errorf("invalid composite static type location encoding: %w", err)
}

// Skip obsolete element at array index encodedCompositeStaticTypeTypeIDFieldKey
// Skip obsolete element at array index encodedCompositeStaticTypeTypeIDFieldKeyV5
err = d.decoder.Skip()
if err != nil {
return nil, err
}

// Decode qualified identifier at array index encodedCompositeStaticTypeQualifiedIdentifierFieldKey
// Decode qualified identifier at array index encodedCompositeStaticTypeQualifiedIdentifierFieldKeyV5
qualifiedIdentifier, err := d.decoder.DecodeString()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down Expand Up @@ -1300,19 +1297,19 @@ func (d *DecoderV5) decodeInterfaceStaticType() (InterfaceStaticType, error) {
)
}

// Decode location at array index encodedInterfaceStaticTypeLocationFieldKey
// Decode location at array index encodedInterfaceStaticTypeLocationFieldKeyV5
location, err := d.decodeLocation()
if err != nil {
return InterfaceStaticType{}, fmt.Errorf("invalid interface static type location encoding: %w", err)
}

// Skip obsolete element at array index encodedInterfaceStaticTypeTypeIDFieldKey
// Skip obsolete element at array index encodedInterfaceStaticTypeTypeIDFieldKeyV5
err = d.decoder.Skip()
if err != nil {
return InterfaceStaticType{}, err
}

// Decode qualified identifier at array index encodedInterfaceStaticTypeQualifiedIdentifierFieldKey
// Decode qualified identifier at array index encodedInterfaceStaticTypeQualifiedIdentifierFieldKeyV5
qualifiedIdentifier, err := d.decoder.DecodeString()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down Expand Up @@ -1364,7 +1361,7 @@ func (d *DecoderV5) decodeConstantSizedStaticType() (StaticType, error) {
)
}

// Decode size at array index encodedConstantSizedStaticTypeSizeFieldKey
// Decode size at array index encodedConstantSizedStaticTypeSizeFieldKeyV5
size, err := d.decoder.DecodeUint64()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand All @@ -1382,7 +1379,7 @@ func (d *DecoderV5) decodeConstantSizedStaticType() (StaticType, error) {
)
}

// Decode type at array index encodedConstantSizedStaticTypeTypeFieldKey
// Decode type at array index encodedConstantSizedStaticTypeTypeFieldKeyV5
staticType, err := d.decodeStaticType()
if err != nil {
return nil, fmt.Errorf("invalid constant-sized static type inner type encoding: %w", err)
Expand Down Expand Up @@ -1416,7 +1413,7 @@ func (d *DecoderV5) decodeReferenceStaticType() (StaticType, error) {
)
}

// Decode authorized at array index encodedReferenceStaticTypeAuthorizedFieldKey
// Decode authorized at array index encodedReferenceStaticTypeAuthorizedFieldKeyV5
authorized, err := d.decoder.DecodeBool()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand All @@ -1425,7 +1422,7 @@ func (d *DecoderV5) decodeReferenceStaticType() (StaticType, error) {
return nil, err
}

// Decode type at array index encodedReferenceStaticTypeTypeFieldKey
// Decode type at array index encodedReferenceStaticTypeTypeFieldKeyV5
staticType, err := d.decodeStaticType()
if err != nil {
return nil, fmt.Errorf("invalid reference static type inner type encoding: %w", err)
Expand Down Expand Up @@ -1459,13 +1456,13 @@ func (d *DecoderV5) decodeDictionaryStaticType() (StaticType, error) {
)
}

// Decode key type at array index encodedDictionaryStaticTypeKeyTypeFieldKey
// Decode key type at array index encodedDictionaryStaticTypeKeyTypeFieldKeyV5
keyType, err := d.decodeStaticType()
if err != nil {
return nil, fmt.Errorf("invalid dictionary static type key type encoding: %w", err)
}

// Decode value type at array index encodedDictionaryStaticTypeValueTypeFieldKey
// Decode value type at array index encodedDictionaryStaticTypeValueTypeFieldKeyV5
valueType, err := d.decodeStaticType()
if err != nil {
return nil, fmt.Errorf("invalid dictionary static type value type encoding: %w", err)
Expand Down Expand Up @@ -1499,13 +1496,13 @@ func (d *DecoderV5) decodeRestrictedStaticType() (StaticType, error) {
)
}

// Decode restricted type at array index encodedRestrictedStaticTypeTypeFieldKey
// Decode restricted type at array index encodedRestrictedStaticTypeTypeFieldKeyV5
restrictedType, err := d.decodeStaticType()
if err != nil {
return nil, fmt.Errorf("invalid restricted static type key type encoding: %w", err)
}

// Decode restrictions at array index encodedRestrictedStaticTypeRestrictionsFieldKey
// Decode restrictions at array index encodedRestrictedStaticTypeRestrictionsFieldKeyV5
restrictionSize, err := d.decoder.DecodeArrayHead()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down Expand Up @@ -1567,7 +1564,7 @@ func (d *DecoderV5) decodeType() (TypeValue, error) {
)
}

// Decode type at array index encodedTypeValueTypeFieldKey
// Decode type at array index encodedTypeValueTypeFieldKeyV5
var staticType StaticType

// Optional type can be CBOR nil.
Expand Down Expand Up @@ -1612,6 +1609,10 @@ func (d *DecoderV5) decodeCapabilityStaticType() (StaticType, error) {
// This also extracts out the fields raw content and cache it separately inside the value.
//
func decodeCompositeMetaInfo(v *CompositeValue, content []byte) error {
if v.encodingVersion < 5 {
return decodeCompositeMetaInfoV4(v, content)
}

d, err := NewByteDecoder(content, v.Owner, v.encodingVersion, v.decodeCallback)
if err != nil {
return err
Expand Down Expand Up @@ -1642,7 +1643,7 @@ func decodeCompositeMetaInfo(v *CompositeValue, content []byte) error {

// Location

// Decode location at array index encodedCompositeValueLocationFieldKey
// Decode location at array index encodedCompositeValueLocationFieldKeyV5
location, err := d.decodeLocation()
if err != nil {
return fmt.Errorf(
Expand All @@ -1652,15 +1653,15 @@ func decodeCompositeMetaInfo(v *CompositeValue, content []byte) error {
)
}

// Skip obsolete element at array index encodedCompositeValueTypeIDFieldKey
// Skip obsolete element at array index encodedCompositeValueTypeIDFieldKeyV5
err = d.decoder.Skip()
if err != nil {
return err
}

// Kind

// Decode kind at array index encodedCompositeValueKindFieldKey
// Decode kind at array index encodedCompositeValueKindFieldKeyV5
encodedKind, err := d.decoder.DecodeUint64()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down Expand Up @@ -1698,7 +1699,7 @@ func decodeCompositeMetaInfo(v *CompositeValue, content []byte) error {

// Qualified identifier

// Decode qualified identifier at array index encodedCompositeValueQualifiedIdentifierFieldKey
// Decode qualified identifier at array index encodedCompositeValueQualifiedIdentifierFieldKeyV5
qualifiedIdentifier, err := d.decoder.DecodeString()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand All @@ -1722,13 +1723,16 @@ func decodeCompositeMetaInfo(v *CompositeValue, content []byte) error {
// decodeCompositeFields decodes fields from the byte content and updates the composite value.
//
func decodeCompositeFields(v *CompositeValue, content []byte) error {
if v.encodingVersion < 5 {
return decodeCompositeFieldsV4(v, content)
}

d, err := NewByteDecoder(content, v.Owner, v.encodingVersion, v.decodeCallback)
if err != nil {
return err
}

// Decode fields at array index encodedCompositeValueFieldsFieldKey
// Decode fields at array index encodedCompositeValueFieldsFieldKeyV5
fieldsSize, err := d.decoder.DecodeArrayHead()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down Expand Up @@ -1796,6 +1800,12 @@ func decodeCompositeFields(v *CompositeValue, content []byte) error {
}

func decodeArrayMetaInfo(array *ArrayValue, content []byte) error {
if array.encodingVersion < 5 {
// In encoding version 4, no meta info was available for arrays.
// The raw content only consist of the elements.
array.elementsContent = content
return nil
}

d, err := NewByteDecoder(content, array.Owner, array.encodingVersion, array.decodeCallback)
if err != nil {
Expand All @@ -1807,7 +1817,7 @@ func decodeArrayMetaInfo(array *ArrayValue, content []byte) error {
return err
}

// Decode type at array index encodedArrayValueStaticTypeFieldKey
// Decode type at array index encodedArrayValueStaticTypeFieldKeyV5
arrayStaticType, err := d.decodeStaticType()
if err != nil {
return err
Expand Down Expand Up @@ -1842,6 +1852,9 @@ func decodeArrayMetaInfo(array *ArrayValue, content []byte) error {
}

func decodeArrayElements(array *ArrayValue, elementContent []byte) error {
if array.encodingVersion < 5 {
return decodeArrayElementsV4(array, elementContent)
}

d, err := NewByteDecoder(elementContent, array.Owner, array.encodingVersion, array.decodeCallback)
if err != nil {
Expand All @@ -1858,6 +1871,12 @@ func decodeArrayElements(array *ArrayValue, elementContent []byte) error {
}

func decodeDictionaryMetaInfo(v *DictionaryValue, content []byte) error {
if v.encodingVersion < 5 {
// In encoding version 4, no meta info was available for dictionaries.
// The raw content only consist of the entries.
v.entriesContent = content
return nil
}

d, err := NewByteDecoder(content, v.Owner, v.encodingVersion, v.decodeCallback)
if err != nil {
Expand Down Expand Up @@ -1953,13 +1972,16 @@ func decodeDictionaryMetaInfo(v *DictionaryValue, content []byte) error {
}

func decodeDictionaryEntries(v *DictionaryValue, content []byte) error {
if v.encodingVersion < 5 {
return decodeDictionaryEntriesV4(v, content)
}

d, err := NewByteDecoder(content, v.Owner, v.encodingVersion, v.decodeCallback)
if err != nil {
return err
}

// Decode keys at array index encodedDictionaryValueKeysFieldKey
// Decode keys at array index encodedDictionaryValueKeysFieldKeyV5
//nolint:gocritic
keysPath := append(v.valuePath, dictionaryKeyPathPrefix)

Expand Down Expand Up @@ -1991,7 +2013,7 @@ func decodeDictionaryEntries(v *DictionaryValue, content []byte) error {
)
}

// Decode entries at array index encodedDictionaryValueEntriesFieldKey
// Decode entries at array index encodedDictionaryValueEntriesFieldKeyV5
entryCount, err := d.decoder.DecodeArrayHead()
if err != nil {
if e, ok := err.(*cbor.WrongTypeError); ok {
Expand Down
Loading