Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shed): add forest-tool shed f3 check-activation-raw #5309

Merged
merged 6 commits into from
Feb 20, 2025

Conversation

hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Feb 19, 2025

Summary of changes

Part of #5307

Changes introduced in this pull request:

Lotus code:

var f3CheckActivationRaw = &cli.Command{
	Name:  "check-activation-raw",
	Usage: "queries f3 parameters contract using raw logic",

	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "contract",
			Usage: "address contract to query",
		},
	},
	Action: func(cctx *cli.Context) error {
		// this code is raw logic for cross-checking
		// the cleaner code is in chain/lf3/manifest.go
		address, err := ethtypes.ParseEthAddress(cctx.String("contract"))
		if err != nil {
			return fmt.Errorf("trying to parse contract address: %s: %w", cctx.String("contract"), err)
		}

		ethCall := ethtypes.EthCall{
			To:   &address,
			Data: must.One(ethtypes.DecodeHexString("0x2587660d")), // method ID of activationInformation()
		}
		fMessage, err := ethCall.ToFilecoinMessage()
		if err != nil {
			return fmt.Errorf("converting to filecoin message: %w", err)
		}

		ctx := cliutil.ReqContext(cctx)
		api, closer, err := cliutil.GetFullNodeAPIV1(cctx)
		if err != nil {
			return fmt.Errorf("getting api: %w", err)
		}
		defer closer()

		msgRes, err := api.StateCall(ctx, fMessage, types.EmptyTSK)
		if err != nil {
			return fmt.Errorf("state call error: %w", err)
		}
		if msgRes.MsgRct.ExitCode != 0 {
			return fmt.Errorf("message returned exit code: %v", msgRes.MsgRct.ExitCode)
		}

		var ethReturn abi.CborBytes
		err = ethReturn.UnmarshalCBOR(bytes.NewReader(msgRes.MsgRct.Return))
		if err != nil {
			return fmt.Errorf("could not decode return value: %w", err)
		}
		fmt.Printf("Raw data: %X\n", ethReturn)
		slot, retBytes := []byte{}, []byte(ethReturn)
		_ = slot
		// 3*32 because there should be 3 slots minimum
		if len(retBytes) < 3*32 {
			return fmt.Errorf("no activation information")
		}

		// split off first slot
		slot, retBytes = retBytes[:32], retBytes[32:]
		// it is uint64 so we want the last 8 bytes
		slot = slot[24:32]
		activationEpoch := binary.BigEndian.Uint64(slot)
		_ = activationEpoch

		slot, retBytes = retBytes[:32], retBytes[32:]
		for i := 0; i < 31; i++ {
			if slot[i] != 0 {
				return fmt.Errorf("wrong value for offest (padding): slot[%d] = 0x%x != 0x00", i, slot[i])
			}
		}
		if slot[31] != 0x40 {
			return fmt.Errorf("wrong value for offest : slot[31] = 0x%x != 0x40", slot[31])
		}
		slot, retBytes = retBytes[:32], retBytes[32:]
		slot = slot[24:32]
		pLen := binary.BigEndian.Uint64(slot)
		if pLen > 4<<10 {
			return fmt.Errorf("too long declared payload: %d > %d", pLen, 4<<10)
		}
		payloadLength := int(pLen)

		if payloadLength > len(retBytes) {
			return fmt.Errorf("not enough remaining bytes: %d > %d", payloadLength, retBytes)
		}

		if activationEpoch == math.MaxUint64 || payloadLength == 0 {
			fmt.Printf("no active activation")
		} else {
			compressedManifest := retBytes[:payloadLength]
			reader := io.LimitReader(flate.NewReader(bytes.NewReader(compressedManifest)), 1<<20)
			var m manifest.Manifest
			err = json.NewDecoder(reader).Decode(&m)
			if err != nil {
				return fmt.Errorf("got error while decoding manifest: %w", err)
			}

			if m.BootstrapEpoch < 0 || uint64(m.BootstrapEpoch) != activationEpoch {
				return fmt.Errorf("bootstrap epoch does not match: %d != %d", m.BootstrapEpoch, activationEpoch)
			}
			_, _ = io.Copy(os.Stdout, flate.NewReader(bytes.NewReader(compressedManifest)))
			fmt.Println()
		}
		return nil
	},
}

Output:

➜  lotus git:(master) ✗ ./lotus-shed f3 check-activation-raw --contract 0x476AC9256b9921C9C6a0fC237B7fE05fe9874F50
Raw data: 00000000000000000000000000000000000000000000000000000000004C4B400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000023D8554C172DA3014BCF3150CE78E079B06486FC1D0363321A590B6874E0FB2FC8C35C8922BC90D6926FFDE27D9C27620539F9EB52B7977DF939F07C3E168432A0DA30FC38C700DEFDC8A924652C9BF83D24C0AC4AEDCFAAD6086117E2BB42182DA3D63B7BE90D268A348B92A25CD2D7DEC1E07DE837994EA704F0ABB6194310E54323172E0EA58724699D9C84750088B8AF3FA537B2115AC620FB4E21A110E78200987EEB65816053306E04ECA4342E801C1B0D6111343F36FE50D677B5180301669747AA99FCA24B3EBCFF882AF4BE086E0EBB4C7F2C0024FFF9265E8408AFAB8A881BF568433F3E458EB8A1B861E9D89B021ACC9D1EA233990742B2B916A1FB1D5991326B003A5D490DE81D89BDC99F01FDF42A2244929D1C64A9059B620AE7F6732CF991DB161307993B62B152AB3ED0DC2374968A2F7D131125FEAA6C66D861B504CDAA326E3D7F23E32E17242F0BA1B2D79EA85E653B52E9740999DC846826FFF4FC718B69E6C39BDF67514446D399FFA7A12CC425FBF0FE6A7ADD3209AF97A165CB9EA5723E233E6D219ADBE95BF568B51159C9288411996314A0CAC8E3427620F6D34311A14660BBF2BD0E68115202B5377FA55503B507F409D11A767C43513ACA88A8DE478470DEE22FC62F2D8BA0BBC30BAD0CB4D95ECAAA4235A16381CDA3661256CFCA9BF9CAD693BC0E776F1184D152B8DED5F9565A076756293A895E5F65E187A84FC5FE774734E57BBC6974C53893941EA0ED138799D1F55D873FF83A0EDFFF13A23DF4929BA10A6ED099E509437CEF0BC17E3E065F00F000000
{
  "Pause": false,
  "ProtocolVersion": 5,
  "InitialInstance": 0,
  "BootstrapEpoch": 5000000,
  "NetworkName": "filecoin",
  "ExplicitPower": null,
  "IgnoreECPower": false,
  "InitialPowerTable": null,
  "CommitteeLookback": 10,
  "CatchUpAlignment": 15000000000,
  "Gpbft": {
    "Delta": 6000000000,
    "DeltaBackOffExponent": 2,
    "QualityDeltaMultiplier": 1,
    "MaxLookaheadRounds": 5,
    "ChainProposedLength": 100,
    "RebroadcastBackoffBase": 6000000000,
    "RebroadcastBackoffExponent": 1.3,
    "RebroadcastBackoffSpread": 0.1,
    "RebroadcastBackoffMax": 60000000000
  },
  "EC": {
    "Period": 30000000000,
    "Finality": 900,
    "DelayMultiplier": 2,
    "BaseDecisionBackoffTable": [
      1.3,
      1.69,
      2.2,
      2.86,
      3.71,
      4.83,
      6.27,
      7.5
    ],
    "HeadLookback": 0,
    "Finalize": true
  },
  "CertificateExchange": {
    "ClientRequestTimeout": 10000000000,
    "ServerRequestTimeout": 60000000000,
    "MinimumPollInterval": 30000000000,
    "MaximumPollInterval": 120000000000
  },
  "PubSub": {
    "CompressionEnabled": false
  },
  "ChainExchange": {
    "SubscriptionBufferSize": 32,
    "MaxChainLength": 100,
    "MaxInstanceLookahead": 10,
    "MaxDiscoveredChainsPerInstance": 1000,
    "MaxWantedChainsPerInstance": 1000,
    "RebroadcastInterval": 2000000000,
    "MaxTimestampAge": 8000000000
  }
}
➜  lotus git:(master) forest-tool shed f3 check-activation-raw --contract 0x476AC9256b9921C9C6a0fC237B7fE05fe9874F50
Raw data: 00000000000000000000000000000000000000000000000000000000004c4b400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000023d8554c172da3014bcf3150ce78e079b06486fc1d0363321a590b6874e0fb2fc8c35c8922bc90d6926ffde27d9c27620539f9eb52b7977df939f07c3e168432a0da30fc38c700defdc8a924652c9bf83d24c0ac4aedcfaad6086117e2bb42182da3d63b7be90d268a348b92a25cd2d7dec1e07de837994ea704f0abb6194310e54323172e0ea58724699d9c84750088b8af3fa537b2115ac620fb4e21a110e78200987eeb65816053306e04eca4342e801c1b0d6111343f36fe50d677b5180301669747aa99fca24b3ebcff882af4be086e0ebb4c7f2c0024fff9265e8408afab8a881bf568433f3e458eb8a1b861e9d89b021acc9d1ea233990742b2b916a1fb1d5991326b003a5d490de81d89bdc99f01fdf42a2244929d1c64a9059b620ae7f6732cf991db161307993b62b152ab3ed0dc2374968a2f7d131125feaa6c66d861b504cdaa326e3d7f23e32e17242f0ba1b2d79ea85e653b52e9740999dc846826fff4fc718b69e6c39bdf67514446d399ffa7a12cc425fbf0fe6a7add3209af97a165cb9ea5723e233e6d219adbe95bf568b51159c9288411996314a0cac8e3427620f6d34311a14660bbf2bd0e68115202b5377fa55503b507f409d11a767c43513aca88a8de478470dee22fc62f2d8ba0bbc30bad0cb4d95ecaaa4235a16381cda3661256cfca9bf9cad693bc0e776f1184d152b8ded5f9565a076756293a895e5f65e187a84fc5fe774734e57bbc6974c53893941ea0ed138799d1f55d873ff83a0edfff13a23df4929ba10a6ed099e509437cef0bc17e3e065f00f000000
{
  "ProtocolVersion": 5,
  "Pause": false,
  "InitialInstance": 0,
  "BootstrapEpoch": 5000000,
  "NetworkName": "filecoin",
  "ExplicitPower": null,
  "IgnoreECPower": false,
  "InitialPowerTable": null,
  "CommitteeLookback": 10,
  "CatchUpAlignment": 15000000000,
  "Gpbft": {
    "Delta": 6000000000,
    "DeltaBackOffExponent": 2.0,
    "QualityDeltaMultiplier": 1.0,
    "MaxLookaheadRounds": 5,
    "ChainProposedLength": 100,
    "RebroadcastBackoffBase": 6000000000,
    "RebroadcastBackoffExponent": 1.3,
    "RebroadcastBackoffSpread": 0.1,
    "RebroadcastBackoffMax": 60000000000
  },
  "EC": {
    "Period": 30000000000,
    "Finality": 900,
    "DelayMultiplier": 2.0,
    "BaseDecisionBackoffTable": [
      1.3,
      1.69,
      2.2,
      2.86,
      3.71,
      4.83,
      6.27,
      7.5
    ],
    "HeadLookback": 0,
    "Finalize": true
  },
  "CertificateExchange": {
    "ClientRequestTimeout": 10000000000,
    "ServerRequestTimeout": 60000000000,
    "MinimumPollInterval": 30000000000,
    "MaximumPollInterval": 120000000000
  },
  "PubSub": {
    "CompressionEnabled": false
  },
  "ChainExchange": {
    "SubscriptionBufferSize": 32,
    "MaxChainLength": 100,
    "MaxInstanceLookahead": 10,
    "MaxDiscoveredChainsPerInstance": 1000,
    "MaxWantedChainsPerInstance": 1000,
    "RebroadcastInterval": 2000000000,
    "MaxTimestampAge": 8000000000
  }
}

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

@hanabi1224 hanabi1224 force-pushed the hm/f3-check-activation-raw branch from 2f5ec93 to 60f6a3c Compare February 20, 2025 12:18
@hanabi1224 hanabi1224 marked this pull request as ready for review February 20, 2025 12:20
@hanabi1224 hanabi1224 requested a review from a team as a code owner February 20, 2025 12:20
@hanabi1224 hanabi1224 requested review from lemmih and LesnyRumcajs and removed request for a team February 20, 2025 12:20
CheckActivation {
/// Contract address
#[arg(long, required = true)]
contract: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge deal given it's a testing tool, but it'd be great to use concrete types here instead of String if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

pub enum F3Commands {
CheckActivation {
/// Contract address
#[arg(long, required = true)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps let's just make this a positional argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to keep the usage the same as lotus shed f3 as this command is not expected to be used often. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a test command so no strong opinion.

Copy link
Member

@LesnyRumcajs LesnyRumcajs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a test tool it's fine. For code actually running with the node, we'd need more safety mechanisms and documentation, especially given the amount of magic numbers.

@hanabi1224 hanabi1224 enabled auto-merge February 20, 2025 14:10
@hanabi1224 hanabi1224 added this pull request to the merge queue Feb 20, 2025
@LesnyRumcajs LesnyRumcajs removed this pull request from the merge queue due to a manual request Feb 20, 2025
@LesnyRumcajs
Copy link
Member

Let's add it to the CLI docs reference? It's a new command so it won't be automagically picked up by the leshy bot.

@hanabi1224
Copy link
Contributor Author

Let's add it to the CLI docs reference? It's a new command so it won't be automagically picked up by the leshy bot.

@LesnyRumcajs will do in a subsequent PR

@hanabi1224 hanabi1224 added this pull request to the merge queue Feb 20, 2025
Merged via the queue into main with commit f203279 Feb 20, 2025
44 checks passed
@hanabi1224 hanabi1224 deleted the hm/f3-check-activation-raw branch February 20, 2025 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants