nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: NethermindEth/juno
Pull request #16 (2022-01-28 03:57:29)

feat: encode chain id
## Changes

Add a function to encode chain ID constants based on the following references:
- https://starknet.io/documentation/chain-ids/
- https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json

## Types of changes

What types of changes does your code introduce?
_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation Update
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Other (please describe): 

## Testing
**Requires testing**

- [X] Yes
- [ ] No

**In case you checked yes, did you write tests??**

- [X] Yes
- [ ] No

**Comments about testing , should you have some** (optional)

Tests are written based on the values obtained in Python as set out in  https://starknet.io/documentation/chain-ids/ for "SN_MAIN" and "SN_GOERLI".

## Further comments (optional)

None



Patch:
diff --git a/internal/chainid.go b/internal/chainid.go
new file mode 100644
index 00000000..d20ae414
--- /dev/null
+++ b/internal/chainid.go
@@ -0,0 +1,10 @@
+package chainid
+
+import "github.com/ethereum/go-ethereum/common/hexutil"
+
+// Takes in a Starknet chain ID constant as a string (e.g. "SN_MAIN")
+// and returns the Starknet chain ID as a hex string
+func EncodeChainId(chain string) string {
+		b := []byte(chain)
+		return hexutil.Encode(b)
+}


Test patch:
diff --git a/internal/chainid_test.go b/internal/chainid_test.go
new file mode 100644
index 00000000..9b278e35
--- /dev/null
+++ b/internal/chainid_test.go
@@ -0,0 +1,17 @@
+package chainid
+
+import "testing"
+
+func TestEncodeChainId(t *testing.T) {
+	mainnetChainId := EncodeChainId("SN_MAIN")
+	expectedMainnetChainId := "0x534e5f4d41494e"
+	if mainnetChainId != expectedMainnetChainId {
+			t.Errorf("Got %q, expected %q", mainnetChainId, expectedMainnetChainId)
+	}
+
+	goerliChainId := EncodeChainId("SN_GOERLI")
+	expectedGoerliChainId := "0x534e5f474f45524c49"
+	if goerliChainId != expectedGoerliChainId {
+			t.Errorf("Got %q, expected %q", goerliChainId, expectedGoerliChainId)
+	}
+}
Repository: NethermindEth/juno
Pull request #146 (2022-05-09 17:11:02)

Add Close method
## Changes:
- Add the `Close` method to the `BlockSpecificDatabase`

## Types of changes

What types of changes does your code introduce?
_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation Update
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Other (please describe): 

## Testing
**Requires testing**

- [x] Yes
- [ ] No

**In case you checked yes, did you write tests??**

- [x] Yes
- [ ] No



Patch:
diff --git a/pkg/db/blockSpecific.go b/pkg/db/blockSpecific.go
index 764b3eaf..d4a952fd 100644
--- a/pkg/db/blockSpecific.go
+++ b/pkg/db/blockSpecific.go
@@ -104,6 +104,10 @@ func (db *BlockSpecificDatabase) Put(key Key, blockNumber uint64, value Value) {
 	db.put(rawKey, rawValue)
 }
 
+func (db *BlockSpecificDatabase) Close() {
+	db.database.Close()
+}
+
 func (db *BlockSpecificDatabase) get(key []byte) []byte {
 	data, err := db.database.Get(key)
 	if err != nil {


Test patch:
diff --git a/pkg/db/blockSpecific_test.go b/pkg/db/blockSpecific_test.go
index f96162b7..3bdc22f2 100644
--- a/pkg/db/blockSpecific_test.go
+++ b/pkg/db/blockSpecific_test.go
@@ -66,6 +66,7 @@ func TestBlockSpecificDatabase_Put(t *testing.T) {
 	for _, test := range tests {
 		db.Put(&test.Key, test.BlockNumber, &test.Value)
 	}
+	db.Close()
 }
 
 func TestBlockSpecificDatabase_Get(t *testing.T) {
@@ -168,4 +169,5 @@ func TestBlockSpecificDatabase_Get(t *testing.T) {
 			t.Errorf("db.Get(%s, %d) = %s, want: %s", string(test.Key), test.BlockNumber, string(result), test.Want)
 		}
 	}
+	db.Close()
 }