Skip to content

Commit

Permalink
Merge pull request #113 from dappledger/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
needkane authored May 6, 2020
2 parents 3b21365 + ba46491 commit 4cf6a6c
Show file tree
Hide file tree
Showing 23 changed files with 792 additions and 134 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
env: GO111MODULE=on TEST_PLATFORM=binary TEST_GENESIS_IMAGE=$GOPATH/src/github.com/dappledger/AnnChain/build/genesis TEST_CONSENSUS_TYPE=pbft
before_script:
- make
- make image
script:
- make test
- go test ./gemmill/... -coverprofile=coverage.txt -covermode=atomic
Expand Down
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# compile environment;
FROM annchain/builder:go1.12 as builder
#copy files;
ADD . /AnnChain
WORKDIR /AnnChain
RUN GO111MODULE="on" GOPROXY="https://goproxy.io" make genesis

# package environment;
FROM annchain/runner:alpine3.11
WORKDIR /genesis
COPY --from=builder /AnnChain/build/genesis /bin/
ENTRYPOINT ["genesis"]

13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,23 @@ test-gemmill:
go test -v ./gemmill/modules/go-common
go test -v ./gemmill/modules/go-db
go test -v ./gemmill/modules/go-events
# 'go test flowrate' failed maybe your machine upgrade required.
go test -v ./gemmill/modules/go-flowrate/flowrate
go test -v ./gemmill/modules/go-log
go test -v ./gemmill/modules/go-merkle
go test -v ./gemmill/p2p
go test -v ./gemmill/refuse_list
go test -v ./gemmill/types
go test -v ./gemmill/utils

image:
docker build -t genesis:latest -f Dockerfile .
docker tag genesis:latest annchain/genesis:latest

# docker build and run
fastrun:image
docker-compose -f docker-compose.yaml up

clean_fastrun:
docker-compose -f docker-compose.yaml stop
docker-compose -f docker-compose.yaml rm
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ consensus = "raft"
#### Single node

``` shell
./build.sh genesis

./build/genesis init

./build/genesis run
Expand All @@ -130,11 +132,11 @@ consensus = "raft"
#### Local cluster using docker-compose

``` shell
## start cluster
➜ docker-compose up
# docker build image and docker-compose run
make fastrun

## remove cluster
➜ docker-compose down
# remove cluster
make clean_fastrun
```

## Usage
Expand Down
10 changes: 6 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ consensus = "raft"
#### 单节点

``` shell
./build.sh genesis

./build/genesis init

./build/genesis run
Expand All @@ -129,11 +131,11 @@ consensus = "raft"
#### 使用docker-compose的本地集群

``` shell
## start cluster
➜ docker-compose up
# docker build image and docker-compose run
make fastrun

## remove cluster
➜ docker-compose down
# remove cluster
make clean_fastrun
```

## 用法
Expand Down
74 changes: 62 additions & 12 deletions chain/app/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ type EVMApp struct {
currentHeader *etypes.Header
chainConfig *params.ChainConfig

stateDb ethdb.Database
stateMtx sync.Mutex
state *estate.StateDB
currentState *estate.StateDB
stateDb ethdb.Database
keyValueHistoryManager *KeyValueHistoryManager
stateMtx sync.Mutex
state *estate.StateDB
currentState *estate.StateDB

receipts etypes.Receipts
kvs rtypes.KVs
Signer etypes.Signer
receipts etypes.Receipts
kvs rtypes.KVs
keyValueHistories gtypes.KeyValueHistories
Signer etypes.Signer
}

type LastBlockInfo struct {
Expand Down Expand Up @@ -144,6 +146,12 @@ func NewEVMApp(config *viper.Viper) (*EVMApp, error) {
log.Error("OpenDatabase error", zap.Error(err))
return nil, errors.Wrap(err, "app error")
}
if kvdb, err := OpenDatabase(app.datadir, "kv_update_history", DatabaseCache, DatabaseHandles); err != nil {
log.Error("OpenDatabase error", zap.Error(err))
return nil, errors.Wrap(err, "app error")
} else {
app.keyValueHistoryManager = NewKeyValueHistoryManager(kvdb)
}

app.pool = NewEthTxPool(app, config)

Expand Down Expand Up @@ -220,6 +228,7 @@ func (app *EVMApp) GetTxPool() gtypes.TxPool {
func (app *EVMApp) Stop() {
app.BaseApplication.Stop()
app.stateDb.Close()
app.keyValueHistoryManager.Close()
}

func (app *EVMApp) GetAngineHooks() gtypes.Hooks {
Expand Down Expand Up @@ -286,6 +295,7 @@ func (app *EVMApp) genExecFun(block *gtypes.Block, res *gtypes.ExecuteResult) Be
stateSnapshot := state.Snapshot()
temReceipt := make([]*etypes.Receipt, 0)
temKv := make([]*rtypes.KV, 0)
tempKeyValueUpdateHistories := make([]*gtypes.KeyValueHistory, 0)

execFunc := func(txIndex int, raw []byte, tx *etypes.Transaction) error {
txType := common.Bytes2Hex(tx.Data())
Expand All @@ -295,6 +305,18 @@ func (app *EVMApp) genExecFun(block *gtypes.Block, res *gtypes.ExecuteResult) Be
return err
}
temKv = append(temKv, kv)
txBytes, _ := rlp.EncodeToBytes(tx)
history := &gtypes.KeyValueHistory{
Key: kv.Key,
ValueUpdateHistory: &gtypes.ValueUpdateHistory{
Value: kv.Value,
TxHash: gtypes.Tx(txBytes).Hash(),
BlockHeight: uint64(block.Height),
TimeStamp: uint64(block.Time.Unix()),
TxIndex:uint32(txIndex),
},
}
tempKeyValueUpdateHistories = append(tempKeyValueUpdateHistories, history)
} else {
receipt, err := app.executeOriginTx(blockHash, state, txIndex, raw, tx)
if err != nil {
Expand All @@ -317,6 +339,7 @@ func (app *EVMApp) genExecFun(block *gtypes.Block, res *gtypes.ExecuteResult) Be
}
app.receipts = append(app.receipts, temReceipt...)
app.kvs = append(app.kvs, temKv...)
app.keyValueHistories = append(app.keyValueHistories, tempKeyValueUpdateHistories...)
res.ValidTxs = append(res.ValidTxs, raw)
return true
}
Expand Down Expand Up @@ -398,7 +421,7 @@ func (app *EVMApp) GetAddressFromTx(tx *etypes.Transaction) (from common.Address
return
}

func (app *EVMApp) CheckTx(bs []byte) (from common.Address,nonce uint64, err error) {
func (app *EVMApp) CheckTx(bs []byte) (from common.Address, nonce uint64, err error) {
tx := &etypes.Transaction{}
err = rlp.DecodeBytes(bs, &tx)
if err != nil {
Expand Down Expand Up @@ -436,10 +459,6 @@ func (app *EVMApp) CheckTx(bs []byte) (from common.Address,nonce uint64, err err
err = fmt.Errorf("key or value too big,MaxKey:%v,MaxValue:%v", MaxKey, MaxValue)
return
}
if ok, _ := app.stateDb.Has(append(KvPrefix, kvData.Key...)); ok {
err = fmt.Errorf("duplicate key :%v", kvData.Key)
return
}
}
return
}
Expand Down Expand Up @@ -477,6 +496,12 @@ func (app *EVMApp) SaveReceipts() ([]byte, error) {
if err := receiptBatch.Write(); err != nil {
return nil, fmt.Errorf("persist receipts failed:%v", err.Error())
}
err := app.keyValueHistoryManager.SaveKeyHistory(app.keyValueHistories)
if err != nil {
log.Warnf("save key value history error", zap.Error(err))
}

app.keyValueHistories = nil
rHash := merkle.SimpleHashFromHashes(savedReceipts)
return rHash, nil
}
Expand Down Expand Up @@ -525,6 +550,13 @@ func (app *EVMApp) Query(query []byte) (res gtypes.Result) {
res = app.queryKeyWithPrefix(load)
case rtypes.QueryType_Pending_Nonce:
res = app.queryPendingNonce(load)
case rtypes.QueryType_Key_Update_History:
if len(load) < (PageNumLen + PageSizeLen+1) {
return gtypes.NewError(gtypes.CodeType_BaseInvalidInput, "wrong pageNo and pageSize")
}
pageNo := binary.BigEndian.Uint32(load[:PageNumLen])
pageSize := binary.BigEndian.Uint32(load[PageNumLen:PageNumLen+PageSizeLen])
res = app.queryKeyUpdateHistory(load[PageNumLen+PageSizeLen:], pageNo, pageSize)
default:
res = gtypes.NewError(gtypes.CodeType_BaseInvalidInput, "unimplemented query")
}
Expand Down Expand Up @@ -733,6 +765,24 @@ func (app *EVMApp) queryKeyWithPrefix(load []byte) gtypes.Result {
return gtypes.NewResultOK(bytKvs, "")
}

func (app *EVMApp) queryKeyUpdateHistory(key []byte, pageNo uint32, pageSize uint32) gtypes.Result {

kvs, total, err := app.keyValueHistoryManager.Query(key, pageNo, pageSize)
if err != nil {
return gtypes.NewError(gtypes.CodeType_InternalError, fmt.Sprintf("fail to get value history %s %v:",string(key),err))
}
result := gtypes.ValueHistoryResult{
Key: key,
ValueUpdateHistories: kvs,
Total: total,
}
byteKvs, err := rlp.EncodeToBytes(result)
if err != nil {
return gtypes.NewError(gtypes.CodeType_WrongRLP, "rlp encode error:"+err.Error())
}
return gtypes.NewResultOK(byteKvs, "")
}

func (app *EVMApp) SetCore(core gtypes.Core) {
app.core = core
}
Loading

0 comments on commit 4cf6a6c

Please sign in to comment.