github.com/annchain/OG@v0.0.9/rpc/archive_controller.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rpc
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	math2 "github.com/annchain/OG/arefactor/common/math"
    21  	"github.com/annchain/OG/og/types"
    22  
    23  	"github.com/annchain/OG/status"
    24  	"github.com/gin-gonic/gin"
    25  	"github.com/sirupsen/logrus"
    26  	"net/http"
    27  	"sync/atomic"
    28  	"time"
    29  )
    30  
    31  var archiveId uint32
    32  
    33  func getArchiveId() uint32 {
    34  	if archiveId > math2.MaxUint32-1000 {
    35  		archiveId = 10
    36  	}
    37  	return atomic.AddUint32(&archiveId, 1)
    38  }
    39  
    40  func (r *RpcController) NewArchive(c *gin.Context) {
    41  	var (
    42  		tx    types.Txi
    43  		txReq NewArchiveRequest
    44  	)
    45  	now := time.Now()
    46  	id := getArchiveId()
    47  	if !status.ArchiveMode {
    48  		Response(c, http.StatusBadRequest, fmt.Errorf("not archive mode"), nil)
    49  		return
    50  	}
    51  	err := c.ShouldBindJSON(&txReq)
    52  	if err != nil {
    53  		Response(c, http.StatusBadRequest, fmt.Errorf("request format error: %v", err), nil)
    54  		return
    55  	}
    56  	//c.Request.Context()
    57  	if len(txReq.Data) == 0 {
    58  		Response(c, http.StatusBadRequest, fmt.Errorf("request format error: no data"), nil)
    59  		return
    60  	}
    61  	var buf bytes.Buffer
    62  	buf.Write(txReq.Data)
    63  	//TODO compress data
    64  	logrus.WithField("id ", id).WithField("data  ", string(txReq.Data)).Trace("got archive request")
    65  	tx, err = r.TxCreator.NewArchiveWithSeal(buf.Bytes())
    66  	if err != nil {
    67  		Response(c, http.StatusInternalServerError, fmt.Errorf("new tx failed %v", err), nil)
    68  		return
    69  	}
    70  	logrus.WithField("id ", id).WithField("tx", tx).Debugf("tx generated")
    71  	if !r.SyncerManager.IncrementalSyncer.Enabled {
    72  		Response(c, http.StatusOK, fmt.Errorf("tx is disabled when syncing"), nil)
    73  		return
    74  	}
    75  	//TODO which one is fast
    76  	//r.SyncerManager.IncrementalSyncer.CacheTx(tx)
    77  
    78  	r.TxBuffer.ReceivedNewTxChan <- tx
    79  	logrus.WithField("used time ", time.Since(now)).WithField("id ", id).WithField("tx ", tx).Trace("send ok")
    80  
    81  	Response(c, http.StatusOK, nil, tx.GetHash().Hex())
    82  	return
    83  }