mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add reverts, forge in test ethClient, update auction
This commit is contained in:
@@ -11,7 +11,7 @@ type Proof struct {
|
||||
// BatchInfo contans the Batch information
|
||||
type BatchInfo struct {
|
||||
batchNum common.BatchNum
|
||||
serverProof *ServerProofInfo
|
||||
serverProof ServerProofInterface
|
||||
zkInputs *common.ZKInputs
|
||||
proof *Proof
|
||||
L1UserTxsExtra []*common.L1Tx
|
||||
@@ -21,8 +21,8 @@ type BatchInfo struct {
|
||||
}
|
||||
|
||||
// NewBatchInfo creates a new BatchInfo with the given batchNum &
|
||||
// ServerProofInfo
|
||||
func NewBatchInfo(batchNum common.BatchNum, serverProof *ServerProofInfo) BatchInfo {
|
||||
// ServerProof
|
||||
func NewBatchInfo(batchNum common.BatchNum, serverProof ServerProofInterface) BatchInfo {
|
||||
return BatchInfo{
|
||||
batchNum: batchNum,
|
||||
serverProof: serverProof,
|
||||
@@ -43,8 +43,8 @@ func (bi *BatchInfo) SetZKInputs(zkInputs *common.ZKInputs) {
|
||||
bi.zkInputs = zkInputs
|
||||
}
|
||||
|
||||
// SetServerProof sets the ServerProofInfo to the BatchInfo data structure
|
||||
func (bi *BatchInfo) SetServerProof(serverProof *ServerProofInfo) {
|
||||
// SetServerProof sets the ServerProof to the BatchInfo data structure
|
||||
func (bi *BatchInfo) SetServerProof(serverProof ServerProofInterface) {
|
||||
bi.serverProof = serverProof
|
||||
}
|
||||
|
||||
@@ -53,29 +53,29 @@ func (bi *BatchInfo) SetProof(proof *Proof) {
|
||||
bi.proof = proof
|
||||
}
|
||||
|
||||
// BatchQueue implements a FIFO queue of BatchInfo
|
||||
type BatchQueue struct {
|
||||
queue []*BatchInfo
|
||||
}
|
||||
|
||||
// NewBatchQueue returns a new *BatchQueue
|
||||
func NewBatchQueue() *BatchQueue {
|
||||
return &BatchQueue{
|
||||
queue: []*BatchInfo{},
|
||||
}
|
||||
}
|
||||
|
||||
// Push adds the given BatchInfo to the BatchQueue
|
||||
func (bq *BatchQueue) Push(b *BatchInfo) {
|
||||
bq.queue = append(bq.queue, b)
|
||||
}
|
||||
|
||||
// Pop pops the first BatchInfo from the BatchQueue
|
||||
func (bq *BatchQueue) Pop() *BatchInfo {
|
||||
if len(bq.queue) == 0 {
|
||||
return nil
|
||||
}
|
||||
b := bq.queue[0]
|
||||
bq.queue = bq.queue[1:]
|
||||
return b
|
||||
}
|
||||
// // BatchQueue implements a FIFO queue of BatchInfo
|
||||
// type BatchQueue struct {
|
||||
// queue []*BatchInfo
|
||||
// }
|
||||
//
|
||||
// // NewBatchQueue returns a new *BatchQueue
|
||||
// func NewBatchQueue() *BatchQueue {
|
||||
// return &BatchQueue{
|
||||
// queue: []*BatchInfo{},
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Push adds the given BatchInfo to the BatchQueue
|
||||
// func (bq *BatchQueue) Push(b *BatchInfo) {
|
||||
// bq.queue = append(bq.queue, b)
|
||||
// }
|
||||
//
|
||||
// // Pop pops the first BatchInfo from the BatchQueue
|
||||
// func (bq *BatchQueue) Pop() *BatchInfo {
|
||||
// if len(bq.queue) == 0 {
|
||||
// return nil
|
||||
// }
|
||||
// b := bq.queue[0]
|
||||
// bq.queue = bq.queue[1:]
|
||||
// return b
|
||||
// }
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
package coordinator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBatchQueue(t *testing.T) {
|
||||
bq := BatchQueue{}
|
||||
|
||||
bq.Push(&BatchInfo{
|
||||
batchNum: 0,
|
||||
})
|
||||
bq.Push(&BatchInfo{
|
||||
batchNum: 2,
|
||||
})
|
||||
bq.Push(&BatchInfo{
|
||||
batchNum: 1,
|
||||
})
|
||||
|
||||
assert.Equal(t, common.BatchNum(0), bq.Pop().batchNum)
|
||||
assert.Equal(t, common.BatchNum(2), bq.Pop().batchNum)
|
||||
assert.Equal(t, common.BatchNum(1), bq.Pop().batchNum)
|
||||
assert.Nil(t, bq.Pop())
|
||||
}
|
||||
// import (
|
||||
// "testing"
|
||||
//
|
||||
// "github.com/hermeznetwork/hermez-node/common"
|
||||
// "github.com/stretchr/testify/assert"
|
||||
// )
|
||||
//
|
||||
// func TestBatchQueue(t *testing.T) {
|
||||
// bq := BatchQueue{}
|
||||
//
|
||||
// bq.Push(&BatchInfo{
|
||||
// batchNum: 0,
|
||||
// })
|
||||
// bq.Push(&BatchInfo{
|
||||
// batchNum: 2,
|
||||
// })
|
||||
// bq.Push(&BatchInfo{
|
||||
// batchNum: 1,
|
||||
// })
|
||||
//
|
||||
// assert.Equal(t, common.BatchNum(0), bq.Pop().batchNum)
|
||||
// assert.Equal(t, common.BatchNum(2), bq.Pop().batchNum)
|
||||
// assert.Equal(t, common.BatchNum(1), bq.Pop().batchNum)
|
||||
// assert.Nil(t, bq.Pop())
|
||||
// }
|
||||
|
||||
@@ -31,7 +31,7 @@ type Coordinator struct {
|
||||
config Config
|
||||
|
||||
batchNum common.BatchNum
|
||||
serverProofPool ServerProofPool
|
||||
serverProofPool *ServerProofPool
|
||||
|
||||
// synchronizer *synchronizer.Synchronizer
|
||||
hdb *historydb.HistoryDB
|
||||
@@ -47,14 +47,20 @@ func NewCoordinator(conf Config,
|
||||
hdb *historydb.HistoryDB,
|
||||
txsel *txselector.TxSelector,
|
||||
bb *batchbuilder.BatchBuilder,
|
||||
serverProofs []ServerProofInterface,
|
||||
ethClient *eth.Client) *Coordinator { // once synchronizer is ready, synchronizer.Synchronizer will be passed as parameter here
|
||||
serverProofPool := NewServerProofPool(len(serverProofs))
|
||||
for _, serverProof := range serverProofs {
|
||||
serverProofPool.Add(serverProof)
|
||||
}
|
||||
c := Coordinator{
|
||||
config: conf,
|
||||
hdb: hdb,
|
||||
txsel: txsel,
|
||||
batchBuilder: bb,
|
||||
ethClient: ethClient,
|
||||
ethTxStore: memory.NewMemoryStorage(),
|
||||
config: conf,
|
||||
serverProofPool: serverProofPool,
|
||||
hdb: hdb,
|
||||
txsel: txsel,
|
||||
batchBuilder: bb,
|
||||
ethClient: ethClient,
|
||||
ethTxStore: memory.NewMemoryStorage(),
|
||||
}
|
||||
return &c
|
||||
}
|
||||
@@ -97,13 +103,14 @@ func (c *Coordinator) ForgeLoopFn(outBatchCh chan *BatchInfo, stopCh chan bool)
|
||||
// 0. If there's an available server proof: Start pipeline for batchNum = batchNum + 1.
|
||||
// non-blocking call, returns nil if a server proof is
|
||||
// not available, or non-nil otherwise.
|
||||
serverProofInfo, err := c.serverProofPool.GetNextAvailable(stopCh)
|
||||
serverProof, err := c.serverProofPool.Get(stopCh)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
log.Debugw("got serverProof", "server", serverProof)
|
||||
|
||||
log.Debugw("start forge")
|
||||
batchInfo, err := c.forge(serverProofInfo)
|
||||
batchInfo, err := c.forge(serverProof)
|
||||
if err != nil {
|
||||
log.Errorw("forge", "error", err)
|
||||
return true, err
|
||||
@@ -123,7 +130,7 @@ func (c *Coordinator) GetProofCallForgeLoopFn(inBatchCh, outBatchCh chan *BatchI
|
||||
return ErrStop
|
||||
case batchInfo := <-inBatchCh:
|
||||
log.Debugw("start getProofCallForge", "batchNum", batchInfo.batchNum)
|
||||
if err := c.getProofCallForge(batchInfo); err != nil {
|
||||
if err := c.getProofCallForge(batchInfo, stopCh); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debugw("end getProofCallForge", "batchNum", batchInfo.batchNum)
|
||||
@@ -150,7 +157,7 @@ func (c *Coordinator) ForgeCallConfirmLoopFn(inBatchCh chan *BatchInfo, stopCh c
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Coordinator) forge(serverProofInfo *ServerProofInfo) (*BatchInfo, error) {
|
||||
func (c *Coordinator) forge(serverProof ServerProofInterface) (*BatchInfo, error) {
|
||||
// remove transactions from the pool that have been there for too long
|
||||
err := c.purgeRemoveByTimeout()
|
||||
if err != nil {
|
||||
@@ -158,7 +165,7 @@ func (c *Coordinator) forge(serverProofInfo *ServerProofInfo) (*BatchInfo, error
|
||||
}
|
||||
|
||||
c.batchNum = c.batchNum + 1
|
||||
batchInfo := NewBatchInfo(c.batchNum, serverProofInfo) // to accumulate metadata of the batch
|
||||
batchInfo := NewBatchInfo(c.batchNum, serverProof) // to accumulate metadata of the batch
|
||||
|
||||
var poolL2Txs []*common.PoolL2Tx
|
||||
// var feesInfo
|
||||
@@ -216,9 +223,9 @@ func (c *Coordinator) forge(serverProofInfo *ServerProofInfo) (*BatchInfo, error
|
||||
}
|
||||
|
||||
// getProofCallForge gets the generated zkProof & sends it to the SmartContract
|
||||
func (c *Coordinator) getProofCallForge(batchInfo *BatchInfo) error {
|
||||
serverProofInfo := batchInfo.serverProof
|
||||
proof, err := serverProofInfo.GetProof() // blocking call, until not resolved don't continue. Returns when the proof server has calculated the proof
|
||||
func (c *Coordinator) getProofCallForge(batchInfo *BatchInfo, stopCh chan bool) error {
|
||||
serverProof := batchInfo.serverProof
|
||||
proof, err := serverProof.GetProof(stopCh) // blocking call, until not resolved don't continue. Returns when the proof server has calculated the proof
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -254,26 +261,26 @@ func (c *Coordinator) forgeCallConfirm(batchInfo *BatchInfo) error {
|
||||
}
|
||||
|
||||
func (c *Coordinator) handleReorg() error {
|
||||
return nil
|
||||
return nil // TODO
|
||||
}
|
||||
|
||||
// isForgeSequence returns true if the node is the Forger in the current ethereum block
|
||||
func (c *Coordinator) isForgeSequence() bool {
|
||||
return c.isForgeSeq
|
||||
return c.isForgeSeq // TODO
|
||||
}
|
||||
|
||||
func (c *Coordinator) purgeRemoveByTimeout() error {
|
||||
return nil
|
||||
return nil // TODO
|
||||
}
|
||||
|
||||
func (c *Coordinator) purgeInvalidDueToL2TxsSelection(l2Txs []*common.PoolL2Tx) error {
|
||||
return nil
|
||||
return nil // TODO
|
||||
}
|
||||
|
||||
func (c *Coordinator) shouldL1L2Batch() bool {
|
||||
return false
|
||||
return false // TODO
|
||||
}
|
||||
|
||||
func (c *Coordinator) prepareForgeBatchArgs(batchInfo *BatchInfo) *eth.RollupForgeBatchArgs {
|
||||
return nil
|
||||
return nil // TODO
|
||||
}
|
||||
|
||||
@@ -126,7 +126,9 @@ func TestCoordinator(t *testing.T) {
|
||||
|
||||
conf := Config{}
|
||||
hdb := &historydb.HistoryDB{}
|
||||
c := NewCoordinator(conf, hdb, txsel, bb, ð.Client{})
|
||||
serverProofs := []ServerProofInterface{&ServerProof{}, &ServerProof{}}
|
||||
ethClient := ð.Client{}
|
||||
c := NewCoordinator(conf, hdb, txsel, bb, serverProofs, ethClient)
|
||||
cn := NewCoordNode(c)
|
||||
cn.Start()
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
@@ -5,35 +5,61 @@ import (
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
)
|
||||
|
||||
// ServerProofInfo contains the data related to a ServerProof
|
||||
type ServerProofInfo struct {
|
||||
type ServerProofInterface interface {
|
||||
CalculateProof(zkInputs *common.ZKInputs) error
|
||||
GetProof(stopCh chan bool) (*Proof, error)
|
||||
}
|
||||
|
||||
// ServerProof contains the data related to a ServerProof
|
||||
type ServerProof struct {
|
||||
// TODO
|
||||
URL string
|
||||
Available bool
|
||||
}
|
||||
|
||||
func NewServerProof(URL string) *ServerProof {
|
||||
return &ServerProof{URL: URL}
|
||||
}
|
||||
|
||||
// CalculateProof sends the *common.ZKInputs to the ServerProof to compute the
|
||||
// Proof
|
||||
func (p *ServerProofInfo) CalculateProof(zkInputs *common.ZKInputs) error {
|
||||
func (p *ServerProof) CalculateProof(zkInputs *common.ZKInputs) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetProof retreives the Proof from the ServerProof
|
||||
func (p *ServerProofInfo) GetProof() (*Proof, error) {
|
||||
func (p *ServerProof) GetProof(stopCh chan bool) (*Proof, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// ServerProofPool contains the multiple ServerProofInfo
|
||||
// ServerProofPool contains the multiple ServerProof
|
||||
type ServerProofPool struct {
|
||||
// pool []ServerProofInfo
|
||||
pool chan ServerProofInterface
|
||||
}
|
||||
|
||||
// GetNextAvailable returns the available ServerProofInfo
|
||||
func (p *ServerProofPool) GetNextAvailable(stopCh chan bool) (*ServerProofInfo, error) {
|
||||
func NewServerProofPool(maxServerProofs int) *ServerProofPool {
|
||||
return &ServerProofPool{
|
||||
pool: make(chan ServerProofInterface, maxServerProofs),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ServerProofPool) Add(serverProof ServerProofInterface) {
|
||||
p.pool <- serverProof
|
||||
}
|
||||
|
||||
// Get returns the available ServerProof
|
||||
func (p *ServerProofPool) Get(stopCh chan bool) (ServerProofInterface, error) {
|
||||
select {
|
||||
case <-stopCh:
|
||||
log.Info("ServerProofPool.GetNextAvailable stopped")
|
||||
log.Info("ServerProofPool.Get stopped")
|
||||
return nil, ErrStop
|
||||
default:
|
||||
select {
|
||||
case <-stopCh:
|
||||
log.Info("ServerProofPool.Get stopped")
|
||||
return nil, ErrStop
|
||||
case serverProof := <-p.pool:
|
||||
return serverProof, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user