Skip to content

Commit

Permalink
make contract immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
LexLuthr committed Jan 31, 2025
1 parent 25872f5 commit f4aa99b
Show file tree
Hide file tree
Showing 10 changed files with 531 additions and 49 deletions.
6 changes: 6 additions & 0 deletions market/ipni/ipni-provider/ipni-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/filecoin-project/curio/build"
"github.com/go-chi/chi/v5"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -456,6 +457,11 @@ func Routes(r *chi.Mux, p *Provider) {

// StartPublishing starts a poller which publishes the head for each provider every 10 minutes.
func (p *Provider) StartPublishing(ctx context.Context) {
// Do not publish for any network except mainnet
if build.BuildType != build.BuildMainnet {
return
}

// A poller which publishes head for each provider
// every 10 minutes
ticker := time.NewTicker(publishInterval)
Expand Down
25 changes: 22 additions & 3 deletions market/ipni/ipni-provider/spark.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,34 @@ func (p *Provider) updateSparkContract(ctx context.Context) error {
return xerrors.Errorf("failed to get spark params for %s: %w", pInfo.Miner.String(), err)
}
} else {
pd := spark.SparkMessage{}
pd := struct {
PeerID string
SignedMessage []byte
}{}

err = parsedABI.UnpackIntoInterface(&pd, "getPeerData", res.MsgRct.Return)
if err != nil {
log.Fatalf("Failed to unpack result: %v", err)
}

if pd.Peer == pInfo.ID.String() {
continue
if pd.PeerID == pInfo.ID.String() {
detail := spark.SparkMessage{
Miner: pInfo.SPID,
Peer: pInfo.ID.String(),
}

jdetail, err := json.Marshal(detail)
if err != nil {
return xerrors.Errorf("failed to marshal spark message: %w", err)
}

ok, err := pKey.GetPublic().Verify(jdetail, pd.SignedMessage)
if err != nil {
return xerrors.Errorf("failed to verify signed message: %w", err)
}
if ok {
continue
}
}

params, err = p.getSparkParams(pInfo.SPID, pInfo.ID.String(), pKey, "update")
Expand Down
35 changes: 4 additions & 31 deletions market/ipni/spark/sol/contracts/MinerPeerIDMapping.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,15 @@ contract MinerPeerIDMapping {
bytes signedMessage;
}

// State variables
address public owner;
mapping(uint64 => PeerData) public minerToPeerData;

// Events
event PeerDataAdded(uint64 indexed minerID, string peerID);
event PeerDataUpdated(uint64 indexed minerID, string oldPeerID, string newPeerID);
event PeerDataDeleted(uint64 indexed minerID);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

// Modifier to restrict access to the owner
modifier onlyOwner() {
require(msg.sender == owner, "Caller is not the owner");
_;
}

// Constructor: Set the deployer as the initial owner
constructor() {
owner = msg.sender;
emit OwnershipTransferred(address(0), msg.sender);
}

/**
* @notice Transfer ownership to a new address.
* @param newOwner The address of the new owner.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "New owner is the zero address");
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
// The contract is immutable from the owner now, no ownership transfer is possible
}

/**
Expand All @@ -55,9 +34,7 @@ contract MinerPeerIDMapping {
) public {
require(bytes(minerToPeerData[minerID].peerID).length == 0, "Peer data already exists for this MinerID");

if (msg.sender != owner) {
require(isControllingAddress(msg.sender, minerID), "Caller is not the controlling address");
}
require(isControllingAddress(msg.sender, minerID), "Caller is not the controlling address");

minerToPeerData[minerID] = PeerData(newPeerID, signedMessage);

Expand All @@ -77,9 +54,7 @@ contract MinerPeerIDMapping {
) public {
require(bytes(minerToPeerData[minerID].peerID).length > 0, "No peer data exists for this MinerID");

if (msg.sender != owner) {
require(isControllingAddress(msg.sender, minerID), "Caller is not the controlling address");
}
require(isControllingAddress(msg.sender, minerID), "Caller is not the controlling address");

string memory oldPeerID = minerToPeerData[minerID].peerID;
minerToPeerData[minerID] = PeerData(newPeerID, signedMessage);
Expand All @@ -94,9 +69,7 @@ contract MinerPeerIDMapping {
function deletePeerData(uint64 minerID) public {
require(bytes(minerToPeerData[minerID].peerID).length > 0, "No peer data exists for this MinerID");

if (msg.sender != owner) {
require(isControllingAddress(msg.sender, minerID), "Caller is not the controlling address");
}
require(isControllingAddress(msg.sender, minerID), "Caller is not the controlling address");

delete minerToPeerData[minerID];

Expand Down
1 change: 1 addition & 0 deletions market/ipni/spark/sol/deployments/mainnet/.chainId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
314
414 changes: 414 additions & 0 deletions market/ipni/spark/sol/deployments/mainnet/MinerPeerIDMapping.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions market/ipni/spark/sol/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "@nomicfoundation/hardhat-ethers";
import "dotenv/config"; // For .env variables
import "hardhat-deploy";

const PRIVATE_KEY = process.env.PRIVATE_KEY;

Expand Down
8 changes: 4 additions & 4 deletions market/ipni/spark/sol/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion market/ipni/spark/sol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
"dotenv": "^16.4.5",
"ethers": "^6.13.2",
"hardhat": "^2.22.10",
"hardhat-deploy": "^0.14.0",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
},
"dependencies": {
"@zondax/filecoin-solidity": "^4.0.3",
"hardhat-deploy": "^0.12.4",
"hardhat-deploy-ethers": "^0.4.2"
},
"keywords": [
Expand Down
13 changes: 3 additions & 10 deletions market/ipni/spark/spark.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@ type SparkMessage struct {
}

func GetContractAddress() (string, error) {
if build.BuildType == build.BuildMainnet {
return "", nil
}

if build.BuildType == build.BuildCalibnet {
return "0xE08bBc65aF1f1a40BD3cbaD290a23925F83b8BBB", nil
}

if build.BuildType == build.Build2k || build.BuildType == build.BuildDebug {
return "", errors.New("manual contract is required for debug build")
if build.BuildType != build.BuildMainnet {
return "", errors.New("not supported on this network")
}

return "", errors.New("unknown build type")
return "0x599172a7654E47f070576ca7e56Ab9564FbD4041", nil
}

const AddPeerAbi = `[
Expand Down

0 comments on commit f4aa99b

Please sign in to comment.