Skip to content

Commit

Permalink
Feat: assertion hash embeds common.Hash (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain authored Jul 24, 2023
1 parent 9201c47 commit fa67faf
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 40 deletions.
2 changes: 1 addition & 1 deletion assertions/poster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_findLatestValidAssertion(t *testing.T) {
}

func mockId(x uint64) protocol.AssertionHash {
return protocol.AssertionHash(common.BytesToHash([]byte(fmt.Sprintf("%d", x))))
return protocol.AssertionHash{Hash: common.BytesToHash([]byte(fmt.Sprintf("%d", x)))}
}

func setupAssertions(
Expand Down
4 changes: 2 additions & 2 deletions assertions/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (s *Scanner) checkForAssertionAdded(
)
}
_, processErr := retry.UntilSucceeds(ctx, func() (bool, error) {
return true, s.ProcessAssertionCreation(ctx, it.Event.AssertionHash)
return true, s.ProcessAssertionCreation(ctx, protocol.AssertionHash{Hash: it.Event.AssertionHash})
})
if processErr != nil {
return processErr
Expand All @@ -175,7 +175,7 @@ func (s *Scanner) ProcessAssertionCreation(
if err != nil {
return err
}
prevAssertion, err := s.chain.GetAssertion(ctx, protocol.AssertionHash(creationInfo.ParentAssertionHash))
prevAssertion, err := s.chain.GetAssertion(ctx, protocol.AssertionHash{Hash: creationInfo.ParentAssertionHash})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions assertions/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestScanner_ProcessAssertionCreation(t *testing.T) {
p := &mocks.MockProtocol{}
p.On("SpecChallengeManager", ctx).Return(&mocks.MockSpecChallengeManager{}, nil)
p.On("ReadAssertionCreationInfo", ctx, mockId(2)).Return(&protocol.AssertionCreatedInfo{
ParentAssertionHash: common.Hash(mockId(1)),
ParentAssertionHash: mockId(1).Hash,
AfterState: rollupgen.ExecutionState{},
}, nil)
p.On("GetAssertion", ctx, mockId(2)).Return(ev, nil)
Expand Down Expand Up @@ -157,5 +157,5 @@ func setupChallengeManager(t *testing.T) (*challengemanager.Manager, *mocks.Mock
}

func mockId(x uint64) protocol.AssertionHash {
return protocol.AssertionHash(common.BytesToHash([]byte(fmt.Sprintf("%d", x))))
return protocol.AssertionHash{Hash: common.BytesToHash([]byte(fmt.Sprintf("%d", x)))}
}
4 changes: 3 additions & 1 deletion chain-abstraction/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (

// AssertionHash represents a unique identifier for an assertion
// constructed as a keccak256 hash of some of its internals.
type AssertionHash common.Hash
type AssertionHash struct {
common.Hash
}

// Protocol --
type Protocol interface {
Expand Down
35 changes: 22 additions & 13 deletions chain-abstraction/sol-implementation/assertion_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ func NewAssertionChain(
}

func (a *AssertionChain) GetAssertion(ctx context.Context, assertionHash protocol.AssertionHash) (protocol.Assertion, error) {
res, err := a.userLogic.GetAssertion(&bind.CallOpts{Context: ctx}, assertionHash)
var b [32]byte
copy(b[:], assertionHash.Bytes())
res, err := a.userLogic.GetAssertion(&bind.CallOpts{Context: ctx}, b)
if err != nil {
return nil, err
}
Expand All @@ -124,7 +126,7 @@ func (a *AssertionChain) LatestConfirmed(ctx context.Context) (protocol.Assertio
if err != nil {
return nil, err
}
return a.GetAssertion(ctx, res)
return a.GetAssertion(ctx, protocol.AssertionHash{Hash: res})
}

// CreateAssertion makes an on-chain claim given a previous assertion hash, execution state,
Expand Down Expand Up @@ -166,7 +168,7 @@ func (a *AssertionChain) CreateAssertion(
if err != nil {
return nil, errors.Wrap(err, "could not compute assertion hash")
}
existingAssertion, err := a.GetAssertion(ctx, computedHash)
existingAssertion, err := a.GetAssertion(ctx, protocol.AssertionHash{Hash: computedHash})
switch {
case err == nil:
return existingAssertion, nil
Expand Down Expand Up @@ -216,7 +218,7 @@ func (a *AssertionChain) CreateAssertion(
if !found {
return nil, errors.New("could not find assertion created event in logs")
}
return a.GetAssertion(ctx, assertionCreated.AssertionHash)
return a.GetAssertion(ctx, protocol.AssertionHash{Hash: assertionCreated.AssertionHash})
}

func (a *AssertionChain) GenesisAssertionHash(ctx context.Context) (common.Hash, error) {
Expand All @@ -230,7 +232,9 @@ func (a *AssertionChain) ConfirmAssertionByChallengeWinner(
assertionHash protocol.AssertionHash,
winningEdgeId protocol.EdgeId,
) error {
node, err := a.userLogic.GetAssertion(&bind.CallOpts{Context: ctx}, assertionHash)
var b [32]byte
copy(b[:], assertionHash.Bytes())
node, err := a.userLogic.GetAssertion(&bind.CallOpts{Context: ctx}, b)
if err != nil {
return err
}
Expand All @@ -245,15 +249,15 @@ func (a *AssertionChain) ConfirmAssertionByChallengeWinner(
if creationInfo.ParentAssertionHash == [32]byte{} {
return nil
}
prevCreationInfo, err := a.ReadAssertionCreationInfo(ctx, protocol.AssertionHash(creationInfo.ParentAssertionHash))
prevCreationInfo, err := a.ReadAssertionCreationInfo(ctx, protocol.AssertionHash{Hash: creationInfo.ParentAssertionHash})
if err != nil {
return err
}
latestConfirmed, err := a.LatestConfirmed(ctx)
if err != nil {
return err
}
if creationInfo.ParentAssertionHash != common.Hash(latestConfirmed.Id()) {
if creationInfo.ParentAssertionHash != latestConfirmed.Id().Hash {
return fmt.Errorf(
"parent id %#x is not the latest confirmed assertion %#x",
creationInfo.ParentAssertionHash,
Expand All @@ -266,7 +270,7 @@ func (a *AssertionChain) ConfirmAssertionByChallengeWinner(
receipt, err := transact(ctx, a.backend, func() (*types.Transaction, error) {
return a.userLogic.RollupUserLogicTransactor.ConfirmAssertion(
copyTxOpts(a.txOpts),
assertionHash,
b,
creationInfo.ParentAssertionHash,
creationInfo.AfterState,
winningEdgeId,
Expand Down Expand Up @@ -310,7 +314,9 @@ func (a *AssertionChain) SpecChallengeManager(ctx context.Context) (protocol.Spe
// assertion's parent, and from that parent, computes second_child_creation_block - first_child_creation_block.
// If an assertion is a second child, this function will return 0.
func (a *AssertionChain) AssertionUnrivaledBlocks(ctx context.Context, assertionHash protocol.AssertionHash) (uint64, error) {
wantNode, err := a.rollup.GetAssertion(&bind.CallOpts{Context: ctx}, assertionHash)
var b [32]byte
copy(b[:], assertionHash.Bytes())
wantNode, err := a.rollup.GetAssertion(&bind.CallOpts{Context: ctx}, b)
if err != nil {
return 0, err
}
Expand All @@ -333,7 +339,8 @@ func (a *AssertionChain) AssertionUnrivaledBlocks(ctx context.Context, assertion
if err != nil {
return 0, err
}
prevNode, err := a.rollup.GetAssertion(&bind.CallOpts{Context: ctx}, prevId)
copy(b[:], prevId.Bytes())
prevNode, err := a.rollup.GetAssertion(&bind.CallOpts{Context: ctx}, b)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -436,7 +443,7 @@ func (a *AssertionChain) LatestCreatedAssertion(ctx context.Context) (protocol.A
if err != nil {
return nil, err
}
return a.GetAssertion(ctx, creationEvent.AssertionHash)
return a.GetAssertion(ctx, protocol.AssertionHash{Hash: creationEvent.AssertionHash})
}

// ReadAssertionCreationInfo for an assertion sequence number by looking up its creation
Expand All @@ -457,12 +464,14 @@ func (a *AssertionChain) ReadAssertionCreationInfo(
creationBlock = rollupDeploymentBlock.Uint64()
topics = [][]common.Hash{{assertionCreatedId}}
} else {
node, err := a.rollup.GetAssertion(&bind.CallOpts{Context: ctx}, id)
var b [32]byte
copy(b[:], id.Bytes())
node, err := a.rollup.GetAssertion(&bind.CallOpts{Context: ctx}, b)
if err != nil {
return nil, err
}
creationBlock = node.CreatedAtBlock
topics = [][]common.Hash{{assertionCreatedId}, {common.Hash(id)}}
topics = [][]common.Hash{{assertionCreatedId}, {id.Hash}}
}
var query = ethereum.FilterQuery{
FromBlock: new(big.Int).SetUint64(creationBlock),
Expand Down
6 changes: 3 additions & 3 deletions chain-abstraction/sol-implementation/assertion_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestCreateAssertion(t *testing.T) {

genesisHash, err := chain.GenesisAssertionHash(ctx)
require.NoError(t, err)
genesisInfo, err := chain.ReadAssertionCreationInfo(ctx, protocol.AssertionHash(genesisHash))
genesisInfo, err := chain.ReadAssertionCreationInfo(ctx, protocol.AssertionHash{Hash: genesisHash})
require.NoError(t, err)

t.Run("OK", func(t *testing.T) {
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestAssertionUnrivaledBlocks(t *testing.T) {
}
genesisHash, err := chain.GenesisAssertionHash(ctx)
require.NoError(t, err)
genesisInfo, err := chain.ReadAssertionCreationInfo(ctx, protocol.AssertionHash(genesisHash))
genesisInfo, err := chain.ReadAssertionCreationInfo(ctx, protocol.AssertionHash{Hash: genesisHash})
require.NoError(t, err)

postState := &protocol.ExecutionState{
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestAssertionBySequenceNum(t *testing.T) {
_, err = chain.GetAssertion(ctx, latestConfirmed.Id())
require.NoError(t, err)

_, err = chain.GetAssertion(ctx, protocol.AssertionHash(common.BytesToHash([]byte("foo"))))
_, err = chain.GetAssertion(ctx, protocol.AssertionHash{Hash: common.BytesToHash([]byte("foo"))})
require.ErrorIs(t, err, solimpl.ErrNotFound)
}

Expand Down
14 changes: 11 additions & 3 deletions chain-abstraction/sol-implementation/edge_challenge_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func (e *SpecEdge) EndCommitment() (protocol.Height, common.Hash) {
}

func (e *SpecEdge) AssertionHash(ctx context.Context) (protocol.AssertionHash, error) {
return e.manager.caller.GetPrevAssertionHash(&bind.CallOpts{Context: ctx}, e.id)
h, err := e.manager.caller.GetPrevAssertionHash(&bind.CallOpts{Context: ctx}, e.id)
if err != nil {
return protocol.AssertionHash{}, err
}
return protocol.AssertionHash{Hash: common.Hash(h)}, nil
}

func (e *SpecEdge) TimeUnrivaled(ctx context.Context) (uint64, error) {
Expand Down Expand Up @@ -235,9 +239,13 @@ func (e *SpecEdge) ConfirmByTimer(ctx context.Context, ancestorIds []protocol.Ed
if topEdge.GetType() != protocol.BlockChallengeEdge {
return errors.New("top level ancestor must be a block challenge edge")
}
assertionHash = protocol.AssertionHash(topEdge.ClaimId().Unwrap())
assertionHash = protocol.AssertionHash{
Hash: common.Hash(topEdge.ClaimId().Unwrap()),
}
} else {
assertionHash = e.inner.ClaimId
assertionHash = protocol.AssertionHash{
Hash: e.inner.ClaimId,
}
}
assertionCreation, err := e.manager.assertionChain.ReadAssertionCreationInfo(ctx, assertionHash)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions chain-abstraction/sol-implementation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (a *Assertion) PrevId(ctx context.Context) (protocol.AssertionHash, error)
if err != nil {
return protocol.AssertionHash{}, err
}
return creationEvent.ParentAssertionHash, nil
return protocol.AssertionHash{Hash: creationEvent.ParentAssertionHash}, nil
}

func (a *Assertion) HasSecondChild() (bool, error) {
Expand All @@ -64,7 +64,9 @@ func (a *Assertion) HasSecondChild() (bool, error) {
}

func (a *Assertion) inner() (*rollupgen.AssertionNode, error) {
assertionNode, err := a.chain.userLogic.GetAssertion(&bind.CallOpts{}, a.id)
var b [32]byte
copy(b[:], a.id.Bytes())
assertionNode, err := a.chain.userLogic.GetAssertion(&bind.CallOpts{}, b)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions challenge-manager/chain-watcher/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//runtime",
"//solgen/go/challengeV2gen",
"@com_github_ethereum_go_ethereum//accounts/abi/bind",
"@com_github_ethereum_go_ethereum//common",
"@com_github_ethereum_go_ethereum//log",
"@com_github_ethereum_go_ethereum//metrics",
"@com_github_pkg_errors//:errors",
Expand Down
7 changes: 4 additions & 3 deletions challenge-manager/chain-watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
retry "github.com/OffchainLabs/challenge-protocol-v2/runtime"
"github.com/OffchainLabs/challenge-protocol-v2/solgen/go/challengeV2gen"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/pkg/errors"
Expand Down Expand Up @@ -370,7 +371,7 @@ func (w *Watcher) processEdgeAddedEvent(
chal, ok := w.challenges.TryGet(assertionHash)
if !ok {
tree := challengetree.New(
event.OriginId,
protocol.AssertionHash{Hash: event.OriginId},
w.chain,
w.histChecker,
w.validatorName,
Expand Down Expand Up @@ -574,11 +575,11 @@ func (w *Watcher) processEdgeConfirmation(

// Check if we should confirm the assertion by challenge winner.
if edge.GetType() == protocol.BlockChallengeEdge {
if confirmAssertionErr := w.chain.ConfirmAssertionByChallengeWinner(ctx, protocol.AssertionHash(claimId), edgeId); confirmAssertionErr != nil {
if confirmAssertionErr := w.chain.ConfirmAssertionByChallengeWinner(ctx, protocol.AssertionHash{Hash: common.Hash(claimId)}, edgeId); confirmAssertionErr != nil {
return confirmAssertionErr
}
srvlog.Info("Assertion confirmed by challenge win", log.Ctx{
"assertionHash": containers.Trunc(assertionHash[:]),
"assertionHash": containers.Trunc(assertionHash.Bytes()),
})
}

Expand Down
12 changes: 6 additions & 6 deletions challenge-manager/chain-watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func TestWatcher_processEdgeConfirmation(t *testing.T) {
ctx,
).Return(mockChallengeManager, nil)

assertionHash := protocol.AssertionHash(common.BytesToHash([]byte("foo")))
assertionHash := protocol.AssertionHash{Hash: common.BytesToHash([]byte("foo"))}
edgeId := protocol.EdgeId(common.BytesToHash([]byte("bar")))
edge := &mocks.MockSpecEdge{}

mockChallengeManager.On(
"GetEdge", ctx, edgeId,
).Return(option.Some(protocol.SpecEdge(edge)), nil)

edge.On("ClaimId").Return(option.Some(protocol.ClaimId(assertionHash)))
edge.On("ClaimId").Return(option.Some(protocol.ClaimId(assertionHash.Hash)))
edge.On("Id").Return(edgeId)
edge.On("GetType").Return(protocol.BigStepChallengeEdge)
edge.On(
Expand All @@ -57,7 +57,7 @@ func TestWatcher_processEdgeConfirmation(t *testing.T) {

chal, ok := watcher.challenges.TryGet(assertionHash)
require.Equal(t, true, ok)
ok = chal.confirmedLevelZeroEdgeClaimIds.Has(protocol.ClaimId(assertionHash))
ok = chal.confirmedLevelZeroEdgeClaimIds.Has(protocol.ClaimId(assertionHash.Hash))
require.Equal(t, true, ok)
}

Expand All @@ -70,7 +70,7 @@ func TestWatcher_processEdgeAddedEvent(t *testing.T) {
ctx,
).Return(mockChallengeManager, nil)

assertionHash := protocol.AssertionHash(common.BytesToHash([]byte("foo")))
assertionHash := protocol.AssertionHash{Hash: common.BytesToHash([]byte("foo"))}
edgeId := protocol.EdgeId(common.BytesToHash([]byte("bar")))
edge := &mocks.MockSpecEdge{}

Expand Down Expand Up @@ -108,7 +108,7 @@ func TestWatcher_processEdgeAddedEvent(t *testing.T) {

edge.On("Id").Return(edgeId)
edge.On("CreatedAtBlock").Return(uint64(0), nil)
edge.On("ClaimId").Return(option.Some(protocol.ClaimId(assertionHash)))
edge.On("ClaimId").Return(option.Some(protocol.ClaimId(assertionHash.Hash)))
edge.On("MutualId").Return(protocol.MutualId{})
edge.On("GetType").Return(protocol.BlockChallengeEdge)
startCommit := common.BytesToHash([]byte("nyan"))
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestWatcher_processEdgeAddedEvent(t *testing.T) {
}
err := watcher.processEdgeAddedEvent(ctx, &challengeV2gen.EdgeChallengeManagerEdgeAdded{
EdgeId: edgeId,
OriginId: assertionHash,
OriginId: assertionHash.Hash,
})
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions challenge-manager/challenge-tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAddEdge(t *testing.T) {
honestBigStepLevelZeroEdges: threadsafe.NewSlice[protocol.ReadOnlyEdge](),
honestSmallStepLevelZeroEdges: threadsafe.NewSlice[protocol.ReadOnlyEdge](),
}
ht.topLevelAssertionHash = protocol.AssertionHash(common.BytesToHash([]byte("foo")))
ht.topLevelAssertionHash = protocol.AssertionHash{Hash: common.BytesToHash([]byte("foo"))}
ctx := context.Background()
edge := newEdge(&newCfg{t: t, edgeId: "blk-0.a-16.a", createdAt: 1})

Expand All @@ -41,7 +41,7 @@ func TestAddEdge(t *testing.T) {
t.Run("ignores if disagrees with top level assertion hash of edge", func(t *testing.T) {
ht.metadataReader = &mockMetadataReader{
assertionErr: nil,
assertionHash: protocol.AssertionHash(common.BytesToHash([]byte("bar"))),
assertionHash: protocol.AssertionHash{Hash: common.BytesToHash([]byte("bar"))},
}
_, err := ht.AddEdge(ctx, edge)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion challenge-manager/challenges.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (m *Manager) ChallengeAssertion(ctx context.Context, id protocol.AssertionH

srvlog.Info("Successfully created level zero edge for block challenge", log.Ctx{
"name": m.name,
"assertionHash": containers.Trunc(id[:]),
"assertionHash": containers.Trunc(id.Bytes()),
})
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion testing/setup/rollup_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func CreateTwoValidatorFork(
if err != nil {
return nil, err
}
genesisCreationInfo, err := setup.Chains[1].ReadAssertionCreationInfo(ctx, protocol.AssertionHash(genesisHash))
genesisCreationInfo, err := setup.Chains[1].ReadAssertionCreationInfo(ctx, protocol.AssertionHash{Hash: genesisHash})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fa67faf

Please sign in to comment.