Skip to content

Commit

Permalink
feat(prover): normal prover should wait targetProofTime before submit…
Browse files Browse the repository at this point in the history
…ting proofs (taikoxyz#232)
  • Loading branch information
RogerLamTd authored May 19, 2023
1 parent 7dd107a commit 99343d6
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ccecd708276bce3eca84b92c7c48c95b2156dd18
285c756c270fa4041c10aa06d95e2067fcc1b69f
6 changes: 6 additions & 0 deletions prover/proof_producer/dummy_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type DummyProofProducer struct {
RandomDummyProofDelayUpperBound *time.Duration
}

func (d *DummyProofProducer) CalcDelay(
header *types.Header,
) time.Duration {
return time.Duration(0)
}

// RequestProof implements the ProofProducer interface.
func (d *DummyProofProducer) RequestProof(
ctx context.Context,
Expand Down
2 changes: 2 additions & 0 deletions prover/proof_producer/proof_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -54,6 +55,7 @@ type ProofProducer interface {
resultCh chan *ProofWithHeader,
) error
Cancel(ctx context.Context, blockID *big.Int) error
CalcDelay(header *types.Header) time.Duration
}

func DegreeToCircuitsIdx(degree uint64) (uint16, error) {
Expand Down
7 changes: 7 additions & 0 deletions prover/proof_producer/special_proof_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -60,6 +61,12 @@ func NewSpecialProofProducer(
}, nil
}

func (p *SpecialProofProducer) CalcDelay(
header *types.Header,
) time.Duration {
return time.Duration(0)
}

// RequestProof implements the ProofProducer interface.
func (p *SpecialProofProducer) RequestProof(
ctx context.Context,
Expand Down
44 changes: 31 additions & 13 deletions prover/proof_producer/zkevm_rpcd_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ZkevmRpcdProducer struct {
L2Endpoint string // a L2 execution engine's RPC endpoint
Retry bool // retry proof computation if error
CustomProofHook func() ([]byte, uint64, error) // only for testing purposes
ProofTimeTarget uint64 // used for calculating proof delay
}

// RequestProofBody represents the JSON body for requesting the proof.
Expand Down Expand Up @@ -90,16 +91,31 @@ func NewZkevmRpcdProducer(
l1Endpoint string,
l2Endpoint string,
retry bool,
proofTimeTarget uint64,
) (*ZkevmRpcdProducer, error) {
return &ZkevmRpcdProducer{
RpcdEndpoint: rpcdEndpoint,
Param: param,
L1Endpoint: l1Endpoint,
L2Endpoint: l2Endpoint,
Retry: retry,
RpcdEndpoint: rpcdEndpoint,
Param: param,
L1Endpoint: l1Endpoint,
L2Endpoint: l2Endpoint,
Retry: retry,
ProofTimeTarget: proofTimeTarget,
}, nil
}

func (p *ZkevmRpcdProducer) CalcDelay(
header *types.Header,
) time.Duration {
// if > 0, delay has not yet elapsed; proof should be delayed.
// if <= 0, delay has already elapsed; proof does not need delay.
delay := ((p.ProofTimeTarget + header.Time) - uint64(time.Now().Unix()))
if delay > 0 {
return time.Duration(delay)
} else {
return time.Duration(0)
}
}

// RequestProof implements the ProofProducer interface.
func (p *ZkevmRpcdProducer) RequestProof(
ctx context.Context,
Expand Down Expand Up @@ -131,14 +147,16 @@ func (p *ZkevmRpcdProducer) RequestProof(
return err
}

resultCh <- &ProofWithHeader{
BlockID: blockID,
Header: header,
Meta: meta,
ZkProof: proof,
Degree: degree,
Opts: opts,
}
time.AfterFunc(p.CalcDelay(header), func() {
resultCh <- &ProofWithHeader{
BlockID: blockID,
Header: header,
Meta: meta,
ZkProof: proof,
Degree: degree,
Opts: opts,
}
})

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion prover/proof_producer/zkevm_rpcd_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestNewZkevmRpcdProducer(t *testing.T) {
dummpyZkevmRpcdProducer, err := NewZkevmRpcdProducer("http://localhost:18545", "", "", "", false)
dummpyZkevmRpcdProducer, err := NewZkevmRpcdProducer("http://localhost:18545", "", "", "", false, 0)
require.Nil(t, err)

dummpyZkevmRpcdProducer.CustomProofHook = func() ([]byte, uint64, error) {
Expand Down
7 changes: 7 additions & 0 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ func InitFromConfig(ctx context.Context, p *Prover, cfg *Config) (err error) {
isSystemProver := cfg.SystemProver
isOracleProver := cfg.OracleProver

stateVars, err := p.rpc.GetProtocolStateVariables(nil)
if err != nil {
log.Error("error retrieving protocol state variables", "error", err)
return
}

if isSystemProver || isOracleProver {
var specialProverAddress common.Address
var privateKey *ecdsa.PrivateKey
Expand Down Expand Up @@ -188,6 +194,7 @@ func InitFromConfig(ctx context.Context, p *Prover, cfg *Config) (err error) {
cfg.L1HttpEndpoint,
cfg.L2HttpEndpoint,
true,
stateVars.ProofTimeTarget,
); err != nil {
return err
}
Expand Down

0 comments on commit 99343d6

Please sign in to comment.