This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prover): change block signing to use timestamp as key (#466)
- Loading branch information
1 parent
e48a1b6
commit eb5bc7a
Showing
7 changed files
with
148 additions
and
59 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
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 |
---|---|---|
@@ -1,11 +1,48 @@ | ||
package db | ||
|
||
import "fmt" | ||
import ( | ||
"bytes" | ||
"math/big" | ||
"strconv" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
var ( | ||
BlockKeyPrefix = "blockid-" | ||
BlockKeyPrefix = "block-" | ||
) | ||
|
||
func BuildBlockKey(blockID string) []byte { | ||
return []byte(fmt.Sprintf("%v%v", BlockKeyPrefix, blockID)) | ||
type SignedBlockData struct { | ||
BlockID *big.Int | ||
BlockHash common.Hash | ||
Signature string | ||
} | ||
|
||
// BuildBlockKey will build a block key for a signed block | ||
func BuildBlockKey(blockTimestamp uint64) []byte { | ||
return bytes.Join( | ||
[][]byte{ | ||
[]byte(BlockKeyPrefix), | ||
[]byte(strconv.Itoa(int(blockTimestamp))), | ||
}, []byte{}) | ||
} | ||
|
||
// BuildBlockValue will build a block value for a signed block | ||
func BuildBlockValue(hash []byte, signature []byte, blockID *big.Int) []byte { | ||
return bytes.Join( | ||
[][]byte{ | ||
hash, | ||
signature, | ||
blockID.Bytes(), | ||
}, []byte("-")) | ||
} | ||
|
||
func SignedBlockDataFromValue(val []byte) SignedBlockData { | ||
v := bytes.Split(val, []byte("-")) | ||
|
||
return SignedBlockData{ | ||
BlockID: new(big.Int).SetBytes(v[2]), | ||
BlockHash: common.BytesToHash(v[0]), | ||
Signature: common.Bytes2Hex(v[1]), | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,35 @@ | ||
package db | ||
|
||
import ( | ||
"bytes" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_BuildBlockKey(t *testing.T) { | ||
assert.Equal(t, BuildBlockKey("1"), []byte("blockid-1")) | ||
assert.Equal(t, []byte("block-1"), BuildBlockKey(1)) | ||
} | ||
|
||
func Test_BuildBlockValue(t *testing.T) { | ||
v := BuildBlockValue([]byte("hash"), []byte("sig"), big.NewInt(1)) | ||
spl := bytes.Split(v, []byte("-")) | ||
assert.Equal(t, "hash", string(spl[0])) | ||
assert.Equal(t, "sig", string(spl[1])) | ||
assert.Equal(t, uint64(1), new(big.Int).SetBytes(spl[2]).Uint64()) | ||
} | ||
|
||
func Test_SignedBlockDataFromValue(t *testing.T) { | ||
hash := common.HexToHash("1ada5c5ba58cfca1fbcd4531f4132f8cfef736c2cf40209a1315c489717dfc49") | ||
// nolint: lll | ||
sig := common.Hex2Bytes("789a80053e4927d0a898db8e065e948f5cf086e32f9ccaa54c1908e22ac430c62621578113ddbb62d509bf6049b8fb544ab06d36f916685a2eb8e57ffadde02301") | ||
|
||
v := BuildBlockValue(hash.Bytes(), sig, big.NewInt(1)) | ||
data := SignedBlockDataFromValue(v) | ||
|
||
assert.Equal(t, common.Bytes2Hex(sig), data.Signature) | ||
assert.Equal(t, hash, data.BlockHash) | ||
assert.Equal(t, data.BlockID, big.NewInt(1)) | ||
} |
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
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