func TestCommitChainMarshal(t *testing.T) { fmt.Printf("---\nTestCommitChainMarshal\n---\n") cc := common.NewCommitChain() // test MarshalBinary on a zeroed CommitChain if p, err := cc.MarshalBinary(); err != nil { t.Error(err) } else if z := make([]byte, common.CommitChainSize); string(p) != string(z) { t.Errorf("Marshal failed on zeroed CommitChain") } // build a CommitChain for testing cc.Version = 0 cc.MilliTime = &[6]byte{1, 1, 1, 1, 1, 1} p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") cc.ChainIDHash.SetBytes(p) p, _ = hex.DecodeString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") cc.Weld.SetBytes(p) p, _ = hex.DecodeString("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc") cc.EntryHash.SetBytes(p) cc.Credits = 11 // make a key and sign the msg if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil { t.Error(err) } else { cc.ECPubKey = pub cc.Sig = ed.Sign(privkey, cc.CommitMsg()) } // marshal and unmarshal the commit and see if it matches cc2 := common.NewCommitChain() if p, err := cc.MarshalBinary(); err != nil { t.Error(err) } else { t.Logf("%x\n", p) cc2.UnmarshalBinary(p) } if !cc2.IsValid() { t.Errorf("signature did not match after unmarshalbinary") } }
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver. // This is part of the Message interface implementation. func (msg *MsgCommitChain) BtcDecode(r io.Reader, pver uint32) error { bytes, err := readVarBytes(r, pver, uint32(common.CommitChainSize), CmdEntry) if err != nil { return err } msg.CommitChain = common.NewCommitChain() if err := msg.CommitChain.UnmarshalBinary(bytes); err != nil { return err } return nil }
func handleCommitChain(ctx *web.Context) { type commitchain struct { CommitChainMsg string } c := new(commitchain) if p, err := ioutil.ReadAll(ctx.Request.Body); err != nil { wsLog.Error(err) ctx.WriteHeader(httpBad) ctx.Write([]byte(err.Error())) return } else { if err := json.Unmarshal(p, c); err != nil { wsLog.Error(err) ctx.WriteHeader(httpBad) ctx.Write([]byte(err.Error())) return } } commit := common.NewCommitChain() if p, err := hex.DecodeString(c.CommitChainMsg); err != nil { wsLog.Error(err) ctx.WriteHeader(httpBad) ctx.Write([]byte(err.Error())) return } else { _, err := commit.UnmarshalBinaryData(p) if err != nil { wsLog.Error(err) ctx.WriteHeader(httpBad) ctx.Write([]byte(err.Error())) return } } if err := factomapi.CommitChain(commit); err != nil { wsLog.Error(err) ctx.WriteHeader(httpBad) ctx.Write([]byte(err.Error())) return } // ctx.WriteHeader(httpOK) }
// NewMsgCommitChain returns a new Commit Chain message that conforms to the // Message interface. See MsgInv for details. func NewMsgCommitChain() *MsgCommitChain { m := new(MsgCommitChain) m.CommitChain = common.NewCommitChain() return m }
func TestECBlockMarshal(t *testing.T) { ecb1 := common.NewECBlock() // build a CommitChain for testing cc := common.NewCommitChain() cc.Version = 0 cc.MilliTime = &[6]byte{1, 1, 1, 1, 1, 1} cc.ChainIDHash.SetBytes(byteof(0xaa)) cc.Weld.SetBytes(byteof(0xbb)) cc.EntryHash.SetBytes(byteof(0xcc)) cc.Credits = 11 // make a key and sign the msg if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil { t.Error(err) } else { cc.ECPubKey = pub cc.Sig = ed.Sign(privkey, cc.CommitMsg()) } // create a ECBlock for testing ecb1.Header.ECChainID.SetBytes(byteof(0x11)) ecb1.Header.BodyHash.SetBytes(byteof(0x22)) ecb1.Header.PrevHeaderHash.SetBytes(byteof(0x33)) ecb1.Header.PrevLedgerKeyMR.SetBytes(byteof(0x44)) ecb1.Header.EBHeight = 10 ecb1.Header.HeaderExpansionArea = byteof(0x55) ecb1.Header.ObjectCount = 0 // add the CommitChain to the ECBlock ecb1.AddEntry(cc) m1 := common.NewMinuteNumber() m1.Number = 0x01 ecb1.AddEntry(m1) // add a ServerIndexNumber si1 := common.NewServerIndexNumber() si1.Number = 3 ecb1.AddEntry(si1) // create an IncreaseBalance for testing ib := common.NewIncreaseBalance() pub := new([32]byte) copy(pub[:], byteof(0xaa)) ib.ECPubKey = pub ib.TXID.SetBytes(byteof(0xbb)) ib.NumEC = uint64(13) // add the IncreaseBalance ecb1.AddEntry(ib) m2 := common.NewMinuteNumber() m2.Number = 0x02 ecb1.AddEntry(m2) ecb2 := common.NewECBlock() if p, err := ecb1.MarshalBinary(); err != nil { t.Error(err) } else { if err := ecb2.UnmarshalBinary(p); err != nil { t.Error(err) } t.Log(spew.Sdump(ecb1)) t.Log(spew.Sdump(ecb2)) if q, err := ecb2.MarshalBinary(); err != nil { t.Error(err) } else if string(p) != string(q) { t.Errorf("ecb1 = %x\n", p) t.Errorf("ecb2 = %x\n", q) } } }