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

Remove encoding and decoding support for storage references #819

Merged
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
remove encoding and decoding support for storage references
  • Loading branch information
turbolent committed Apr 21, 2021
commit c163860dbd971f3c971ee011aeb64fc2cfb4859b
37 changes: 0 additions & 37 deletions runtime/interpreter/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,6 @@ func (d *DecoderV4) decodeValue(v interface{}, path []string) (Value, error) {
case cborTagCapabilityValue:
return d.decodeCapability(v.Content)

case cborTagStorageReferenceValue:
return d.decodeStorageReference(v.Content)

case cborTagLinkValue:
return d.decodeLink(v.Content)

Expand Down Expand Up @@ -934,40 +931,6 @@ func (d *DecoderV4) decodeSome(v interface{}, path []string) (*SomeValue, error)
}, nil
}

func (d *DecoderV4) decodeStorageReference(v interface{}) (*StorageReferenceValue, error) {
encoded, ok := v.(cborArray)
const expectedLength = encodedStorageReferenceValueLength
if !ok || len(encoded) != expectedLength {
return nil, fmt.Errorf("invalid storage reference encoding: expected [%d]interface{}, got %T",
expectedLength,
v,
)
}

authorized, ok := encoded[encodedStorageReferenceValueAuthorizedFieldKey].(bool)
if !ok {
return nil, fmt.Errorf("invalid storage reference authorized encoding: %T", authorized)
}

targetStorageAddressBytes, ok := encoded[encodedStorageReferenceValueTargetStorageAddressFieldKey].([]byte)
if !ok {
return nil, fmt.Errorf("invalid storage reference target storage address encoding: %T", authorized)
}

targetStorageAddress := common.BytesToAddress(targetStorageAddressBytes)

targetKey, ok := encoded[encodedStorageReferenceValueTargetKeyFieldKey].(string)
if !ok {
return nil, fmt.Errorf("invalid storage reference target key encoding: %T", targetKey)
}

return &StorageReferenceValue{
Authorized: authorized,
TargetStorageAddress: targetStorageAddress,
TargetKey: targetKey,
}, nil
}

func (d *DecoderV4) checkAddressLength(addressBytes []byte) error {
actualLength := len(addressBytes)
const expectedLength = common.AddressLength
Expand Down
7 changes: 7 additions & 0 deletions runtime/interpreter/decode_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,13 @@ func (d *DecoderV3) decodeSome(v interface{}, path []string) (*SomeValue, error)
}, nil
}

// NOTE: NEVER change, only add/increment; ensure uint64
const (
encodedStorageReferenceValueAuthorizedFieldKey uint64 = 0
encodedStorageReferenceValueTargetStorageAddressFieldKey uint64 = 1
encodedStorageReferenceValueTargetKeyFieldKey uint64 = 2
)

func (d *DecoderV3) decodeStorageReference(v interface{}) (*StorageReferenceValue, error) {
encoded, ok := v.(map[interface{}]interface{})
if !ok {
Expand Down
29 changes: 1 addition & 28 deletions runtime/interpreter/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const (

cborTagPathValue
cborTagCapabilityValue
cborTagStorageReferenceValue
cborTagStorageReferenceValue // deprecated
cborTagLinkValue
_
_
Expand Down Expand Up @@ -412,9 +412,6 @@ func (e *Encoder) prepare(

// Storage

case *StorageReferenceValue:
return e.prepareStorageReferenceValue(v), nil

case PathValue:
return e.preparePathValue(v), nil

Expand Down Expand Up @@ -858,30 +855,6 @@ func (e *Encoder) prepareSomeValue(
}, nil
}

// NOTE: NEVER change, only add/increment; ensure uint64
const (
encodedStorageReferenceValueAuthorizedFieldKey uint64 = 0
encodedStorageReferenceValueTargetStorageAddressFieldKey uint64 = 1
encodedStorageReferenceValueTargetKeyFieldKey uint64 = 2

// !!! *WARNING* !!!
//
// encodedStorageReferenceValueLength MUST be updated when new element is added.
// It is used to verify encoded storage reference length during decoding.
encodedStorageReferenceValueLength int = 3
)

func (e *Encoder) prepareStorageReferenceValue(v *StorageReferenceValue) interface{} {
return cbor.Tag{
Number: cborTagStorageReferenceValue,
Content: cborArray{
encodedStorageReferenceValueAuthorizedFieldKey: v.Authorized,
encodedStorageReferenceValueTargetStorageAddressFieldKey: v.TargetStorageAddress.Bytes(),
encodedStorageReferenceValueTargetKeyFieldKey: v.TargetKey,
},
}
}

func (e *Encoder) prepareAddressValue(v AddressValue) cbor.Tag {
return cbor.Tag{
Number: cborTagAddressValue,
Expand Down
132 changes: 0 additions & 132 deletions runtime/interpreter/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3218,138 +3218,6 @@ func TestEncodeDecodeUFix64Value(t *testing.T) {
})
}

func TestEncodeDecodeStorageReferenceValue(t *testing.T) {

t.Parallel()

t.Run("not-authorized", func(t *testing.T) {
value := &StorageReferenceValue{
Authorized: false,
TargetKey: "test-key1",
TargetStorageAddress: common.BytesToAddress([]byte{0x11}),
}

encoded := []byte{
// tag
0xd8, cborTagStorageReferenceValue,
// array, 3 items follow
0x83,
// false
0xf4,
// byte sequence, length 1
0x41,
// positive integer 0x11
0x11,
// UTF-8 string, 9 bytes follow
0x69,
// t, e, s, t, -, k, e, y, 1
0x74, 0x65, 0x73, 0x74, 0x2d, 0x6b, 0x65, 0x79, 0x31,
}

version3Encoded := []byte{
// tag
0xd8, cborTagStorageReferenceValue,
// map, 3 pairs of items follow
0xa3,
// key 0
0x0,
// false
0xf4,
// key 1
0x1,
// byte sequence, length 1
0x41,
// positive integer 0x11
0x11,
// key2
0x2,
// UTF-8 string, 9 bytes follow
0x69,
// t, e, s, t, -, k, e, y, 1
0x74, 0x65, 0x73, 0x74, 0x2d, 0x6b, 0x65, 0x79, 0x31,
}

testEncodeDecode(t,
encodeDecodeTest{
value: value,
encoded: encoded,
},
)

testEncodeDecodeOldFormat(t,
encodeDecodeTest{
value: value,
encoded: encoded,
},
3,
version3Encoded,
)
})

t.Run("authorized", func(t *testing.T) {
value := &StorageReferenceValue{
Authorized: true,
TargetKey: "test-key2",
TargetStorageAddress: common.BytesToAddress([]byte{0x12}),
}

encoded := []byte{
// tag
0xd8, cborTagStorageReferenceValue,
// array, 3 items follow
0x83,
// true
0xf5,
// byte sequence, length 1
0x41,
// positive integer 0x12
0x12,
// UTF-8 string, 9 bytes follow
0x69,
// t, e, s, t, -, k, e, y, 2
0x74, 0x65, 0x73, 0x74, 0x2d, 0x6b, 0x65, 0x79, 0x32,
}

version3Encoded := []byte{
// tag
0xd8, cborTagStorageReferenceValue,
// map, 3 pairs of items follow
0xa3,
// key 0
0x0,
// true
0xf5,
// key 1
0x1,
// byte sequence, length 1
0x41,
// positive integer 0x12
0x12,
// key 2
0x2,
// UTF-8 string, 9 bytes follow
0x69,
// t, e, s, t, -, k, e, y, 2
0x74, 0x65, 0x73, 0x74, 0x2d, 0x6b, 0x65, 0x79, 0x32,
}
testEncodeDecode(t,
encodeDecodeTest{
value: value,
encoded: encoded,
},
)

testEncodeDecodeOldFormat(t,
encodeDecodeTest{
value: value,
encoded: encoded,
},
3,
version3Encoded,
)
})
}

func TestEncodeDecodeAddressValue(t *testing.T) {

t.Parallel()
Expand Down