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