From b977e5a62f27071b5605436ce680e817e428dae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Garamv=C3=B6lgyi?= Date: Sun, 6 Aug 2023 07:41:29 +0200 Subject: [PATCH] fix: handle importGenesisBatch in bridge-history calldata decoder (#731) --- bridge-history-api/utils/utils.go | 54 ++++---------------------- bridge-history-api/utils/utils_test.go | 7 ++++ common/version/version.go | 2 +- 3 files changed, 16 insertions(+), 47 deletions(-) diff --git a/bridge-history-api/utils/utils.go b/bridge-history-api/utils/utils.go index 0039e8be2..1e4674ff0 100644 --- a/bridge-history-api/utils/utils.go +++ b/bridge-history-api/utils/utils.go @@ -1,7 +1,6 @@ package utils import ( - "bytes" "context" "encoding/binary" "errors" @@ -76,6 +75,14 @@ func GetBatchRangeFromCalldataV2(calldata []byte) (uint64, uint64, uint64, error method := backendabi.ScrollChainV2ABI.Methods["commitBatch"] values, err := method.Inputs.Unpack(calldata[4:]) if err != nil { + // special case: import genesis batch + method = backendabi.ScrollChainV2ABI.Methods["importGenesisBatch"] + _, err2 := method.Inputs.Unpack(calldata[4:]) + if err2 == nil { + // genesis batch + return 0, 0, 0, nil + } + // none of "commitBatch" and "importGenesisBatch" match, give up return 0, 0, 0, err } args := commitBatchArgs{} @@ -110,48 +117,3 @@ func GetBatchRangeFromCalldataV2(calldata []byte) (uint64, uint64, uint64, error return batchIndex, startBlock, finishBlock, err } - -// GetBatchRangeFromCalldataV1 find the block range from calldata, both inclusive. -func GetBatchRangeFromCalldataV1(calldata []byte) ([]uint64, []uint64, []uint64, error) { - var batchIndices []uint64 - var startBlocks []uint64 - var finishBlocks []uint64 - if bytes.Equal(calldata[0:4], common.Hex2Bytes("cb905499")) { - // commitBatches - method := backendabi.ScrollChainABI.Methods["commitBatches"] - values, err := method.Inputs.Unpack(calldata[4:]) - if err != nil { - return batchIndices, startBlocks, finishBlocks, err - } - args := make([]backendabi.IScrollChainBatch, len(values)) - err = method.Inputs.Copy(&args, values) - if err != nil { - return batchIndices, startBlocks, finishBlocks, err - } - - for i := 0; i < len(args); i++ { - batchIndices = append(batchIndices, args[i].BatchIndex) - startBlocks = append(startBlocks, args[i].Blocks[0].BlockNumber) - finishBlocks = append(finishBlocks, args[i].Blocks[len(args[i].Blocks)-1].BlockNumber) - } - } else if bytes.Equal(calldata[0:4], common.Hex2Bytes("8c73235d")) { - // commitBatch - method := backendabi.ScrollChainABI.Methods["commitBatch"] - values, err := method.Inputs.Unpack(calldata[4:]) - if err != nil { - return batchIndices, startBlocks, finishBlocks, err - } - - args := backendabi.IScrollChainBatch{} - err = method.Inputs.Copy(&args, values) - if err != nil { - return batchIndices, startBlocks, finishBlocks, err - } - batchIndices = append(batchIndices, args.BatchIndex) - startBlocks = append(startBlocks, args.Blocks[0].BlockNumber) - finishBlocks = append(finishBlocks, args.Blocks[len(args.Blocks)-1].BlockNumber) - } else { - return batchIndices, startBlocks, finishBlocks, errors.New("invalid selector") - } - return batchIndices, startBlocks, finishBlocks, nil -} diff --git a/bridge-history-api/utils/utils_test.go b/bridge-history-api/utils/utils_test.go index cd616ba82..d457a8786 100644 --- a/bridge-history-api/utils/utils_test.go +++ b/bridge-history-api/utils/utils_test.go @@ -33,4 +33,11 @@ func TestGetBatchRangeFromCalldataV2(t *testing.T) { assert.Equal(t, start, uint64(10)) assert.Equal(t, finish, uint64(20)) assert.Equal(t, batchIndex, uint64(2)) + + // genesis batch + batchIndex, start, finish, err = utils.GetBatchRangeFromCalldataV2(common.Hex2Bytes("3fdeecb200000000000000000000000000000000000000000000000000000000000000402dcb5308098d24a37fc1487a229fcedb09fa4343ede39cbad365bc925535bb09000000000000000000000000000000000000000000000000000000000000005900000000000000000000000000000000000000000000000000c252bc9780c4d83cf11f14b8cd03c92c4d18ce07710ba836d31d12da216c8330000000000000000000000000000000000000000000000000000000000000000000000000000000")) + assert.NoError(t, err) + assert.Equal(t, start, uint64(0)) + assert.Equal(t, finish, uint64(0)) + assert.Equal(t, batchIndex, uint64(0)) } diff --git a/common/version/version.go b/common/version/version.go index b795970ca..c539c1d4b 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.1.8" +var tag = "v4.1.9" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok {