Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Refactor subnet ID and infer ChainID from SubnetID #187

Merged
merged 9 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified build/actors/ipc-actors.car
Binary file not shown.
Binary file modified build/genesis/spacenet.car
Binary file not shown.
Binary file modified build/openrpc/full.json.gz
Binary file not shown.
7 changes: 6 additions & 1 deletion chain/consensus/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ func checkBlockMessages(ctx context.Context, sm *stmgr.StateManager, cs *store.C
}
}

chainID, err := stmgr.GetChainID(ctx, sm)
if err != nil {
return xerrors.Errorf("failed to get chain ID: %w", err)
}

nonces := make(map[address.Address]uint64)

stateroot, _, err := sm.TipSetState(ctx, baseTs)
Expand Down Expand Up @@ -293,7 +298,7 @@ func checkBlockMessages(ctx context.Context, sm *stmgr.StateManager, cs *store.C
return xerrors.Errorf("failed to resolve key addr: %w", err)
}

if err := AuthenticateMessage(m, kaddr); err != nil {
if err := AuthenticateMessage(m, kaddr, int(chainID)); err != nil {
return xerrors.Errorf("failed to validate signature: %w", err)
}
}
Expand Down
4 changes: 1 addition & 3 deletions chain/consensus/mir/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import (
"path"
"time"

"github.com/consensus-shipyard/go-ipc-types/validator"
golog "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors"

"github.com/consensus-shipyard/go-ipc-types/validator"

"github.com/filecoin-project/go-state-types/abi"

"github.com/filecoin-project/mir"
"github.com/filecoin-project/mir/pkg/checkpoint"
mircrypto "github.com/filecoin-project/mir/pkg/crypto"
Expand Down
6 changes: 5 additions & 1 deletion chain/consensus/mir/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ func (sm *StateManager) ApplyTXs(txs []*requestpb.Request) error {
}
valSetMsgs = append(valSetMsgs, initialConfigMsg)
// the rootnet doesn't need to be explicitly initialized.
if string(sm.netName) != sdk.RootStr {
sn, err := sdk.NewSubnetIDFromString(string(sm.netName))
if err != nil {
return xerrors.Errorf("error getting subnetID from network name: %w", err)
}
if !sn.IsRoot() {
initializeMsg, err := membership.NewInitGenesisEpochMsg(genesis.DefaultIPCGatewayAddr, sm.genesisEpoch)
if err != nil {
return xerrors.Errorf("error initializing subnet: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions chain/consensus/signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
// SignedMessage was signed by the indicated Address, computing the correct
// signature payload depending on the signature type. The supplied Address type
// must be recognized by the registered verifier for the signature type.
func AuthenticateMessage(msg *types.SignedMessage, signer address.Address) error {
func AuthenticateMessage(msg *types.SignedMessage, signer address.Address, chainID int) error {
var digest []byte

typ := msg.Signature.Type
switch typ {
case crypto.SigTypeDelegated:
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(&msg.Message)
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(&msg.Message, chainID)
if err != nil {
return xerrors.Errorf("failed to reconstruct eth transaction: %w", err)
}
roundTripMsg, err := txArgs.ToUnsignedMessage(msg.Message.From)
roundTripMsg, err := txArgs.ToUnsignedMessage(msg.Message.From, chainID)
if err != nil {
return xerrors.Errorf("failed to reconstruct filecoin msg: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion chain/gen/genesis/f02_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func SetupRewardActor(ctx context.Context, bs bstore.Blockstore, qaPower big.Int
// let's not allocate any initial balance into the reward actor if this is not
// the rootnet.
balance := abi.NewTokenAmount(0)
if networkName == ipctypes.RootSubnet.String() {
id, err := ipctypes.NewSubnetIDFromString(networkName)
if err != nil {
return nil, xerrors.Errorf("failed to parse network name %s: %w", networkName, err)
}
if id.IsRoot() {
balance = types.BigInt{Int: build.InitialRewardBalance}
}

Expand Down
13 changes: 8 additions & 5 deletions chain/gen/genesis/f08_ipc_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/consensus-shipyard/go-ipc-types/gateway"
"github.com/consensus-shipyard/go-ipc-types/sdk"
ipctypes "github.com/consensus-shipyard/go-ipc-types/sdk"
"github.com/consensus-shipyard/go-ipc-types/voting"
cbor "github.com/ipfs/go-ipld-cbor"
Expand Down Expand Up @@ -49,7 +48,7 @@ func constructState(store adt.Store, network ipctypes.SubnetID, buPeriod, tdPeri

// if it is the rootnet no need to explicitly initialize the gateway.
initialized := false
if network == sdk.RootSubnet {
if network.IsRoot() {
initialized = true
}

Expand Down Expand Up @@ -80,12 +79,12 @@ func SetupIPCGateway(ctx context.Context, bs bstore.Blockstore, av actorstypes.V
// TODO: Make this configurable
dst, err := constructState(adt.WrapStore(ctx, cbor.NewCborStore(bs)), network, checkPeriod, checkPeriod)
if err != nil {
return nil, err
return nil, xerrors.Errorf("cannot construct state: %w", err)
}

statecid, err := cst.Put(ctx, dst)
if err != nil {
return nil, err
return nil, xerrors.Errorf("cannot put state: %w", err)
}

actcid, ok := actors.GetActorCodeID(av, gateway.ManifestID)
Expand All @@ -97,7 +96,11 @@ func SetupIPCGateway(ctx context.Context, bs bstore.Blockstore, av actorstypes.V
// to mint new tokens in subnets when top-down messages are executed.
// This balance is zero in the root, as now top-down messages can be executed in the root.
balance := abi.NewTokenAmount(0)
if network != ipctypes.RootSubnet {
id, err := ipctypes.NewSubnetIDFromString(networkName)
if err != nil {
return nil, xerrors.Errorf("failed to parse network name %s: %w", networkName, err)
}
if !id.IsRoot() {
balance = types.BigInt{Int: build.InitialRewardBalance}
}

Expand Down
2 changes: 1 addition & 1 deletion chain/gen/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func VerifyPreSealedData(ctx context.Context, cs *store.ChainStore, sys vm.Sysca
NetworkVersion: nv,
BaseFee: big.Zero(),
}
vm, err := vm.NewVM(ctx, &vmopt)
vm, err := vm.NewVM(ctx, &vmopt, build.Eip155ChainId)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create VM: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions chain/gen/genesis/miners.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/rand"

"github.com/consensus-shipyard/go-ipc-types/sdk"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
cbg "github.com/whyrusleeping/cbor-gen"
Expand Down Expand Up @@ -33,6 +34,7 @@ import (
builtin6 "github.com/filecoin-project/specs-actors/v6/actors/builtin"
runtime7 "github.com/filecoin-project/specs-actors/v7/actors/runtime"

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
Expand Down Expand Up @@ -101,7 +103,7 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.Syscal
BaseFee: big.Zero(),
}

return vm.NewVM(ctx, vmopt)
return vm.NewVM(ctx, vmopt, build.Eip155ChainId)
}

genesisVm, err := newVM(sroot)
Expand Down Expand Up @@ -324,7 +326,7 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.Syscal
return cid.Undef, xerrors.Errorf("setting power state: %w", err)
}

rewact, err := SetupRewardActor(ctx, cs.StateBlockstore(), big.Zero(), av, "/root")
rewact, err := SetupRewardActor(ctx, cs.StateBlockstore(), big.Zero(), av, sdk.NewRootID(build.Eip155ChainId).String())
if err != nil {
return cid.Undef, xerrors.Errorf("setup reward actor: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion chain/ipcagent/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestClientCompatibleWithIPCAgent(t *testing.T) {
subnet string
tipSet string
}{
subnet: "/root/test",
subnet: "/r123/test",
tipSet: "QmPK1s3pNYLi9ERiq3BDxKa3XosgWwFRQUydHUtz4YgpqB",
}

Expand Down
16 changes: 15 additions & 1 deletion chain/messagepool/messagepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"github.com/consensus-shipyard/go-ipc-types/sdk"
"github.com/hashicorp/go-multierror"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -798,7 +799,11 @@ func (mp *MessagePool) VerifyMsgSig(m *types.SignedMessage) error {
return nil
}

if err := consensus.AuthenticateMessage(m, m.Message.From); err != nil {
chainID, err := mp.GetChainID(context.Background())
if err != nil {
return xerrors.Errorf("failed to get chain ID: %w", err)
}
if err := consensus.AuthenticateMessage(m, m.Message.From, int(chainID)); err != nil {
return xerrors.Errorf("failed to validate signature: %w", err)
}

Expand Down Expand Up @@ -1018,6 +1023,14 @@ func (mp *MessagePool) GetActor(_ context.Context, addr address.Address, _ types
return mp.api.GetActorAfter(addr, mp.curTs)
}

func (mp *MessagePool) GetChainID(ctc context.Context) (uint64, error) {
sn, err := sdk.NewSubnetIDFromString(string(mp.netName))
if err != nil {
return 0, err
}
return sn.ChainID(), err
}

func (mp *MessagePool) getNonceLocked(ctx context.Context, addr address.Address, curTs *types.TipSet) (uint64, error) {
stateNonce, err := mp.getStateNonce(ctx, addr, curTs) // sanity check
if err != nil {
Expand Down Expand Up @@ -1609,4 +1622,5 @@ func getBaseFeeLowerBound(baseFee, factor types.BigInt) types.BigInt {
type MpoolNonceAPI interface {
GetNonce(context.Context, address.Address, types.TipSetKey) (uint64, error)
GetActor(context.Context, address.Address, types.TipSetKey) (*types.Actor, error)
GetChainID(context.Context) (uint64, error)
}
10 changes: 7 additions & 3 deletions chain/messagesigner/messagesigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func (ms *MessageSigner) SignMessage(ctx context.Context, msg *types.Message, sp
// Sign the message with the nonce
msg.Nonce = nonce

sb, err := SigningBytes(msg, msg.From.Protocol())
chainID, err := ms.mpool.GetChainID(ctx)
if err != nil {
return nil, xerrors.Errorf("failed to get chain ID: %w", err)
}
sb, err := SigningBytes(msg, msg.From.Protocol(), chainID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -194,9 +198,9 @@ func (ms *MessageSigner) dstoreKey(addr address.Address) datastore.Key {
return datastore.KeyWithNamespaces([]string{dsKeyActorNonce, addr.String()})
}

func SigningBytes(msg *types.Message, sigType address.Protocol) ([]byte, error) {
func SigningBytes(msg *types.Message, sigType address.Protocol, chainID uint64) ([]byte, error) {
if sigType == address.Delegated {
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(msg)
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(msg, int(chainID))
if err != nil {
return nil, xerrors.Errorf("failed to reconstruct eth transaction: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions chain/messagesigner/messagesigner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/filecoin-project/go-address"

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/messagepool"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
Expand Down Expand Up @@ -46,6 +47,10 @@ func (mp *mockMpool) GetActor(_ context.Context, addr address.Address, _ types.T
panic("don't use it")
}

func (mp *mockMpool) GetChainID(_ context.Context) (uint64, error) {
return build.Eip155ChainId, nil
}

func TestMessageSignerSignMessage(t *testing.T) {
ctx := context.Background()

Expand Down
4 changes: 4 additions & 0 deletions chain/stmgr/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,8 @@ func (sm *StateManager) MarketBalance(ctx context.Context, addr address.Address,
return out, nil
}

func (sm *StateManager) GetChainID(ctx context.Context) (uint64, error) {
return GetChainID(ctx, sm)
}

var _ StateManagerAPI = (*StateManager)(nil)
13 changes: 11 additions & 2 deletions chain/stmgr/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ func (sm *StateManager) callInternal(ctx context.Context, msg *types.Message, pr
TipSetGetter: TipSetGetterForTipset(sm.cs, ts),
Tracing: true,
}
vmi, err := sm.newVM(ctx, vmopt)

chainID, err := GetChainID(ctx, sm)
if err != nil {
return nil, err
}
vmi, err := sm.newVM(ctx, vmopt, chainID)
if err != nil {
return nil, xerrors.Errorf("failed to set up vm: %w", err)
}
Expand Down Expand Up @@ -201,7 +206,11 @@ func (sm *StateManager) callInternal(ctx context.Context, msg *types.Message, pr
vmopt.BaseFee = big.Zero()
vmopt.StateBase = stateCid

vmi, err = sm.newVM(ctx, vmopt)
chainID, err := GetChainID(ctx, sm)
if err != nil {
return nil, err
}
vmi, err = sm.newVM(ctx, vmopt, chainID)
if err != nil {
return nil, xerrors.Errorf("failed to set up estimation vm: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions chain/stmgr/forks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestForkHeightTriggers(t *testing.T) {
registry := builtin.MakeRegistryLegacy([]rtt.VMActor{testActor{}})
inv.Register(actorstypes.Version0, nil, registry)

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts, chainID uint64) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
if err != nil {
return nil, err
Expand Down Expand Up @@ -295,7 +295,7 @@ func testForkRefuseCall(t *testing.T, nullsBefore, nullsAfter int) {
registry := builtin.MakeRegistryLegacy([]rtt.VMActor{testActor{}})
inv.Register(actorstypes.Version0, nil, registry)

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts, chainID uint64) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
if err != nil {
return nil, err
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestForkPreMigration(t *testing.T) {
registry := builtin.MakeRegistryLegacy([]rtt.VMActor{testActor{}})
inv.Register(actorstypes.Version0, nil, registry)

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts, chainID uint64) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
if err != nil {
return nil, err
Expand Down Expand Up @@ -590,7 +590,7 @@ func TestDisablePreMigration(t *testing.T) {
registry := builtin.MakeRegistryLegacy([]rtt.VMActor{testActor{}})
inv.Register(actorstypes.Version0, nil, registry)

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts, chainID uint64) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
require.NoError(t, err)
nvm.SetInvoker(inv)
Expand Down Expand Up @@ -644,7 +644,7 @@ func TestMigrtionCache(t *testing.T) {
registry := builtin.MakeRegistryLegacy([]rtt.VMActor{testActor{}})
inv.Register(actorstypes.Version0, nil, registry)

sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts, chainID uint64) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
require.NoError(t, err)
nvm.SetInvoker(inv)
Expand Down Expand Up @@ -687,7 +687,7 @@ func TestMigrtionCache(t *testing.T) {
metadataDs,
)
require.NoError(t, err)
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts) (vm.Interface, error) {
sm.SetVMConstructor(func(ctx context.Context, vmopt *vm.VMOpts, chainID uint64) (vm.Interface, error) {
nvm, err := vm.NewLegacyVM(ctx, vmopt)
require.NoError(t, err)
nvm.SetInvoker(inv)
Expand Down
13 changes: 13 additions & 0 deletions chain/stmgr/rpc/rpcstatemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpcstmgr
import (
"context"

"github.com/consensus-shipyard/go-ipc-types/sdk"
cbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -56,4 +57,16 @@ func (s *RPCStateManager) Call(ctx context.Context, msg *types.Message, ts *type
return nil, xerrors.Errorf("RPCStateManager does not implement StateManager.Call")
}

func (s *RPCStateManager) GetChainID(ctx context.Context) (uint64, error) {
nn, err := s.gapi.StateNetworkName(ctx)
if err != nil {
return 0, err
}
sn, err := sdk.NewSubnetIDFromString(string(nn))
if err != nil {
return 0, err
}
return sn.ChainID(), nil
}

var _ stmgr.StateManagerAPI = (*RPCStateManager)(nil)
Loading