Dual impl
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
8e4661e6d2
commit
9577348422
@ -319,7 +319,7 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err
|
||||
return err
|
||||
}
|
||||
|
||||
receipts = append(receipts, receipt)
|
||||
receipts = append(receipts, receipt.MessageReceipt)
|
||||
}
|
||||
|
||||
cst := hamt.CSTFromBstore(syncer.store.Blockstore())
|
||||
|
@ -92,6 +92,8 @@ func (vmc *VMContext) Origin() address.Address {
|
||||
return vmc.origin
|
||||
}
|
||||
|
||||
var oldSend = false
|
||||
|
||||
// Send allows the current execution context to invoke methods on other actors in the system
|
||||
func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt, params []byte) ([]byte, aerrors.ActorError) {
|
||||
ctx, span := trace.StartSpan(vmc.ctx, "vm.send")
|
||||
@ -111,6 +113,7 @@ func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt
|
||||
Value: value,
|
||||
Params: params,
|
||||
}
|
||||
if oldSend {
|
||||
|
||||
toAct, err := vmc.state.GetActor(to)
|
||||
if err != nil {
|
||||
@ -127,6 +130,10 @@ func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt
|
||||
toAct.Head = nvmctx.Storage().GetHead()
|
||||
|
||||
return res, nil
|
||||
} else {
|
||||
ret, err, _ := vmc.vm.send(ctx, vmc.origin, msg)
|
||||
return ret, err
|
||||
}
|
||||
}
|
||||
|
||||
// BlockHeight returns the height of the block this message was added to the chain in
|
||||
@ -204,6 +211,40 @@ type ApplyRet struct {
|
||||
ActorErr aerrors.ActorError
|
||||
}
|
||||
|
||||
func (vm *VM) send(ctx context.Context, origin address.Address, msg *types.Message) ([]byte, aerrors.ActorError, *VMContext) {
|
||||
st := vm.cstate
|
||||
fromActor, err := st.GetActor(msg.From)
|
||||
if err != nil {
|
||||
return nil, aerrors.Absorb(err, 1, "could not find source actor"), nil
|
||||
}
|
||||
|
||||
toActor, err := st.GetActor(msg.To)
|
||||
if err != nil {
|
||||
if xerrors.Is(err, types.ErrActorNotFound) {
|
||||
a, err := TryCreateAccountActor(st, msg.To)
|
||||
if err != nil {
|
||||
return nil, aerrors.Absorb(err, 1, "could not create account"), nil
|
||||
}
|
||||
toActor = a
|
||||
} else {
|
||||
return nil, aerrors.Escalate(err, "getting actor"), nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := DeductFunds(fromActor, msg.Value); err != nil {
|
||||
return nil, aerrors.Absorb(err, 1, "failed to deduct funds"), nil
|
||||
}
|
||||
DepositFunds(toActor, msg.Value)
|
||||
vmctx := vm.makeVMContext(ctx, toActor.Head, origin, msg)
|
||||
if msg.Method != 0 {
|
||||
ret, err := vm.Invoke(toActor, vmctx, msg.Method, msg.Params)
|
||||
toActor.Head = vmctx.Storage().GetHead()
|
||||
return ret, err, vmctx
|
||||
}
|
||||
|
||||
return nil, nil, vmctx
|
||||
}
|
||||
|
||||
func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "vm.ApplyMessage")
|
||||
defer span.End()
|
||||
@ -218,6 +259,7 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
|
||||
return nil, xerrors.Errorf("from actor not found: %w", err)
|
||||
}
|
||||
|
||||
if oldSend {
|
||||
gascost := types.BigMul(msg.GasLimit, msg.GasPrice)
|
||||
totalCost := types.BigAdd(gascost, msg.Value)
|
||||
if types.BigCmp(fromActor.Balance, totalCost) < 0 {
|
||||
@ -297,6 +339,51 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
|
||||
},
|
||||
ActorErr: actorError,
|
||||
}, nil
|
||||
} else {
|
||||
gascost := types.BigMul(msg.GasLimit, msg.GasPrice)
|
||||
totalCost := types.BigAdd(gascost, msg.Value)
|
||||
if types.BigCmp(fromActor.Balance, totalCost) < 0 {
|
||||
return nil, xerrors.Errorf("not enough funds")
|
||||
}
|
||||
if err := DeductFunds(fromActor, gascost); err != nil {
|
||||
return nil, xerrors.Errorf("failed to deduct funds: %w", err)
|
||||
}
|
||||
|
||||
if msg.Nonce != fromActor.Nonce {
|
||||
return nil, xerrors.Errorf("invalid nonce (got %d, expected %d)", msg.Nonce, fromActor.Nonce)
|
||||
}
|
||||
fromActor.Nonce++
|
||||
ret, actorErr, vmctx := vm.send(ctx, msg.From, msg)
|
||||
|
||||
var errcode uint8
|
||||
if errcode = aerrors.RetCode(actorErr); errcode != 0 {
|
||||
// revert all state changes since snapshot
|
||||
if err := st.Revert(); err != nil {
|
||||
return nil, xerrors.Errorf("revert state failed: %w", err)
|
||||
}
|
||||
} else {
|
||||
// refund unused gas
|
||||
refund := types.BigMul(types.BigSub(msg.GasLimit, vmctx.GasUsed()), msg.GasPrice)
|
||||
DepositFunds(fromActor, refund)
|
||||
}
|
||||
|
||||
miner, err := st.GetActor(vm.blockMiner)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("getting block miner actor (%s) failed: %w", vm.blockMiner, err)
|
||||
}
|
||||
|
||||
gasReward := types.BigMul(msg.GasPrice, vmctx.GasUsed())
|
||||
DepositFunds(miner, gasReward)
|
||||
|
||||
return &ApplyRet{
|
||||
MessageReceipt: types.MessageReceipt{
|
||||
ExitCode: errcode,
|
||||
Return: ret,
|
||||
GasUsed: vmctx.GasUsed(),
|
||||
},
|
||||
ActorErr: actorErr,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *VM) Flush(ctx context.Context) (cid.Cid, error) {
|
||||
|
@ -111,7 +111,8 @@ func (a *FullNodeAPI) ChainCall(ctx context.Context, msg *types.Message, ts *typ
|
||||
}
|
||||
|
||||
// TODO: maybe just use the invoker directly?
|
||||
return vmi.ApplyMessage(ctx, msg)
|
||||
ret, err := vmi.ApplyMessage(ctx, msg)
|
||||
return &ret.MessageReceipt, err
|
||||
}
|
||||
|
||||
func (a *FullNodeAPI) MpoolPending(ctx context.Context, ts *types.TipSet) ([]*types.SignedMessage, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user