-
Notifications
You must be signed in to change notification settings - Fork 116
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
Implement parachain inherents #2566
Merged
kishansagathiya
merged 125 commits into
development
from
kishan/feat/parachain-inherents
Oct 7, 2022
Merged
Changes from 122 commits
Commits
Show all changes
125 commits
Select commit
Hold shift + click to select a range
ac5e507
temp
kishansagathiya 2115a15
Implement Parachain Inherents
kishansagathiya 298a2bd
clean up:
kishansagathiya 02a931b
filling up more structs
kishansagathiya a0f8c1e
filling more structs
kishansagathiya d833b8b
more structs
kishansagathiya b11b9cf
fix lint
kishansagathiya 2d48c7d
addressing some reviews
kishansagathiya 5abb342
converted enums to VaryingDataType
kishansagathiya f548252
Update lib/runtime/wasmer/imports.go
kishansagathiya 944ba17
Update lib/runtime/wasmer/imports.go
kishansagathiya e2d01c3
Update lib/runtime/wasmer/imports.go
kishansagathiya 2973d33
Update lib/runtime/wasmer/imports.go
kishansagathiya 6fd9518
Update lib/runtime/wasmer/imports.go
kishansagathiya 3ecf9b1
Update lib/babe/parachain_inherents.go
kishansagathiya 781d462
Update lib/babe/parachain_inherents.go
kishansagathiya d7e716b
addressed some of the reviews
kishansagathiya d1cb494
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya e16faaf
some parachain inherent tests
kishansagathiya 37a7c1d
more tests
kishansagathiya bad42ee
wip decode functionality
timwu20 123fbbe
wip encode custom vdt
timwu20 f46408c
refactor tests
timwu20 f5d0810
more tests, fix decode for nested vdt
timwu20 9e8ef6a
update readme, provide example of nested VDTs
timwu20 24977ea
add copyright
timwu20 4a4be32
fix lint
timwu20 034efdb
Merge branch 'tim/custom-vdt' of github.com:ChainSafe/gossamer into k…
kishansagathiya cdb5b71
temp
kishansagathiya 7cd88b3
Fixed tests
kishansagathiya e3e1347
fix lint
kishansagathiya fc92757
little more changes
kishansagathiya 74c8d2b
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 6d7c387
fix lint
kishansagathiya 40eb6fc
more fix up
kishansagathiya 8a0c07b
make license
kishansagathiya f0f0522
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 9522517
some more changes
kishansagathiya 8b96d4f
more changes
kishansagathiya 41b4ac9
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya bc09949
update genesis file with latest runtime
kishansagathiya 914374e
Revert "update genesis file with latest runtime"
kishansagathiya fbd59c5
modify genesis files with latest runtime
kishansagathiya e88438c
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya bf2fe8a
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 98b0356
temp
kishansagathiya c616756
modified genesis files
kishansagathiya 937a05d
reverting genesis
kishansagathiya 4f22128
more genesis changes
kishansagathiya 3b5d06f
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 99c1c0c
test scaled encoding of parachain inherents
kishansagathiya 02981ba
test scaled encoding of inherents
kishansagathiya bc4601d
add parent header in parachain inherent data
kishansagathiya 90e81be
fix lint
kishansagathiya a1f2b3e
reverting uncessary change
kishansagathiya 66503a6
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya afc80f3
Update dot/types/inherents.go
kishansagathiya c0d9f9f
Update lib/babe/build.go
kishansagathiya 9c2900e
Update lib/babe/parachain_inherents.go
kishansagathiya 74405cb
Update lib/babe/parachain_inherents_test.go
kishansagathiya ae1617e
Update dot/types/inherents.go
kishansagathiya 9452425
Update lib/babe/build.go
kishansagathiya 6fb11f6
Update lib/babe/parachain_inherents_test.go
kishansagathiya 1407220
addressed some comments
kishansagathiya 775aec1
addressed some reviews
kishansagathiya ec3f3d8
Update lib/babe/parachain_inherents.go
kishansagathiya 9d0bd03
Update lib/babe/parachain_inherents.go
kishansagathiya 0c48685
Update lib/babe/parachain_inherents.go
kishansagathiya c7f4c46
Update lib/babe/parachain_inherents.go
kishansagathiya a3d196e
Update lib/babe/parachain_inherents.go
kishansagathiya ba6a995
addressed some of the review
kishansagathiya 0d590fe
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya 895ac4d
addressed some reviews
kishansagathiya 8ea0ddc
Update lib/babe/parachain_inherents_test.go
kishansagathiya 44fef33
addressed more changes
kishansagathiya ca11b5d
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya 99aa387
more reviews
kishansagathiya dbfb390
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 9474c83
fix lint
kishansagathiya 7ccabb8
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 476c1d6
try
kishansagathiya 7fbef75
removed phantom data comment
kishansagathiya b6fcb7c
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya eec8635
more reviews
kishansagathiya 941c704
Update lib/babe/parachain_inherents.go
kishansagathiya d1a8bf5
Update lib/babe/parachain_inherents.go
kishansagathiya 1b6bb12
Update lib/babe/parachain_inherents.go
kishansagathiya b46cb1e
some review addressed
kishansagathiya 37e0441
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya ffe8bac
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 7b91a7d
revise SetStructInherent and SetInt64Inherent to generic SetInherent,…
timwu20 6d5ddaa
suppressing deepsource warnings
kishansagathiya 116c52f
skip-cq to skipcq
kishansagathiya 3fc10d8
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 9a6ea91
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya d7707aa
inherent improvements (#2815)
kishansagathiya 0082def
Merge branch 'tim/kishan/feat/parachain-inherents' of github.com:Chai…
kishansagathiya bcbf177
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya ba71cdd
add copyright
kishansagathiya e032467
some fixes
kishansagathiya 4657d9f
Update lib/babe/build.go
kishansagathiya b139ec0
Update dot/types/inherents.go
kishansagathiya 693adbb
Update lib/babe/build.go
kishansagathiya 6484640
Update lib/babe/parachain_inherents_test.go
kishansagathiya 8d82ec5
Update lib/babe/parachain_inherents_test.go
kishansagathiya 3815c0e
Update lib/babe/parachain_inherents.go
kishansagathiya 5e927ec
use MapRange to preserve order of keys in map while encoding
kishansagathiya ba45f8f
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya f9f2740
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 62ca950
fix test
kishansagathiya 942429e
Update lib/babe/parachain_inherents_test.go
kishansagathiya f4f5716
Update lib/babe/parachain_inherents_test.go
kishansagathiya 4a697d0
Update lib/babe/parachain_inherents_test.go
kishansagathiya b658c9e
added some values in parachain inherents for tests
kishansagathiya e414ecf
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya d5affe4
more non-zero values
kishansagathiya d0eab91
revert scale map encoding changes
kishansagathiya 25d94a3
iterate through map in orderly way
kishansagathiya b176471
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 5e3d3c4
rename InherentsData to InherentData
kishansagathiya 2601321
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 3572771
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya 5e67259
Merge branch 'development' into kishan/feat/parachain-inherents
kishansagathiya e5ef1ed
hopefully final fix for having consistant order in scale encoding of
kishansagathiya d690816
Merge branch 'kishan/feat/parachain-inherents' of github.com:ChainSaf…
kishansagathiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -5,68 +5,87 @@ package types | |
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
"reflect" | ||
|
||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
) | ||
|
||
var ( | ||
// Timstap0 is an inherent key. | ||
Timstap0 = []byte("timstap0") | ||
// Babeslot is an inherent key. | ||
Babeslot = []byte("babeslot") | ||
// Uncles00 is an inherent key. | ||
Uncles00 = []byte("uncles00") | ||
// InherentIdentifier is an identifier for an inherent. | ||
type InherentIdentifier uint | ||
|
||
const ( | ||
// Timstap0 is the identifier for the `timestamp` inherent. | ||
Timstap0 InherentIdentifier = iota | ||
// Babeslot is the BABE inherent identifier. | ||
Babeslot | ||
// Uncles00 is the identifier for the `uncles` inherent. | ||
Uncles00 | ||
// Parachn0 is an inherent key for parachains inherent. | ||
Parachn0 | ||
// Newheads is an inherent key for new minimally-attested parachain heads. | ||
Newheads | ||
) | ||
|
||
// InherentsData contains a mapping of inherent keys to values | ||
// Bytes returns a byte array of given inherent identifier. | ||
func (ii InherentIdentifier) Bytes() [8]byte { | ||
|
||
kb := [8]byte{} | ||
switch ii { | ||
case Timstap0: | ||
copy(kb[:], []byte("timstap0")) | ||
case Babeslot: | ||
copy(kb[:], []byte("babeslot")) | ||
case Uncles00: | ||
copy(kb[:], []byte("uncles00")) | ||
case Parachn0: | ||
copy(kb[:], []byte("parachn0")) | ||
case Newheads: | ||
copy(kb[:], []byte("newheads")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit why is it named |
||
default: | ||
panic("invalid inherent identifier") | ||
} | ||
|
||
return kb | ||
} | ||
|
||
// InherentData contains a mapping of inherent keys to values | ||
// keys must be 8 bytes, values are a scale-encoded byte array | ||
type InherentsData struct { | ||
data map[[8]byte]([]byte) | ||
type InherentData struct { | ||
Data map[[8]byte][]byte | ||
kishansagathiya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// NewInherentsData returns InherentsData | ||
func NewInherentsData() *InherentsData { | ||
return &InherentsData{ | ||
data: make(map[[8]byte]([]byte)), | ||
// NewInherentData returns InherentData | ||
func NewInherentData() *InherentData { | ||
return &InherentData{ | ||
Data: make(map[[8]byte][]byte), | ||
} | ||
} | ||
|
||
func (d *InherentsData) String() string { | ||
func (d *InherentData) String() string { | ||
str := "" | ||
for k, v := range d.data { | ||
for k, v := range d.Data { | ||
str = str + fmt.Sprintf("key=%v\tvalue=%v\n", k, v) | ||
} | ||
return str | ||
} | ||
|
||
// SetInt64Inherent set an inherent of type uint64 | ||
func (d *InherentsData) SetInt64Inherent(key []byte, data uint64) error { | ||
if len(key) != 8 { | ||
return errors.New("inherent key must be 8 bytes") | ||
} | ||
|
||
val := make([]byte, 8) | ||
binary.LittleEndian.PutUint64(val, data) | ||
|
||
venc, err := scale.Marshal(val) | ||
// SetInherent sets a inherent. | ||
func (d *InherentData) SetInherent(inherentIdentifier InherentIdentifier, value any) error { | ||
data, err := scale.Marshal(value) | ||
if err != nil { | ||
return err | ||
kishansagathiya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
kb := [8]byte{} | ||
copy(kb[:], key) | ||
d.Data[inherentIdentifier.Bytes()] = data | ||
|
||
d.data[kb] = venc | ||
return nil | ||
} | ||
|
||
// Encode will encode a given []byte using scale.Encode | ||
func (d *InherentsData) Encode() ([]byte, error) { | ||
length := big.NewInt(int64(len(d.data))) | ||
func (d *InherentData) Encode() ([]byte, error) { | ||
length := big.NewInt(int64(len(d.Data))) | ||
buffer := bytes.Buffer{} | ||
|
||
l, err := scale.Marshal(length) | ||
|
@@ -79,15 +98,24 @@ func (d *InherentsData) Encode() ([]byte, error) { | |
return nil, err | ||
} | ||
|
||
for k, v := range d.data { | ||
for i := reflect.ValueOf(d.Data).MapRange(); i.Next(); { | ||
k := i.Key().Interface().([8]byte) | ||
v := i.Value().Interface().([]byte) | ||
|
||
_, err = buffer.Write(k[:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = buffer.Write(v) | ||
|
||
venc, err := scale.Marshal(v) | ||
if err != nil { | ||
return nil, fmt.Errorf("scale encoding encoded value: %w", err) | ||
} | ||
_, err = buffer.Write(venc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return buffer.Bytes(), nil | ||
} |
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,45 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInherentDataMarshal(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
getInherentData func(t *testing.T) *InherentData | ||
want []byte | ||
}{ | ||
{ | ||
/* | ||
let mut data = InherentData::new(); | ||
let timestamp: u64 = 99; | ||
data.put_data(*b"babeslot", ×tamp).unwrap(); | ||
data.put_data(*b"timstap0", ×tamp).unwrap(); | ||
*/ | ||
getInherentData: func(t *testing.T) *InherentData { | ||
id := NewInherentData() | ||
err := id.SetInherent(Babeslot, uint64(99)) | ||
require.NoError(t, err) | ||
|
||
err = id.SetInherent(Timstap0, uint64(99)) | ||
require.NoError(t, err) | ||
return id | ||
}, | ||
want: []byte{8, 98, 97, 98, 101, 115, 108, 111, 116, 32, 99, 0, 0, 0, 0, 0, 0, 0, 116, 105, 109, 115, 116, 97, 112, 48, 32, 99, 0, 0, 0, 0, 0, 0, 0}, //nolint:lll | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
idata := tt.getInherentData(t) | ||
got, err := idata.Encode() | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit