Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
eae14b4
checks if type has Encode function
edwardmack Feb 5, 2020
75a131a
Added DecodeCustom to handle calling Decode func
edwardmack Feb 6, 2020
3c86ca2
go fmt'ed
edwardmack Feb 6, 2020
68f5350
Merge branch 'development' into ed/525-custom-scale-methods
edwardmack Feb 6, 2020
b6a06de
fixed lint rants
edwardmack Feb 7, 2020
4f6aa93
Merge branch 'ed/525-custom-scale-methods' of https://github.com/Chai…
edwardmack Feb 7, 2020
acd1ad4
working on determining auth index
noot Feb 7, 2020
8e74749
Merge remote-tracking branch 'origin/ed/525-custom-scale-methods' int…
noot Feb 7, 2020
7fbf63e
update grandpaAuthorities call; add auth data to gssmr0.json
noot Feb 7, 2020
36b2898
update authority data fields
noot Feb 7, 2020
4f6e34d
update scale to correctly decode babe configuration
noot Feb 8, 2020
588ae01
cleanup
noot Feb 8, 2020
16d67d0
testing epoch threshold
noot Feb 8, 2020
e271d72
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot Feb 9, 2020
cd6b824
cleanup
noot Feb 9, 2020
a3593a3
merge with development
noot Feb 10, 2020
e64f5e4
lint
noot Feb 10, 2020
fe29744
merge with development
noot Feb 11, 2020
cf2c39a
fix core, babe tests
noot Feb 11, 2020
1a4ea54
cleanup
noot Feb 11, 2020
0b9e270
lint
noot Feb 11, 2020
ebf21cf
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot Feb 13, 2020
fb7a015
merge with development
noot Feb 13, 2020
08f8fb0
merge with development
noot Feb 13, 2020
d7c02d8
merge with development
noot Feb 15, 2020
d8e2a41
lint
noot Feb 15, 2020
52cd513
rename to IsBabeAuthority
noot Feb 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions codec/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ func (sd *Decoder) DecodeArray(t interface{}) (interface{}, error) {
copy(arr[:], buf)
*ptr = arr
default:
err = errors.New("could not decode invalid slice or array")
var res interface{}
res, err = sd.DecodeCustom(sl.Index(i).Interface())
if err != nil {
return nil, err
}
arrayValue.Set(reflect.ValueOf(res))
}

if err != nil {
Expand Down Expand Up @@ -544,10 +549,12 @@ func (sd *Decoder) DecodeTuple(t interface{}) (interface{}, error) {
ptr := fieldValue.(*[]string)
*ptr = o.([]string)
default:
_, err = sd.Decode(v.Field(i).Interface())
o, err = sd.Decode(v.Field(i).Interface())
if err != nil {
break
}
// TODO: clean up this function, can use field.Set everywhere (remove switch case?)
Comment thread
noot marked this conversation as resolved.
field.Set(reflect.ValueOf(o))
}

if err != nil {
Expand Down
23 changes: 21 additions & 2 deletions codec/decode_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func DecodeCustom(in []byte, t interface{}) error {
someType := reflect.TypeOf(t)
_, ok := someType.MethodByName("Decode")
if ok {
meth := reflect.ValueOf(t).MethodByName("Decode")
method := reflect.ValueOf(t).MethodByName("Decode")
inVal := []reflect.Value{reflect.ValueOf(in)}
res := meth.Call(inVal)
res := method.Call(inVal)
err := res[0].Interface()
if err != nil {
return err.(error)
Expand All @@ -40,6 +40,25 @@ func DecodeCustom(in []byte, t interface{}) error {
return DecodePtr(in, t)
}

// DecodeCustom check if interface has method Decode(io.Reader), if so use that, otherwise use regular scale decoding
func (sd *Decoder) DecodeCustom(t interface{}) (interface{}, error) {
someType := reflect.TypeOf(t)
_, ok := someType.MethodByName("Decode")
if ok {
meth := reflect.ValueOf(t).MethodByName("Decode")
inVal := []reflect.Value{reflect.ValueOf(sd.Reader)}
res := meth.Call(inVal)
err := res[1].Interface()
if err != nil {
return nil, err.(error)
}
t = res[0].Interface()
return t, nil
}

return nil, errors.New("cannot decode custom type")
}

// DecodePtr is the high level function wrapping the specific type decoding functions
// The results of decode are written to t interface by reference (instead of returning
// value as Decode does)
Expand Down
12 changes: 12 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,15 @@ func ReadUint64(r io.Reader) (uint64, error) {
}
return binary.LittleEndian.Uint64(buf), nil
}

// Read32Bytes reads 32 bytes from the reader and returns it
func Read32Bytes(r io.Reader) ([32]byte, error) {
buf := make([]byte, 32)
_, err := r.Read(buf)
if err != nil {
return [32]byte{}, err
}
h := [32]byte{}
copy(h[:], buf)
return h, nil
}
1 change: 1 addition & 0 deletions config/gssmr0.json

Large diffs are not rendered by default.

76 changes: 52 additions & 24 deletions consensus/babe/babe.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type SessionConfig struct {
Keypair *sr25519.Keypair
Runtime *runtime.Runtime
NewBlocks chan<- types.Block
AuthorityIndex uint64
AuthData []*AuthorityData
EpochThreshold *big.Int // should only be used for testing
Done chan<- struct{}
Expand All @@ -76,7 +75,6 @@ func NewSession(cfg *SessionConfig) (*Session, error) {
txQueue: new(tx.PriorityQueue),
slotToProof: make(map[uint64]*VrfOutputAndProof),
newBlocks: cfg.NewBlocks,
authorityIndex: cfg.AuthorityIndex,
authorityData: cfg.AuthData,
epochThreshold: cfg.EpochThreshold,
done: cfg.Done,
Expand All @@ -91,6 +89,13 @@ func NewSession(cfg *SessionConfig) (*Session, error) {

babeSession.randomness = [sr25519.VrfOutputLength]byte{babeSession.config.Randomness}

err = babeSession.setAuthorityIndex()
if err != nil {
return nil, err
}

log.Trace("BABE session", "authority index", babeSession.authorityIndex)

return babeSession, nil
}

Expand All @@ -103,6 +108,8 @@ func (b *Session) Start() error {
}
}

log.Trace("BABE", "epochThreshold", b.epochThreshold)

var i uint64 = 0
var err error
for ; i < b.config.EpochLength; i++ {
Expand All @@ -127,9 +134,27 @@ func (b *Session) PeekFromTxQueue() *tx.ValidTransaction {
return b.txQueue.Peek()
}

func (b *Session) SetEpochData(data *NextEpochDescriptor) {
func (b *Session) AuthorityData() []*AuthorityData {
return b.authorityData
}

func (b *Session) SetEpochData(data *NextEpochDescriptor) error {
b.authorityData = data.Authorities
b.randomness = data.Randomness
return b.setAuthorityIndex()
}

func (b *Session) setAuthorityIndex() error {
pub := b.keypair.Public()

for i, auth := range b.authorityData {
if bytes.Equal(pub.Encode(), auth.ID.Encode()) {
b.authorityIndex = uint64(i)
return nil
}
}

return fmt.Errorf("key not in BABE authority data")
}

func (b *Session) invokeBlockAuthoring() {
Expand All @@ -147,32 +172,34 @@ func (b *Session) invokeBlockAuthoring() {
}

for ; slotNum < b.config.EpochLength; slotNum++ {
parentHeader := b.blockState.LatestHeader()
if parentHeader == nil {
log.Error("BABE block authoring", "error", "parent header is nil")
} else {
currentSlot := Slot{
start: uint64(time.Now().Unix()),
duration: b.config.SlotDuration,
number: slotNum,
}

block, err := b.buildBlock(parentHeader, currentSlot)
if err != nil {
log.Error("BABE block authoring", "error", err)
if b.slotToProof[slotNum] != nil {
parentHeader := b.blockState.LatestHeader()
if parentHeader == nil {
log.Error("BABE block authoring", "error", "parent header is nil")
} else {
hash := block.Header.Hash()
log.Info("BABE", "built block", hash.String(), "number", block.Header.Number)
log.Debug("BABE built block", "header", block.Header, "body", block.Body)
b.newBlocks <- *block
err = b.blockState.AddBlock(block)
currentSlot := Slot{
start: uint64(time.Now().Unix()),
duration: b.config.SlotDuration,
number: slotNum,
}

block, err := b.buildBlock(parentHeader, currentSlot)
if err != nil {
log.Error("BABE block authoring", "error", err)
} else {
hash := block.Header.Hash()
log.Info("BABE", "built block", hash.String(), "number", block.Header.Number)
log.Debug("BABE built block", "header", block.Header, "body", block.Body)
b.newBlocks <- *block
err = b.blockState.AddBlock(block)
if err != nil {
log.Error("BABE block authoring", "error", err)
}
}
}
}

time.Sleep(time.Millisecond * time.Duration(b.config.SlotDuration))
time.Sleep(time.Millisecond * time.Duration(b.config.SlotDuration))
}
}

if b.newBlocks != nil {
Expand Down Expand Up @@ -210,6 +237,7 @@ func (b *Session) runLottery(slot uint64) (*VrfOutputAndProof, error) {
copy(outbytes[:], output)
proofbytes := [sr25519.VrfProofLength]byte{}
copy(proofbytes[:], proof)
log.Trace("BABE lottery", "won slot", slot)
return &VrfOutputAndProof{
output: outbytes,
proof: proofbytes,
Expand Down Expand Up @@ -241,7 +269,7 @@ func (b *Session) setEpochThreshold() error {
func (b *Session) authorityWeights() []uint64 {
weights := make([]uint64, len(b.authorityData))
for i, auth := range b.authorityData {
weights[i] = auth.weight
weights[i] = auth.Weight
}
return weights
}
Expand Down
Loading