Skip to content

Commit

Permalink
feat: adding more tests and refactoring thw dockerfile to link static…
Browse files Browse the repository at this point in the history
…aly teh rust lib with golang
  • Loading branch information
henriquemarlon committed Oct 13, 2024
1 parent 0793945 commit e4f1c5f
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 464 deletions.
4 changes: 2 additions & 2 deletions build/Dockerfile.machine
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ARG APP_NAME
WORKDIR /app

# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev libressl-dev pkgconfig git
RUN apk add --no-cache clang lld libressl-dev pkgconfig

RUN rustup target add riscv64gc-unknown-linux-gnu

Expand All @@ -44,7 +44,7 @@ ARG APP_NAME
WORKDIR /src

# Install Go dependencies
RUN apt-get update && apt-get install -y gcc musl-dev libstdc++6 g++-riscv64-linux-gnu
RUN apt-get update && apt-get install -y gcc g++-riscv64-linux-gnu

COPY --from=rust_build /usr/lib/lib${APP_NAME}.a /usr/lib/

Expand Down
12 changes: 3 additions & 9 deletions cmd/dapp/dapp.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package main

/*
#cgo LDFLAGS: -L./ -lverifier
#cgo CFLAGS: -I./include
#include <stdint.h>
int32_t add_numbers(int32_t a, int32_t b);
*/
import (
"C"
"log"

"github.com/tribeshq/tribes/configs"
"github.com/tribeshq/tribes/pkg/router"
)
Expand Down Expand Up @@ -52,6 +44,7 @@ func NewDApp() *router.Router {
app.HandleAdvance("finishAuction", ah.AuctionAdvanceHandlers.FinishAuctionHandler)

app.HandleAdvance("withdraw", ah.UserAdvanceHandlers.WithdrawHandler)
app.HandleAdvance("withdrawApp", ms.RBAC.Middleware(ah.UserAdvanceHandlers.WithdrawAppHandler, "admin"))

app.HandleAdvance("createUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.CreateUserHandler, "admin"))
app.HandleAdvance("deleteUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.DeleteUserByAddressHandler, "admin"))
Expand Down Expand Up @@ -111,6 +104,7 @@ func NewDAppMemory() *router.Router {
app.HandleAdvance("finishAuction", ah.AuctionAdvanceHandlers.FinishAuctionHandler)

app.HandleAdvance("withdraw", ah.UserAdvanceHandlers.WithdrawHandler)
app.HandleAdvance("withdrawApp", ms.RBAC.Middleware(ah.UserAdvanceHandlers.WithdrawAppHandler, "admin"))

app.HandleAdvance("createUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.CreateUserHandler, "admin"))
app.HandleAdvance("deleteUser", ms.RBAC.Middleware(ah.UserAdvanceHandlers.DeleteUserByAddressHandler, "admin"))
Expand Down
44 changes: 29 additions & 15 deletions cmd/dapp/dapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ func (s *AppSuite) SetupTest() {
s.tester = rollmelette.NewTester(app)
}

// ////////////// User ///////////////////

func (s *AppSuite) TestItCreateUser() {
admin := common.HexToAddress("0x0142f501EE21f4446009C3505c51d0043feC5c68")
input := []byte(`{"path":"createUser","payload":{"address":"0x70997970C51812dc3A010C7d01b50e0d17dc79C8","role":"admin","username":"vitalik"}}`)
expectedOutput := fmt.Sprintf(`user created - {"id":3,"role":"admin","username":"vitalik","address":"0x70997970C51812dc3A010C7d01b50e0d17dc79C8","created_at":%d}`, time.Now().Unix())
result := s.tester.Advance(admin, input)
s.Len(result.Notices, 1)
s.Equal(expectedOutput, string(result.Notices[0].Payload))
}

////////////// Auction //////////////////

func (s *AppSuite) TestItCreateAuctionAndFinishAuctionWithoutPartialSellingAndPayingAllBidder() {
admin := common.HexToAddress("0x0142f501EE21f4446009C3505c51d0043feC5c68")
creator := common.HexToAddress("0x0000000000000000000000000000000000000007")
Expand All @@ -64,7 +51,7 @@ func (s *AppSuite) TestItCreateAuctionAndFinishAuctionWithoutPartialSellingAndPa

createAuctionInput := []byte(fmt.Sprintf(`{"path":"createAuction","payload":{"max_interest_rate":"10","expires_at":%d,"debt_issued":%d}}`, time.Now().Add(5*time.Second).Unix(), 2020))
result = s.tester.Advance(creator, createAuctionInput)
expectedOutput = fmt.Sprintf(`created auction - {"id":1,"creator":"0x0000000000000000000000000000000000000007","debt_issued":"2020","max_interest_rate":"10","state":"ongoing","expires_at":%d,"created_at":%d}`, time.Now().Add(5*time.Second).Unix(), time.Now().Unix())
expectedOutput = fmt.Sprintf(`auction created - {"id":1,"creator":"0x0000000000000000000000000000000000000007","debt_issued":"2020","max_interest_rate":"10","state":"ongoing","expires_at":%d,"created_at":%d}`, time.Now().Add(5*time.Second).Unix(), time.Now().Unix())
s.Len(result.Notices, 1)
s.Equal(expectedOutput, string(result.Notices[0].Payload))

Expand Down Expand Up @@ -102,8 +89,35 @@ func (s *AppSuite) TestItCreateAuctionAndFinishAuctionWithoutPartialSellingAndPa

finishAuctionInput := []byte(`{"path":"finishAuction"}`)
result = s.tester.Advance(admin, finishAuctionInput)
expectedOutput = `finished auction with - id: 1, required amount: 2020 and max interest rate: 10`
expectedOutput = fmt.Sprintf(`auction finished - {"id":1,"creator":"0x0000000000000000000000000000000000000007","debt_issued":"2020","max_interest_rate":"10","state":"finished","bids":[{"id":3,"auction_id":1,"bidder":"0x0000000000000000000000000000000000000003","amount":"200","interest_rate":"4","state":"accepted","created_at":%d,"updated_at":%d},{"id":5,"auction_id":1,"bidder":"0x0000000000000000000000000000000000000005","amount":"400","interest_rate":"4","state":"accepted","created_at":%d,"updated_at":%d},{"id":4,"auction_id":1,"bidder":"0x0000000000000000000000000000000000000004","amount":"300","interest_rate":"6","state":"accepted","created_at":%d,"updated_at":%d},{"id":2,"auction_id":1,"bidder":"0x0000000000000000000000000000000000000002","amount":"520","interest_rate":"8","state":"accepted","created_at":%d,"updated_at":%d},{"id":1,"auction_id":1,"bidder":"0x0000000000000000000000000000000000000001","amount":"600","interest_rate":"9","state":"accepted","created_at":%d,"updated_at":%d}],"expires_at":%d,"created_at":%d,"updated_at":%d}`, time.Now().Add(-5 * time.Second).Unix(), time.Now().Unix(), time.Now().Add(-5 * time.Second).Unix(), time.Now().Unix(), time.Now().Add(-5 * time.Second).Unix(), time.Now().Unix(),time.Now().Add(-5 * time.Second).Unix(), time.Now().Unix(),time.Now().Add(-5 * time.Second).Unix(), time.Now().Unix(), time.Now().Unix(), time.Now().Add(-5 * time.Second).Unix(), time.Now().Unix())
s.Len(result.Notices, 1)
s.Equal(expectedOutput, string(result.Notices[0].Payload))

creatorWithdrawInput := []byte(`{"path":"withdraw"}`)
expectedNoticePayload := `withdrawn STABLECOIN of 1919 from 0x0000000000000000000000000000000000000007 with voucher index: 1`
expectedWithdrawVoucherPayload := make([]byte, 0, 4+32+32)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, 0xa9, 0x05, 0x9c, 0xbb)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, make([]byte, 12)...)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, creator[:]...)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, big.NewInt(1919).FillBytes(make([]byte, 32))...)
withdrawResult := s.tester.Advance(creator, creatorWithdrawInput)
s.Len(withdrawResult.Notices, 1)
s.Len(withdrawResult.Vouchers, 1)
s.Equal(expectedWithdrawVoucherPayload, withdrawResult.Vouchers[0].Payload)
s.Equal(common.HexToAddress("0x0000000000000000000000000000000000000009"), withdrawResult.Vouchers[0].Destination)
s.Equal(expectedNoticePayload, string(withdrawResult.Notices[0].Payload))

tribesProfitWithdrawInput := []byte(`{"path":"withdrawApp"}`)
expectedNoticePayload = `withdrawn STABLECOIN of 101 from 0x0142f501EE21f4446009C3505c51d0043feC5c68 with voucher index: 1`
expectedWithdrawVoucherPayload = make([]byte, 0, 4+32+32)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, 0xa9, 0x05, 0x9c, 0xbb)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, make([]byte, 12)...)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, admin[:]...)
expectedWithdrawVoucherPayload = append(expectedWithdrawVoucherPayload, big.NewInt(101).FillBytes(make([]byte, 32))...)
withdrawResult = s.tester.Advance(admin, tribesProfitWithdrawInput)
s.Len(withdrawResult.Notices, 1)
s.Len(withdrawResult.Vouchers, 1)
s.Equal(expectedWithdrawVoucherPayload, withdrawResult.Vouchers[0].Payload)
s.Equal(common.HexToAddress("0x0000000000000000000000000000000000000009"), withdrawResult.Vouchers[0].Destination)
s.Equal(expectedNoticePayload, string(withdrawResult.Notices[0].Payload))
}
4 changes: 4 additions & 0 deletions cmd/dapp/wire_gen.go

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

Loading

0 comments on commit e4f1c5f

Please sign in to comment.