-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not rely on a hardcoded NitroAdjudicator address #121
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,41 +2,80 @@ package chain | |
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"io" | ||
"log" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
"github.com/statechannels/go-nitro/client/engine/chainservice" | ||
NitroAdjudicator "github.com/statechannels/go-nitro/client/engine/chainservice/adjudicator" | ||
Create2Deployer "github.com/statechannels/go-nitro/client/engine/chainservice/create2deployer" | ||
"github.com/testground/sdk-go/runtime" | ||
"github.com/testground/sdk-go/sync" | ||
) | ||
|
||
func NewChainService(seq int64, logDestination io.Writer) chainservice.ChainService { | ||
func NewChainService(ctx context.Context, syncClient sync.Client, runEnv *runtime.RunEnv, seq int64, logDestination io.Writer) chainservice.ChainService { | ||
client, err := ethclient.Dial("ws://hardhat:8545/") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// TODO: do not hardcode the NitroAdjudicator address. Instead, find address on chain | ||
naAddress := common.HexToAddress("0x5fbdb2315678afecb367f032d93f642f64180aa3") | ||
na, err := NitroAdjudicator.NewNitroAdjudicator(naAddress, client) | ||
txSubmitter, err := bind.NewKeyedTransactorWithChainID(getFundedPrivateKey(uint(seq)), big.NewInt(1337)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
txSubmitter.GasLimit = uint64(30_000_000) // in units | ||
|
||
txSubmitter, err := bind.NewKeyedTransactorWithChainID(getFundedPrivateKey(uint(seq)), big.NewInt(1337)) | ||
gasPrice, err := client.SuggestGasPrice(context.Background()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
txSubmitter.GasLimit = uint64(300000) // in units | ||
txSubmitter.GasPrice = gasPrice | ||
|
||
gasPrice, err := client.SuggestGasPrice(context.Background()) | ||
deployer, err := Create2Deployer.NewCreate2Deployer(common.HexToAddress("0x5fbdb2315678afecb367f032d93f642f64180aa3"), client) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
hexBytecode, err := hex.DecodeString(NitroAdjudicator.NitroAdjudicatorMetaData.Bin[2:]) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
naAddress, err := deployer.ComputeAddress(&bind.CallOpts{}, [32]byte{}, crypto.Keccak256Hash(hexBytecode)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// One testground instance attempts to deploy NitroAdjudicator | ||
if seq == 1 { | ||
bytecode, err := client.CodeAt(ctx, naAddress, nil) // nil is latest block | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Has NitroAdjudicator been deployed? If not, deploy it. | ||
if len(bytecode) == 0 { | ||
_, err = deployer.Deploy(txSubmitter, big.NewInt(0), [32]byte{}, hexBytecode) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
Comment on lines
+54
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if the deployment code should be in a method That would avoid the need to change the API and have the chain service polluted with testground types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds like a good idea. I am not sure I fully understand how to implement the idea. The sequence of events in
If below sequence is split up, we either need:
Neither of the above is obviously cleaner to me than the existing code. |
||
|
||
// All instances wait for until the NitroAdjudicator has been deployed. | ||
contractSetup := sync.State("contractSetup") | ||
syncClient.MustSignalEntry(ctx, contractSetup) | ||
syncClient.MustBarrier(ctx, contractSetup, runEnv.TestInstanceCount) | ||
|
||
na, err := NitroAdjudicator.NewNitroAdjudicator(naAddress, client) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
txSubmitter.GasPrice = gasPrice | ||
|
||
cs, err := chainservice.NewEthChainService(client, na, naAddress, common.Address{}, common.Address{}, txSubmitter, logDestination) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think statechannels/hardhat-docker#5 now just applies here. Not a priority, but we should probably deploy the consensus app and virtual payment app alongside the adjudicator here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. Filed #122.