Skip to content

Commit

Permalink
Updated encoding of BlockHeader with support for optional field.
Browse files Browse the repository at this point in the history
  • Loading branch information
abourget committed Jun 13, 2018
1 parent 24ab4cb commit 494a9c0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
5 changes: 3 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (d *Decoder) Decode(v interface{}) (err error) {
rv = reflect.Indirect(newRV)
}

switch v.(type) {
switch realV := v.(type) {
case *string:
s, e := d.readString()
if e != nil {
Expand Down Expand Up @@ -223,7 +223,7 @@ func (d *Decoder) Decode(v interface{}) (err error) {
rv.Set(reflect.ValueOf(asset))
return

case *OptionalProducerSchedule:
case **OptionalProducerSchedule:
isPresent, e := d.readByte()
if e != nil {
err = fmt.Errorf("decode: OptionalProducerSchedule isPresent, %s", e)
Expand All @@ -232,6 +232,7 @@ func (d *Decoder) Decode(v interface{}) (err error) {

if isPresent == 0 {
println("Skipping optional OptionalProducerSchedule")
*realV = nil
return
}

Expand Down
25 changes: 19 additions & 6 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (e *Encoder) Encode(v interface{}) (err error) {
return e.writeCurrencyName(cv)
case Asset:
return e.writeAsset(cv)
// case *OptionalProducerSchedule:
// isPresent := cv != nil
// e.writeBool(isPresent)
// if isPresent {

// }
case ActionData:
println("ActionData")
return e.writeActionData(cv)
Expand Down Expand Up @@ -149,25 +155,32 @@ func (e *Encoder) Encode(v interface{}) (err error) {
println(fmt.Sprintf("Encode: struct [%T] with %d field.", v, l))
//prefix = append(prefix, " ")

n := 0
for i := 0; i < l; i++ {
field := t.Field(i)
println(fmt.Sprintf("field -> %s", field.Name))
//fmt.Println(fmt.Sprintf("field -> %s", field.Name))

if tag := field.Tag.Get("eos"); tag == "-" {
tag := field.Tag.Get("eos")
if tag == "-" {
continue
}

if v := rv.Field(i); t.Field(i).Name != "_" {
if v.CanInterface() {
iface := v.Interface()
if iface != nil {
if err = e.Encode(iface); err != nil {
isPresent := true
if tag == "optional" {
isPresent = !v.IsNil()
e.writeBool(isPresent)
}

//fmt.Printf("IS PRESENT: %T %#v\n", iface, iface, isPresent)

if isPresent {
if err = e.Encode(v.Interface()); err != nil {
return
}
}
}
n++
}
}
//prefix = prefix[:len(prefix)-1]
Expand Down
31 changes: 17 additions & 14 deletions p2ptypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"encoding/binary"
"encoding/hex"
"encoding/json"

"github.com/eoscanada/eos-go/ecc"
Expand Down Expand Up @@ -186,15 +187,15 @@ type ProducerSchedule struct {
}

type BlockHeader struct {
Timestamp BlockTimestamp `json:"timestamp"`
Producer AccountName `json:"producer"`
Confirmed uint16 `json:"confirmed"`
Previous SHA256Bytes `json:"previous"`
TransactionMRoot SHA256Bytes `json:"transaction_mroot"`
ActionMRoot SHA256Bytes `json:"action_mroot"`
ScheduleVersion uint32 `json:"schedule_version"`
NewProducers OptionalProducerSchedule `json:"new_producers"`
HeaderExtensions []*Extension `json:"header_extensions"`
Timestamp BlockTimestamp `json:"timestamp"`
Producer AccountName `json:"producer"`
Confirmed uint16 `json:"confirmed"`
Previous SHA256Bytes `json:"previous"`
TransactionMRoot SHA256Bytes `json:"transaction_mroot"`
ActionMRoot SHA256Bytes `json:"action_mroot"`
ScheduleVersion uint32 `json:"schedule_version"`
NewProducers *OptionalProducerSchedule `json:"new_producers" eos:"optional"`
HeaderExtensions []*Extension `json:"header_extensions"`
}

func (b *BlockHeader) BlockNumber() uint32 {
Expand All @@ -207,19 +208,21 @@ func (b *BlockHeader) BlockID() (SHA256Bytes, error) {
return nil, err
}

fmt.Println("MAM", hex.EncodeToString(cereal), b.BlockNumber(), hex.EncodeToString(b.Previous))

h := sha256.New()
_, _ = h.Write(cereal)
return SHA256Bytes(h.Sum(nil)), nil
hashed := h.Sum(nil)

binary.BigEndian.PutUint32(hashed, b.BlockNumber())

return SHA256Bytes(hashed), nil
}

type OptionalProducerSchedule struct {
ProducerSchedule
}

func (a *OptionalProducerSchedule) OptionalBinaryMarshalerPresent() bool {
return a == nil
}

type SignedBlockHeader struct {
BlockHeader
ProducerSignature ecc.Signature `json:"producer_signature"`
Expand Down

0 comments on commit 494a9c0

Please sign in to comment.