Skip to content

Commit

Permalink
Return validation error if there are duplicates in the offchain data …
Browse files Browse the repository at this point in the history
…list
  • Loading branch information
begmaroman committed Aug 13, 2024
1 parent bd6eed4 commit 6bf0e40
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
13 changes: 13 additions & 0 deletions services/datacom/datacom.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func (d *Endpoints) signSequence(signedSequence types.SignedSequenceInterface) (
return "0x0", rpc.NewRPCError(rpc.DefaultErrorCode, "unauthorized")
}

// Make sure there are no duplicates in the given offchain data list
existingData := make(map[string]struct{})
offChainData := signedSequence.OffChainData()
for _, data := range offChainData {
key := data.Key.Hex()
if _, ok := existingData[key]; ok {
// The given key already exist in the offchain data list
return "0x0", rpc.NewRPCError(rpc.DefaultErrorCode, fmt.Sprintf("duplicate key: %s", key))
}

existingData[key] = struct{}{}
}

// Store off-chain data by hash (hash(L2Data): L2Data)
if err = d.db.StoreOffChainData(context.Background(), signedSequence.OffChainData()); err != nil {
return "0x0", rpc.NewRPCError(rpc.DefaultErrorCode,
Expand Down
51 changes: 42 additions & 9 deletions services/datacom/datacom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ func TestDataCom_SignSequence(t *testing.T) {
storeOffChainDataReturns []interface{}
sender *ecdsa.PrivateKey
signer *ecdsa.PrivateKey
sequence types.Sequence
expectedError string
}

sequence := types.Sequence{
types.ArgBytes([]byte{0, 1}),
types.ArgBytes([]byte{2, 3}),
}

privateKey, err := crypto.GenerateKey()
require.NoError(t, err)

Expand All @@ -51,7 +47,7 @@ func TestDataCom_SignSequence(t *testing.T) {
dbMock := mocks.NewDB(t)

if len(cfg.storeOffChainDataReturns) > 0 {
dbMock.On("StoreOffChainData", mock.Anything, sequence.OffChainData()).Return(
dbMock.On("StoreOffChainData", mock.Anything, cfg.sequence.OffChainData()).Return(
cfg.storeOffChainDataReturns...).Once()
}

Expand All @@ -68,15 +64,15 @@ func TestDataCom_SignSequence(t *testing.T) {
sqr.Start(context.Background())

if cfg.sender != nil {
signature, err := sequence.Sign(cfg.sender)
signature, err := cfg.sequence.Sign(cfg.sender)
require.NoError(t, err)
signedSequence = &types.SignedSequence{
Sequence: sequence,
Sequence: cfg.sequence,
Signature: signature,
}
} else {
signedSequence = &types.SignedSequence{
Sequence: sequence,
Sequence: cfg.sequence,
Signature: []byte{},
}
}
Expand Down Expand Up @@ -106,6 +102,10 @@ func TestDataCom_SignSequence(t *testing.T) {

testFn(t, testConfig{
expectedError: "failed to verify sender",
sequence: types.Sequence{
types.ArgBytes{0, 1},
types.ArgBytes{2, 3},
},
})
})

Expand All @@ -115,6 +115,10 @@ func TestDataCom_SignSequence(t *testing.T) {
testFn(t, testConfig{
sender: privateKey,
expectedError: "unauthorized",
sequence: types.Sequence{
types.ArgBytes{0, 1},
types.ArgBytes{2, 3},
},
})
})

Expand All @@ -124,6 +128,23 @@ func TestDataCom_SignSequence(t *testing.T) {
testFn(t, testConfig{
sender: privateKey,
expectedError: "unauthorized",
sequence: types.Sequence{
types.ArgBytes{0, 1},
types.ArgBytes{2, 3},
},
})
})

t.Run("Duplicates found", func(t *testing.T) {
t.Parallel()

testFn(t, testConfig{
sender: otherPrivateKey,
expectedError: "duplicate key: 0x49d03a195e239b52779866b33024210fc7dc66e9c2998975c0aa45c1702549d5",
sequence: types.Sequence{
types.ArgBytes{0, 1},
types.ArgBytes{0, 1},
},
})
})

Expand All @@ -134,6 +155,10 @@ func TestDataCom_SignSequence(t *testing.T) {
sender: otherPrivateKey,
expectedError: "failed to store offchain data",
storeOffChainDataReturns: []interface{}{errors.New("error")},
sequence: types.Sequence{
types.ArgBytes{0, 1},
types.ArgBytes{2, 3},
},
})
})

Expand All @@ -150,6 +175,10 @@ func TestDataCom_SignSequence(t *testing.T) {
signer: key,
storeOffChainDataReturns: []interface{}{nil},
expectedError: "failed to sign",
sequence: types.Sequence{
types.ArgBytes{0, 1},
types.ArgBytes{2, 3},
},
})
})

Expand All @@ -159,6 +188,10 @@ func TestDataCom_SignSequence(t *testing.T) {
testFn(t, testConfig{
sender: otherPrivateKey,
storeOffChainDataReturns: []interface{}{nil},
sequence: types.Sequence{
types.ArgBytes{0, 1},
types.ArgBytes{2, 3},
},
})
})
}

0 comments on commit 6bf0e40

Please sign in to comment.