func (b *IncreaseBalance) UnmarshalBinaryData(data []byte) (newData []byte, err error) { buf := bytes.NewBuffer(data) hash := make([]byte, 32) _, err = buf.Read(hash) if err != nil { return } b.ECPubKey = new(primitives.ByteSlice32) copy(b.ECPubKey[:], hash) _, err = buf.Read(hash) if err != nil { return } if b.TXID == nil { b.TXID = primitives.NewZeroHash() } b.TXID.SetBytes(hash) tmp := make([]byte, 0) b.Index, tmp = primitives.DecodeVarInt(buf.Bytes()) b.NumEC, tmp = primitives.DecodeVarInt(tmp) newData = tmp return }
func (b *IncreaseBalance) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling IncreaseBalance: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) _, err = buf.Read(hash) if err != nil { return } b.ECPubKey = new(primitives.ByteSlice32) copy(b.ECPubKey[:], hash) _, err = buf.Read(hash) if err != nil { return } if b.TXID == nil { b.TXID = primitives.NewZeroHash() } b.TXID.SetBytes(hash) tmp := make([]byte, 0) b.Index, tmp = primitives.DecodeVarInt(buf.DeepCopyBytes()) b.NumEC, tmp = primitives.DecodeVarInt(tmp) newData = tmp return }
func (b *ABlockHeader) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling Admin Block Header: %v", r) } }() newData = data newData, err = b.GetAdminChainID().UnmarshalBinaryData(newData) if err != nil { return } b.PrevBackRefHash = new(primitives.Hash) newData, err = b.PrevBackRefHash.UnmarshalBinaryData(newData) if err != nil { return } b.DBHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:] b.HeaderExpansionSize, newData = primitives.DecodeVarInt(newData) b.HeaderExpansionArea, newData = newData[:b.HeaderExpansionSize], newData[b.HeaderExpansionSize:] b.MessageCount, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:] b.BodySize, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:] return }
func (e *ECBlockHeader) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) if _, err = buf.Read(hash); err != nil { return } else { if fmt.Sprintf("%x", hash) != "000000000000000000000000000000000000000000000000000000000000000c" { err = fmt.Errorf("Invalid ChainID - %x", hash) return } } if _, err = buf.Read(hash); err != nil { return } else { e.BodyHash.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevHeaderHash.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevFullHash.SetBytes(hash) } if err = binary.Read(buf, binary.BigEndian, &e.DBHeight); err != nil { return } // read the Header Expansion Area hesize, tmp := primitives.DecodeVarInt(buf.DeepCopyBytes()) buf = primitives.NewBuffer(tmp) e.HeaderExpansionArea = make([]byte, hesize) if _, err = buf.Read(e.HeaderExpansionArea); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.ObjectCount); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.BodySize); err != nil { return } newData = buf.DeepCopyBytes() return }
func (e *ECBlock) unmarshalHeaderBinaryData(data []byte) (newData []byte, err error) { buf := primitives.NewBuffer(data) hash := make([]byte, 32) if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetECChainID().SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetBodyHash().SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetPrevHeaderHash().SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetPrevFullHash().SetBytes(hash) } h := e.Header.GetDBHeight() if err = binary.Read(buf, binary.BigEndian, &h); err != nil { return } // read the Header Expansion Area hesize, tmp := primitives.DecodeVarInt(buf.DeepCopyBytes()) buf = primitives.NewBuffer(tmp) e.Header.SetHeaderExpansionArea(make([]byte, hesize)) if _, err = buf.Read(e.Header.GetHeaderExpansionArea()); err != nil { return } oc := e.Header.GetObjectCount() if err = binary.Read(buf, binary.BigEndian, &oc); err != nil { return } sz := e.Header.GetBodySize() if err = binary.Read(buf, binary.BigEndian, &sz); err != nil { return } newData = buf.DeepCopyBytes() return }
func (t *TransAddress) UnmarshalBinaryData(data []byte) (newData []byte, err error) { if len(data) < 36 { return nil, fmt.Errorf("Data source too short to UnmarshalBinary() an address: %d", len(data)) } t.Amount, data = primitives.DecodeVarInt(data) t.Address = new(Address) data, err = t.Address.UnmarshalBinaryData(data) return data, err }
func (e *ECBlockHeader) UnmarshalBinaryData(data []byte) (newData []byte, err error) { buf := bytes.NewBuffer(data) hash := make([]byte, 32) if _, err = buf.Read(hash); err != nil { return } else { e.ECChainID.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.BodyHash.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevHeaderHash.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevLedgerKeyMR.SetBytes(hash) } if err = binary.Read(buf, binary.BigEndian, &e.DBHeight); err != nil { return } // read the Header Expansion Area hesize, tmp := primitives.DecodeVarInt(buf.Bytes()) buf = bytes.NewBuffer(tmp) e.HeaderExpansionArea = make([]byte, hesize) if _, err = buf.Read(e.HeaderExpansionArea); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.ObjectCount); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.BodySize); err != nil { return } newData = buf.Bytes() return }
// UnmarshalBinary assumes that the Binary is all good. We do error // out if there isn't enough data, or the transaction is too large. func (t *Transaction) UnmarshalBinaryData(data []byte) (newData []byte, err error) { // To catch memory errors, I capture the panic and turn it into // a reported error. defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling transaction: %v", r) } }() v, data := primitives.DecodeVarInt(data) if v != t.GetVersion() { return nil, fmt.Errorf("Wrong Transaction Version encountered. Expected %v and found %v", t.GetVersion(), v) } hd, data := binary.BigEndian.Uint32(data[:]), data[4:] ld, data := binary.BigEndian.Uint16(data[:]), data[2:] t.MilliTimestamp = (uint64(hd) << 16) + uint64(ld) numInputs := int(data[0]) data = data[1:] numOutputs := int(data[0]) data = data[1:] numOutECs := int(data[0]) data = data[1:] t.Inputs = make([]interfaces.IInAddress, numInputs, numInputs) t.Outputs = make([]interfaces.IOutAddress, numOutputs, numOutputs) t.OutECs = make([]interfaces.IOutECAddress, numOutECs, numOutECs) for i, _ := range t.Inputs { t.Inputs[i] = new(InAddress) data, err = t.Inputs[i].UnmarshalBinaryData(data) if err != nil || t.Inputs[i] == nil { return nil, err } } for i, _ := range t.Outputs { t.Outputs[i] = new(OutAddress) data, err = t.Outputs[i].UnmarshalBinaryData(data) if err != nil { return nil, err } } for i, _ := range t.OutECs { t.OutECs[i] = new(OutECAddress) data, err = t.OutECs[i].UnmarshalBinaryData(data) if err != nil { return nil, err } } t.RCDs = make([]interfaces.IRCD, len(t.Inputs)) t.SigBlocks = make([]interfaces.ISignatureBlock, len(t.Inputs)) for i := 0; i < len(t.Inputs); i++ { t.RCDs[i] = CreateRCD(data) data, err = t.RCDs[i].UnmarshalBinaryData(data) if err != nil { return nil, err } t.SigBlocks[i] = new(SignatureBlock) data, err = t.SigBlocks[i].UnmarshalBinaryData(data) if err != nil { return nil, err } } return data, nil }
// UnmarshalBinary assumes that the Binary is all good. We do error // out if there isn't enough data, or the transaction is too large. func (b *FBlock) UnmarshalBinaryData(data []byte) (newdata []byte, err error) { // To catch memory errors, we capture the panic and turn it into // a reported error. defer func() { return if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling transaction: %v", r) } }() if bytes.Compare(data[:constants.ADDRESS_LENGTH], constants.FACTOID_CHAINID[:]) != 0 { panic(fmt.Sprintf("error in: %x %x", data[:35], constants.FACTOID_CHAINID[:])) return nil, fmt.Errorf("Block does not begin with the Factoid ChainID") } newdata = data[32:] b.BodyMR = new(primitives.Hash) newdata, err = b.BodyMR.UnmarshalBinaryData(newdata) if err != nil { return nil, err } b.PrevKeyMR = new(primitives.Hash) newdata, err = b.PrevKeyMR.UnmarshalBinaryData(newdata) if err != nil { return nil, err } b.PrevLedgerKeyMR = new(primitives.Hash) newdata, err = b.PrevLedgerKeyMR.UnmarshalBinaryData(newdata) if err != nil { return nil, err } b.ExchRate, newdata = binary.BigEndian.Uint64(newdata[0:8]), newdata[8:] b.DBHeight, newdata = binary.BigEndian.Uint32(newdata[0:4]), newdata[4:] skip, newdata := primitives.DecodeVarInt(newdata) // Skip the Expansion Header, if any, since newdata = newdata[skip:] // we don't know what to do with it. cnt, newdata := binary.BigEndian.Uint32(newdata[0:4]), newdata[4:] newdata = newdata[4:] // Just skip the size... We don't really need it. b.Transactions = make([]interfaces.ITransaction, cnt, cnt) for i, _ := range b.endOfPeriod { b.endOfPeriod[i] = 0 } var periodMark = 0 for i := uint32(0); i < cnt; i++ { for newdata[0] == constants.MARKER { b.endOfPeriod[periodMark] = int(i) newdata = newdata[1:] periodMark++ } trans := new(Transaction) newdata, err = trans.UnmarshalBinaryData(newdata) if err != nil { return nil, fmt.Errorf("Failed to unmarshal a transaction in block.\n" + err.Error()) } b.Transactions[i] = trans } for periodMark < len(b.endOfPeriod) { newdata = newdata[1:] b.endOfPeriod[periodMark] = int(cnt) periodMark++ } return newdata, nil }