-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: alvarius <[email protected]>
- Loading branch information
Showing
38 changed files
with
2,845 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/services": patch | ||
--- | ||
|
||
protocol-parser in Go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package protocolparser | ||
|
||
import schematype "latticexyz/mud/packages/services/pkg/schema-type" | ||
|
||
func AbiTypesToSchema(abiTypes []schematype.SchemaType) Schema { | ||
staticFields := []schematype.SchemaType{} | ||
dynamicFields := []schematype.SchemaType{} | ||
|
||
for _, abiType := range abiTypes { | ||
if abiType.IsDynamic() { | ||
dynamicFields = append(dynamicFields, abiType) | ||
} else { | ||
staticFields = append(staticFields, abiType) | ||
} | ||
} | ||
|
||
return Schema{ | ||
StaticFields: staticFields, | ||
DynamicFields: dynamicFields, | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/services/pkg/protocol-parser/abiTypesToSchema_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package protocolparser_test | ||
|
||
import ( | ||
protocolparser "latticexyz/mud/packages/services/pkg/protocol-parser" | ||
. "latticexyz/mud/packages/services/pkg/schema-type" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestAbiTypesToSchema(testing *testing.T) { | ||
abiTypesList := [][]SchemaType{ | ||
{ | ||
BOOL, | ||
}, | ||
{ | ||
BOOL, | ||
BOOL_ARRAY, | ||
}, | ||
{ | ||
BYTES32, | ||
INT32, | ||
UINT256_ARRAY, | ||
ADDRESS_ARRAY, | ||
BYTES, | ||
STRING, | ||
}, | ||
} | ||
|
||
expectedSchemas := []protocolparser.Schema{ | ||
{ | ||
StaticFields: []SchemaType{ | ||
BOOL, | ||
}, | ||
DynamicFields: []SchemaType{}, | ||
}, | ||
{ | ||
StaticFields: []SchemaType{ | ||
BOOL, | ||
}, | ||
DynamicFields: []SchemaType{ | ||
BOOL_ARRAY, | ||
}, | ||
}, | ||
{ | ||
StaticFields: []SchemaType{ | ||
BYTES32, | ||
INT32, | ||
}, | ||
DynamicFields: []SchemaType{ | ||
UINT256_ARRAY, | ||
ADDRESS_ARRAY, | ||
BYTES, | ||
STRING, | ||
}, | ||
}, | ||
} | ||
|
||
for i, abiTypes := range abiTypesList { | ||
schema := protocolparser.AbiTypesToSchema(abiTypes) | ||
if !reflect.DeepEqual(schema, expectedSchemas[i]) { | ||
testing.Errorf("expected schema to be %v, got %v", expectedSchemas[i], schema) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package protocolparser | ||
|
||
import schematype "latticexyz/mud/packages/services/pkg/schema-type" | ||
|
||
type Schema struct { | ||
StaticFields []schematype.SchemaType | ||
DynamicFields []schematype.SchemaType | ||
} | ||
|
||
type TableSchema struct { | ||
KeySchema Schema | ||
ValueSchema Schema | ||
} |
137 changes: 137 additions & 0 deletions
137
packages/services/pkg/protocol-parser/decodeDynamicField.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package protocolparser | ||
|
||
import ( | ||
. "latticexyz/mud/packages/services/pkg/schema-type" | ||
|
||
"github.com/andriidski/abiencode-go/convert" | ||
) | ||
|
||
func DecodeDynamicField(schemaType SchemaType, data string) interface{} { | ||
if schemaType == BYTES { | ||
return convert.HexToBytes(data) | ||
} | ||
if schemaType == STRING { | ||
return convert.HexToString(data) | ||
} | ||
|
||
if len(data) > 3 && len(data)%2 != 0 { | ||
panic(ErrInvalidHexLength) | ||
} | ||
|
||
dataSize := (len(data) - 2) / 2 | ||
|
||
switch schemaType { | ||
case UINT8_ARRAY, | ||
UINT16_ARRAY, | ||
UINT24_ARRAY, | ||
UINT32_ARRAY, | ||
UINT40_ARRAY, | ||
UINT48_ARRAY, | ||
UINT56_ARRAY, | ||
UINT64_ARRAY, | ||
UINT72_ARRAY, | ||
UINT80_ARRAY, | ||
UINT88_ARRAY, | ||
UINT96_ARRAY, | ||
UINT104_ARRAY, | ||
UINT112_ARRAY, | ||
UINT120_ARRAY, | ||
UINT128_ARRAY, | ||
UINT136_ARRAY, | ||
UINT144_ARRAY, | ||
UINT152_ARRAY, | ||
UINT160_ARRAY, | ||
UINT168_ARRAY, | ||
UINT176_ARRAY, | ||
UINT184_ARRAY, | ||
UINT192_ARRAY, | ||
UINT200_ARRAY, | ||
UINT208_ARRAY, | ||
UINT216_ARRAY, | ||
UINT224_ARRAY, | ||
UINT232_ARRAY, | ||
UINT240_ARRAY, | ||
UINT248_ARRAY, | ||
UINT256_ARRAY, | ||
INT8_ARRAY, | ||
INT16_ARRAY, | ||
INT24_ARRAY, | ||
INT32_ARRAY, | ||
INT40_ARRAY, | ||
INT48_ARRAY, | ||
INT56_ARRAY, | ||
INT64_ARRAY, | ||
INT72_ARRAY, | ||
INT80_ARRAY, | ||
INT88_ARRAY, | ||
INT96_ARRAY, | ||
INT104_ARRAY, | ||
INT112_ARRAY, | ||
INT120_ARRAY, | ||
INT128_ARRAY, | ||
INT136_ARRAY, | ||
INT144_ARRAY, | ||
INT152_ARRAY, | ||
INT160_ARRAY, | ||
INT168_ARRAY, | ||
INT176_ARRAY, | ||
INT184_ARRAY, | ||
INT192_ARRAY, | ||
INT200_ARRAY, | ||
INT208_ARRAY, | ||
INT216_ARRAY, | ||
INT224_ARRAY, | ||
INT232_ARRAY, | ||
INT240_ARRAY, | ||
INT248_ARRAY, | ||
INT256_ARRAY, | ||
BYTES1_ARRAY, | ||
BYTES2_ARRAY, | ||
BYTES3_ARRAY, | ||
BYTES4_ARRAY, | ||
BYTES5_ARRAY, | ||
BYTES6_ARRAY, | ||
BYTES7_ARRAY, | ||
BYTES8_ARRAY, | ||
BYTES9_ARRAY, | ||
BYTES10_ARRAY, | ||
BYTES11_ARRAY, | ||
BYTES12_ARRAY, | ||
BYTES13_ARRAY, | ||
BYTES14_ARRAY, | ||
BYTES15_ARRAY, | ||
BYTES16_ARRAY, | ||
BYTES17_ARRAY, | ||
BYTES18_ARRAY, | ||
BYTES19_ARRAY, | ||
BYTES20_ARRAY, | ||
BYTES21_ARRAY, | ||
BYTES22_ARRAY, | ||
BYTES23_ARRAY, | ||
BYTES24_ARRAY, | ||
BYTES25_ARRAY, | ||
BYTES26_ARRAY, | ||
BYTES27_ARRAY, | ||
BYTES28_ARRAY, | ||
BYTES29_ARRAY, | ||
BYTES30_ARRAY, | ||
BYTES31_ARRAY, | ||
BYTES32_ARRAY, | ||
BOOL_ARRAY, | ||
ADDRESS_ARRAY: | ||
staticAbiType := ArrayAbiTypeToStaticAbiType(schemaType) | ||
itemByteLength := staticAbiType.ByteLength() | ||
if dataSize%itemByteLength != 0 { | ||
panic(ErrInvalidHexLengthForArrayField) | ||
} | ||
itemCount := dataSize / int(itemByteLength) | ||
items := make([]interface{}, itemCount) | ||
for i := 0; i < itemCount; i++ { | ||
itemData := HexSlice(data, i*itemByteLength, (i+1)*itemByteLength) | ||
items[i] = DecodeStaticField(staticAbiType, itemData) | ||
} | ||
return items | ||
default: | ||
panic("unsupported type") | ||
} | ||
} |
Oops, something went wrong.