Skip to content
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

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions chain/chain-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Filed #122.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the deployment code should be in a method (cs *ChainService) DeployAdjudicator, and then the "coordination" code (looking at the sequence number, calling out to the syncClient) can be put into the actual test script?

That would avoid the need to change the API and have the chain service polluted with testground types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 NewChainService is:

  1. Initialize ethclient.Client client.
  2. Initialize bind.TransactOpts txSubmitter.
  3. Use client to compute NitroAdjudicator address.
  4. Use client and txSubmitter to deploy NitroAdjudicator if needed.
  5. Use coordination code to block all instances.
  6. Use client to set up a NitroAdjudicator object bound to the chain contract.
  7. Use client, txSubmitter, and bound NitroAdjudicator object to create a new chain service.

If below sequence is split up, we either need:

  1. A stateful object that contains the client and txSubmitter.
  2. Pass around client and txSubmitter.

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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/miguelmota/go-ethereum-hdwallet v0.1.1
github.com/multiformats/go-multiaddr v0.6.0
github.com/statechannels/go-nitro v0.0.0-20220922174011-3e33cafaa1f3
github.com/statechannels/go-nitro v0.0.0-20221004165356-5d2a8306f2b4
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,8 @@ github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzu
github.com/spf13/pflag v1.0.1-0.20171106142849-4c012f6dcd95/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/statechannels/go-nitro v0.0.0-20220922174011-3e33cafaa1f3 h1:YJtZyilH56jp592m3am56ASaJ46Uge/YhaGghD/It0k=
github.com/statechannels/go-nitro v0.0.0-20220922174011-3e33cafaa1f3/go.mod h1:QFdUCZT0Du/1kCBTUUhcgvGxyvgHcGs71AVdpZseNfA=
github.com/statechannels/go-nitro v0.0.0-20221004165356-5d2a8306f2b4 h1:+fKrz+oFnh4vgD2lCYH7Ev/zf6+2HeiUzyJu66MDD8w=
github.com/statechannels/go-nitro v0.0.0-20221004165356-5d2a8306f2b4/go.mod h1:QFdUCZT0Du/1kCBTUUhcgvGxyvgHcGs71AVdpZseNfA=
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 h1:Gb2Tyox57NRNuZ2d3rmvB3pcmbu7O1RS3m8WRx7ilrg=
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
2 changes: 1 addition & 1 deletion tests/virtual-payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func CreateVirtualPaymentTest(runEnv *runtime.RunEnv, init *run.InitContext) err
// The outputs folder will be copied when results are collected.
logDestination, _ := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0666)

nClient := nitro.New(ms, chain.NewChainService(seq, logDestination), store, logDestination, &engine.PermissivePolicy{}, runEnv.R())
nClient := nitro.New(ms, chain.NewChainService(ctx, client, runEnv, seq, logDestination), store, logDestination, &engine.PermissivePolicy{}, runEnv.R())

cm := utils.NewCompletionMonitor(&nClient, runEnv.RecordMessage)
defer cm.Close()
Expand Down