From cb37bff5439afbd8f0d5119bc20aabb4df406c19 Mon Sep 17 00:00:00 2001 From: TianyueLi1227 Date: Sat, 27 Apr 2024 11:17:39 +0800 Subject: [PATCH] fix: generate proof during root update --- apis/apis.go | 30 +++--------------------------- apis/verify.go | 4 ++++ newProof_test.go | 9 ++++++--- ord/stateless/queue.go | 4 +++- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/apis/apis.go b/apis/apis.go index 1309470..0710b86 100644 --- a/apis/apis.go +++ b/apis/apis.go @@ -138,40 +138,17 @@ func GetBlockHeight(c *gin.Context, queue *stateless.Queue) { } func GetLatestStateProof(c *gin.Context, queue *stateless.Queue) { - - lastIndex := len(queue.History) - 1 - postState := queue.Header.Root - // TODO: High. Use another root to store the preState after the flushing to disk has been done. - // TODO: Urgent. Rollingback here is unsafe because we don't lock the queue. - preState, keys := stateless.Rollingback(queue.Header, &queue.History[lastIndex]) - - if len(keys) == 0 { - res := Brc20VerifiableLatestStateProofResult{ - StateDiff: make([]string, 0), - OrdTransfers: make([]OrdTransferJSON, 0), - } + if queue.LastStateProof == nil { c.JSON(http.StatusOK, Brc20VerifiableLatestStateProofResponse{ Error: nil, - Result: &res, - Proof: nil, - }) - return - } - - proofOfKeys, _, _, _, err := verkle.MakeVerkleMultiProof(preState, postState, keys, stateless.NodeResolveFn) - if err != nil { - errStr := fmt.Sprintf("Failed to generate proof due to %v", err) - c.JSON(http.StatusInternalServerError, Brc20VerifiableLatestStateProofResponse{ - Error: &errStr, Result: nil, Proof: nil, }) return } - - vProof, stateDiff, err := verkle.SerializeProof(proofOfKeys) + vProof, stateDiff, err := verkle.SerializeProof(queue.LastStateProof) if err != nil { - errStr := fmt.Sprintf("Failed to serialize proof due to %v", err) + errStr := fmt.Sprintf("Failed to generate proof due to %v", err) c.JSON(http.StatusInternalServerError, Brc20VerifiableLatestStateProofResponse{ Error: &errStr, Result: nil, @@ -179,7 +156,6 @@ func GetLatestStateProof(c *gin.Context, queue *stateless.Queue) { }) return } - vProofBytes, err := vProof.MarshalJSON() if err != nil { errStr := fmt.Sprintf("Failed to marshal the proof to JSON due to %v", err) diff --git a/apis/verify.go b/apis/verify.go index 2bf6224..d83db89 100644 --- a/apis/verify.go +++ b/apis/verify.go @@ -196,6 +196,10 @@ func GeneratePostRoot(rootC *verkle.Point, blockHeight uint, resp *Brc20Verifiab Hash: "", } + if resp.Result == nil { + return preHeader.Root, nil + } + var ordTransfers []getter.OrdTransfer for _, tran := range resp.Result.OrdTransfers { contentBytes, _ := base64.StdEncoding.DecodeString(tran.Content) diff --git a/newProof_test.go b/newProof_test.go index 6791bd7..f6dc8bf 100644 --- a/newProof_test.go +++ b/newProof_test.go @@ -26,7 +26,7 @@ func Test_NewProof(t *testing.T) { if VerifyProof(queue) { log.Printf("Block: %d is verified!\n", ordGetterTest.LatestBlockHeight) } else { - log.Printf("Block: %d cannot pass verification!\n", ordGetterTest.LatestBlockHeight) + log.Fatalf("Block: %d cannot pass verification!\n", ordGetterTest.LatestBlockHeight) } ordGetterTest.LatestBlockHeight++ } @@ -52,8 +52,11 @@ func VerifyProof(queue *stateless.Queue) bool { return false } finalproof := base64.StdEncoding.EncodeToString(vProofBytes[:]) - log.Println("VerifyProof finalproof:", finalproof) - return finalproof == RollingbackProof(queue) + rollingbackProof := RollingbackProof(queue) + if rollingbackProof == "" { + return true + } + return finalproof == rollingbackProof } func RollingbackProof(queue *stateless.Queue) string { diff --git a/ord/stateless/queue.go b/ord/stateless/queue.go index b022ec0..4d33758 100644 --- a/ord/stateless/queue.go +++ b/ord/stateless/queue.go @@ -306,7 +306,9 @@ func generateProofFromUpdate(header *Header, stateDiff *DiffState) (*verkle.Proo sort.Strings(paths) cis := make([]*verkle.Point, len(pe.ByPath)-1) for i, path := range paths { - cis[i] = pe.ByPath[path] + src := pe.ByPath[path] + dst := *src + cis[i] = &dst } proof := &verkle.Proof{