Move Actor, BigInt, MessageReceipt

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-07-12 12:23:05 +02:00
parent ea2039ecb6
commit 9746b88bb3
10 changed files with 187 additions and 151 deletions

View File

@ -6,6 +6,7 @@ import (
"sync"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/ipfs/go-cid"
datastore "github.com/ipfs/go-datastore"
@ -27,7 +28,7 @@ type GenesisBootstrap struct {
MinerKey address.Address
}
func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*Actor, error) {
func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*types.Actor, error) {
var ias InitActorState
ias.NextID = 100
@ -56,7 +57,7 @@ func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*Actor, erro
return nil, err
}
act := &Actor{
act := &types.Actor{
Code: InitActorCodeCid,
Head: statecid,
}
@ -103,18 +104,18 @@ func MakeGenesisBlock(bs bstore.Blockstore, w *Wallet) (*GenesisBootstrap, error
return nil, err
}
err = state.SetActor(NetworkAddress, &Actor{
err = state.SetActor(NetworkAddress, &types.Actor{
Code: AccountActorCodeCid,
Balance: NewInt(100000000000),
Balance: types.NewInt(100000000000),
Head: emptyobject,
})
if err != nil {
return nil, err
}
err = state.SetActor(minerAddr, &Actor{
err = state.SetActor(minerAddr, &types.Actor{
Code: AccountActorCodeCid,
Balance: NewInt(5000000),
Balance: types.NewInt(5000000),
Head: emptyobject,
})
if err != nil {
@ -140,7 +141,7 @@ func MakeGenesisBlock(bs bstore.Blockstore, w *Wallet) (*GenesisBootstrap, error
ElectionProof: []byte("the Genesis block"),
Parents: []cid.Cid{},
Height: 0,
ParentWeight: NewInt(0),
ParentWeight: types.NewInt(0),
StateRoot: stateroot,
Messages: emptyroot,
MessageReceipts: emptyroot,

View File

@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/ipfs/go-cid"
)
@ -12,7 +13,7 @@ type invoker struct {
builtInCode map[cid.Cid]nativeCode
}
type invokeFunc func(act *Actor, vmctx *VMContext, params []byte) (InvokeRet, error)
type invokeFunc func(act *types.Actor, vmctx *VMContext, params []byte) (InvokeRet, error)
type nativeCode []invokeFunc
type InvokeRet struct {
result []byte
@ -27,7 +28,7 @@ func newInvoker() *invoker {
return inv
}
func (inv *invoker) Invoke(act *Actor, vmctx *VMContext, method uint64, params []byte) (InvokeRet, error) {
func (inv *invoker) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params []byte) (InvokeRet, error) {
code, ok := inv.builtInCode[act.Code]
if !ok {
@ -77,10 +78,10 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) {
}
if t.NumIn() != 3 {
return nil, newErr("wrong number of inputs should be: " +
"*Actor, *VMContext, <type of parameter>")
"*types.Actor, *VMContext, <type of parameter>")
}
if t.In(0) != reflect.TypeOf(&Actor{}) {
return nil, newErr("first arguemnt should be *Actor")
if t.In(0) != reflect.TypeOf(&types.Actor{}) {
return nil, newErr("first arguemnt should be *types.Actor")
}
if t.In(1) != reflect.TypeOf(&VMContext{}) {
return nil, newErr("second argument should be *VMContext")

View File

@ -10,11 +10,12 @@ import (
sharray "github.com/whyrusleeping/sharray"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
bls "github.com/filecoin-project/go-lotus/lib/bls-signatures"
)
func miningRewardForBlock(base *TipSet) BigInt {
return NewInt(10000)
func miningRewardForBlock(base *TipSet) types.BigInt {
return types.NewInt(10000)
}
func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, tickets []Ticket, proof ElectionProof, msgs []*SignedMessage) (*FullBlock, error) {
@ -96,7 +97,7 @@ func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, ti
next.BLSAggregate = aggSig
next.StateRoot = stateRoot
pweight := cs.Weight(parents)
next.ParentWeight = NewInt(pweight)
next.ParentWeight = types.NewInt(pweight)
fullBlock := &FullBlock{
Header: next,

View File

@ -4,11 +4,12 @@ import (
"context"
"fmt"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/ipfs/go-cid"
hamt "github.com/ipfs/go-hamt-ipld"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
)
var ErrActorNotFound = fmt.Errorf("actor not found")
@ -17,7 +18,7 @@ type StateTree struct {
root *hamt.Node
store *hamt.CborIpldStore
actorcache map[address.Address]*Actor
actorcache map[address.Address]*types.Actor
snapshot cid.Cid
}
@ -25,7 +26,7 @@ func NewStateTree(cst *hamt.CborIpldStore) (*StateTree, error) {
return &StateTree{
root: hamt.NewNode(cst),
store: cst,
actorcache: make(map[address.Address]*Actor),
actorcache: make(map[address.Address]*types.Actor),
}, nil
}
@ -39,11 +40,11 @@ func LoadStateTree(cst *hamt.CborIpldStore, c cid.Cid) (*StateTree, error) {
return &StateTree{
root: nd,
store: cst,
actorcache: make(map[address.Address]*Actor),
actorcache: make(map[address.Address]*types.Actor),
}, nil
}
func (st *StateTree) SetActor(addr address.Address, act *Actor) error {
func (st *StateTree) SetActor(addr address.Address, act *types.Actor) error {
if addr.Protocol() != address.ID {
iaddr, err := st.lookupID(addr)
if err != nil {
@ -76,7 +77,7 @@ func (st *StateTree) lookupID(addr address.Address) (address.Address, error) {
return ias.Lookup(st.store, addr)
}
func (st *StateTree) GetActor(addr address.Address) (*Actor, error) {
func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) {
if addr.Protocol() != address.ID {
iaddr, err := st.lookupID(addr)
if err != nil {
@ -101,7 +102,7 @@ func (st *StateTree) GetActor(addr address.Address) (*Actor, error) {
return nil, err
}
var act Actor
var act types.Actor
badout, err := cbor.DumpObject(thing)
if err != nil {
return nil, err
@ -122,7 +123,7 @@ func (st *StateTree) Flush() (cid.Cid, error) {
return cid.Undef, err
}
}
st.actorcache = make(map[address.Address]*Actor)
st.actorcache = make(map[address.Address]*types.Actor)
if err := st.root.Flush(context.TODO()); err != nil {
return cid.Undef, err
@ -141,9 +142,9 @@ func (st *StateTree) Snapshot() error {
return nil
}
func (st *StateTree) RegisterNewAddress(addr address.Address, act *Actor) (address.Address, error) {
func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor) (address.Address, error) {
var out address.Address
err := st.MutateActor(InitActorAddress, func(initact *Actor) error {
err := st.MutateActor(InitActorAddress, func(initact *types.Actor) error {
var ias InitActorState
if err := st.store.Get(context.TODO(), initact.Head, &ias); err != nil {
return err
@ -185,7 +186,7 @@ func (st *StateTree) Revert() error {
return nil
}
func (st *StateTree) MutateActor(addr address.Address, f func(*Actor) error) error {
func (st *StateTree) MutateActor(addr address.Address, f func(*types.Actor) error) error {
act, err := st.GetActor(addr)
if err != nil {
return err

View File

@ -6,6 +6,7 @@ import (
"sync"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/ipfs/go-cid"
dstore "github.com/ipfs/go-datastore"
@ -604,20 +605,20 @@ func (syncer *Syncer) ValidateBlock(b *FullBlock) error {
}
func DeductFunds(act *Actor, amt BigInt) error {
if BigCmp(act.Balance, amt) < 0 {
func DeductFunds(act *types.Actor, amt types.BigInt) error {
if types.BigCmp(act.Balance, amt) < 0 {
return fmt.Errorf("not enough funds")
}
act.Balance = BigSub(act.Balance, amt)
act.Balance = types.BigSub(act.Balance, amt)
return nil
}
func DepositFunds(act *Actor, amt BigInt) {
act.Balance = BigAdd(act.Balance, amt)
func DepositFunds(act *types.Actor, amt types.BigInt) {
act.Balance = types.BigAdd(act.Balance, amt)
}
func TryCreateAccountActor(st *StateTree, addr address.Address) (*Actor, error) {
func TryCreateAccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
act, err := makeActor(st, addr)
if err != nil {
return nil, err
@ -631,7 +632,7 @@ func TryCreateAccountActor(st *StateTree, addr address.Address) (*Actor, error)
return act, nil
}
func makeActor(st *StateTree, addr address.Address) (*Actor, error) {
func makeActor(st *StateTree, addr address.Address) (*types.Actor, error) {
switch addr.Protocol() {
case address.BLS:
return NewBLSAccountActor(st, addr)
@ -646,7 +647,7 @@ func makeActor(st *StateTree, addr address.Address) (*Actor, error) {
}
}
func NewBLSAccountActor(st *StateTree, addr address.Address) (*Actor, error) {
func NewBLSAccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
var acstate AccountActorState
acstate.Address = addr
@ -655,19 +656,19 @@ func NewBLSAccountActor(st *StateTree, addr address.Address) (*Actor, error) {
return nil, err
}
nact := &Actor{
nact := &types.Actor{
Code: AccountActorCodeCid,
Balance: NewInt(0),
Balance: types.NewInt(0),
Head: c,
}
return nact, nil
}
func NewSecp256k1AccountActor(st *StateTree, addr address.Address) (*Actor, error) {
nact := &Actor{
func NewSecp256k1AccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
nact := &types.Actor{
Code: AccountActorCodeCid,
Balance: NewInt(0),
Balance: types.NewInt(0),
Head: EmptyObjectCid,
}

View File

@ -1,11 +1,9 @@
package chain
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"math/big"
"github.com/filecoin-project/go-lotus/chain/address"
@ -15,30 +13,16 @@ import (
ipld "github.com/ipfs/go-ipld-format"
"github.com/multiformats/go-multihash"
"github.com/polydawn/refmt/obj/atlas"
"github.com/filecoin-project/go-lotus/chain/types"
)
func init() {
ipld.Register(0x1f, IpldDecode)
cbor.RegisterCborType(MessageReceipt{})
cbor.RegisterCborType(Actor{})
cbor.RegisterCborType(BlockMsg{})
///*
cbor.RegisterCborType(atlas.BuildEntry(BigInt{}).UseTag(2).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(i BigInt) ([]byte, error) {
if i.Int == nil {
return []byte{}, nil
}
return i.Bytes(), nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x []byte) (BigInt, error) {
return BigFromBytes(x), nil
})).
Complete())
//*/
cbor.RegisterCborType(atlas.BuildEntry(SignedMessage{}).UseTag(45).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
@ -109,18 +93,18 @@ func init() {
return Message{}, fmt.Errorf("expected uint64 nonce at index 2")
}
value := arr[3].(BigInt)
gasPrice := arr[4].(BigInt)
gasLimit := arr[5].(BigInt)
value := arr[3].(types.BigInt)
gasPrice := arr[4].(types.BigInt)
gasLimit := arr[5].(types.BigInt)
method, _ := arr[6].(uint64)
params, _ := arr[7].([]byte)
if gasPrice.Nil() {
gasPrice = NewInt(0)
gasPrice = types.NewInt(0)
}
if gasLimit.Nil() {
gasLimit = NewInt(0)
gasLimit = types.NewInt(0)
}
return Message{
@ -175,7 +159,7 @@ func init() {
for _, p := range parentsArr {
parents = append(parents, p.(cid.Cid))
}
parentWeight := arr[4].(BigInt)
parentWeight := arr[4].(types.BigInt)
height := arr[5].(uint64)
stateRoot := arr[6].(cid.Cid)
@ -197,65 +181,6 @@ func init() {
Complete())
}
type BigInt struct {
*big.Int
}
func NewInt(i uint64) BigInt {
return BigInt{big.NewInt(0).SetUint64(i)}
}
func BigFromBytes(b []byte) BigInt {
i := big.NewInt(0).SetBytes(b)
return BigInt{i}
}
func BigMul(a, b BigInt) BigInt {
return BigInt{big.NewInt(0).Mul(a.Int, b.Int)}
}
func BigAdd(a, b BigInt) BigInt {
return BigInt{big.NewInt(0).Add(a.Int, b.Int)}
}
func BigSub(a, b BigInt) BigInt {
return BigInt{big.NewInt(0).Sub(a.Int, b.Int)}
}
func BigCmp(a, b BigInt) int {
return a.Int.Cmp(b.Int)
}
func (bi *BigInt) Nil() bool {
return bi.Int == nil
}
func (bi *BigInt) MarshalJSON() ([]byte, error) {
return json.Marshal(bi.String())
}
func (bi *BigInt) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, ok := big.NewInt(0).SetString(s, 10)
if !ok {
return fmt.Errorf("failed to parse bigint string")
}
bi.Int = i
return nil
}
type Actor struct {
Code cid.Cid
Head cid.Cid
Nonce uint64
Balance BigInt
}
type BlockHeader struct {
Miner address.Address
@ -265,7 +190,7 @@ type BlockHeader struct {
Parents []cid.Cid
ParentWeight BigInt
ParentWeight types.BigInt
Height uint64
@ -321,10 +246,10 @@ type Message struct {
Nonce uint64
Value BigInt
Value types.BigInt
GasPrice BigInt
GasLimit BigInt
GasPrice types.BigInt
GasLimit types.BigInt
Method uint64
Params []byte
@ -382,18 +307,6 @@ func (m *SignedMessage) Cid() cid.Cid {
return sb.Cid()
}
type MessageReceipt struct {
ExitCode uint8
Return []byte
GasUsed BigInt
}
func (mr *MessageReceipt) Equals(o *MessageReceipt) bool {
return mr.ExitCode == o.ExitCode && bytes.Equal(mr.Return, o.Return) && BigCmp(mr.GasUsed, o.GasUsed) == 0
}
type SignedMessage struct {
Message Message
Signature Signature

17
chain/types/actor.go Normal file
View File

@ -0,0 +1,17 @@
package types
import (
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
)
func init() {
cbor.RegisterCborType(Actor{})
}
type Actor struct {
Code cid.Cid
Head cid.Cid
Nonce uint64
Balance BigInt
}

79
chain/types/bigint.go Normal file
View File

@ -0,0 +1,79 @@
package types
import (
"encoding/json"
"fmt"
"math/big"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/polydawn/refmt/obj/atlas"
)
func init() {
cbor.RegisterCborType(atlas.BuildEntry(BigInt{}).UseTag(2).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(i BigInt) ([]byte, error) {
if i.Int == nil {
return []byte{}, nil
}
return i.Bytes(), nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x []byte) (BigInt, error) {
return BigFromBytes(x), nil
})).
Complete())
}
type BigInt struct {
*big.Int
}
func NewInt(i uint64) BigInt {
return BigInt{big.NewInt(0).SetUint64(i)}
}
func BigFromBytes(b []byte) BigInt {
i := big.NewInt(0).SetBytes(b)
return BigInt{i}
}
func BigMul(a, b BigInt) BigInt {
return BigInt{big.NewInt(0).Mul(a.Int, b.Int)}
}
func BigAdd(a, b BigInt) BigInt {
return BigInt{big.NewInt(0).Add(a.Int, b.Int)}
}
func BigSub(a, b BigInt) BigInt {
return BigInt{big.NewInt(0).Sub(a.Int, b.Int)}
}
func BigCmp(a, b BigInt) int {
return a.Int.Cmp(b.Int)
}
func (bi *BigInt) Nil() bool {
return bi.Int == nil
}
func (bi *BigInt) MarshalJSON() ([]byte, error) {
return json.Marshal(bi.String())
}
func (bi *BigInt) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, ok := big.NewInt(0).SetString(s, 10)
if !ok {
return fmt.Errorf("failed to parse bigint string")
}
bi.Int = i
return nil
}

View File

@ -0,0 +1,21 @@
package types
import (
"bytes"
cbor "github.com/ipfs/go-ipld-cbor"
)
func init() {
cbor.RegisterCborType(MessageReceipt{})
}
type MessageReceipt struct {
ExitCode uint8
Return []byte
GasUsed BigInt
}
func (mr *MessageReceipt) Equals(o *MessageReceipt) bool {
return mr.ExitCode == o.ExitCode && bytes.Equal(mr.Return, o.Return) && BigCmp(mr.GasUsed, o.GasUsed) == 0
}

View File

@ -6,6 +6,7 @@ import (
"math/big"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/lib/bufbstore"
bserv "github.com/ipfs/go-blockservice"
@ -49,8 +50,8 @@ func (vmc *VMContext) BlockHeight() uint64 {
return vmc.height
}
func (vmc *VMContext) GasUsed() BigInt {
return NewInt(0)
func (vmc *VMContext) GasUsed() types.BigInt {
return types.NewInt(0)
}
func makeVMContext(state *StateTree, msg *Message, height uint64) *VMContext {
@ -90,7 +91,7 @@ func NewVM(base cid.Cid, height uint64, maddr address.Address, cs *ChainStore) (
}, nil
}
func (vm *VM) ApplyMessage(msg *Message) (*MessageReceipt, error) {
func (vm *VM) ApplyMessage(msg *Message) (*types.MessageReceipt, error) {
st := vm.cstate
st.Snapshot()
fromActor, err := st.GetActor(msg.From)
@ -98,9 +99,9 @@ func (vm *VM) ApplyMessage(msg *Message) (*MessageReceipt, error) {
return nil, errors.Wrap(err, "from actor not found")
}
gascost := BigMul(msg.GasLimit, msg.GasPrice)
totalCost := BigAdd(gascost, msg.Value)
if BigCmp(fromActor.Balance, totalCost) < 0 {
gascost := types.BigMul(msg.GasLimit, msg.GasPrice)
totalCost := types.BigAdd(gascost, msg.Value)
if types.BigCmp(fromActor.Balance, totalCost) < 0 {
return nil, fmt.Errorf("not enough funds")
}
@ -140,13 +141,13 @@ func (vm *VM) ApplyMessage(msg *Message) (*MessageReceipt, error) {
if errcode != 0 {
// revert all state changes since snapshot
st.Revert()
gascost := BigMul(vmctx.GasUsed(), msg.GasPrice)
gascost := types.BigMul(vmctx.GasUsed(), msg.GasPrice)
if err := DeductFunds(fromActor, gascost); err != nil {
panic("invariant violated: " + err.Error())
}
} else {
// refund unused gas
refund := BigMul(BigSub(msg.GasLimit, vmctx.GasUsed()), msg.GasPrice)
refund := types.BigMul(types.BigSub(msg.GasLimit, vmctx.GasUsed()), msg.GasPrice)
DepositFunds(fromActor, refund)
}
}
@ -157,10 +158,10 @@ func (vm *VM) ApplyMessage(msg *Message) (*MessageReceipt, error) {
return nil, errors.Wrap(err, "getting block miner actor failed")
}
gasReward := BigMul(msg.GasPrice, vmctx.GasUsed())
gasReward := types.BigMul(msg.GasPrice, vmctx.GasUsed())
DepositFunds(miner, gasReward)
return &MessageReceipt{
return &types.MessageReceipt{
ExitCode: errcode,
Return: ret,
GasUsed: vmctx.GasUsed(),
@ -183,7 +184,7 @@ func (vm *VM) Flush(ctx context.Context) (cid.Cid, error) {
return root, nil
}
func (vm *VM) TransferFunds(from, to address.Address, amt BigInt) error {
func (vm *VM) TransferFunds(from, to address.Address, amt types.BigInt) error {
if from == to {
return nil
}
@ -206,7 +207,7 @@ func (vm *VM) TransferFunds(from, to address.Address, amt BigInt) error {
return nil
}
func (vm *VM) Invoke(act *Actor, vmctx *VMContext, method uint64, params []byte) ([]byte, byte, error) {
func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params []byte) ([]byte, byte, error) {
ret, err := vm.inv.Invoke(act, vmctx, method, params)
if err != nil {
return nil, 0, err