Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
caed6a3d
Commit
caed6a3d
authored
Jun 30, 2023
by
Andreas Bigger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Preimage alphabet trace provider method
parent
a8e6061d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
18 deletions
+36
-18
alphabet_provider.go
op-challenger/fault/alphabet_provider.go
+18
-10
alphabet_provider_test.go
op-challenger/fault/alphabet_provider_test.go
+17
-8
types.go
op-challenger/fault/types.go
+1
-0
No files found.
op-challenger/fault/alphabet_provider.go
View file @
caed6a3d
...
...
@@ -18,27 +18,35 @@ type AlphabetProvider struct {
func
NewAlphabetProvider
(
state
string
,
depth
uint64
)
*
AlphabetProvider
{
return
&
AlphabetProvider
{
state
:
strings
.
Split
(
state
,
""
),
maxLen
:
(
1
<<
depth
),
maxLen
:
uint64
(
1
<<
depth
),
}
}
// Get
returns the claim value at the given index in the trace
.
func
(
ap
*
AlphabetProvider
)
Get
(
i
uint64
)
(
common
.
Hash
,
error
)
{
// Get
Preimage returns the preimage for the given hash
.
func
(
ap
*
AlphabetProvider
)
Get
Preimage
(
i
uint64
)
([]
byte
,
error
)
{
// The index cannot be larger than the maximum index as computed by the depth.
if
i
>=
ap
.
maxLen
{
return
common
.
Hash
{},
ErrIndexTooLarge
return
[]
byte
{},
ErrIndexTooLarge
}
// We extend the deepest hash to the maximum depth if the trace is not expansive.
if
i
>=
uint64
(
len
(
ap
.
state
))
{
return
ap
.
Get
(
uint64
(
len
(
ap
.
state
))
-
1
)
return
ap
.
GetPreimage
(
uint64
(
len
(
ap
.
state
))
-
1
)
}
return
buildAlphabetClaimBytes
(
i
,
ap
.
state
[
i
]),
nil
}
// Get returns the claim value at the given index in the trace.
func
(
ap
*
AlphabetProvider
)
Get
(
i
uint64
)
(
common
.
Hash
,
error
)
{
claimBytes
,
err
:=
ap
.
GetPreimage
(
i
)
if
err
!=
nil
{
return
common
.
Hash
{},
err
}
return
ap
.
ComputeAlphabetClaim
(
i
),
nil
return
common
.
BytesToHash
(
claimBytes
),
nil
}
// ComputeAlphabetClaim computes the claim for the given index in the trace.
func
(
ap
*
AlphabetProvider
)
ComputeAlphabetClaim
(
i
uint64
)
common
.
Hash
{
concatenated
:=
append
(
IndexToBytes
(
i
),
[]
byte
(
ap
.
state
[
i
])
...
)
return
common
.
BytesToHash
(
concatenated
)
// buildAlphabetClaimBytes constructs the claim bytes for the index and state item.
func
buildAlphabetClaimBytes
(
i
uint64
,
letter
string
)
[]
byte
{
return
append
(
IndexToBytes
(
i
),
[]
byte
(
letter
)
...
)
}
// IndexToBytes converts an index to a byte slice big endian
...
...
op-challenger/fault/alphabet_provider_test.go
View file @
caed6a3d
...
...
@@ -50,17 +50,26 @@ func FuzzIndexToBytes(f *testing.F) {
})
}
// TestComputeAlphabetClaim tests the ComputeAlphabetClaim function.
func
TestComputeAlphabetClaim
(
t
*
testing
.
T
)
{
// TestGetPreimage_Succeeds tests the GetPreimage function
// returns the correct pre-image for a index.
func
TestGetPreimage_Succeeds
(
t
*
testing
.
T
)
{
ap
:=
NewAlphabetProvider
(
"abc"
,
2
)
claim
:=
ap
.
ComputeAlphabetClaim
(
0
)
concatenated
:=
append
(
IndexToBytes
(
0
),
[]
byte
(
"a"
)
...
)
expected
:=
common
.
BytesToHash
(
concatenated
)
require
.
Equal
(
t
,
expected
,
claim
)
expected
:=
append
(
IndexToBytes
(
uint64
(
0
)),
[]
byte
(
"a"
)
...
)
retrieved
,
err
:=
ap
.
GetPreimage
(
uint64
(
0
))
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
expected
,
retrieved
)
}
// TestGetPreimage_TooLargeIndex_Fails tests the GetPreimage
// function errors if the index is too large.
func
TestGetPreimage_TooLargeIndex_Fails
(
t
*
testing
.
T
)
{
ap
:=
NewAlphabetProvider
(
"abc"
,
2
)
_
,
err
:=
ap
.
GetPreimage
(
4
)
require
.
ErrorIs
(
t
,
err
,
ErrIndexTooLarge
)
}
// TestGet tests the Get function.
func
TestGet
(
t
*
testing
.
T
)
{
// TestGet
_Succeeds
tests the Get function.
func
TestGet
_Succeeds
(
t
*
testing
.
T
)
{
ap
:=
NewAlphabetProvider
(
"abc"
,
2
)
claim
,
err
:=
ap
.
Get
(
0
)
require
.
NoError
(
t
,
err
)
...
...
op-challenger/fault/types.go
View file @
caed6a3d
...
...
@@ -17,6 +17,7 @@ var (
// The [AlphabetProvider] is a minimal implementation of this interface.
type
TraceProvider
interface
{
Get
(
i
uint64
)
(
common
.
Hash
,
error
)
GetPreimage
(
i
uint64
)
([]
byte
,
error
)
}
// ClaimData is the core of a claim. It must be unique inside a specific game.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment