@@ -41,28 +41,69 @@ func NewStorageKey(b []byte) StorageKey {
4141func CreateStorageKey (meta * Metadata , prefix , method string , args ... []byte ) (StorageKey , error ) {
4242 stringKey := []byte (prefix + " " + method )
4343
44+ validateAndTrimArgs := func (args [][]byte ) ([][]byte , error ) {
45+ nonNilCount := - 1
46+ for i , arg := range args {
47+ if len (arg ) == 0 {
48+ nonNilCount = i
49+ break
50+ }
51+ }
52+
53+ if nonNilCount == - 1 {
54+ return args , nil
55+ }
56+
57+ for i := nonNilCount ; i < len (args ); i ++ {
58+ if len (args [i ]) != 0 {
59+ return nil , fmt .Errorf ("non-nil arguments cannot be preceded by nil arguments" )
60+ }
61+ }
62+
63+ trimmedArgs := make ([][]byte , nonNilCount )
64+ for i := 0 ; i < nonNilCount ; i ++ {
65+ trimmedArgs [i ] = args [i ]
66+ }
67+
68+ return trimmedArgs , nil
69+ }
70+
71+ validatedArgs , err := validateAndTrimArgs (args )
72+ if err != nil {
73+ return nil , err
74+ }
75+
4476 entryMeta , err := meta .FindStorageEntryMetadata (prefix , method )
4577 if err != nil {
4678 return nil , err
4779 }
4880
4981 if entryMeta .IsNMap () {
50- return createKeyNMap (meta , method , prefix , args , entryMeta )
82+ return createKeyNMap (meta , method , prefix , validatedArgs , entryMeta )
5183 }
5284
5385 if entryMeta .IsDoubleMap () {
54- if len (args ) != 2 {
86+ if len (validatedArgs ) != 2 {
5587 return nil , fmt .Errorf ("%v is a double map, therefore requires precisely two arguments. " +
56- "received: %d" , method , len (args ))
88+ "received: %d" , method , len (validatedArgs ))
5789 }
58- return createKeyDoubleMap (meta , method , prefix , stringKey , args [0 ], args [1 ], entryMeta )
90+ return createKeyDoubleMap (meta , method , prefix , stringKey , validatedArgs [0 ], validatedArgs [1 ], entryMeta )
5991 }
6092
61- if len (args ) != 1 {
62- return nil , fmt .Errorf ("%v is a map, therefore requires precisely one argument. " +
63- "received: %d" , method , len (args ))
93+ if entryMeta .IsMap () {
94+ if len (validatedArgs ) != 1 {
95+ return nil , fmt .Errorf ("%v is a map, therefore requires precisely one argument. " +
96+ "received: %d" , method , len (validatedArgs ))
97+ }
98+ return createKey (meta , method , prefix , stringKey , validatedArgs [0 ], entryMeta )
99+ }
100+
101+ if entryMeta .IsPlain () && len (validatedArgs ) != 0 {
102+ return nil , fmt .Errorf ("%v is a plain key, therefore requires no argument. " +
103+ "received: %d" , method , len (validatedArgs ))
64104 }
65- return createKey (meta , method , prefix , stringKey , args [0 ], entryMeta )
105+
106+ return createKey (meta , method , prefix , stringKey , nil , entryMeta )
66107}
67108
68109// Encode implements encoding for StorageKey, which just unwraps the bytes of StorageKey
0 commit comments