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

inherent improvements #2815

Merged
4 changes: 2 additions & 2 deletions dot/sync/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func BuildBlock(t *testing.T, instance runtime.Instance, parent *types.Header, e
require.NoError(t, err)

idata := types.NewInherentsData()
err = idata.SetInt64Inherent(types.Timstap0, uint64(time.Now().Unix()))
err = idata.SetInherent(types.Timstap0, uint64(time.Now().Unix()))
require.NoError(t, err)

err = idata.SetInt64Inherent(types.Babeslot, 1)
err = idata.SetInherent(types.Babeslot, 1)
require.NoError(t, err)

ienc, err := idata.Encode()
Expand Down
55 changes: 35 additions & 20 deletions dot/types/inherents.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,42 @@ package types

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math/big"

"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
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
)

// 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"))
default:
panic("invalid inherent identifier")
}

return kb
}

// InherentsData contains a mapping of inherent keys to values
// keys must be 8 bytes, values are a scale-encoded byte array
type InherentsData struct {
Expand All @@ -43,24 +62,20 @@ func (d *InherentsData) String() string {
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")
// SetInherent sets a inherent.
func (d *InherentsData) SetInherent(inherentIdentifier InherentIdentifier, value interface{}) error {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
data, err := scale.Marshal(value)
if err != nil {
return err
}

val := make([]byte, 8)
binary.LittleEndian.PutUint64(val, data)

venc, err := scale.Marshal(val)
venc, err := scale.Marshal(data)
if err != nil {
return err
}

kb := [8]byte{}
copy(kb[:], key)
d.data[inherentIdentifier.Bytes()] = venc

d.data[kb] = venc
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions lib/babe/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,13 @@ func buildBlockInherents(slot Slot, rt runtime.Instance) ([][]byte, error) {
// Setup inherents: add timstap0
idata := types.NewInherentsData()
timestamp := uint64(time.Now().UnixMilli())
err := idata.SetInt64Inherent(types.Timstap0, timestamp)
err := idata.SetInherent(types.Timstap0, timestamp)
if err != nil {
return nil, err
}

// add babeslot
err = idata.SetInt64Inherent(types.Babeslot, slot.number)
err = idata.SetInherent(types.Babeslot, slot.number)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ func InitializeRuntimeToTest(t *testing.T, instance Instance, parentHash common.
require.NoError(t, err)

idata := types.NewInherentsData()
err = idata.SetInt64Inherent(types.Timstap0, 1)
err = idata.SetInherent(types.Timstap0, 1)
require.NoError(t, err)

err = idata.SetInt64Inherent(types.Babeslot, 1)
err = idata.SetInherent(types.Babeslot, 1)
require.NoError(t, err)

ienc, err := idata.Encode()
Expand Down