func newCommitChain() *CommitChainMsg { msg := new(CommitChainMsg) cc := entryCreditBlock.NewCommitChain() cc.Version = 0x11 cc.MilliTime = (*primitives.ByteSlice6)(&[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 { panic(err) } else { cc.ECPubKey = (*primitives.ByteSlice32)(pub) cc.Sig = (*primitives.ByteSlice64)(ed.Sign(privkey, cc.CommitMsg())) } msg.CommitChain = cc //msg.Timestamp = primitives.NewTimestampNow() return msg }
func HandleV2CommitChain(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) { commitChainMsg := new(MessageRequest) err := MapToObject(params, commitChainMsg) if err != nil { return nil, NewInvalidParamsError() } commit := entryCreditBlock.NewCommitChain() if p, err := hex.DecodeString(commitChainMsg.Message); err != nil { return nil, NewInvalidCommitChainError() } else { _, err := commit.UnmarshalBinaryData(p) if err != nil { return nil, NewInvalidCommitChainError() } } msg := new(messages.CommitChainMsg) msg.CommitChain = commit state.APIQueue() <- msg state.IncECCommits() resp := new(CommitChainResponse) resp.Message = "Chain Commit Success" resp.TxID = commit.GetSigHash().String() return resp, nil }
func NewCommitChain(eBlock *entryBlock.EBlock) *entryCreditBlock.CommitChain { commit := entryCreditBlock.NewCommitChain() commit.Version = 1 err := commit.MilliTime.UnmarshalBinary([]byte{0, 0, 0, 0, 0, byte(eBlock.GetHeader().GetDBHeight())}) if err != nil { panic(err) } commit.ChainIDHash = eBlock.GetHashOfChainIDHash() commit.Weld = eBlock.GetWeldHashes()[0] commit.EntryHash = eBlock.Body.EBEntries[0] bin, err := commit.MarshalBinary() if err != nil { panic(err) } cost, err := util.EntryCost(bin) if err != nil { panic(err) } commit.Credits = cost SignCommit(0, commit) return commit }
func (m *CommitChainMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling Commit Chain Message: %v", r) } }() newData = data if newData[0] != m.Type() { return nil, fmt.Errorf("Invalid Message type") } newData = newData[1:] cc := entryCreditBlock.NewCommitChain() newData, err = cc.UnmarshalBinaryData(newData) if err != nil { return nil, err } m.CommitChain = cc if len(newData) > 0 { m.Signature = new(primitives.Signature) newData, err = m.Signature.UnmarshalBinaryData(newData) if err != nil { return nil, err } } return newData, nil }
func (m *CommitChainMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling: %v", r) } }() newData = data[1:] cc := entryCreditBlock.NewCommitChain() newData, err = cc.UnmarshalBinaryData(newData) if err != nil { return nil, err } m.CommitChain = cc return newData, nil }
func HandleCommitChain(ctx *web.Context) { state := ctx.Server.Env["state"].(interfaces.IState) type commitchain struct { CommitChainMsg string } c := new(commitchain) if p, err := ioutil.ReadAll(ctx.Request.Body); err != nil { returnMsg(ctx, "Bad commit message", false) return } else { if err := json.Unmarshal(p, c); err != nil { returnMsg(ctx, "Bad commit message", false) return } } commit := entryCreditBlock.NewCommitChain() if p, err := hex.DecodeString(c.CommitChainMsg); err != nil { returnMsg(ctx, "Bad commit message", false) return } else { _, err := commit.UnmarshalBinaryData(p) if err != nil { returnMsg(ctx, "Bad commit message", false) return } } msg := new(messages.CommitChainMsg) msg.CommitChain = commit msg.Timestamp = state.GetTimestamp() state.InMsgQueue() <- msg returnMsg(ctx, "Chain Commit Success", true) }