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 (s *State) UpdateECs(ec interfaces.IEntryCreditBlock) { now := s.GetTimestamp() for _, entry := range ec.GetEntries() { cc, ok := entry.(*entryCreditBlock.CommitChain) if ok && s.Replay.IsTSValid_(constants.INTERNAL_REPLAY, cc.GetSigHash().Fixed(), cc.GetTimestamp(), now) { if s.NoEntryYet(cc.EntryHash, cc.GetTimestamp()) { cmsg := new(messages.CommitChainMsg) cmsg.CommitChain = cc s.PutCommit(cc.EntryHash, cmsg) } continue } ce, ok := entry.(*entryCreditBlock.CommitEntry) if ok && s.Replay.IsTSValid_(constants.INTERNAL_REPLAY, ce.GetSigHash().Fixed(), ce.GetTimestamp(), now) { if s.NoEntryYet(ce.EntryHash, ce.GetTimestamp()) { emsg := new(messages.CommitEntryMsg) emsg.CommitEntry = ce s.PutCommit(ce.EntryHash, emsg) } continue } } }
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) }