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

Unify types: PublicKey and Address32 #273

Merged
merged 12 commits into from
Nov 10, 2023
14 changes: 7 additions & 7 deletions api/account_nonce/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ const (
apiVersion = 1
)

type Module struct {
type Module[T types.PublicKey] struct {
systemModule system.Module
memUtils utils.WasmMemoryTranslator
}

func New(systemModule system.Module) Module {
return Module{
func New[T types.PublicKey](systemModule system.Module) Module[T] {
return Module[T]{
systemModule: systemModule,
memUtils: utils.NewMemoryTranslator(),
}
}

func (m Module) Name() string {
func (m Module[T]) Name() string {
return ApiModuleName
}

func (m Module) Item() types.ApiItem {
func (m Module[T]) Item() types.ApiItem {
hash := hashing.MustBlake2b8([]byte(ApiModuleName))
return types.NewApiItem(hash, apiVersion)
}
Expand All @@ -43,11 +43,11 @@ func (m Module) Item() types.ApiItem {
// which represent the SCALE-encoded AccountId.
// Returns a pointer-size of the SCALE-encoded nonce of the AccountId.
// [Specification](https://spec.polkadot.network/chap-runtime-api#sect-accountnonceapi-account-nonce)
func (m Module) AccountNonce(dataPtr int32, dataLen int32) int64 {
func (m Module[T]) AccountNonce(dataPtr int32, dataLen int32) int64 {
b := m.memUtils.GetWasmMemorySlice(dataPtr, dataLen)
buffer := bytes.NewBuffer(b)

publicKey, err := types.DecodePublicKey(buffer)
publicKey, err := types.DecodeAccountId[T](buffer)
if err != nil {
log.Critical(err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions api/account_nonce/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_Module_Item(t *testing.T) {
func Test_Module_AccountNonce(t *testing.T) {
target := setup()

publicKey := constants.OneAddress.FixedSequence
publicKey := constants.OneAddressAccountId
nonce := sc.U32(5)
accountInfo := types.AccountInfo{
Nonce: nonce,
Expand All @@ -55,11 +55,11 @@ func Test_Module_AccountNonce(t *testing.T) {
mockMemoryUtils.AssertCalled(t, "BytesToOffsetAndSize", nonce.Bytes())
}

func setup() Module {
func setup() Module[types.Ed25519PublicKey] {
mockSystem = new(mocks.SystemModule)
mockMemoryUtils = new(mocks.MemoryTranslator)

target := New(mockSystem)
target := New[types.Ed25519PublicKey](mockSystem)
target.memUtils = mockMemoryUtils

return target
Expand Down
2 changes: 1 addition & 1 deletion api/grandpa/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_Authorities_None(t *testing.T) {

authorities := sc.Sequence[types.Authority]{
{
Id: constants.ZeroAddress.FixedSequence,
Id: constants.ZeroAddressAccountId,
Weight: sc.U64(64),
},
}
Expand Down
20 changes: 10 additions & 10 deletions api/session_keys/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ const (
apiVersion = 1
)

type Module struct {
type Module[T types.PublicKey] struct {
sessions []types.Session
crypto io.Crypto
memUtils utils.WasmMemoryTranslator
}

func New(sessions []types.Session) Module {
return Module{
func New[T types.PublicKey](sessions []types.Session) Module[T] {
return Module[T]{
sessions: sessions,
crypto: io.NewCrypto(),
memUtils: utils.NewMemoryTranslator(),
}
}

func (m Module) Name() string {
func (m Module[T]) Name() string {
return ApiModuleName
}

func (m Module) Item() types.ApiItem {
func (m Module[T]) Item() types.ApiItem {
hash := hashing.MustBlake2b8([]byte(ApiModuleName))
return types.NewApiItem(hash, apiVersion)
}
Expand All @@ -47,7 +47,7 @@ func (m Module) Item() types.ApiItem {
// which represent the SCALE-encoded optional seed.
// Returns a pointer-size of the SCALE-encoded set of keys.
// [Specification](https://spec.polkadot.network/chap-runtime-api#id-sessionkeys_generate_session_keys)
func (m Module) GenerateSessionKeys(dataPtr int32, dataLen int32) int64 {
func (m Module[T]) GenerateSessionKeys(dataPtr int32, dataLen int32) int64 {
b := m.memUtils.GetWasmMemorySlice(dataPtr, dataLen)
buffer := bytes.NewBuffer(b)

Expand Down Expand Up @@ -77,7 +77,7 @@ func (m Module) GenerateSessionKeys(dataPtr int32, dataLen int32) int64 {
// which represent the SCALE-encoded keys.
// Returns a pointer-size of the SCALE-encoded set of raw keys and their respective key type.
// [Specification](https://spec.polkadot.network/chap-runtime-api#id-sessionkeys_decode_session_keys)
func (m Module) DecodeSessionKeys(dataPtr int32, dataLen int32) int64 {
func (m Module[T]) DecodeSessionKeys(dataPtr int32, dataLen int32) int64 {
b := m.memUtils.GetWasmMemorySlice(dataPtr, dataLen)
buffer := bytes.NewBuffer(b)
sequence, err := sc.DecodeSequenceWith(buffer, sc.DecodeU8)
Expand All @@ -88,19 +88,19 @@ func (m Module) DecodeSessionKeys(dataPtr int32, dataLen int32) int64 {
buffer = bytes.NewBuffer(sc.SequenceU8ToBytes(sequence))
sessionKeys := sc.Sequence[types.SessionKey]{}
for _, session := range m.sessions {
pk, err := types.DecodePublicKey(buffer)
pk, err := types.DecodeAccountId[T](buffer)
if err != nil {
log.Critical(err.Error())
}
sessionKey := types.NewSessionKey(sc.FixedSequenceU8ToBytes(pk), session.KeyTypeId())
sessionKey := types.NewSessionKey(pk.Bytes(), session.KeyTypeId())
sessionKeys = append(sessionKeys, sessionKey)
}

result := sc.NewOption[sc.Sequence[types.SessionKey]](sessionKeys)
return m.memUtils.BytesToOffsetAndSize(result.Bytes())
}

func getKeyFunction(m Module, keyType types.PublicKeyType) func([]byte, []byte) []byte {
func getKeyFunction[T types.PublicKey](m Module[T], keyType types.PublicKeyType) func([]byte, []byte) []byte {
switch keyType {
case types.PublicKeyEd25519:
return m.crypto.Ed25519Generate
Expand Down
4 changes: 2 additions & 2 deletions api/session_keys/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func Test_Module_DecodeSessionKeys(t *testing.T) {
mockMemoryUtils.AssertCalled(t, "BytesToOffsetAndSize", expect.Bytes())
}

func setup() Module {
func setup() Module[primitives.Ed25519PublicKey] {
mockCrypto = new(mocks.IoCrypto)
mockMemoryUtils = new(mocks.MemoryTranslator)
mockSessionKey = new(mocks.AuraModule)
Expand All @@ -155,7 +155,7 @@ func setup() Module {
mockSessionKey,
}

target := New(sessions)
target := New[primitives.Ed25519PublicKey](sessions)
target.crypto = mockCrypto
target.memUtils = mockMemoryUtils

Expand Down
Binary file modified build/runtime.wasm
Binary file not shown.
9 changes: 6 additions & 3 deletions constants/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package constants
import primitives "github.com/LimeChain/gosemble/primitives/types"

var (
ZeroAddress, _ = primitives.NewAddress32(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
OneAddress, _ = primitives.NewAddress32(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
TwoAddress, _ = primitives.NewAddress32(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2)
ed25519SignerZero, _ = primitives.NewEd25519PublicKey(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
ed25519SignerOne, _ = primitives.NewEd25519PublicKey(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
ed25519SignerTwo, _ = primitives.NewEd25519PublicKey(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2)
ZeroAddressAccountId = primitives.NewAccountId[primitives.PublicKey](ed25519SignerZero)
OneAddressAccountId = primitives.NewAccountId[primitives.PublicKey](ed25519SignerOne)
TwoAddressAccountId = primitives.NewAccountId[primitives.PublicKey](ed25519SignerTwo)
)
12 changes: 6 additions & 6 deletions execution/types/checked_extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
type checkedExtrinsic struct {
// Who this purports to be from and the number of extrinsics have come before
// from the same signer, if anyone (note this is not a signature).
signer sc.Option[primitives.Address32]
signer sc.Option[primitives.AccountId[primitives.PublicKey]]
function primitives.Call
extra primitives.SignedExtra
transactional support.Transactional[primitives.PostDispatchInfo, primitives.DispatchError]
}

func NewCheckedExtrinsic(signer sc.Option[primitives.Address32], function primitives.Call, extra primitives.SignedExtra) primitives.CheckedExtrinsic {
func NewCheckedExtrinsic(signer sc.Option[primitives.AccountId[primitives.PublicKey]], function primitives.Call, extra primitives.SignedExtra) primitives.CheckedExtrinsic {
return checkedExtrinsic{
signer: signer,
function: function,
Expand All @@ -37,7 +37,7 @@ func (c checkedExtrinsic) Function() primitives.Call {

func (c checkedExtrinsic) Apply(validator primitives.UnsignedValidator, info *primitives.DispatchInfo, length sc.Compact) (primitives.DispatchResultWithPostInfo[primitives.PostDispatchInfo], primitives.TransactionValidityError) {
var (
maybeWho sc.Option[primitives.Address32]
maybeWho sc.Option[primitives.AccountId[primitives.PublicKey]]
maybePre sc.Option[sc.Sequence[primitives.Pre]]
)

Expand All @@ -47,7 +47,7 @@ func (c checkedExtrinsic) Apply(validator primitives.UnsignedValidator, info *pr
if err != nil {
return primitives.DispatchResultWithPostInfo[primitives.PostDispatchInfo]{}, err
}
maybeWho, maybePre = sc.NewOption[primitives.Address32](id), sc.NewOption[sc.Sequence[primitives.Pre]](pre)
maybeWho, maybePre = sc.NewOption[primitives.AccountId[primitives.PublicKey]](id), sc.NewOption[sc.Sequence[primitives.Pre]](pre)
} else {
// Do any pre-flight stuff for an unsigned transaction.
//
Expand All @@ -67,7 +67,7 @@ func (c checkedExtrinsic) Apply(validator primitives.UnsignedValidator, info *pr
return primitives.DispatchResultWithPostInfo[primitives.PostDispatchInfo]{}, err
}

maybeWho, maybePre = sc.NewOption[primitives.Address32](nil), sc.NewOption[sc.Sequence[primitives.Pre]](nil)
maybeWho, maybePre = sc.NewOption[primitives.AccountId[primitives.PublicKey]](nil), sc.NewOption[sc.Sequence[primitives.Pre]](nil)
}

var resWithInfo primitives.DispatchResultWithPostInfo[primitives.PostDispatchInfo]
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c checkedExtrinsic) Validate(validator primitives.UnsignedValidator, sourc
return valid.CombineWith(unsignedValidation), nil
}

func (c checkedExtrinsic) dispatch(maybeWho sc.Option[primitives.Address32]) (primitives.PostDispatchInfo, primitives.DispatchError) {
func (c checkedExtrinsic) dispatch(maybeWho sc.Option[primitives.AccountId[primitives.PublicKey]]) (primitives.PostDispatchInfo, primitives.DispatchError) {
resWithInfo := c.function.Dispatch(primitives.RawOriginFrom(maybeWho), c.function.Args())

if resWithInfo.HasError {
Expand Down
6 changes: 3 additions & 3 deletions execution/types/checked_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var (
signerOption = sc.NewOption[types.Address32](constants.OneAddress)
emptySigner = sc.NewOption[types.Address32](nil)
signerOption = sc.NewOption[types.AccountId[types.PublicKey]](constants.ZeroAddressAccountId)
emptySigner = sc.NewOption[types.AccountId[types.PublicKey]](nil)

txSource = types.NewTransactionSourceExternal()
dispatchInfo = &types.DispatchInfo{
Expand Down Expand Up @@ -467,7 +467,7 @@ func Test_CheckedExtrinsic_dispatch_Fails(t *testing.T) {
mockCall.AssertCalled(t, "Dispatch", types.RawOriginFrom(signerOption), args)
}

func setupCheckedExtrinsic(signer sc.Option[types.Address32]) checkedExtrinsic {
func setupCheckedExtrinsic(signer sc.Option[types.AccountId[types.PublicKey]]) checkedExtrinsic {
mockCall = new(mocks.Call)
mockSignedExtra = new(mocks.SignedExtra)
mockTransactional = new(mocks.IoTransactional[types.PostDispatchInfo, types.DispatchError])
Expand Down
14 changes: 7 additions & 7 deletions execution/types/runtime_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ type RuntimeDecoder interface {
DecodeCall(buffer *bytes.Buffer) (primitives.Call, error)
}

type runtimeDecoder struct {
type runtimeDecoder[T primitives.PublicKey] struct {
modules []types.Module
extra primitives.SignedExtra
}

func NewRuntimeDecoder(modules []types.Module, extra primitives.SignedExtra) RuntimeDecoder {
return runtimeDecoder{
func NewRuntimeDecoder[T primitives.PublicKey](modules []types.Module, extra primitives.SignedExtra) RuntimeDecoder {
return runtimeDecoder[T]{
modules: modules,
extra: extra,
}
}

func (rd runtimeDecoder) DecodeBlock(buffer *bytes.Buffer) (primitives.Block, error) {
func (rd runtimeDecoder[T]) DecodeBlock(buffer *bytes.Buffer) (primitives.Block, error) {
header, err := primitives.DecodeHeader(buffer)
if err != nil {
return nil, err
Expand All @@ -51,7 +51,7 @@ func (rd runtimeDecoder) DecodeBlock(buffer *bytes.Buffer) (primitives.Block, er
return NewBlock(header, extrinsics), nil
}

func (rd runtimeDecoder) DecodeUncheckedExtrinsic(buffer *bytes.Buffer) (primitives.UncheckedExtrinsic, error) {
func (rd runtimeDecoder[T]) DecodeUncheckedExtrinsic(buffer *bytes.Buffer) (primitives.UncheckedExtrinsic, error) {
// This is a little more complicated than usual since the binary format must be compatible
// with SCALE's generic `Vec<u8>` type. Basically this just means accepting that there
// will be a prefix of vector length.
Expand All @@ -71,7 +71,7 @@ func (rd runtimeDecoder) DecodeUncheckedExtrinsic(buffer *bytes.Buffer) (primiti
var extSignature sc.Option[primitives.ExtrinsicSignature]
isSigned := version&ExtrinsicBitSigned != 0
if isSigned {
sig, err := primitives.DecodeExtrinsicSignature(rd.extra, buffer)
sig, err := primitives.DecodeExtrinsicSignature[T](rd.extra, buffer)
if err != nil {
return nil, err
}
Expand All @@ -93,7 +93,7 @@ func (rd runtimeDecoder) DecodeUncheckedExtrinsic(buffer *bytes.Buffer) (primiti
return NewUncheckedExtrinsic(sc.U8(version), extSignature, function, rd.extra), nil
}

func (rd runtimeDecoder) DecodeCall(buffer *bytes.Buffer) (primitives.Call, error) {
func (rd runtimeDecoder[T]) DecodeCall(buffer *bytes.Buffer) (primitives.Call, error) {
moduleIndex, err := sc.DecodeU8(buffer)
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions execution/types/runtime_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
signedExtrinsicVersion = 132
)

type testPublicKeyType = primitives.Ed25519PublicKey

var (
signedExtrinsicBytes = []byte{
byte(signedExtrinsicVersion), // version
Expand Down Expand Up @@ -53,7 +55,7 @@ var (

func Test_RuntimeDecoder_New(t *testing.T) {
target := setupRuntimeDecoder()
expect := runtimeDecoder{
expect := runtimeDecoder[testPublicKeyType]{
modules: []primitives.Module{mockModuleOne},
extra: mockSignedExtra,
}
Expand Down Expand Up @@ -355,5 +357,5 @@ func setupRuntimeDecoder() RuntimeDecoder {

apis := []primitives.Module{mockModuleOne}

return NewRuntimeDecoder(apis, mockSignedExtra)
return NewRuntimeDecoder[testPublicKeyType](apis, mockSignedExtra)
}
8 changes: 4 additions & 4 deletions execution/types/unchecked_extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ func (uxt uncheckedExtrinsic) Check() (primitives.CheckedExtrinsic, primitives.T
return nil, invalidTransactionBadProof
}

return NewCheckedExtrinsic(sc.NewOption[primitives.Address32](signerAddress), uxt.function, extra), nil
return NewCheckedExtrinsic(sc.NewOption[primitives.AccountId[primitives.PublicKey]](signerAddress), uxt.function, extra), nil
}

return NewCheckedExtrinsic(sc.NewOption[primitives.Address32](nil), uxt.function, uxt.extra), nil
return NewCheckedExtrinsic(sc.NewOption[primitives.AccountId[primitives.PublicKey]](nil), uxt.function, uxt.extra), nil
}

func (uxt uncheckedExtrinsic) verify(signature primitives.MultiSignature, msg sc.Sequence[sc.U8], signer primitives.Address32) bool {
func (uxt uncheckedExtrinsic) verify(signature primitives.MultiSignature, msg sc.Sequence[sc.U8], signer primitives.AccountId[primitives.PublicKey]) bool {
msgBytes := sc.SequenceU8ToBytes(msg)
signerBytes := sc.FixedSequenceU8ToBytes(signer.FixedSequence)
signerBytes := signer.Bytes()
radkomih marked this conversation as resolved.
Show resolved Hide resolved

if signature.IsEd25519() {
sigEd25519, err := signature.AsEd25519()
Expand Down
11 changes: 6 additions & 5 deletions execution/types/unchecked_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ var (
signerAddressBytes = []byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
signerAddress, _ = types.NewAddress32(sc.BytesToSequenceU8(signerAddressBytes)...)
signer = types.NewMultiAddressId(types.AccountId{Address32: signerAddress})
signer25519Address, _ = types.NewEd25519PublicKey(sc.BytesToSequenceU8(signerAddressBytes)...)
signerAccountId = types.NewAccountId[types.PublicKey](signer25519Address)
signer = types.NewMultiAddressId(signerAccountId)

signatureBytes = []byte{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Expand Down Expand Up @@ -219,7 +220,7 @@ func Test_IsSigned(t *testing.T) {

func Test_Check_UnsignedUncheckedExtrinsic(t *testing.T) {
setup(signatureEd25519)
expect := NewCheckedExtrinsic(sc.NewOption[types.Address32](nil), mockCall, types.SignedExtra(nil)).(checkedExtrinsic)
expect := NewCheckedExtrinsic(sc.NewOption[types.AccountId[types.PublicKey]](nil), mockCall, types.SignedExtra(nil)).(checkedExtrinsic)

result, err := targetUnsigned.Check()

Expand Down Expand Up @@ -295,7 +296,7 @@ func Test_Check_SignedUncheckedExtrinsic_LongEncoding_BadProofError(t *testing.T

func Test_Check_SignedUncheckedExtrinsic_Success(t *testing.T) {
setup(signatureEd25519)
expect := NewCheckedExtrinsic(sc.NewOption[types.Address32](signerAddress), mockCall, mockSignedExtra).(checkedExtrinsic)
expect := NewCheckedExtrinsic(sc.NewOption[types.AccountId[types.PublicKey]](signerAccountId), mockCall, mockSignedExtra).(checkedExtrinsic)

mocksSignedPayload.On("Bytes").Return(encodedPayloadBytes)
mockCrypto.On("Ed25519Verify", signatureBytes, encodedPayloadBytes, signerAddressBytes).Return(true)
Expand All @@ -315,7 +316,7 @@ func Test_Check_SignedUncheckedExtrinsic_Success(t *testing.T) {

func Test_Check_SignedUncheckedExtrinsic_Success_Sr25519(t *testing.T) {
setup(signatureSr25519)
expect := NewCheckedExtrinsic(sc.NewOption[types.Address32](signerAddress), mockCall, mockSignedExtra).(checkedExtrinsic)
expect := NewCheckedExtrinsic(sc.NewOption[types.AccountId[types.PublicKey]](signerAccountId), mockCall, mockSignedExtra).(checkedExtrinsic)

mocksSignedPayload.On("Bytes").Return(encodedPayloadBytes)
mockCrypto.On("Sr25519Verify", signatureBytes, encodedPayloadBytes, signerAddressBytes).Return(true)
Expand Down
Loading