Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 35 additions & 1 deletion blockchain/abi/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ var MethodSignatures = struct {
IsPeerIdMemberOfPool string
GetMemberPeerIds string
RemoveMemberPeerId string
JoinRequests string
}{
Pools: "0xced08b2d", // pools(uint32) - updated signature
PoolIds: "0x69883b4e", // poolIds(uint256) - index-based pool discovery
IsPeerIdMemberOfPool: "0xb098a605", // isPeerIdMemberOfPool(uint32,bytes32) - corrected from block explorer
GetMemberPeerIds: "0x31db3ae8", // getMemberPeerIds(uint32,address)
RemoveMemberPeerId: "0x12345678", // removeMemberPeerId(uint32,bytes32) - TODO: Update with actual signature
RemoveMemberPeerId: "0x3d71233a", // removeMemberPeerId(uint32,bytes32) — keccak256 of the signature
JoinRequests: "0xccc9fc03", // joinRequests(uint32,bytes32) — keccak256 of the signature
}

// DecodePoolsResult decodes the result from pools(uint32) contract call
Expand Down Expand Up @@ -394,3 +396,35 @@ func EncodeRemoveMemberPeerIdCall(poolID uint32, peerIDBytes32 string) string {

return fmt.Sprintf("%s%064x%s", MethodSignatures.RemoveMemberPeerId, poolID, peerIDBytes32)
}

// EncodeJoinRequestsCall encodes the joinRequests(uint32,bytes32) public-mapping getter call
func EncodeJoinRequestsCall(poolID uint32, peerIDBytes32 string) string {
// Remove 0x prefix from peerIDBytes32 if present
if strings.HasPrefix(peerIDBytes32, "0x") {
peerIDBytes32 = peerIDBytes32[2:]
}

return fmt.Sprintf("%s%064x%s", MethodSignatures.JoinRequests, poolID, peerIDBytes32)
}

// DecodeJoinRequestStatus returns the status field of a joinRequests(uint32,bytes32) result: the getter returns
// (account, poolId, timestamp, index, approvals, rejections, status, peerId), 8 words; status 1 = pending,
// 0 = no request.
func DecodeJoinRequestStatus(data string) (uint8, error) {
// Remove 0x prefix
if strings.HasPrefix(data, "0x") {
data = data[2:]
}

// 8 * 32 bytes = 512 hex chars
if len(data) < 512 {
return 0, fmt.Errorf("insufficient data length: %d, expected 512", len(data))
}

status, err := strconv.ParseUint(data[6*64:7*64], 16, 8)
if err != nil {
return 0, fmt.Errorf("failed to parse join request status: %w", err)
}

return uint8(status), nil
}
41 changes: 41 additions & 0 deletions blockchain/abi/join_requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package abi

import (
"fmt"
"strings"
"testing"
)

func TestEncodeJoinRequestsCall(t *testing.T) {
peer := "0x" + strings.Repeat("ab", 32)
got := EncodeJoinRequestsCall(7, peer)
want := "0xccc9fc03" + fmt.Sprintf("%064x", 7) + strings.Repeat("ab", 32)
if got != want {
t.Fatalf("EncodeJoinRequestsCall = %s, want %s", got, want)
}
}

func TestDecodeJoinRequestStatus(t *testing.T) {
word := func(v int) string { return fmt.Sprintf("%064x", v) }
// (account, poolId, timestamp, index, approvals, rejections, status, peerId)
pending := "0x" + word(0xabc) + word(1) + word(1700000000) + word(0) + word(2) + word(0) + word(1) + strings.Repeat("cd", 32)
status, err := DecodeJoinRequestStatus(pending)
if err != nil || status != 1 {
t.Fatalf("pending request: status=%d err=%v, want 1", status, err)
}
none := "0x" + strings.Repeat(word(0), 8)
status, err = DecodeJoinRequestStatus(none)
if err != nil || status != 0 {
t.Fatalf("no request: status=%d err=%v, want 0", status, err)
}
if _, err := DecodeJoinRequestStatus("0x" + word(1)); err == nil {
t.Fatal("short data must be an error")
}
}

func TestRemoveMemberPeerIdSelector(t *testing.T) {
// keccak256("removeMemberPeerId(uint32,bytes32)")[:4] — the old placeholder 0x12345678 matched nothing on-chain.
if MethodSignatures.RemoveMemberPeerId != "0x3d71233a" {
t.Fatalf("RemoveMemberPeerId selector = %s", MethodSignatures.RemoveMemberPeerId)
}
}
Loading
Loading