diff --git a/decoder.go b/decoder.go index c698e1c0..d13e13fd 100644 --- a/decoder.go +++ b/decoder.go @@ -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 { @@ -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) @@ -232,6 +232,7 @@ func (d *Decoder) Decode(v interface{}) (err error) { if isPresent == 0 { println("Skipping optional OptionalProducerSchedule") + *realV = nil return } diff --git a/encoder.go b/encoder.go index 0d62d426..12d4b87c 100644 --- a/encoder.go +++ b/encoder.go @@ -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) @@ -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] diff --git a/p2ptypes.go b/p2ptypes.go index 33fc0dfa..2a9bc986 100644 --- a/p2ptypes.go +++ b/p2ptypes.go @@ -5,6 +5,7 @@ import ( "fmt" "encoding/binary" + "encoding/hex" "encoding/json" "github.com/eoscanada/eos-go/ecc" @@ -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 { @@ -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"`