bitxhub/pkg/vm/boltvm/bolt_stub.go

151 lines
3.2 KiB
Go
Raw Normal View History

2020-03-29 21:32:01 +08:00
package boltvm
import (
"encoding/json"
"github.com/meshplus/bitxhub-core/boltvm"
"github.com/meshplus/bitxhub-core/validator"
2020-03-29 21:32:01 +08:00
"github.com/meshplus/bitxhub-kit/types"
"github.com/meshplus/bitxhub-model/pb"
"github.com/meshplus/bitxhub/pkg/vm"
"github.com/sirupsen/logrus"
)
var _ boltvm.Stub = (*BoltStubImpl)(nil)
2020-03-29 21:32:01 +08:00
type BoltStubImpl struct {
bvm *BoltVM
ctx *vm.Context
ve validator.Engine
2020-03-29 21:32:01 +08:00
}
func (b *BoltStubImpl) Caller() string {
2020-10-21 23:46:21 +08:00
return b.ctx.Caller.String()
2020-03-29 21:32:01 +08:00
}
func (b *BoltStubImpl) Callee() string {
2020-10-21 23:46:21 +08:00
return b.ctx.Callee.String()
2020-03-29 21:32:01 +08:00
}
func (b *BoltStubImpl) Logger() logrus.FieldLogger {
return b.ctx.Logger
}
// GetTxHash returns the transaction hash
2020-10-21 23:46:21 +08:00
func (b *BoltStubImpl) GetTxHash() *types.Hash {
2020-03-29 21:32:01 +08:00
hash := b.ctx.TransactionHash
return hash
}
func (b *BoltStubImpl) GetTxIndex() uint64 {
return b.ctx.TransactionIndex
}
func (b *BoltStubImpl) Has(key string) bool {
exist, _ := b.ctx.Ledger.GetState(b.ctx.Callee, []byte(key))
return exist
}
func (b *BoltStubImpl) Get(key string) (bool, []byte) {
return b.ctx.Ledger.GetState(b.ctx.Callee, []byte(key))
}
func (b *BoltStubImpl) Delete(key string) {
2021-02-02 15:55:33 +08:00
b.ctx.Ledger.SetState(b.ctx.Callee, []byte(key), nil)
2020-03-29 21:32:01 +08:00
}
func (b *BoltStubImpl) GetObject(key string, ret interface{}) bool {
ok, data := b.Get(key)
if !ok {
return ok
}
err := json.Unmarshal(data, ret)
return err == nil
}
func (b *BoltStubImpl) Set(key string, value []byte) {
b.ctx.Ledger.SetState(b.ctx.Callee, []byte(key), value)
}
2020-09-29 15:13:05 +08:00
func (b *BoltStubImpl) Add(key string, value []byte) {
b.ctx.Ledger.AddState(b.ctx.Callee, []byte(key), value)
}
2020-03-29 21:32:01 +08:00
func (b *BoltStubImpl) SetObject(key string, value interface{}) {
data, err := json.Marshal(value)
if err != nil {
panic(err)
}
b.Set(key, data)
}
2020-09-29 15:13:05 +08:00
func (b *BoltStubImpl) AddObject(key string, value interface{}) {
data, err := json.Marshal(value)
if err != nil {
panic(err)
}
b.Add(key, data)
}
2020-03-29 21:32:01 +08:00
func (b *BoltStubImpl) Query(prefix string) (bool, [][]byte) {
return b.ctx.Ledger.QueryByPrefix(b.ctx.Callee, prefix)
}
func (b *BoltStubImpl) PostEvent(event interface{}) {
b.postEvent(false, event)
}
func (b *BoltStubImpl) PostInterchainEvent(event interface{}) {
b.postEvent(true, event)
}
func (b *BoltStubImpl) postEvent(interchain bool, event interface{}) {
data, err := json.Marshal(event)
if err != nil {
panic(err)
}
b.ctx.Ledger.AddEvent(&pb.Event{
Interchain: interchain,
Data: data,
TxHash: b.GetTxHash(),
})
}
func (b *BoltStubImpl) CrossInvoke(address, method string, args ...*pb.Arg) *boltvm.Response {
2020-10-21 23:46:21 +08:00
addr := types.NewAddressByStr(address)
2020-03-29 21:32:01 +08:00
payload := &pb.InvokePayload{
Method: method,
Args: args,
}
ctx := &vm.Context{
Caller: b.bvm.ctx.Caller,
2020-10-22 13:49:05 +08:00
Callee: addr,
2020-03-29 21:32:01 +08:00
Ledger: b.bvm.ctx.Ledger,
TransactionIndex: b.bvm.ctx.TransactionIndex,
TransactionHash: b.bvm.ctx.TransactionHash,
Logger: b.bvm.ctx.Logger,
}
data, err := payload.Marshal()
if err != nil {
return boltvm.Error(err.Error())
2020-03-29 21:32:01 +08:00
}
2020-04-29 15:03:31 +08:00
bvm := New(ctx, b.ve, b.bvm.contracts)
2020-03-29 21:32:01 +08:00
ret, err := bvm.Run(data)
if err != nil {
return boltvm.Error(err.Error())
2020-03-29 21:32:01 +08:00
}
return boltvm.Success(ret)
2020-03-29 21:32:01 +08:00
}
func (b *BoltStubImpl) ValidationEngine() validator.Engine {
return b.ve
2020-03-29 21:32:01 +08:00
}