Skip to content

Commit

Permalink
add unstake nonce (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: liutian <liutianv1@gmail.com>
  • Loading branch information
tiantianlikeu and liutian committed Apr 15, 2024
1 parent 3851e17 commit f338652
Show file tree
Hide file tree
Showing 9 changed files with 2,814 additions and 686 deletions.
7 changes: 4 additions & 3 deletions appchain/appchain_l1.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (l *L1AppChainListener) eventUnpack(event event.ContractEvent) error {
case bindings.StakingManagerAbi.Events["UnstakeRequested"].ID.String():
err := unpack.UnstakeRequested(l.chainId, event, l.db)
return err
case bindings.StakingManagerAbi.Events["UnstakeRequestClaimed"].ID.String():
case bindings.UnstakeRequestsManagerAbi.Events["UnstakeRequestClaimed"].ID.String():
err := unpack.UnstakeRequestClaimed(event, l.db)
return err
}
Expand All @@ -170,10 +170,11 @@ func (l *L1AppChainListener) notifyRelayerBatch() error {
for _, batch := range batchList {
destChainId := batch.DestChainId
sourceChainId := batch.SourceChainId
bridge := batch.Bridge
strategy := batch.L2Strategy
txHash := batch.TxHash
unstakeBatch, err := l.bridgeRpcService.UnstakeBatch(txHash.String(), bridge.String(), strategy.String(), sourceChainId, destChainId)
strategyMap := make(map[string]uint64)
strategyMap[strategy.String()] = batch.UnstakeNonce.Uint64()
unstakeBatch, err := l.bridgeRpcService.UnstakeBatch(txHash.String(), sourceChainId, destChainId, strategyMap)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions appchain/bindings/abi_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
stake_bindings "github.com/cornerstone-labs/acorus/appchain/bindings/staking_manager/bindings"
strategybasebindings "github.com/cornerstone-labs/acorus/appchain/bindings/strategy_base/bindings"
stratege_bindings "github.com/cornerstone-labs/acorus/appchain/bindings/strategy_manager/bindings"
unstake_manager_bindings "github.com/cornerstone-labs/acorus/appchain/bindings/unstake_requests_manager/bindings"
)

var (
StakingManagerAbi, _ = stake_bindings.StakingManagerMetaData.GetAbi()
StrategyManagerAbi, _ = stratege_bindings.StrategyManagerMetaData.GetAbi()
DelegationManagerAbi, _ = delegation_bindings.DelegationManagerMetaData.GetAbi()
StrategyBaseAbi, _ = strategybasebindings.StrategyBaseMetaData.GetAbi()
StakingManagerAbi, _ = stake_bindings.StakingManagerMetaData.GetAbi()
StrategyManagerAbi, _ = stratege_bindings.StrategyManagerMetaData.GetAbi()
DelegationManagerAbi, _ = delegation_bindings.DelegationManagerMetaData.GetAbi()
StrategyBaseAbi, _ = strategybasebindings.StrategyBaseMetaData.GetAbi()
UnstakeRequestsManagerAbi, _ = unstake_manager_bindings.UnstakeRequestsManagerMetaData.GetAbi()
)
730 changes: 244 additions & 486 deletions appchain/bindings/staking_manager/bindings/bvm_staking_manager.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions appchain/unpack/log_unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
stake_bindings "github.com/cornerstone-labs/acorus/appchain/bindings/staking_manager/bindings"
strategybasebindings "github.com/cornerstone-labs/acorus/appchain/bindings/strategy_base/bindings"
stratege_bindings "github.com/cornerstone-labs/acorus/appchain/bindings/strategy_manager/bindings"
unstake_manager_bindings "github.com/cornerstone-labs/acorus/appchain/bindings/unstake_requests_manager/bindings"
"github.com/cornerstone-labs/acorus/database"
"github.com/cornerstone-labs/acorus/database/appchain"
"github.com/cornerstone-labs/acorus/database/event"
)

var (
StakeUnpack, _ = stake_bindings.NewStakingManager(common.Address{}, nil)
StrategyUnpack, _ = stratege_bindings.NewStrategyManager(common.Address{}, nil)
DelegateUnpack, _ = delegation_bindings.NewDelegationManager(common.Address{}, nil)
StrategyBaseUnpack, _ = strategybasebindings.NewStrategyBase(common.Address{}, nil)
StakeUnpack, _ = stake_bindings.NewStakingManager(common.Address{}, nil)
StrategyUnpack, _ = stratege_bindings.NewStrategyManager(common.Address{}, nil)
DelegateUnpack, _ = delegation_bindings.NewDelegationManager(common.Address{}, nil)
StrategyBaseUnpack, _ = strategybasebindings.NewStrategyBase(common.Address{}, nil)
UnstakeManagerUnpack, _ = unstake_manager_bindings.NewUnstakeRequestsManager(common.Address{}, nil)
)

const (
Expand All @@ -29,19 +31,16 @@ const (

func UnstakeRequestClaimed(event event.ContractEvent, db *database.DB) error {
rlpLog := event.RLPLog
uEvent, unpackErr := StakeUnpack.ParseUnstakeRequestClaimed(*rlpLog)
uEvent, unpackErr := UnstakeManagerUnpack.ParseUnstakeRequestClaimed(*rlpLog)
if unpackErr != nil {
return unpackErr
}
unStakeSingle := appchain.AppChainUnStake{
ClaimTxHash: rlpLog.TxHash,
Bridge: uEvent.Bridge,
Staker: uEvent.Staker,
SourceChainId: uEvent.SourceChainId.String(),
DestChainId: uEvent.DestChainId.String(),
L2Strategy: uEvent.L2Strategy,
Updated: event.Timestamp,
Status: Claim,
ClaimTxHash: rlpLog.TxHash,
Bridge: uEvent.BridgeAddress,
UnstakeNonce: uEvent.UnStakeMessageNonce,
Updated: event.Timestamp,
Status: Claim,
}
return db.AppChainUnStake.ClaimAppChainUnStake(unStakeSingle, Pending)
}
Expand All @@ -61,6 +60,7 @@ func UnstakeRequested(chainId string, event event.ContractEvent, db *database.DB
DETHLocked: uEvent.DETHLocked,
SourceChainId: chainId,
DestChainId: uEvent.DestChainId.String(),
UnstakeNonce: uEvent.UnStakeMessageNonce,
Created: event.Timestamp,
Status: Pending,
}
Expand Down
9 changes: 4 additions & 5 deletions database/appchain/appchain_unstake.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type AppChainUnStake struct {
Bridge common.Address `json:"bridge" gorm:"serializer:bytes"`
SourceChainId string `json:"source_chain_id"`
DestChainId string `json:"dest_chain_id"`
UnstakeNonce *big.Int `json:"unstake_nonce" gorm:"serializer:u256"`
Status uint8 `json:"status"`
NotifyRelayer bool `json:"notify_relayer"`
Created uint64 `json:"created"`
Expand Down Expand Up @@ -112,11 +113,9 @@ func (db appChainUnStakeDB) NotifyAppChainUnStake(txHash string) error {

func (db appChainUnStakeDB) ClaimAppChainUnStake(chainUnStakeBatch AppChainUnStake, noClaim uint8) error {
var exits AppChainUnStake
err := db.gorm.Table(chainUnStakeBatch.TableName()).Where(AppChainUnStake{L2Strategy: chainUnStakeBatch.L2Strategy,
SourceChainId: chainUnStakeBatch.SourceChainId,
DestChainId: chainUnStakeBatch.DestChainId,
Staker: chainUnStakeBatch.Staker,
Status: noClaim,
err := db.gorm.Table(chainUnStakeBatch.TableName()).Where(AppChainUnStake{
UnstakeNonce: chainUnStakeBatch.UnstakeNonce,
Status: noClaim,
}).Take(&exits)
if err != nil {
if errors.Is(err.Error, gorm.ErrRecordNotFound) {
Expand Down
1 change: 1 addition & 0 deletions migrations/00001_create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ create table if not exists ac_chain_unstake
bridge varchar,
source_chain_id varchar,
dest_chain_id varchar,
unstake_nonce UINT256 default 0,
status int default 0,
notify_relayer boolean default false,
created INTEGER CHECK (created > 0),
Expand Down
15 changes: 7 additions & 8 deletions rpc/bridge/bridge_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type BridgeRpcService interface {
receiveAddress, tokenAddress, hash string) (*pb.UpdateWithdrawFundingPoolBalanceResponse, error)
UpdateDepositFundingPoolBalance(sourceChainId, destChainId, amount,
receiveAddress, tokenAddress, hash string) (*pb.UpdateDepositFundingPoolBalanceResponse, error)
UnstakeBatch(sourceHash, bridgeAddress, strategyAddress, sourceChainId, destChainId string) (*pb.UnstakeBatchResponse, error)
UnstakeBatch(sourceHash, sourceChainId, destChainId string, strategyMap map[string]uint64) (*pb.UnstakeBatchResponse, error)
BatchMint(batchId uint64, batchMint map[string]string) (*pb.BatchMintResponse, error)
TransferToL2DappLinkBridge(batchId uint64, ChainId, StrategyAddress string) (*pb.TransferToL2DappLinkBridgeResponse, error)
}
Expand Down Expand Up @@ -97,15 +97,14 @@ func (r *bridgeRpcService) UpdateWithdrawFundingPoolBalance(sourceChainId, destC
return poolBalanceResponse, err
}

func (r *bridgeRpcService) UnstakeBatch(sourceHash, bridgeAddress, strategyAddress, sourceChainId, destChainId string) (*pb.UnstakeBatchResponse, error) {
func (r *bridgeRpcService) UnstakeBatch(sourceHash, sourceChainId, destChainId string, strategyMap map[string]uint64) (*pb.UnstakeBatchResponse, error) {
ctx := context.Background()
upstakeBatchReq := &pb.UnstakeBatchRequest{
BridgeAddress: bridgeAddress,
SourceChainId: sourceChainId,
DestChainId: destChainId,
SourceHash: sourceHash,
L2StrategyAddress: strategyAddress,
GasLimit: "200000",
SourceChainId: sourceChainId,
DestChainId: destChainId,
SourceHash: sourceHash,
Strategy: strategyMap,
GasLimit: "200000",
}
log.Info("UnstakeBatchRpc", "upstakeBatchReq", upstakeBatchReq)
return r.bRpcService.UnstakeBatch(ctx, upstakeBatchReq)
Expand Down
Loading

0 comments on commit f338652

Please sign in to comment.