Skip to content

Commit

Permalink
Fix for Core v2.1.x (#3)
Browse files Browse the repository at this point in the history
* dep: 2.1.x dependency
* fix Dockerfile and AppHash issue
---------

Co-authored-by: nghuyenthevinh2000 <nghuyenthevinh@gmail.com>
  • Loading branch information
inon-man and nghuyenthevinh2000 authored Sep 8, 2023
1 parent 431a214 commit 111d8e0
Show file tree
Hide file tree
Showing 42 changed files with 294 additions and 236 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## Unreleased
This release contains a dependency upgrade for [classic-terra/core](https://github.com/classic-terra/core)@v2.1.2 with AppHash fix caused by legacy wasm codec

## [v2.1.1](https://github.com/classic-terra/mantlemint/tree/v2.1.1) (2023-06-11)
This release contains a dependency upgrade for [classic-terra/core](https://github.com/classic-terra/core)@v2.1.1

## [v2.0.2](https://github.com/classic-terra/mantlemint/tree/v2.1.1) (2023-05-12)
This release contains a dependency upgrade for [classic-terra/core](https://github.com/classic-terra/core)@v2.0.2

## [v1.1.0]
This release contains a dependency upgrade for [classic-terra/core](https://github.com/classic-terra/core)@v1.1.0

## [v1.0.5]
This release contains a dependency upgrade for [classic-terra/core](https://github.com/classic-terra/core)@v1.0.5

## [v0.1.2](https://github.com/terra-money/mantlemint/tree/v0.1.2) (2022-04-14)

This release contains a depencency upgrade for [core](https://github.com/terra-money/core)@0.5.17...@0.5.18. Apart from the dependency bump all functionalities should be exactly the same as v0.1.1.
Expand Down
66 changes: 50 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,63 @@
# docker build . -t cosmwasm/wasmd:latest
# docker run --rm -it cosmwasm/wasmd:latest /bin/sh
FROM golang:1.17-alpine3.14 AS go-builder
FROM golang:1.18-alpine3.17 AS go-builder
ARG BUILDPLATFORM=linux/amd64

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
# with some changes to support our toolchain, etc
RUN set -eux; apk add --no-cache ca-certificates build-base;
# NOTE: add libusb-dev to run with LEDGER_ENABLED=true
RUN set -eux &&\
apk update &&\
apk add --no-cache \
ca-certificates \
linux-headers \
build-base \
cmake \
git

RUN apk add git build-base cmake linux-headers
# NOTE: add these to run with LEDGER_ENABLED=true
# RUN apk add libusb-dev linux-headers
# use mimalloc for musl
WORKDIR ${GOPATH}/src/mimalloc
RUN set -eux &&\
git clone --depth 1 \
https://github.com/microsoft/mimalloc . &&\
mkdir -p build &&\
cd build &&\
cmake .. &&\
make -j$(nproc) &&\
make install

WORKDIR /code
COPY . /code/

# See https://github.com/CosmWasm/wasmvm/releases
ADD https://github.com/cosmwasm/wasmvm/releases/download/v0.16.6/libwasmvm_muslc.x86_64.a /lib/libwasmvm_muslc.a

# use mimalloc for musl
RUN git clone --depth 1 https://github.com/microsoft/mimalloc; cd mimalloc; mkdir build; cd build; cmake ..; make -j$(nproc); make install
# Cosmwasm - Download correct libwasmvm version and verify checksum
RUN set -eux &&\
WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm | cut -d ' ' -f 2) && \
WASMVM_DOWNLOADS="https://github.com/CosmWasm/wasmvm/releases/download/${WASMVM_VERSION}"; \
wget ${WASMVM_DOWNLOADS}/checksums.txt -O /tmp/checksums.txt; \
if [ ${BUILDPLATFORM} = "linux/amd64" ]; then \
WASMVM_URL="${WASMVM_DOWNLOADS}/libwasmvm_muslc.x86_64.a"; \
elif [ ${BUILDPLATFORM} = "linux/arm64" ]; then \
WASMVM_URL="${WASMVM_DOWNLOADS}/libwasmvm_muslc.aarch64.a"; \
else \
echo "Unsupported Build Platfrom ${BUILDPLATFORM}"; \
exit 1; \
fi; \
wget ${WASMVM_URL} -O /lib/libwasmvm_muslc.a; \
CHECKSUM=`sha256sum /lib/libwasmvm_muslc.a | cut -d" " -f1`; \
grep ${CHECKSUM} /tmp/checksums.txt; \
rm /tmp/checksums.txt

# force it to use static lib (from above) not standard libgo_cosmwasm.so file
RUN LEDGER_ENABLED=false go build -work -tags muslc,linux -mod=readonly -ldflags="-extldflags '-L/code/mimalloc/build -lmimalloc -static'" -o build/mantlemint ./sync.go
RUN LEDGER_ENABLED=false \
go build \
-mod=readonly \
-tags "muslc,linux" \
-ldflags " \
-w -s -linkmode=external -extldflags \
'-L/go/src/mimalloc/build -lmimalloc -Wl,-z,muldefs -static' \
" \
-trimpath \
-o build/mantlemint ./sync.go

FROM alpine:3.12
FROM alpine:3.17

WORKDIR /root

Expand All @@ -34,4 +68,4 @@ EXPOSE 1317
# grpc
EXPOSE 9090

CMD ["/usr/local/bin/mantlemint"]
CMD ["/usr/local/bin/mantlemint"]
17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/make -f

BUILDDIR=build
BUILDDIR ?= $(CURDIR)/build

build: go.sum
ifeq ($(OS),Windows_NT)
Expand All @@ -9,6 +9,14 @@ else
go build -mod=readonly $(BUILD_FLAGS) -o build/mantlemint ./sync.go
endif

lint:
golangci-lint run --out-format=tab

lint-fix:
golangci-lint run --fix --out-format=tab --issues-exit-code=0

lint-strict:
find . -path './_build' -prune -o -type f -name '*.go' -exec gofumpt -w -l {} +

build-static:
mkdir -p $(BUILDDIR)
Expand All @@ -19,3 +27,10 @@ build-static:

install: go.sum
go install -mod=readonly $(BUILD_FLAGS) ./

go.sum: go.mod
@echo "--> Ensure dependencies have not been modified"
@go mod verify

clean:
rm -rf $(BUILDDIR)/
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# terra-money/mantlemint
# classic-terra/mantlemint


## What is Mantlemint?
Expand All @@ -9,8 +9,8 @@ Native query performance on RPC is slow and is not suitable for massive query ha

If you are looking to serve any kind of public node accepting varying degrees of end-user queries, it is recommended that you run a mantlemint instance alongside of your RPC. While mantlemint is indeed faster at resolving queries, due to the absence of IAVL tree and native tendermint, it cannot join p2p network by itself. Rather, you would have to relay finalized blocks to mantlemint, using RPC's websocket.

## Currently supported terra-money/core versions
- columbus-5 | terra-money/core@0.5.x | tendermint v0.34.x
## Currently supported classic-terra/core versions
- columbus-5 | classic-terra/core@2.1.x | tendermint v0.34.x


## Features
Expand All @@ -24,7 +24,7 @@ If you are looking to serve any kind of public node accepting varying degrees of

## Installation

This specific directory contains mantlemint implementation for [@terra-money/core@0.5.x](https://github.com/terra-money/core) (compatible with [tendermint@0.34.x](https://github.com/tendermint/tendermint)).
This specific directory contains mantlemint implementation for [@classic-terra/core@2.1.x](https://github.com/terra-money/core) (compatible with [tendermint@0.34.x](https://github.com/tendermint/tendermint)).

Go v1.17+ is recommended for this project.

Expand All @@ -47,7 +47,7 @@ $ make install # results in $GOPATH/bin/mantlemint

Since mantlemint cannot join p2p network by itself, it depends on RPC to receive recently proposed blocks.

Any [Terra node](https://github.com/terra-money/core) with port 26657 enabled can be used for this.
Any [Terra node](https://github.com/classic-terra/core) with port 26657 enabled can be used for this.

#### 2. `config/app.toml`, a genesis file

Expand Down Expand Up @@ -94,12 +94,12 @@ INDEXER_DB=indexer \
# Flag to enable/disable mantlemint sync, mainly for debugging
DISABLE_SYNC=false \

# Run sync binary
sync
# Run mantlemint binary
mantlemint

# Optional: crisis module's invariant check is known to take hours.
# You can skip it by providing --x-crisis-skip-assert-invariants flag
sync --x-crisis-skip-assert-invariants
mantlemint --x-crisis-skip-assert-invariants
```

### Adjusting smart contract memory cache size
Expand Down Expand Up @@ -138,14 +138,14 @@ Please note that mantlemint still is able to serve queries while `/health` retur
- `/index/tx/by_height/{height}`: List all transactions and their responses in a block. Equivalent to `tendermint/block?height=xxx`, with tx responses base64-decoded for better usability.
- `/index/tx/by_hash/{txHash}`: Get transaction and its response by hash. Equivalent to `lcd/txs/{hash}`, but without hitting RPC.

## Notable Differences from [core](https://github.com/terra-money/core)
## Notable Differences from [core](https://github.com/classic-terra/core)

- Uses a forked [tendermint/tm-db](https://github.com/terra-money/tm-db/commit/c71e8b6e9f20d7f5be32527db4a92ae19ac0d2b2): Disables unncessary mutexes in `prefixdb` methods
- Replaces ABCIClient with [NewConcurrentQueryClient](https://github.com/terra-money/mantlemint/blob/main/mantlemint/client.go#L110): Removal of mutexes allow better concurrency, even during block injection
- Uses single batch-protected db: All state changes are flushed at once, making it safe to read from db during block injection
- Automatic failover: In case of block injection failure, mantlemint reverts back to the previous known state and retry
- Strictly no `tendermint`; some parameters in app.toml would not affect `mantlemint`
- Following endpoints are not implemented
- Following endpoints are not implemented
- `GET /blocks/`
- `GET /blocks/latest`
- `GET /txs/{hash}`
Expand All @@ -156,7 +156,7 @@ Please note that mantlemint still is able to serve queries while `/health` retur

## FAQ

### Q1. Can I use public RPC (http://public-node.terra.dev:26657) as RPC and WS endpoints?
### Q1. Can I use public RPC and WS endpoints?

While you can, we do NOT recommend doing so. We only expose public node as a seed node for p2p, and its http/ws connection may not be stable. It is safer to have your own RPC

Expand Down
4 changes: 2 additions & 2 deletions block_feed/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func NewAggregateBlockFeed(
rpcEndpoints []string,
wsEndpoints []string,
) *AggregateSubscription {
var rpc, rpcErr = NewRpcSubscription(rpcEndpoints)
rpc, rpcErr := NewRpcSubscription(rpcEndpoints)
if rpcErr != nil {
panic(rpcErr)
}

// ws starts with 1st occurrence of ws endpoints
var ws, wsErr = NewWSSubscription(wsEndpoints)
ws, wsErr := NewWSSubscription(wsEndpoints)
if wsErr != nil {
panic(wsErr)
}
Expand Down
10 changes: 5 additions & 5 deletions block_feed/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package block_feed

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
)
Expand All @@ -11,18 +11,18 @@ var _ BlockFeed = (*RPCSubscription)(nil)

type RPCSubscription struct {
rpcEndpoints []string
cSub chan *BlockResult
cSub chan *BlockResult
}

func NewRpcSubscription(rpcEndpoints []string) (*RPCSubscription, error) {
return &RPCSubscription{
rpcEndpoints: rpcEndpoints,
cSub: make(chan *BlockResult),
cSub: make(chan *BlockResult),
}, nil
}

func (rpc *RPCSubscription) SyncFromUntil(from int64, to int64, rpcIndex int) {
var cSub = rpc.cSub
cSub := rpc.cSub

log.Printf("[block_feed/rpc] subscription started, from=%d, to=%d\n", from, to)

Expand All @@ -35,7 +35,7 @@ func (rpc *RPCSubscription) SyncFromUntil(from int64, to int64, rpcIndex int) {
log.Fatalf("block request failed, %v", err)
}

resBytes, err := ioutil.ReadAll(res.Body)
resBytes, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("block request failed, %v", err)
}
Expand Down
8 changes: 3 additions & 5 deletions block_feed/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package block_feed

import (
"encoding/json"
"github.com/gorilla/websocket"
"log"

"github.com/gorilla/websocket"
)

var _ BlockFeed = (*WSSubscription)(nil)
Expand Down Expand Up @@ -34,15 +35,14 @@ func NewWSSubscription(wsEndpoints []string) (*WSSubscription, error) {

func (ws *WSSubscription) Subscribe(rpcIndex int) (chan *BlockResult, error) {
socket, _, err := websocket.DefaultDialer.Dial(ws.wsEndpoints[rpcIndex], nil)

// return err, handle failures gracefully
if err != nil {
return nil, err
}

ws.ws = socket

var request = &handshake{
request := &handshake{
JSONRPC: "2.0",
Method: "subscribe",
ID: 0,
Expand Down Expand Up @@ -85,7 +85,6 @@ func (ws *WSSubscription) Close() error {
// "data" field present
func handleInitialHandhake(ws *websocket.Conn) error {
_, _, err := ws.ReadMessage()

if err != nil {
return err
}
Expand All @@ -98,7 +97,6 @@ func receiveBlockEvents(ws *websocket.Conn, c chan *BlockResult) {
defer close(c)
for {
_, message, err := ws.ReadMessage()

// if read message failed,
// scrap the whole ws thing
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions db/heleveldb/leveldb_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/terra-money/mantlemint/db/rollbackable"
)

var _ hld.HeightLimitEnabledBatch = (*LevelBatch)(nil)
var _ rollbackable.HasRollbackBatch = (*LevelBatch)(nil)
var (
_ hld.HeightLimitEnabledBatch = (*LevelBatch)(nil)
_ rollbackable.HasRollbackBatch = (*LevelBatch)(nil)
)

type LevelBatch struct {
height int64
Expand Down
8 changes: 4 additions & 4 deletions db/heleveldb/leveldb_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (d *Driver) Get(maxHeight int64, key []byte) ([]byte, error) {
if maxHeight == 0 {
return d.session.Get(prefixCurrentDataKey(key))
}
var requestHeight = hld.Height(maxHeight).CurrentOrLatest().ToInt64()
var requestHeightMin = hld.Height(0).CurrentOrNever().ToInt64()
requestHeight := hld.Height(maxHeight).CurrentOrLatest().ToInt64()
requestHeightMin := hld.Height(0).CurrentOrNever().ToInt64()

// check if requestHeightMin is
if requestHeightMin > requestHeight {
Expand Down Expand Up @@ -74,8 +74,8 @@ func (d *Driver) Has(maxHeight int64, key []byte) (bool, error) {
if maxHeight == 0 {
return d.session.Has(prefixCurrentDataKey(key))
}
var requestHeight = hld.Height(maxHeight).CurrentOrLatest().ToInt64()
var requestHeightMin = hld.Height(0).CurrentOrNever().ToInt64()
requestHeight := hld.Height(maxHeight).CurrentOrLatest().ToInt64()
requestHeightMin := hld.Height(0).CurrentOrNever().ToInt64()

// check if requestHeightMin is
if requestHeightMin > requestHeight {
Expand Down
2 changes: 1 addition & 1 deletion db/heleveldb/leveldb_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewLevelDBIterator(d *Driver, maxHeight int64, start, end []byte) (*Iterato
end: end,
}, nil
}

func NewLevelDBReverseIterator(d *Driver, maxHeight int64, start, end []byte) (*Iterator, error) {
pdb := tmdb.NewPrefixDB(d.session, cKeysForIteratorPrefix)
iter, err := pdb.ReverseIterator(start, end)
Expand Down Expand Up @@ -77,7 +78,6 @@ func (i *Iterator) Valid() bool {
}
}
return false

}

func (i *Iterator) Value() (value []byte) {
Expand Down
Loading

0 comments on commit 111d8e0

Please sign in to comment.