arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/coordinator/batch.go
coordinator/batch.goRaw
  1package coordinator
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"io/ioutil"
  7	"math/big"
  8	"path"
  9	"time"
 10
 11	"github.com/ethereum/go-ethereum/core/types"
 12	"github.com/hermeznetwork/hermez-node/common"
 13	"github.com/hermeznetwork/hermez-node/eth"
 14	"github.com/hermeznetwork/hermez-node/prover"
 15	"github.com/hermeznetwork/tracerr"
 16)
 17
 18// Status is used to mark the status of the batch
 19type Status string
 20
 21const (
 22	// StatusPending marks the Tx as Pending
 23	StatusPending Status = "pending"
 24	// StatusForged marks the batch as forged internally
 25	StatusForged Status = "forged"
 26	// StatusProof marks the batch as proof calculated
 27	StatusProof Status = "proof"
 28	// StatusSent marks the EthTx as Sent
 29	StatusSent Status = "sent"
 30	// StatusMined marks the EthTx as Mined
 31	StatusMined Status = "mined"
 32	// StatusFailed marks the EthTx as Failed
 33	StatusFailed Status = "failed"
 34)
 35
 36// Debug information related to the Batch
 37type Debug struct {
 38	// StartTimestamp of is the time of batch start
 39	StartTimestamp time.Time
 40	// SendTimestamp  the time of batch sent to ethereum
 41	SendTimestamp time.Time
 42	// Status of the Batch
 43	Status Status
 44	// StartBlockNum is the blockNum when the Batch was started
 45	StartBlockNum int64
 46	// MineBlockNum is the blockNum in which the batch was mined
 47	MineBlockNum int64
 48	// SendBlockNum is the blockNum when the batch was sent to ethereum
 49	SendBlockNum int64
 50	// LastScheduledL1BatchBlockNum is the blockNum when the last L1Batch
 51	// was scheduled
 52	LastScheduledL1BatchBlockNum int64
 53	// LastL1BatchBlock is the blockNum in which the last L1Batch was
 54	// synced
 55	LastL1BatchBlock int64
 56	// LastL1BatchBlockDelta is the number of blocks after the last L1Batch
 57	LastL1BatchBlockDelta int64
 58	// L1BatchBlockScheduleDeadline is the number of blocks after the last
 59	// L1Batch after which an L1Batch will be scheduled
 60	L1BatchBlockScheduleDeadline int64
 61	// StartToMineBlocksDelay is the number of blocks that happen between
 62	// scheduling a batch and having it mined
 63	StartToMineBlocksDelay int64
 64	// StartToSendDelay is the delay between starting a batch and sending
 65	// it to ethereum, in seconds
 66	StartToSendDelay float64
 67}
 68
 69// BatchInfo contans the Batch information
 70type BatchInfo struct {
 71	BatchNum              common.BatchNum
 72	ServerProof           prover.Client
 73	ZKInputs              *common.ZKInputs
 74	Proof                 *prover.Proof
 75	PublicInputs          []*big.Int
 76	L1Batch               bool
 77	VerifierIdx           uint8
 78	L1UserTxsExtra        []common.L1Tx
 79	L1CoordTxs            []common.L1Tx
 80	L1CoordinatorTxsAuths [][]byte
 81	L2Txs                 []common.L2Tx
 82	CoordIdxs             []common.Idx
 83	ForgeBatchArgs        *eth.RollupForgeBatchArgs
 84	// FeesInfo
 85	EthTx   *types.Transaction
 86	Receipt *types.Receipt
 87	Debug   Debug
 88}
 89
 90// DebugStore is a debug function to store the BatchInfo as a json text file in
 91// storePath.  The filename contains the batchNumber followed by a timestamp of
 92// batch start.
 93func (b *BatchInfo) DebugStore(storePath string) error {
 94	batchJSON, err := json.MarshalIndent(b, "", "  ")
 95	if err != nil {
 96		return tracerr.Wrap(err)
 97	}
 98	// nolint reason: hardcoded 1_000_000 is the number of nanoseconds in a
 99	// millisecond
100	//nolint:gomnd
101	filename := fmt.Sprintf("%08d-%v.%v.json", b.BatchNum,
102		b.Debug.StartTimestamp.Unix(), b.Debug.StartTimestamp.Nanosecond()/1_000_000)
103	// nolint reason: 0640 allows rw to owner and r to group
104	//nolint:gosec
105	return ioutil.WriteFile(path.Join(storePath, filename), batchJSON, 0640)
106}