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	// ResendNum is the number of times the tx has been resent
 51	ResendNum int
 52	// LastScheduledL1BatchBlockNum is the blockNum when the last L1Batch
 53	// was scheduled
 54	LastScheduledL1BatchBlockNum int64
 55	// LastL1BatchBlock is the blockNum in which the last L1Batch was
 56	// synced
 57	LastL1BatchBlock int64
 58	// LastL1BatchBlockDelta is the number of blocks after the last L1Batch
 59	LastL1BatchBlockDelta int64
 60	// L1BatchBlockScheduleDeadline is the number of blocks after the last
 61	// L1Batch after which an L1Batch will be scheduled
 62	L1BatchBlockScheduleDeadline int64
 63	// StartToMineBlocksDelay is the number of blocks that happen between
 64	// scheduling a batch and having it mined
 65	StartToMineBlocksDelay int64
 66	// StartToSendDelay is the delay between starting a batch and sending
 67	// it to ethereum, in seconds
 68	StartToSendDelay float64
 69	// StartToMineDelay is the delay between starting a batch and having
 70	// it mined in seconds
 71	StartToMineDelay float64
 72	// SendToMineDelay is the delay between sending a batch tx and having
 73	// it mined in seconds
 74	SendToMineDelay float64
 75}
 76
 77// BatchInfo contans the Batch information
 78type BatchInfo struct {
 79	PipelineNum           int
 80	BatchNum              common.BatchNum
 81	ServerProof           prover.Client
 82	ZKInputs              *common.ZKInputs
 83	Proof                 *prover.Proof
 84	PublicInputs          []*big.Int
 85	L1Batch               bool
 86	VerifierIdx           uint8
 87	L1UserTxsExtra        []common.L1Tx
 88	L1CoordTxs            []common.L1Tx
 89	L1CoordinatorTxsAuths [][]byte
 90	L2Txs                 []common.L2Tx
 91	CoordIdxs             []common.Idx
 92	ForgeBatchArgs        *eth.RollupForgeBatchArgs
 93	// FeesInfo
 94	EthTx    *types.Transaction
 95	EthTxErr error
 96	// SendTimestamp  the time of batch sent to ethereum
 97	SendTimestamp time.Time
 98	Receipt       *types.Receipt
 99	// Fail is true if:
100	// - The receipt status is failed
101	// - A previous parent batch is failed
102	Fail  bool
103	Debug Debug
104}
105
106// DebugStore is a debug function to store the BatchInfo as a json text file in
107// storePath.  The filename contains the batchNumber followed by a timestamp of
108// batch start.
109func (b *BatchInfo) DebugStore(storePath string) error {
110	batchJSON, err := json.MarshalIndent(b, "", "  ")
111	if err != nil {
112		return tracerr.Wrap(err)
113	}
114	// nolint reason: hardcoded 1_000_000 is the number of nanoseconds in a
115	// millisecond
116	//nolint:gomnd
117	filename := fmt.Sprintf("%08d-%v.%03d.json", b.BatchNum,
118		b.Debug.StartTimestamp.Unix(), b.Debug.StartTimestamp.Nanosecond()/1_000_000)
119	// nolint reason: 0640 allows rw to owner and r to group
120	//nolint:gosec
121	return ioutil.WriteFile(path.Join(storePath, filename), batchJSON, 0640)
122}