Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Prevent excess votes #184

Merged
merged 22 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 20 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
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ RUN cd /src && echo $(ls -1 /src)
RUN go mod download
RUN go build -o /bridge ./e2e/evm-evm/example/.

# # final stage
# final stage
FROM debian:stretch-slim
RUN apt-get -y update && apt-get -y upgrade && apt-get install ca-certificates wget -y

COPY --from=builder /bridge ./
RUN chmod +x ./bridge

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ install-subkey:
cargo install --force --git https://github.com/paritytech/substrate subkey

genmocks:
mockgen -source=./chains/evm/evmgaspricer/gas-pricer.go -destination=./chains/evm/evmgaspricer/mock/gas-pricer.go
mockgen -source=chains/evm/evmgaspricer/gas-pricer.go -destination=chains/evm/evmgaspricer/mock/gas-pricer.go
mockgen -source=chains/evm/calls/utils.go -destination=chains/evm/calls/mock/utils.go
mockgen -destination=chains/evm/voter/mock/voter.go github.com/ChainSafe/chainbridge-core/chains/evm/voter ChainClient,MessageHandler

e2e-test:
./scripts/int_tests.sh
Expand Down
21 changes: 7 additions & 14 deletions chains/evm/calls/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,32 +282,25 @@ func GetThreshold(evmCaller ContractCallerClient, bridgeAddress *common.Address)
func ProposalStatus(evmCaller ContractCallerClient, p *proposal.Proposal) (relayer.ProposalStatus, error) {
a, err := abi.JSON(strings.NewReader(consts.BridgeABI))
if err != nil {
return relayer.ProposalStatusInactive, err
return relayer.ProposalStatus{}, err
}
input, err := a.Pack("getProposal", p.Source, p.DepositNonce, SliceTo32Bytes(p.Data))
input, err := a.Pack("getProposal", p.Source, p.DepositNonce, p.GetDataHash())
if err != nil {
return relayer.ProposalStatusInactive, err
return relayer.ProposalStatus{}, err
}

msg := ethereum.CallMsg{From: common.Address{}, To: &p.BridgeAddress, Data: input}
out, err := evmCaller.CallContract(context.TODO(), ToCallArg(msg), nil)
if err != nil {
return relayer.ProposalStatusInactive, err
}

type bridgeProposal struct {
Status uint8
YesVotes *big.Int
YesVotesTotal uint8
ProposedBlock *big.Int
return relayer.ProposalStatus{}, err
}
res, err := a.Unpack("getProposal", out)
if err != nil {
return relayer.ProposalStatusInactive, err
return relayer.ProposalStatus{}, err
}

out0 := *abi.ConvertType(res[0], new(bridgeProposal)).(*bridgeProposal)
return relayer.ProposalStatus(out0.Status), nil
ps := *abi.ConvertType(res[0], new(relayer.ProposalStatus)).(*relayer.ProposalStatus)
return ps, nil
}

func idAndNonce(srcId uint8, nonce uint64) *big.Int {
Expand Down
9 changes: 5 additions & 4 deletions chains/evm/calls/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *ProposalStatusTestSuite) TestProposalStatusFailedContractCall() {

status, err := calls.ProposalStatus(s.mockContractCaller, &proposal.Proposal{})

s.Equal(relayer.ProposalStatusInactive, status)
s.Equal(relayer.ProposalStatus{}, status)
s.NotNil(err)
}

Expand All @@ -60,18 +60,19 @@ func (s *ProposalStatusTestSuite) TestProposalStatusFailedUnpack() {

status, err := calls.ProposalStatus(s.mockContractCaller, &proposal.Proposal{})

s.Equal(relayer.ProposalStatusInactive, status)
s.Equal(relayer.ProposalStatus{}, status)
s.NotNil(err)
}

func (s *ProposalStatusTestSuite) TestProposalStatusSuccessfulCall() {
proposalStatus, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
proposalStatus, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001f")
s.mockContractCaller.EXPECT().CallContract(gomock.Any(), gomock.Any(), nil).Return(proposalStatus, nil)

status, err := calls.ProposalStatus(s.mockContractCaller, &proposal.Proposal{})

s.Equal(relayer.ProposalStatusInactive, status)
s.Nil(err)
s.Equal(status.YesVotesTotal, uint8(3))
s.Equal(status.Status, relayer.ProposalStatusExecuted)
}

func TestPrepareWithdrawInput(t *testing.T) {
Expand Down
17 changes: 12 additions & 5 deletions chains/evm/evmclient/evm-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/rs/zerolog/log"
)

type EVMClient struct {
*ethclient.Client
rpClient *rpc.Client
config *EVMConfig
nonce *big.Int
nonceLock sync.Mutex
gethClient *gethclient.Client
rpClient *rpc.Client
config *EVMConfig
nonce *big.Int
nonceLock sync.Mutex
}

// DepositLogs struct holds event data with all necessary parameters and a handler response
Expand Down Expand Up @@ -74,12 +76,17 @@ func NewEVMClientFromParams(url string, privateKey *ecdsa.PrivateKey) (*EVMClien
kp := secp256k1.NewKeypair(*privateKey)
c := &EVMClient{}
c.Client = ethclient.NewClient(rpcClient)
c.gethClient = gethclient.New(rpcClient)
c.rpClient = rpcClient
c.config = &EVMConfig{}
c.config.kp = kp
return c, nil
}

func (c *EVMClient) SubscribePendingTransactions(ctx context.Context, ch chan<- common.Hash) (*rpc.ClientSubscription, error) {
return c.gethClient.SubscribePendingTransactions(ctx, ch)
}

func (c *EVMClient) Configurate(path string, name string) error {
rawCfg, err := GetConfig(path, name)
if err != nil {
Expand All @@ -106,6 +113,7 @@ func (c *EVMClient) Configurate(path string, name string) error {
}
c.Client = ethclient.NewClient(rpcClient)
c.rpClient = rpcClient
c.gethClient = gethclient.New(rpcClient)

if generalConfig.LatestBlock {
curr, err := c.LatestBlock()
Expand Down Expand Up @@ -154,7 +162,6 @@ func (c *EVMClient) WaitAndReturnTxReceipt(h common.Hash) (*types.Receipt, error
for retry > 0 {
receipt, err := c.Client.TransactionReceipt(context.Background(), h)
if err != nil {
log.Error().Err(err).Msgf("error getting tx receipt %s", h.String())
retry--
time.Sleep(5 * time.Second)
continue
Expand Down
2 changes: 1 addition & 1 deletion chains/evm/evmgaspricer/mock/gas-pricer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

237 changes: 237 additions & 0 deletions chains/evm/voter/mock/voter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading