This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parsing of bitfields (paritytech#635)
Co-authored-by: Alistair Singh <alistair.singh7@gmail.com>
- Loading branch information
1 parent
1b5d8d5
commit e342ca3
Showing
4 changed files
with
114 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bitfield | ||
|
||
import ( | ||
"math/big" | ||
) | ||
|
||
type Bitfield []byte | ||
|
||
func reverse(thing []byte) { | ||
for i, j := 0, len(thing)-1; i < j; i, j = i+1, j-1 { | ||
thing[i], thing[j] = thing[j], thing[i] | ||
} | ||
} | ||
|
||
// New returns a Bitfield initialized from the Solidity representation of a Bitfield (an array of uint256). See below: | ||
// https://github.com/Snowfork/snowbridge/blob/18c6225b21782170156729d54a35404d876a2c7b/ethereum/contracts/utils/Bitfield.sol | ||
func New(input []*big.Int) Bitfield { | ||
const length = 256 / 8 | ||
result := make(Bitfield, length*len(input)) | ||
for i, chunk := range input { | ||
k := i * length | ||
j := (i + 1) * length | ||
chunk.FillBytes(result[k:j]) | ||
reverse(result[k:j]) | ||
} | ||
return result | ||
} | ||
|
||
// Members returns the set bits in the bitfield | ||
func (b Bitfield) Members() []uint64 { | ||
results := []uint64{} | ||
for idx, bits := range b { | ||
if bits == 0 { | ||
continue | ||
} | ||
for i := 0; i < 8; i++ { | ||
if bits&byte(1<<i) > 0 { | ||
results = append(results, uint64((idx*8)+i)) | ||
} | ||
} | ||
} | ||
return results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package bitfield | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBitfieldMembers(t *testing.T) { | ||
x := big.NewInt(1) | ||
y := big.NewInt(1) | ||
z := big.NewInt(1) | ||
|
||
u := New([]*big.Int{x, y, z}) | ||
fmt.Printf("%v\n", u) | ||
fmt.Printf("%v\n", u.Members()) | ||
|
||
assert.Equal(t, u.Members(), []uint64{0, 256, 512}) | ||
} | ||
|
||
func TestBitfieldMembers2(t *testing.T) { | ||
foo := make([]byte, 32) | ||
foo[0] = 128 | ||
foo[31] = 1 | ||
|
||
x := big.NewInt(1) | ||
x.SetBytes(foo) | ||
|
||
u := New([]*big.Int{x}) | ||
fmt.Printf("%v\n", u) | ||
fmt.Printf("%v\n", u.Members()) | ||
|
||
assert.Equal(t, u.Members(), []uint64{0, 255}) | ||
} | ||
|
||
func TestBitfiledMembers3(t *testing.T) { | ||
var x, y, z, w big.Int | ||
|
||
// Four uint256 with first and last bit set | ||
x.SetString("8000000000000000000000000000000000000000000000000000000000000001", 16) | ||
y.SetString("8000000000000000000000000000000000000000000000000000000000000001", 16) | ||
z.SetString("8000000000000000000000000000000000000000000000000000000000000001", 16) | ||
w.SetString("8000000000000000000000000000000000000000000000000000000000000001", 16) | ||
|
||
u := New([]*big.Int{&x, &y, &z, &w}) | ||
fmt.Printf("%v\n", u) | ||
fmt.Printf("%v\n", u.Members()) | ||
|
||
assert.Equal(t, u.Members(), []uint64{ | ||
/* x */ 0, 255, | ||
/* y */ 256, 511, | ||
/* z */ 512, 767, | ||
/* w */ 768, 1023, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters