Skip to content

Commit

Permalink
feat: use previous proof if no transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo-emperor committed Apr 10, 2024
1 parent 7fe9295 commit 4190bf7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 54 deletions.
57 changes: 56 additions & 1 deletion newProof_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"encoding/base64"
"fmt"
"log"
"testing"
"time"

"github.com/RiemaLabs/modular-indexer-committee/ord"
"github.com/RiemaLabs/modular-indexer-committee/ord/stateless"
"github.com/ethereum/go-verkle"
)

func Test_NewProof(t *testing.T) {
Expand All @@ -21,7 +23,7 @@ func Test_NewProof(t *testing.T) {
go ServiceStage(ordGetterTest, &arguments, queue, 10*time.Millisecond)
for {
if ordGetterTest.LatestBlockHeight == queue.LatestHeight() {
if queue.VerifyProof() {
if VerifyProof(queue) {
log.Printf("Block: %d is verified!\n", ordGetterTest.LatestBlockHeight)
} else {
log.Printf("Block: %d cannot pass verification!\n", ordGetterTest.LatestBlockHeight)
Expand All @@ -34,3 +36,56 @@ func Test_NewProof(t *testing.T) {
time.Sleep(10 * time.Millisecond)
}
}

func VerifyProof(queue *stateless.Queue) bool {
if queue.LastStateProof == nil {
log.Println("queue.LastStateProof == nil")
return true
}
vProof, _, err := verkle.SerializeProof(queue.LastStateProof)
if err != nil {
log.Println("[VerifyProof]: verkle.SerializeProof(queue.LastStateProof) failed")
return false
}
vProofBytes, err := vProof.MarshalJSON()
if err != nil {
return false
}
finalproof := base64.StdEncoding.EncodeToString(vProofBytes[:])
log.Println("VerifyProof finalproof:", finalproof)
return finalproof == RollingbackProof(queue)
}

func RollingbackProof(queue *stateless.Queue) string {
// copy most code from apis.GetLatestStateProof
// and then return the finalproof
lastIndex := len(queue.History) - 1
postState := queue.Header.Root
preState, keys := stateless.Rollingback(queue.Header, &queue.History[lastIndex])

if len(keys) == 0 {
log.Println("[RollingbackProof]: len(keys) == 0")
return ""
}

proofOfKeys, _, _, _, err := verkle.MakeVerkleMultiProof(preState, postState, keys, stateless.NodeResolveFn)
if err != nil {
log.Printf("Failed to generate proof due to %v", err)
return ""
}

vProof, _, err := verkle.SerializeProof(proofOfKeys)
if err != nil {
log.Printf("Failed to serialize proof due to %v", err)
return ""
}

vProofBytes, err := vProof.MarshalJSON()
if err != nil {
log.Printf("Failed to marshal the proof to JSON due to %v", err)
return ""
}

finalproof := base64.StdEncoding.EncodeToString(vProofBytes[:])
return finalproof
}
56 changes: 3 additions & 53 deletions ord/stateless/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func (queue *Queue) Update(getter getter.OrdGetter, latestHeight uint) error {
if err != nil {
return err
}
queue.LastStateProof = proof
if proof != nil {
queue.LastStateProof = proof
}

queue.Header.OrdTrans = ordTransfer
// header.Height ++
Expand Down Expand Up @@ -323,55 +325,3 @@ func generateProofFromUpdate(header *Header, stateDiff *DiffState) (*verkle.Proo
}
return proof, nil
}

func (queue *Queue) VerifyProof() bool {
if queue.LastStateProof == nil {
log.Println("queue.LastStateProof == nil")
return true
}
vProof, _, err := verkle.SerializeProof(queue.LastStateProof)
if err != nil {
log.Println("[VerifyProof]: verkle.SerializeProof(queue.LastStateProof) failed")
return false
}
vProofBytes, err := vProof.MarshalJSON()
if err != nil {
return false
}
finalproof := base64.StdEncoding.EncodeToString(vProofBytes[:])
log.Println("VerifyProof finalproof:", finalproof)
return finalproof == queue.RollingbackProof()
}
func (queue *Queue) RollingbackProof() string {
// copy most code from apis.GetLatestStateProof
// and then return the finalproof
lastIndex := len(queue.History) - 1
postState := queue.Header.Root
preState, keys := Rollingback(queue.Header, &queue.History[lastIndex])

if len(keys) == 0 {
log.Println("[RollingbackProof]: len(keys) == 0")
return ""
}

proofOfKeys, _, _, _, err := verkle.MakeVerkleMultiProof(preState, postState, keys, NodeResolveFn)
if err != nil {
log.Printf("Failed to generate proof due to %v", err)
return ""
}

vProof, _, err := verkle.SerializeProof(proofOfKeys)
if err != nil {
log.Printf("Failed to serialize proof due to %v", err)
return ""
}

vProofBytes, err := vProof.MarshalJSON()
if err != nil {
log.Printf("Failed to marshal the proof to JSON due to %v", err)
return ""
}

finalproof := base64.StdEncoding.EncodeToString(vProofBytes[:])
return finalproof
}

0 comments on commit 4190bf7

Please sign in to comment.