forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
qkc/types: add transactions and signing #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6a2137b
qkc/types: add transactions and signing
blockchaindevsh 34c979b
qkc/types: rename transaction typed hash helpers
blockchaindevsh 1e3f38e
qkc/types: align cross-shard list with python
blockchaindevsh 543f466
qkc/types: hash transactions by version
blockchaindevsh 6245898
qkc/types: fix typed transaction byte data
blockchaindevsh 0734b84
qkc/types: flatten cross-shard deposit fields
blockchaindevsh 77ed4e4
qkc/types: remove unused transaction hash field
blockchaindevsh 754a0de
qkc/types: invalidate transaction hash cache on updates
blockchaindevsh a784c9d
qkc/types: remove unused transaction nonce check
blockchaindevsh 432ed4c
qkc/types: defer transaction execution message
blockchaindevsh 3221b8b
qkc/types: defer transaction ordering helpers
blockchaindevsh f84ee04
qkc/types: split signer network identifiers
blockchaindevsh 967072b
qkc/types: encode version 2 signature V
blockchaindevsh 346858b
qkc/types: rename typed transaction recipient helper
blockchaindevsh 0fafe9b
qkc/types: require signer when caching sender
blockchaindevsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,79 @@ | ||
| // Copyright 2026-2027, QuarkChain. | ||
|
|
||
| // Cross-shard transactions follow pyquarkchain-compatible QKC wire encoding. | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/qkc/account" | ||
| "github.com/ethereum/go-ethereum/qkc/serialize" | ||
| ) | ||
|
|
||
| const crossShardTransactionListVersion = uint32(1) | ||
|
|
||
| // CrossShardTransactionDeposit matches pyquarkchain's current | ||
| // CrossShardTransactionDeposit.FIELDS order. | ||
| type CrossShardTransactionDeposit struct { | ||
| TxHash common.Hash | ||
| From account.Address | ||
| To account.Address | ||
| Value *serialize.Uint256 | ||
| GasPrice *serialize.Uint256 | ||
| GasTokenID uint64 | ||
| TransferTokenID uint64 | ||
| GasRemained *serialize.Uint256 | ||
| MessageData []byte `bytesizeofslicelen:"4"` | ||
| CreateContract bool | ||
| IsFromRootChain bool | ||
| RefundRate uint8 | ||
| } | ||
|
|
||
| // CrossShardTransactionList is pyquarkchain's CrossShardTransactionList | ||
| // version 1 wire type. | ||
| type CrossShardTransactionList struct { | ||
| TXList []*CrossShardTransactionDeposit | ||
| } | ||
|
|
||
| func NewCrossShardTransactionList(txList []*CrossShardTransactionDeposit) *CrossShardTransactionList { | ||
| if txList == nil { | ||
| txList = make([]*CrossShardTransactionDeposit, 0) | ||
| } | ||
| return &CrossShardTransactionList{ | ||
| TXList: txList, | ||
| } | ||
| } | ||
|
|
||
| // Serialize writes pyquarkchain CrossShardTransactionList.FIELDS order: | ||
| // tx_list followed by version(uint32). | ||
| func (c *CrossShardTransactionList) Serialize(w *[]byte) error { | ||
| if c == nil { | ||
| return fmt.Errorf("nil cross-shard transaction list") | ||
| } | ||
| if err := serialize.SerializeWithTags(w, c.TXList, serialize.Tags{ByteSizeOfSliceLen: 4}); err != nil { | ||
| return err | ||
| } | ||
| return serialize.Serialize(w, crossShardTransactionListVersion) | ||
| } | ||
|
|
||
| // Deserialize reads the current pyquarkchain CrossShardTransactionList version. | ||
| func (c *CrossShardTransactionList) Deserialize(bb *serialize.ByteBuffer) error { | ||
| if c == nil { | ||
| return fmt.Errorf("nil cross-shard transaction list") | ||
| } | ||
| var txList []*CrossShardTransactionDeposit | ||
| if err := serialize.DeserializeWithTags(bb, &txList, serialize.Tags{ByteSizeOfSliceLen: 4}); err != nil { | ||
| return err | ||
| } | ||
| var version uint32 | ||
|
ping-ke marked this conversation as resolved.
|
||
| if err := serialize.Deserialize(bb, &version); err != nil { | ||
| return err | ||
| } | ||
| if version != crossShardTransactionListVersion { | ||
| return fmt.Errorf("unsupported cross-shard transaction list version %d", version) | ||
| } | ||
| c.TXList = txList | ||
| return nil | ||
| } | ||
This file contains hidden or 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,106 @@ | ||
| // Copyright 2026-2027, QuarkChain. | ||
|
|
||
| // Cross-shard transaction tests exercise pyquarkchain-compatible QKC wire bytes. | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "math/big" | ||
| "testing" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/qkc/account" | ||
| "github.com/ethereum/go-ethereum/qkc/serialize" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCrossShardTransactionList(t *testing.T) { | ||
| c1 := NewCrossShardTransactionList(nil) | ||
| for index := uint64(0); index < 100; index++ { | ||
| u256 := new(serialize.Uint256) | ||
| u256.Value = new(big.Int).SetUint64(index) | ||
| c1.TXList = append(c1.TXList, &CrossShardTransactionDeposit{ | ||
| TxHash: common.BigToHash(new(big.Int).SetUint64(index)), | ||
| From: account.Address{ | ||
| Recipient: common.BigToAddress(new(big.Int).SetUint64(2)), | ||
| FullShardKey: 2, | ||
| }, | ||
| To: account.Address{ | ||
| Recipient: common.BigToAddress(new(big.Int).SetUint64(3)), | ||
| FullShardKey: 3, | ||
| }, | ||
| Value: u256, | ||
| GasPrice: u256, | ||
| GasTokenID: 123, | ||
| TransferTokenID: 456, | ||
| IsFromRootChain: false, | ||
| GasRemained: u256, | ||
| MessageData: []byte{}, | ||
| CreateContract: true, | ||
| RefundRate: uint8(index), | ||
| }) | ||
| } | ||
|
|
||
| data, err := serialize.SerializeToBytes(c1) | ||
| assert.NoError(t, err) | ||
|
|
||
| d1 := NewCrossShardTransactionList(nil) | ||
| err = serialize.DeserializeFromBytes(data, d1) | ||
| assert.NoError(t, err) | ||
| for k, v := range c1.TXList { | ||
| assert.Equal(t, v.TxHash, (*d1).TXList[k].TxHash) | ||
| assert.Equal(t, v.From, (*d1).TXList[k].From) | ||
| assert.Equal(t, v.To, (*d1).TXList[k].To) | ||
| assert.Equal(t, v.Value.Value.Uint64(), (*d1).TXList[k].Value.Value.Uint64()) | ||
| assert.Equal(t, v.GasPrice.Value.Uint64(), (*d1).TXList[k].GasPrice.Value.Uint64()) | ||
| assert.Equal(t, v.GasTokenID, (*d1).TXList[k].GasTokenID) | ||
| assert.Equal(t, v.TransferTokenID, (*d1).TXList[k].TransferTokenID) | ||
| assert.Equal(t, v.IsFromRootChain, (*d1).TXList[k].IsFromRootChain) | ||
| assert.Equal(t, v.GasRemained.Value.Uint64(), (*d1).TXList[k].GasRemained.Value.Uint64()) | ||
| assert.Equal(t, v.MessageData, (*d1).TXList[k].MessageData) | ||
| assert.Equal(t, v.CreateContract, (*d1).TXList[k].CreateContract) | ||
| assert.Equal(t, uint8(k), (*d1).TXList[k].RefundRate) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func TestCrossShardTransactionListPyquarkchainGolden(t *testing.T) { | ||
| assertSerialized := func(t *testing.T, list *CrossShardTransactionList, expected string) { | ||
| t.Helper() | ||
| encoded, err := serialize.SerializeToBytes(list) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, expected, common.Bytes2Hex(encoded)) | ||
|
|
||
| var decoded CrossShardTransactionList | ||
| err = serialize.DeserializeFromBytes(common.FromHex(expected), &decoded) | ||
| assert.NoError(t, err) | ||
| reencoded, err := serialize.SerializeToBytes(&decoded) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, expected, common.Bytes2Hex(reencoded)) | ||
| } | ||
|
|
||
| // Generated by pyquarkchain's CrossShardTransactionList.serialize(). | ||
| assertSerialized(t, NewCrossShardTransactionList(nil), "0000000000000001") | ||
|
|
||
| u256 := func(value int64) *serialize.Uint256 { return &serialize.Uint256{Value: big.NewInt(value)} } | ||
| assertSerialized(t, NewCrossShardTransactionList([]*CrossShardTransactionDeposit{{ | ||
| TxHash: common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111"), | ||
| From: account.Address{Recipient: common.HexToAddress("0x2222222222222222222222222222222222222222"), FullShardKey: 3}, | ||
| To: account.Address{Recipient: common.HexToAddress("0x3333333333333333333333333333333333333333"), FullShardKey: 4}, | ||
| Value: u256(5), | ||
| GasPrice: u256(6), | ||
| GasTokenID: 7, | ||
| TransferTokenID: 8, | ||
| GasRemained: u256(9), | ||
| MessageData: []byte{0xaa, 0xbb}, | ||
| CreateContract: true, | ||
| IsFromRootChain: true, | ||
| RefundRate: 10, | ||
| }}), "0000000111111111111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222000000033333333333333333333333333333333333333333000000040000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000070000000000000008000000000000000000000000000000000000000000000000000000000000000900000002aabb01010a00000001") | ||
| } | ||
|
|
||
| func TestCrossShardTransactionListRejectsUnsupportedVersion(t *testing.T) { | ||
| var list CrossShardTransactionList | ||
| err := serialize.DeserializeFromBytes(common.FromHex("0000000000000000"), &list) | ||
| assert.Error(t, err) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.