Skip to content

Commit

Permalink
feat(accinputhash): part 2, calc missing & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
revitteth committed Nov 28, 2024
1 parent 6ed3622 commit cc0820b
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 180 deletions.
102 changes: 23 additions & 79 deletions turbo/jsonrpc/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ledgerwatch/erigon/smt/pkg/smt"
smtUtils "github.com/ledgerwatch/erigon/smt/pkg/utils"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
"github.com/ledgerwatch/erigon/zk/acc_input_hash"
"github.com/ledgerwatch/erigon/zk/datastream/server"
"github.com/ledgerwatch/erigon/zk/hermez_db"
"github.com/ledgerwatch/erigon/zk/legacy_executor_verifier"
Expand All @@ -46,7 +47,6 @@ import (
"github.com/ledgerwatch/erigon/zk/witness"
"github.com/ledgerwatch/erigon/zkevm/hex"
"github.com/ledgerwatch/erigon/zkevm/jsonrpc/client"
"github.com/ledgerwatch/erigon/zk/acc_input_hash"
)

var sha3UncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
Expand Down Expand Up @@ -389,7 +389,18 @@ func (api *ZkEvmAPIImpl) getOrCalcBatchData(ctx context.Context, tx kv.Tx, dbRea
return nil, err
}

return utils.GenerateBatchDataFromDb(tx, dbReader, batchBlocks, forkId)
lastBlockNoInPreviousBatch := uint64(0)
firstBlockInBatch := batchBlocks[0]
if firstBlockInBatch.NumberU64() != 0 {
lastBlockNoInPreviousBatch = firstBlockInBatch.NumberU64() - 1
}

lastBlockInPreviousBatch, err := rawdb.ReadBlockByNumber(tx, lastBlockNoInPreviousBatch)
if err != nil {
return nil, err
}

return utils.GenerateBatchDataFromDb(tx, dbReader, batchBlocks, lastBlockInPreviousBatch, forkId)
}

type blockGetter interface {
Expand Down Expand Up @@ -626,7 +637,11 @@ func (api *ZkEvmAPIImpl) GetBatchByNumber(ctx context.Context, rpcBatchNumber rp
batch.BatchL2Data = batchL2Data

if api.l1Syncer != nil {
accInputHash, err := acc_input_hash.CalculateAccInputHash(ctx, forkId, batchNo, hermezDb)
calc, err := acc_input_hash.NewCalculator(ctx, tx, hermezDb, forkId)
if err != nil {
return nil, err
}
accInputHash, err := calc.Calculate(batchNo)
if err != nil {
log.Error(fmt.Sprintf("failed to get acc input hash for batch %d: %v", batchNo, err))
}
Expand Down Expand Up @@ -717,81 +732,6 @@ func (api *ZkEvmAPIImpl) fullTxBlockData(ctx context.Context, tx kv.Tx, hermezDb
return batchBlocksJson, batchTransactionsJson, nil
}

//type SequenceReader interface {
// GetRangeSequencesByBatch(batchNo uint64) (*zktypes.L1BatchInfo, *zktypes.L1BatchInfo, error)
// GetForkId(batchNo uint64) (uint64, error)
//}

//func (api *ZkEvmAPIImpl) getAccInputHash(ctx context.Context, db SequenceReader, batchNum uint64) (accInputHash *common.Hash, err error) {
// // get batch sequence
// prevSequence, batchSequence, err := db.GetRangeSequencesByBatch(batchNum)
// if err != nil {
// return nil, fmt.Errorf("failed to get sequence range data for batch %d: %w", batchNum, err)
// }
//
// // if we are asking for genesis return 0x0..0
// if batchNum == 0 && prevSequence.BatchNo == 0 {
// return &common.Hash{}, nil
// }
//
// if prevSequence == nil || batchSequence == nil {
// var missing string
// if prevSequence == nil && batchSequence == nil {
// missing = "previous and current batch sequences"
// } else if prevSequence == nil {
// missing = "previous batch sequence"
// } else {
// missing = "current batch sequence"
// }
// return nil, fmt.Errorf("failed to get %s for batch %d", missing, batchNum)
// }
//
// // get batch range for sequence
// prevSequenceBatch, currentSequenceBatch := prevSequence.BatchNo, batchSequence.BatchNo
// // get call data for tx
// l1Transaction, _, err := api.l1Syncer.GetTransaction(batchSequence.L1TxHash)
// if err != nil {
// return nil, fmt.Errorf("failed to get transaction data for tx %s: %w", batchSequence.L1TxHash, err)
// }
// sequenceBatchesCalldata := l1Transaction.GetData()
// if len(sequenceBatchesCalldata) < 10 {
// return nil, fmt.Errorf("calldata for tx %s is too short", batchSequence.L1TxHash)
// }
//
// currentBatchForkId, err := db.GetForkId(currentSequenceBatch)
// if err != nil {
// return nil, fmt.Errorf("failed to get fork id for batch %d: %w", currentSequenceBatch, err)
// }
//
// prevSequenceAccinputHash, err := api.GetccInputHash(ctx, currentBatchForkId, prevSequenceBatch)
// if err != nil {
// return nil, fmt.Errorf("failed to get old acc input hash for batch %d: %w", prevSequenceBatch, err)
// }
//
// decodedSequenceInterface, err := syncer.DecodeSequenceBatchesCalldata(sequenceBatchesCalldata)
// if err != nil {
// return nil, fmt.Errorf("failed to decode calldata for tx %s: %w", batchSequence.L1TxHash, err)
// }
//
// accInputHashCalcFn, totalSequenceBatches, err := syncer.GetAccInputDataCalcFunction(batchSequence.L1InfoRoot, decodedSequenceInterface)
// if err != nil {
// return nil, fmt.Errorf("failed to get accInputHash calculation func: %w", err)
// }
//
// if totalSequenceBatches == 0 || batchNum-prevSequenceBatch > uint64(totalSequenceBatches) {
// return nil, fmt.Errorf("batch %d is out of range of sequence calldata", batchNum)
// }
//
// // calculate acc input hash
// accInputHash = &prevSequenceAccinputHash
// for i := 0; i < int(batchNum-prevSequenceBatch); i++ {
// accInputHash = accInputHashCalcFn(prevSequenceAccinputHash, i)
// prevSequenceAccinputHash = *accInputHash
// }
//
// return
//}

// GetFullBlockByNumber returns a full block from the current canonical chain. If number is nil, the
// latest known block is returned.
func (api *ZkEvmAPIImpl) GetFullBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (types.Block, error) {
Expand Down Expand Up @@ -1148,7 +1088,11 @@ func (api *ZkEvmAPIImpl) GetProverInput(ctx context.Context, batchNumber uint64,
if err != nil {
return nil, err
}
oaih, err := acc_input_hash.CalculateAccInputHash(ctx, forkId, batchNumber-1, hDb)
calc, err := acc_input_hash.NewCalculator(ctx, tx, hDb, forkId)
if err != nil {
return nil, err
}
oaih, err := calc.Calculate(batchNumber - 1)
if err != nil {
return nil, err
}
Expand Down
13 changes: 9 additions & 4 deletions turbo/jsonrpc/zkevm_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestGetBatchByNumber(t *testing.T) {
assert.NoError(err)
err = hDB.WriteBlockGlobalExitRoot(uint64(i+1), gers[i])
assert.NoError(err)
l1InforTree := &zktypes.L1InfoTreeUpdate{
l1InfoTree := &zktypes.L1InfoTreeUpdate{
Index: uint64(i),
GER: gers[i],
MainnetExitRoot: mainnetExitRoots[i],
Expand All @@ -413,7 +413,11 @@ func TestGetBatchByNumber(t *testing.T) {
Timestamp: times[i],
BlockNumber: uint64(i + 1),
}
err = hDB.WriteL1InfoTreeUpdateToGer(l1InforTree)
err = hDB.WriteL1InfoTreeUpdateToGer(l1InfoTree)
assert.NoError(err)
err = hDB.WriteL1InfoTreeUpdate(l1InfoTree)
assert.NoError(err)
err = hDB.WriteL1InfoTreeRoot(hashes[i], uint64(i))
assert.NoError(err)
}

Expand All @@ -435,6 +439,8 @@ func TestGetBatchByNumber(t *testing.T) {
assert.NoError(err)
}

accInputHash := common.HexToHash("0xd24388f39169a9187e66711e42c8da0dfd9f9089ec46c9a95d29a1f25a58234a")

for i := 0; i < 4; i++ {
_, err := erigonDB.WriteHeader(big.NewInt(int64(i+1)), hashes[i], stateRoots[i], txsRoot[i], parentHashes[i], coinBase, times[i], gasLimits[i], params.TestChainConfig)
assert.NoError(err)
Expand All @@ -443,7 +449,6 @@ func TestGetBatchByNumber(t *testing.T) {
}
tx.Commit()
var response []byte
accInputHash := common.HexToHash("0xd24388f39169a9187e66711e42c8da0dfd9f9089ec46c9a95d29a1f25a58234a")
response = append(response, accInputHash.Bytes()...)
response = append(response, common.Hash{}.Bytes()...)
response = append(response, common.FromHex(fmt.Sprintf("0x%064x", 1))...)
Expand Down Expand Up @@ -480,7 +485,7 @@ func TestGetBatchByNumber(t *testing.T) {
assert.Equal(gers[len(gers)-1], batch.GlobalExitRoot)
assert.Equal(mainnetExitRoots[len(mainnetExitRoots)-1], batch.MainnetExitRoot)
assert.Equal(rollupExitRoots[len(rollupExitRoots)-1], batch.RollupExitRoot)
assert.Equal(common.HexToHash("0x97d1524156ccb46723e5c3c87951da9a390499ba288161d879df1dbc03d49afc"), batch.AccInputHash)
assert.Equal(common.HexToHash("0xebf7acfdbfb4ea6ef8775e7d2246d7d4f3d8c280fbe7649e1b90eb7490ea19e6"), batch.AccInputHash)
assert.Equal(common.HexToHash("0x22ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba97"), *batch.SendSequencesTxHash)
assert.Equal(rpctypes.ArgUint64(1714427009), batch.Timestamp)
assert.Equal(true, batch.Closed)
Expand Down
Loading

0 comments on commit cc0820b

Please sign in to comment.