Skip to content

Commit

Permalink
fix: generate proof during root update
Browse files Browse the repository at this point in the history
  • Loading branch information
TianyueLi1227 committed Apr 27, 2024
1 parent 5be4a23 commit cb37bff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 31 deletions.
30 changes: 3 additions & 27 deletions apis/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,48 +138,24 @@ 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 {

This comment has been minimized.

Copy link
@anqurvanillapy

anqurvanillapy Apr 27, 2024

This pointer looks not thread-safe, you could use an atomic.Pointer[T] to protect this pointer in the queue:

// From:
//LastStateProof *verkle.Proof
// To:
LastStateProof atomic.Pointer[verkle.Proof]
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,
Proof: nil,
})
return
}

vProofBytes, err := vProof.MarshalJSON()
if err != nil {
errStr := fmt.Sprintf("Failed to marshal the proof to JSON due to %v", err)
Expand Down
4 changes: 4 additions & 0 deletions apis/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions newProof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion ord/stateless/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

1 comment on commit cb37bff

@anqurvanillapy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By testing out thread safety, one could run the unit tests with the -race flag to enable the data race detector to see if there are any violations.

Please sign in to comment.