-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
StefanIliev545
committed
Jun 24, 2024
1 parent
eb3a24c
commit c7c491d
Showing
10 changed files
with
284 additions
and
56 deletions.
There are no files selected for viewing
36 changes: 34 additions & 2 deletions
36
contracts/generated/ManagementContract/ManagementContract.go
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,197 @@ | ||
package l1 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
gethcommon "github.com/ethereum/go-ethereum/common" | ||
gethlog "github.com/ethereum/go-ethereum/log" | ||
"github.com/ten-protocol/go-ten/contracts/generated/ManagementContract" | ||
"github.com/ten-protocol/go-ten/go/common" | ||
"github.com/ten-protocol/go-ten/go/common/errutil" | ||
"github.com/ten-protocol/go-ten/go/common/host" | ||
"github.com/ten-protocol/go-ten/go/common/stopcontrol" | ||
"github.com/ten-protocol/go-ten/go/ethadapter" | ||
"github.com/ten-protocol/go-ten/go/ethadapter/mgmtcontractlib" | ||
) | ||
|
||
type ForkUniqueID = gethcommon.Hash | ||
type RollupNumber = uint64 | ||
|
||
type CrossChainStateMachine interface { | ||
GetRollupData(number RollupNumber) (RollupInfo, error) | ||
Synchronize() error | ||
PublishNextBundle() error | ||
host.Service | ||
} | ||
|
||
type crossChainStateMachine struct { | ||
latestRollup RollupInfo | ||
rollupHistory map[RollupNumber]RollupInfo | ||
currentRollup RollupNumber | ||
|
||
enclaveClient common.Enclave | ||
publisher host.L1Publisher | ||
ethClient ethadapter.EthClient | ||
mgmtContractLib mgmtcontractlib.MgmtContractLib // Library to handle Management Contract lib operations | ||
logger gethlog.Logger | ||
hostStopper *stopcontrol.StopControl | ||
} | ||
|
||
type RollupInfo struct { | ||
Hash gethcommon.Hash | ||
ForkUID ForkUniqueID | ||
Number RollupNumber | ||
} | ||
|
||
func NewCrossChainStateMachine( | ||
publisher host.L1Publisher, | ||
mgmtContractLib mgmtcontractlib.MgmtContractLib, | ||
ethClient ethadapter.EthClient, | ||
enclaveClient common.Enclave, | ||
logger gethlog.Logger, | ||
hostStopper *stopcontrol.StopControl, | ||
) CrossChainStateMachine { | ||
return &crossChainStateMachine{ | ||
latestRollup: RollupInfo{ | ||
Hash: gethcommon.Hash{}, | ||
ForkUID: gethcommon.Hash{}, | ||
Number: 0, | ||
}, | ||
rollupHistory: make(map[RollupNumber]RollupInfo), | ||
currentRollup: 0, | ||
publisher: publisher, | ||
ethClient: ethClient, | ||
mgmtContractLib: mgmtContractLib, | ||
enclaveClient: enclaveClient, | ||
logger: logger, | ||
hostStopper: hostStopper, | ||
} | ||
} | ||
|
||
func (c *crossChainStateMachine) Start() error { | ||
return nil | ||
} | ||
func (c *crossChainStateMachine) Stop() error { | ||
return nil | ||
} | ||
func (c *crossChainStateMachine) HealthStatus(context.Context) host.HealthStatus { | ||
errMsg := "" | ||
if c.hostStopper.IsStopping() { | ||
errMsg = "not running" | ||
} | ||
return &host.BasicErrHealthStatus{ErrMsg: errMsg} | ||
|
||
} | ||
|
||
func (c *crossChainStateMachine) GetRollupData(number RollupNumber) (RollupInfo, error) { | ||
if number == c.latestRollup.Number { | ||
return c.latestRollup, nil | ||
} else if number > c.latestRollup.Number { | ||
return RollupInfo{}, errutil.ErrNotFound | ||
} else { | ||
return c.rollupHistory[number], nil | ||
} | ||
} | ||
|
||
func (c *crossChainStateMachine) PublishNextBundle() error { | ||
// If all bundles for the rollups have been published, there is nothing to do. | ||
if c.currentRollup >= c.latestRollup.Number { | ||
return nil | ||
} | ||
|
||
// Get the bundle range from the management contract | ||
nextForkUID, begin, end, err := c.publisher.GetBundleRangeFromManagementContract(big.NewInt(0).SetUint64(c.currentRollup), c.latestRollup.ForkUID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := c.GetRollupData(c.currentRollup + 1) | ||
if err != nil { | ||
return err | ||
} | ||
if data.ForkUID != *nextForkUID { | ||
return errutil.ErrRollupForkMismatch | ||
} | ||
|
||
bundle, err := c.enclaveClient.ExportCrossChainData(context.Background(), begin.Uint64(), end.Uint64()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = c.publisher.PublishCrossChainBundle(bundle) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Move the current rollup to the next rollup | ||
c.currentRollup++ | ||
|
||
return nil | ||
|
||
} | ||
|
||
// Synchronize - checks if there are any new rollups or forks and moves the tracking needle to the latest common ancestor. | ||
func (c *crossChainStateMachine) Synchronize() error { | ||
|
||
forkUID, _, _, err := c.publisher.GetBundleRangeFromManagementContract(big.NewInt(0).SetUint64(c.latestRollup.Number), c.latestRollup.ForkUID) | ||
if err != nil { | ||
if errors.Is(err, errutil.ErrNoNextRollup) { | ||
c.logger.Debug("No new rollup or fork found") | ||
return nil | ||
} | ||
|
||
if errors.Is(err, errutil.ErrRollupForkMismatch) { | ||
return c.RevertToLatestKnownCommonAncestorRollup() | ||
} | ||
|
||
c.logger.Error("Failed to get bundle range from management contract", "error", err) | ||
return err | ||
} | ||
|
||
c.rollupHistory[c.latestRollup.Number] = c.latestRollup | ||
c.latestRollup = RollupInfo{ | ||
Hash: gethcommon.Hash{}, | ||
ForkUID: *forkUID, | ||
Number: c.latestRollup.Number + 1, | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *crossChainStateMachine) RevertToLatestKnownCommonAncestorRollup() error { | ||
|
||
managementContract, err := ManagementContract.NewManagementContract(*c.mgmtContractLib.GetContractAddr(), c.ethClient.EthClient()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hashBytes, _, err := managementContract.GetUniqueForkID(&bind.CallOpts{}, big.NewInt(0).SetUint64(c.latestRollup.Number)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var forkHash gethcommon.Hash | ||
forkHash = gethcommon.BytesToHash(hashBytes[:]) | ||
|
||
for forkHash != c.latestRollup.ForkUID { | ||
// Revert to previous rollup; No need to wipe the map as the synchronization reinserts the latest rollup | ||
c.latestRollup = c.rollupHistory[c.latestRollup.Number-1] //go to previous rollup | ||
|
||
hashBytes, _, err = managementContract.GetUniqueForkID(&bind.CallOpts{}, big.NewInt(0).SetUint64(c.latestRollup.Number)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
forkHash = gethcommon.BytesToHash(hashBytes[:]) | ||
} | ||
|
||
// Rollback current rollup if it was dumped due to a fork. | ||
if c.currentRollup > c.latestRollup.Number { | ||
c.currentRollup = c.latestRollup.Number | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.