-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test to verify the setting, monitoring and serving of contracts
- Loading branch information
1 parent
1263801
commit dc63568
Showing
8 changed files
with
162 additions
and
2 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
110 changes: 110 additions & 0 deletions
110
integration/networktest/actions/l1/important_contracts.go
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,110 @@ | ||
package l1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/obscuronet/go-obscuro/go/common/retry" | ||
"github.com/obscuronet/go-obscuro/go/ethadapter/mgmtcontractlib" | ||
"github.com/obscuronet/go-obscuro/go/obsclient" | ||
"github.com/obscuronet/go-obscuro/integration/common/testlog" | ||
"github.com/obscuronet/go-obscuro/integration/networktest" | ||
"github.com/pkg/errors" | ||
"time" | ||
) | ||
|
||
type setImportantContract struct { | ||
contractKey string | ||
contractAddress common.Address | ||
} | ||
|
||
func SetImportantContract(contractKey string, contractAddress common.Address) networktest.Action { | ||
return &setImportantContract{ | ||
contractKey: contractKey, | ||
contractAddress: contractAddress, | ||
} | ||
} | ||
|
||
func (s *setImportantContract) Run(ctx context.Context, network networktest.NetworkConnector) (context.Context, error) { | ||
obsClient, err := obsclient.Dial(network.ValidatorRPCAddress(0)) | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to dial obsClient") | ||
} | ||
|
||
networkCfg, err := obsClient.GetConfig() | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to get network config") | ||
} | ||
|
||
l1Client, err := network.GetL1Client() | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to get L1 client") | ||
} | ||
|
||
mgmtContract := mgmtcontractlib.NewMgmtContractLib(&networkCfg.ManagementContractAddress, testlog.Logger()) | ||
|
||
msg, err := mgmtContract.SetImportantContractMsg(s.contractKey, s.contractAddress) | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to create SetImportantContractMsg") | ||
} | ||
|
||
txData := &types.LegacyTx{ | ||
To: &networkCfg.ManagementContractAddress, | ||
Data: msg.Data, | ||
} | ||
mcOwner, err := network.GetMCOwnerWallet() | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to get MC owner wallet") | ||
} | ||
// !! Important note !! | ||
// The ownerOnly check in the contract doesn't like the gas estimate in here, to test you may need to hardcode a | ||
// the gas value when the estimate errors | ||
tx, err := l1Client.PrepareTransactionToSend(txData, networkCfg.ManagementContractAddress, mcOwner.GetNonceAndIncrement()) | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to prepare tx") | ||
} | ||
signedTx, err := mcOwner.SignTransaction(tx) | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to sign tx") | ||
} | ||
err = l1Client.SendTransaction(signedTx) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to send tx") | ||
} | ||
|
||
// wait for tx to be mined | ||
return ctx, retry.Do(func() error { | ||
receipt, err := l1Client.TransactionReceipt(signedTx.Hash()) | ||
if err != nil { | ||
return err | ||
} | ||
if receipt.Status != types.ReceiptStatusSuccessful { | ||
return retry.FailFast(errors.New("tx failed")) | ||
} | ||
return nil | ||
}, retry.NewTimeoutStrategy(15*time.Second, 1*time.Second)) | ||
} | ||
|
||
func (s *setImportantContract) Verify(ctx context.Context, network networktest.NetworkConnector) error { | ||
cli, err := obsclient.Dial(network.ValidatorRPCAddress(0)) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to dial obsClient") | ||
} | ||
networkCfg, err := cli.GetConfig() | ||
fmt.Println("networkCfg: ", networkCfg) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get network config") | ||
} | ||
|
||
if networkCfg.ImportantContracts == nil || len(networkCfg.ImportantContracts) == 0 { | ||
return errors.New("no important contracts set") | ||
} | ||
if len(networkCfg.ImportantContracts) > 1 { | ||
return errors.New("more than one important contract set") | ||
} | ||
if addr, ok := networkCfg.ImportantContracts[s.contractKey]; !ok || addr != s.contractAddress { | ||
return errors.New("important contract not set") | ||
} | ||
return nil | ||
} |
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
22 changes: 22 additions & 0 deletions
22
integration/networktest/tests/bridge/important_contracts_test.go
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,22 @@ | ||
package bridge | ||
|
||
import ( | ||
gethcommon "github.com/ethereum/go-ethereum/common" | ||
"github.com/obscuronet/go-obscuro/integration/networktest" | ||
"github.com/obscuronet/go-obscuro/integration/networktest/actions" | ||
"github.com/obscuronet/go-obscuro/integration/networktest/actions/l1" | ||
"github.com/obscuronet/go-obscuro/integration/networktest/env" | ||
"testing" | ||
) | ||
|
||
func TestImportantContractsLookup(t *testing.T) { | ||
networktest.TestOnlyRunsInIDE(t) | ||
networktest.Run( | ||
"important-contracts-lookup", | ||
t, | ||
env.LocalDevNetwork(), | ||
actions.Series( | ||
l1.SetImportantContract("L1TestContract", gethcommon.HexToAddress("0x64")), | ||
), | ||
) | ||
} |
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