-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parachain compatibility (#412)
* modules * `aura-ext` * block executor * `parachain-system` * api * `collect_collation_info` * `validate_block` * internal trie representation for `io.Storage` and `io.TransactionBroker`
- Loading branch information
Showing
175 changed files
with
7,388 additions
and
267 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
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,92 @@ | ||
package collect_collation_info | ||
|
||
import ( | ||
"bytes" | ||
|
||
sc "github.com/LimeChain/goscale" | ||
"github.com/LimeChain/gosemble/constants/metadata" | ||
"github.com/LimeChain/gosemble/frame/parachain_system" | ||
"github.com/LimeChain/gosemble/primitives/hashing" | ||
"github.com/LimeChain/gosemble/primitives/log" | ||
primitives "github.com/LimeChain/gosemble/primitives/types" | ||
"github.com/LimeChain/gosemble/utils" | ||
) | ||
|
||
const ( | ||
ApiModuleName = "CollectCollationInfo" | ||
apiVersion = 2 | ||
) | ||
|
||
// Module implements the CollectCollationInfo Runtime API definition. | ||
// | ||
// For more information about API definition, see: | ||
// https://github.com/paritytech/polkadot-sdk/blob/master/cumulus/primitives/core/src/lib.rs#L378 | ||
type Module struct { | ||
parachainSystem parachain_system.Module | ||
memUtils utils.WasmMemoryTranslator | ||
logger log.RuntimeLogger | ||
} | ||
|
||
func New(parachainSystem parachain_system.Module, logger log.RuntimeLogger) Module { | ||
return Module{ | ||
memUtils: utils.NewMemoryTranslator(), | ||
parachainSystem: parachainSystem, | ||
logger: logger, | ||
} | ||
} | ||
|
||
// Name returns the name of the api module | ||
func (m Module) Name() string { | ||
return ApiModuleName | ||
} | ||
|
||
// Item returns the first 8 bytes of the Blake2b hash of the name and version of the api module. | ||
func (m Module) Item() primitives.ApiItem { | ||
hash := hashing.MustBlake2b8([]byte(ApiModuleName)) | ||
return primitives.NewApiItem(hash, apiVersion) | ||
} | ||
|
||
// CollectCollationInfo returns the collation information by the header of a built block. | ||
// It takes two arguments: | ||
// - dataPtr: Pointer to the data in the Wasm memory. | ||
// - dataLen: Length of the data. | ||
// which represent the SCALE-encoded block header. | ||
// Returns a pointer-size of the SCALE-encoded collation information. | ||
func (m Module) CollectCollationInfo(dataPtr int32, dataLen int32) int64 { | ||
b := m.memUtils.GetWasmMemorySlice(dataPtr, dataLen) | ||
buffer := bytes.NewBuffer(b) | ||
|
||
header, err := primitives.DecodeHeader(buffer) | ||
if err != nil { | ||
m.logger.Critical(err.Error()) | ||
} | ||
|
||
collationInfo, err := m.parachainSystem.CollectCollationInfo(header) | ||
if err != nil { | ||
m.logger.Critical(err.Error()) | ||
} | ||
|
||
return m.memUtils.BytesToOffsetAndSize(collationInfo.Bytes()) | ||
} | ||
|
||
func (m Module) Metadata() primitives.RuntimeApiMetadata { | ||
methods := sc.Sequence[primitives.RuntimeApiMethodMetadata]{ | ||
primitives.RuntimeApiMethodMetadata{ | ||
Name: "collect_collation_info", | ||
Inputs: sc.Sequence[primitives.RuntimeApiMethodParamMetadata]{ | ||
primitives.RuntimeApiMethodParamMetadata{ | ||
Name: "header", | ||
Type: sc.ToCompact(metadata.Header), | ||
}, | ||
}, | ||
Output: sc.ToCompact(metadata.TypesParachainValidationResult), | ||
Docs: sc.Sequence[sc.Str]{"Returns the collation information by the block header."}, | ||
}, | ||
} | ||
|
||
return primitives.RuntimeApiMetadata{ | ||
Name: ApiModuleName, | ||
Methods: methods, | ||
Docs: sc.Sequence[sc.Str]{" The collect collation info api."}, | ||
} | ||
} |
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,146 @@ | ||
package collect_collation_info | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
sc "github.com/LimeChain/goscale" | ||
"github.com/LimeChain/gosemble/constants/metadata" | ||
"github.com/LimeChain/gosemble/mocks" | ||
"github.com/LimeChain/gosemble/primitives/log" | ||
"github.com/LimeChain/gosemble/primitives/parachain" | ||
primitives "github.com/LimeChain/gosemble/primitives/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
dataPtr = int32(0) | ||
dataLen = int32(1) | ||
ptrAndSize = int64(6) | ||
|
||
parentHash = common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c").ToBytes() | ||
stateRoot = common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355b").ToBytes() | ||
extrinsicsRoot = common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355a").ToBytes() | ||
header = primitives.Header{ | ||
ParentHash: primitives.Blake2bHash{ | ||
FixedSequence: sc.BytesToFixedSequenceU8(parentHash)}, | ||
Number: 5, | ||
StateRoot: primitives.H256{FixedSequence: sc.BytesToFixedSequenceU8(stateRoot)}, | ||
ExtrinsicsRoot: primitives.H256{FixedSequence: sc.BytesToFixedSequenceU8(extrinsicsRoot)}, | ||
Digest: primitives.NewDigest(sc.Sequence[primitives.DigestItem]{}), | ||
} | ||
collationInfo = parachain.CollationInfo{ | ||
UpwardMessages: sc.Sequence[parachain.UpwardMessage]{}, | ||
HorizontalMessages: sc.Sequence[parachain.OutboundHrmpMessage]{}, | ||
ValidationCode: sc.Option[sc.Sequence[sc.U8]]{}, | ||
ProcessedDownwardMessages: 0, | ||
HrmpWatermark: 0, | ||
HeadData: sc.Sequence[sc.U8]{}, | ||
} | ||
errPanic = errors.New("panic") | ||
) | ||
|
||
var ( | ||
mockParachainSystemModule *mocks.ParachainSystemModule | ||
mockMemoryUtils *mocks.MemoryTranslator | ||
) | ||
|
||
func Test_Module_Name(t *testing.T) { | ||
target := setup() | ||
|
||
result := target.Name() | ||
|
||
assert.Equal(t, ApiModuleName, result) | ||
} | ||
|
||
func Test_Module_Item(t *testing.T) { | ||
target := setup() | ||
|
||
hexName := common.MustBlake2b8([]byte(ApiModuleName)) | ||
expect := primitives.NewApiItem(hexName, apiVersion) | ||
|
||
result := target.Item() | ||
|
||
assert.Equal(t, expect, result) | ||
} | ||
|
||
func Test_Module_CollectCollationInfo(t *testing.T) { | ||
target := setup() | ||
|
||
mockMemoryUtils.On("GetWasmMemorySlice", dataPtr, dataLen).Return(header.Bytes()) | ||
mockParachainSystemModule.On("CollectCollationInfo", header).Return(collationInfo, nil) | ||
mockMemoryUtils.On("BytesToOffsetAndSize", collationInfo.Bytes()).Return(ptrAndSize) | ||
|
||
result := target.CollectCollationInfo(dataPtr, dataLen) | ||
|
||
assert.Equal(t, ptrAndSize, result) | ||
mockMemoryUtils.AssertCalled(t, "GetWasmMemorySlice", dataPtr, dataLen) | ||
mockParachainSystemModule.AssertCalled(t, "CollectCollationInfo", header) | ||
mockMemoryUtils.AssertCalled(t, "BytesToOffsetAndSize", collationInfo.Bytes()) | ||
} | ||
|
||
func Test_Module_CollectCollationInfo_Header_Panics(t *testing.T) { | ||
target := setup() | ||
|
||
mockMemoryUtils.On("GetWasmMemorySlice", dataPtr, dataLen).Return([]byte{}) | ||
|
||
assert.PanicsWithValue(t, | ||
io.EOF.Error(), | ||
func() { target.CollectCollationInfo(dataPtr, dataLen) }, | ||
) | ||
|
||
mockMemoryUtils.AssertCalled(t, "GetWasmMemorySlice", dataPtr, dataLen) | ||
} | ||
|
||
func Test_Module_CollectCollationInfo_Panics(t *testing.T) { | ||
target := setup() | ||
|
||
expectedErr := errors.New("panic") | ||
|
||
mockMemoryUtils.On("GetWasmMemorySlice", dataPtr, dataLen).Return(header.Bytes()) | ||
mockParachainSystemModule.On("CollectCollationInfo", header).Return(collationInfo, expectedErr) | ||
|
||
assert.PanicsWithValue(t, | ||
errPanic.Error(), | ||
func() { target.CollectCollationInfo(dataPtr, dataLen) }, | ||
) | ||
|
||
mockMemoryUtils.AssertCalled(t, "GetWasmMemorySlice", dataPtr, dataLen) | ||
mockParachainSystemModule.On("CollectCollationInfo", header).Return(collationInfo, expectedErr) | ||
} | ||
|
||
func Test_Module_Metadata(t *testing.T) { | ||
target := setup() | ||
|
||
expect := primitives.RuntimeApiMetadata{ | ||
Name: ApiModuleName, | ||
Methods: sc.Sequence[primitives.RuntimeApiMethodMetadata]{ | ||
primitives.RuntimeApiMethodMetadata{ | ||
Name: "collect_collation_info", | ||
Inputs: sc.Sequence[primitives.RuntimeApiMethodParamMetadata]{ | ||
primitives.RuntimeApiMethodParamMetadata{ | ||
Name: "header", | ||
Type: sc.ToCompact(metadata.Header), | ||
}, | ||
}, | ||
Output: sc.ToCompact(metadata.TypesParachainValidationResult), | ||
Docs: sc.Sequence[sc.Str]{"Returns the collation information by the block header."}, | ||
}, | ||
}, | ||
Docs: sc.Sequence[sc.Str]{" The collect collation info api."}, | ||
} | ||
|
||
assert.Equal(t, expect, target.Metadata()) | ||
} | ||
|
||
func setup() Module { | ||
mockParachainSystemModule = new(mocks.ParachainSystemModule) | ||
mockMemoryUtils = new(mocks.MemoryTranslator) | ||
|
||
target := New(mockParachainSystemModule, log.NewLogger()) | ||
target.memUtils = mockMemoryUtils | ||
|
||
return target | ||
} |
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
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
Oops, something went wrong.