Skip to content

Commit

Permalink
Implement ethtest.SimulatedBackend.Addr(i) as a wrapper for sim.Acc(i…
Browse files Browse the repository at this point in the history
…).From
  • Loading branch information
ARR4N committed Dec 14, 2021
1 parent 0a19413 commit e15afa3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
5 changes: 5 additions & 0 deletions ethtest/simbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func (sb *SimulatedBackend) Acc(account int) *bind.TransactOpts {
}
}

// Addr returns the Address of the specified account number.
func (sb *SimulatedBackend) Addr(account int) common.Address {
return sb.accounts[account].From
}

// WithValueFrom returns a TransactOpts that sends the specified value from the
// account. If value==0, sb.Acc(account) can be used directly.
func (sb *SimulatedBackend) WithValueFrom(account int, value *big.Int) *bind.TransactOpts {
Expand Down
2 changes: 1 addition & 1 deletion tests/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestAddressSignature(t *testing.T) {

signAddr := func(signer *eth.Signer, party int) []byte {
t.Helper()
addr := sim.Acc(party).From
addr := sim.Addr(party)

sig, err := signer.SignAddress(addr)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/erc721/erc721_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func deploy(t *testing.T) (*ethtest.SimulatedBackend, *TestableERC721CommonEnume
if _, err := nft.Mint(sim.Acc(tokenOwner), big.NewInt(exists)); err != nil {
t.Fatalf("Mint(%d) error %v", exists, err)
}
if _, err := nft.Approve(sim.Acc(tokenOwner), sim.Acc(approved).From, big.NewInt(exists)); err != nil {
if _, err := nft.Approve(sim.Acc(tokenOwner), sim.Addr(approved), big.NewInt(exists)); err != nil {
t.Fatalf("Approve(<approved account>, %d) error %v", exists, err)
}

Expand Down
54 changes: 27 additions & 27 deletions tests/sales/sales_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,11 @@ func TestFundsManagement(t *testing.T) {

for i, tt := range tests {
t.Run(fmt.Sprintf("purchase[%d]", i), func(t *testing.T) {
before := sim.BalanceOf(ctx, t, sim.Acc(tt.account).From)
before := sim.BalanceOf(ctx, t, sim.Addr(tt.account))

tx, err := auction.Buy(
sim.WithValueFrom(tt.account, tt.sendValue),
sim.Acc(tt.account).From,
sim.Addr(tt.account),
big.NewInt(tt.num),
)
if diff := errdiff.Check(err, tt.errDiffAgainst); diff != "" {
Expand Down Expand Up @@ -832,7 +832,7 @@ func TestFundsManagement(t *testing.T) {
case got := <-refunds:
got.Raw = types.Log{}
want := &TestableDutchAuctionRefund{
Buyer: sim.Acc(tt.account).From,
Buyer: sim.Addr(tt.account),
Amount: tt.wantRefund,
}
if diff := cmp.Diff(want, got, ethtest.Comparers()...); diff != "" {
Expand All @@ -856,7 +856,7 @@ func TestFundsManagement(t *testing.T) {
}

t.Run("buyer balance decrease", func(t *testing.T) {
after := sim.BalanceOf(ctx, t, sim.Acc(tt.account).From)
after := sim.BalanceOf(ctx, t, sim.Addr(tt.account))
gotSpent := new(big.Int).Sub(before, after)
gotSpent.Sub(gotSpent, sim.GasSpent(ctx, t, tx))

Expand Down Expand Up @@ -927,10 +927,10 @@ func TestFixedPriceSeller(t *testing.T) {
func TestPausing(t *testing.T) {
sim, _, auction := deployConstantPrice(t, eth.Ether(0))

sim.Must(t, "When not paused, Buy()")(auction.Buy(sim.Acc(0), sim.Acc(0).From, big.NewInt(1)))
sim.Must(t, "When not paused, Buy()")(auction.Buy(sim.Acc(0), sim.Addr(0), big.NewInt(1)))
sim.Must(t, "Pause()")(auction.Pause(sim.Acc(0)))

if diff := revert.Paused.Diff(auction.Buy(sim.Acc(0), sim.Acc(0).From, big.NewInt(1))); diff != "" {
if diff := revert.Paused.Diff(auction.Buy(sim.Acc(0), sim.Addr(0), big.NewInt(1))); diff != "" {
t.Errorf("When paused, Buy() %s", diff)
}
}
Expand All @@ -945,12 +945,12 @@ func TestReentrancyGuard(t *testing.T) {
}

if diff := revert.Reentrant.Diff(
attacker.Buy(sim.WithValueFrom(0, eth.Ether(10)), sim.Acc(0).From, big.NewInt(1)),
attacker.Buy(sim.WithValueFrom(0, eth.Ether(10)), sim.Addr(0), big.NewInt(1)),
); diff != "" {
t.Errorf("%T.Buy(); invoking Seller._purchase() through reentrant call; %s", attacker, diff)
}

got, err := auction.Own(nil, sim.Acc(0).From)
got, err := auction.Own(nil, sim.Addr(0))
if err != nil {
t.Fatalf("Own() error %v", err)
}
Expand Down Expand Up @@ -1019,34 +1019,34 @@ func TestReservedFreePurchasing(t *testing.T) {
sim.Must(t, "SetSellerConfig(%+v", cfg)(auction.SetSellerConfig(sim.Acc(deployer), cfg))

t.Run("only owner purchases free", func(t *testing.T) {
if diff := revert.OnlyOwner.Diff(auction.PurchaseFreeOfCharge(sim.Acc(rcvFree), sim.Acc(rcvFree).From, big.NewInt(1))); diff != "" {
if diff := revert.OnlyOwner.Diff(auction.PurchaseFreeOfCharge(sim.Acc(rcvFree), sim.Addr(rcvFree), big.NewInt(1))); diff != "" {
t.Errorf("PurchaseFreeOfCharge() as non-owner; %s", diff)
}
wantOwned(t, auction, sim.Acc(rcvFree).From, 0, 0)
wantOwned(t, auction, sim.Addr(rcvFree), 0, 0)
})

t.Run("reserved free quota honoured", func(t *testing.T) {
n := big.NewInt(totalInventory - freeQuota)
sim.Must(t, "Buy(totalInventory - freeQuota)")(auction.Buy(sim.WithValueFrom(buyer, n), sim.Acc(buyer).From, n))
sim.Must(t, "Buy(totalInventory - freeQuota)")(auction.Buy(sim.WithValueFrom(buyer, n), sim.Addr(buyer), n))

if diff := revert.SoldOut.Diff(
auction.Buy(sim.WithValueFrom(buyer, big.NewInt(1)), sim.Acc(buyer).From, big.NewInt(1)),
auction.Buy(sim.WithValueFrom(buyer, big.NewInt(1)), sim.Addr(buyer), big.NewInt(1)),
); diff != "" {
t.Errorf("After Buy(totalInventory - freeQuota); Buy(1) %s", diff)
}

wantOwned(t, auction, sim.Acc(buyer).From, totalInventory-freeQuota, 0)
wantOwned(t, auction, sim.Addr(buyer), totalInventory-freeQuota, 0)
})

t.Run("free quota enforced", func(t *testing.T) {
sim.Must(t, "PurchaseFreeOfCharge(freeQuota)")(auction.PurchaseFreeOfCharge(sim.Acc(deployer), sim.Acc(rcvFree).From, big.NewInt(freeQuota)))
sim.Must(t, "PurchaseFreeOfCharge(freeQuota)")(auction.PurchaseFreeOfCharge(sim.Acc(deployer), sim.Addr(rcvFree), big.NewInt(freeQuota)))

c := revert.Checker("Seller: Free quota exceeded")
if diff := c.Diff(auction.PurchaseFreeOfCharge(sim.Acc(deployer), sim.Acc(rcvFree).From, big.NewInt(1))); diff != "" {
if diff := c.Diff(auction.PurchaseFreeOfCharge(sim.Acc(deployer), sim.Addr(rcvFree), big.NewInt(1))); diff != "" {
t.Errorf("PurchaseFreeOfCharge(1) after exhausting quota; %s", diff)
}

wantOwned(t, auction, sim.Acc(rcvFree).From, freeQuota, freeQuota)
wantOwned(t, auction, sim.Addr(rcvFree), freeQuota, freeQuota)
})
}

Expand All @@ -1073,14 +1073,14 @@ func TestIssue7Regression(t *testing.T) {
MaxPerTx: big.NewInt(0),
}
sim.Must(t, "SetSellerConfig(%+v", cfg)(auction.SetSellerConfig(sim.Acc(deployer), cfg))
sim.Must(t, "PurchaseFreeOfCharge(freeQuota)")(auction.PurchaseFreeOfCharge(sim.Acc(deployer), sim.Acc(rcvFree).From, big.NewInt(freeQuota)))
sim.Must(t, "Buy(totalInventory-2*freeQuota)")(auction.Buy(sim.WithValueFrom(buyer, big.NewInt(totalInventory-2*freeQuota)), sim.Acc(buyer).From, big.NewInt(totalInventory-2*freeQuota)))
sim.Must(t, "PurchaseFreeOfCharge(freeQuota)")(auction.PurchaseFreeOfCharge(sim.Acc(deployer), sim.Addr(rcvFree), big.NewInt(freeQuota)))
sim.Must(t, "Buy(totalInventory-2*freeQuota)")(auction.Buy(sim.WithValueFrom(buyer, big.NewInt(totalInventory-2*freeQuota)), sim.Addr(buyer), big.NewInt(totalInventory-2*freeQuota)))

// Without the fix, this call would have reverted due to https://github.com/divergencetech/ethier/issues/7
sim.Must(t, "Buy(freeQuota)")(auction.Buy(sim.WithValueFrom(buyer, big.NewInt(freeQuota)), sim.Acc(buyer).From, big.NewInt(freeQuota)))
sim.Must(t, "Buy(freeQuota)")(auction.Buy(sim.WithValueFrom(buyer, big.NewInt(freeQuota)), sim.Addr(buyer), big.NewInt(freeQuota)))

if diff := revert.SoldOut.Diff(
auction.Buy(sim.WithValueFrom(buyer, big.NewInt(1)), sim.Acc(buyer).From, big.NewInt(1)),
auction.Buy(sim.WithValueFrom(buyer, big.NewInt(1)), sim.Addr(buyer), big.NewInt(1)),
); diff != "" {
t.Errorf("After Buy(totalInventory - freeQuota); Buy(1) %s", diff)
}
Expand All @@ -1106,31 +1106,31 @@ func TestUnreservedFreePurchasing(t *testing.T) {

t.Run("unreserved free quota ignored", func(t *testing.T) {
n := big.NewInt(totalInventory - freeQuota)
sim.Must(t, "Buy(totalInventory - freeQuota)")(auction.Buy(sim.WithValueFrom(0, n), sim.Acc(0).From, n))
sim.Must(t, "Buy(totalInventory - freeQuota)")(auction.Buy(sim.WithValueFrom(0, n), sim.Addr(0), n))
sim.Must(t, "After Buy(totalInventory - freeQuota); Buy(1)")(
auction.Buy(sim.WithValueFrom(0, big.NewInt(1)), sim.Acc(0).From, big.NewInt(1)),
auction.Buy(sim.WithValueFrom(0, big.NewInt(1)), sim.Addr(0), big.NewInt(1)),
)

wantOwned(t, auction, sim.Acc(0).From, totalInventory-freeQuota+1, 0)
wantOwned(t, auction, sim.Addr(0), totalInventory-freeQuota+1, 0)
})

t.Run("total inventory honoured even if free", func(t *testing.T) {
sim.Must(t, "PurchsaeFreeOfCharge(freeQuota-1)")(
auction.PurchaseFreeOfCharge(sim.Acc(0), sim.Acc(0).From, big.NewInt(freeQuota-1)),
auction.PurchaseFreeOfCharge(sim.Acc(0), sim.Addr(0), big.NewInt(freeQuota-1)),
)

if diff := revert.SoldOut.Diff(
auction.PurchaseFreeOfCharge(sim.Acc(0), sim.Acc(0).From, big.NewInt(1)),
auction.PurchaseFreeOfCharge(sim.Acc(0), sim.Addr(0), big.NewInt(1)),
); diff != "" {
t.Errorf("PurchaseFreeOfCharge(1) when last unreserved quota already sold; %s", diff)
}

if diff := revert.SoldOut.Diff(
auction.Buy(sim.Acc(0), sim.Acc(0).From, big.NewInt(1)),
auction.Buy(sim.Acc(0), sim.Addr(0), big.NewInt(1)),
); diff != "" {
t.Errorf("Buy(1) when last unreserved quota already sold; %s", diff)
}

wantOwned(t, auction, sim.Acc(0).From, totalInventory, freeQuota-1)
wantOwned(t, auction, sim.Addr(0), totalInventory, freeQuota-1)
})
}
4 changes: 2 additions & 2 deletions tests/utils/ownerpausable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestOwnerPausable(t *testing.T) {
if err != nil {
t.Fatalf("Owner() error %v", err)
}
if want := sim.Acc(owner).From; !cmp.Equal(got, want) {
if want := sim.Addr(owner); !cmp.Equal(got, want) {
t.Fatalf("Owner() got %s; want %s", got, want)
}

Expand Down Expand Up @@ -64,7 +64,7 @@ func TestOwnerPausable(t *testing.T) {
{
desc: "TransferOwnership()",
action: func() error {
_, err := op.TransferOwnership(sim.Acc(owner), sim.Acc(newOwner).From)
_, err := op.TransferOwnership(sim.Acc(owner), sim.Addr(newOwner))
return err
},
},
Expand Down

0 comments on commit e15afa3

Please sign in to comment.