github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/internal/utesting/zktrie_gen_witness_test.go (about)

     1  package utesting
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/scroll-tech/go-ethereum/common"
    11  	"github.com/scroll-tech/go-ethereum/core/types"
    12  	"github.com/scroll-tech/go-ethereum/trie/zkproof"
    13  )
    14  
    15  func init() {
    16  	orderScheme := os.Getenv("OP_ORDER")
    17  	var orderSchemeI int
    18  	if orderScheme != "" {
    19  		if n, err := fmt.Sscanf(orderScheme, "%d", &orderSchemeI); err == nil && n == 1 {
    20  			zkproof.SetOrderScheme(zkproof.MPTWitnessType(orderSchemeI))
    21  		}
    22  	}
    23  }
    24  
    25  func loadStaff(t *testing.T, fname string) *types.BlockTrace {
    26  	f, err := os.Open(fname)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer f.Close()
    31  	bt, err := io.ReadAll(f)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	out := new(types.BlockTrace)
    37  
    38  	err = json.Unmarshal(bt, out)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	return out
    44  }
    45  
    46  func TestWriterCreation(t *testing.T) {
    47  	trace := loadStaff(t, "blocktraces/mpt_witness/deploy.json")
    48  	writer, err := zkproof.NewZkTrieProofWriter(trace.StorageTrace)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	if len(writer.TracingAccounts()) != 3 {
    54  		t.Error("unexpected tracing account data", writer.TracingAccounts())
    55  	}
    56  
    57  	if v, existed := writer.TracingAccounts()[common.HexToAddress("0x08c683b684d1e24cab8ce6de5c8c628d993ac140")]; !existed || v != nil {
    58  		t.Error("wrong tracing status for uninited address", v, existed)
    59  	}
    60  
    61  	if v, existed := writer.TracingAccounts()[common.HexToAddress("0x4cb1aB63aF5D8931Ce09673EbD8ae2ce16fD6571")]; !existed || v == nil {
    62  		t.Error("wrong tracing status for establied address", v, existed)
    63  	}
    64  
    65  }
    66  
    67  func TestGreeterTx(t *testing.T) {
    68  	trace := loadStaff(t, "blocktraces/mpt_witness/greeter.json")
    69  	writer, err := zkproof.NewZkTrieProofWriter(trace.StorageTrace)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	od := zkproof.NewSimpleOrderer()
    75  	theTx := trace.ExecutionResults[0]
    76  	zkproof.HandleTx(od, theTx)
    77  
    78  	t.Log(od)
    79  
    80  	for _, op := range od.SavedOp() {
    81  		_, err = writer.HandleNewState(op)
    82  		if err != nil {
    83  			t.Fatal(err)
    84  		}
    85  	}
    86  
    87  	traces, err := zkproof.HandleBlockTrace(trace)
    88  	t.Log("traces: ", len(traces))
    89  	outObj, _ := json.Marshal(traces)
    90  	t.Log(string(outObj))
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  }
    95  
    96  func TestTokenTx(t *testing.T) {
    97  	trace := loadStaff(t, "blocktraces/mpt_witness/token.json")
    98  	traces, err := zkproof.HandleBlockTrace(trace)
    99  	outObj, _ := json.Marshal(traces)
   100  	t.Log(string(outObj))
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  }
   106  
   107  func TestCallTx(t *testing.T) {
   108  	trace := loadStaff(t, "blocktraces/mpt_witness/call.json")
   109  	traces, err := zkproof.HandleBlockTrace(trace)
   110  	outObj, _ := json.Marshal(traces)
   111  	t.Log(string(outObj))
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	trace = loadStaff(t, "blocktraces/mpt_witness/call_edge.json")
   117  	traces, err = zkproof.HandleBlockTrace(trace)
   118  	outObj, _ = json.Marshal(traces)
   119  	t.Log(string(outObj))
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  }
   124  
   125  func TestCreateTx(t *testing.T) {
   126  	trace := loadStaff(t, "blocktraces/mpt_witness/create.json")
   127  	traces, err := zkproof.HandleBlockTrace(trace)
   128  	outObj, _ := json.Marshal(traces)
   129  	t.Log(string(outObj))
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  
   134  	trace = loadStaff(t, "blocktraces/mpt_witness/deploy.json")
   135  	traces, err = zkproof.HandleBlockTrace(trace)
   136  	outObj, _ = json.Marshal(traces)
   137  	t.Log(string(outObj))
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  
   142  }
   143  
   144  func TestFailedCallTx(t *testing.T) {
   145  	trace := loadStaff(t, "blocktraces/mpt_witness/fail_call.json")
   146  	traces, err := zkproof.HandleBlockTrace(trace)
   147  	outObj, _ := json.Marshal(traces)
   148  	t.Log(string(outObj))
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  	trace = loadStaff(t, "blocktraces/mpt_witness/fail_create.json")
   154  	traces, err = zkproof.HandleBlockTrace(trace)
   155  	outObj, _ = json.Marshal(traces)
   156  	t.Log(string(outObj))
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  
   161  }
   162  
   163  // notice: now only work with OP_ORDER=2
   164  func TestDeleteTx(t *testing.T) {
   165  	trace := loadStaff(t, "blocktraces/mpt_witness/delete.json")
   166  	traces, err := zkproof.HandleBlockTrace(trace)
   167  	outObj, _ := json.Marshal(traces)
   168  	t.Log(string(outObj))
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  }
   173  
   174  // notice: now only work with OP_ORDER=2
   175  func TestDestructTx(t *testing.T) {
   176  	trace := loadStaff(t, "blocktraces/mpt_witness/destruct.json")
   177  	traces, err := zkproof.HandleBlockTrace(trace)
   178  	outObj, _ := json.Marshal(traces)
   179  	t.Log(string(outObj))
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  }
   184  
   185  func TestMutipleTx(t *testing.T) {
   186  	trace := loadStaff(t, "blocktraces/mpt_witness/multi_txs.json")
   187  	traces, err := zkproof.HandleBlockTrace(trace)
   188  	outObj, _ := json.Marshal(traces)
   189  	t.Log(string(outObj))
   190  	if err != nil {
   191  		t.Fatal(err)
   192  	}
   193  }