-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plasma: Abstract CommitmentData to Interface (#10458)
* op-node: Generic Commitment This is a generic commitment to be used by the op-node & op-batcher. * abstract commitments to CommitmentData interface * correct byte-stripping ; add tests ; finish wiring * make GenericCommitment always verify * correct action tests * PR comments * fix unit test * remove fmt.Println --------- Co-authored-by: Joshua Gutow <jgutow@oplabs.co>
- Loading branch information
1 parent
0b4077e
commit 1a20fd9
Showing
11 changed files
with
213 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package plasma | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestCommitmentData tests the CommitmentData type and its implementations, | ||
// by encoding and decoding the commitment data and verifying the input data. | ||
func TestCommitmentData(t *testing.T) { | ||
|
||
type tcase struct { | ||
name string | ||
commType CommitmentType | ||
commData []byte | ||
expectedErr error | ||
} | ||
|
||
testCases := []tcase{ | ||
{ | ||
name: "valid keccak256 commitment", | ||
commType: Keccak256CommitmentType, | ||
commData: []byte("abcdefghijklmnopqrstuvwxyz012345"), | ||
expectedErr: ErrInvalidCommitment, | ||
}, | ||
{ | ||
name: "invalid keccak256 commitment", | ||
commType: Keccak256CommitmentType, | ||
commData: []byte("ab_baddata_yz012345"), | ||
expectedErr: ErrInvalidCommitment, | ||
}, | ||
{ | ||
name: "valid generic commitment", | ||
commType: GenericCommitmentType, | ||
commData: []byte("any length of data! wow, that's so generic!"), | ||
expectedErr: ErrInvalidCommitment, | ||
}, | ||
{ | ||
name: "invalid commitment type", | ||
commType: 9, | ||
commData: []byte("abcdefghijklmnopqrstuvwxyz012345"), | ||
expectedErr: ErrInvalidCommitment, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
comm, err := DecodeCommitmentData(tc.commData) | ||
require.ErrorIs(t, err, tc.expectedErr) | ||
if err == nil { | ||
// Test that the commitment type is correct | ||
require.Equal(t, tc.commType, comm.CommitmentType()) | ||
// Test that reencoding the commitment returns the same data | ||
require.Equal(t, tc.commData, comm.Encode()) | ||
// Test that TxData() returns the same data as the original, prepended with a version byte | ||
require.Equal(t, append([]byte{TxDataVersion1}, tc.commData...), comm.TxData()) | ||
|
||
// Test that Verify() returns no error for the correct data | ||
require.NoError(t, comm.Verify(tc.commData)) | ||
// Test that Verify() returns error for the incorrect data | ||
// don't do this for GenericCommitmentType, which does not do any verification | ||
if tc.commType != GenericCommitmentType { | ||
require.ErrorIs(t, ErrCommitmentMismatch, comm.Verify([]byte("wrong data"))) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.